All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.ibodrov.mica.api.kinds.MicaViewV1 Maven / Gradle / Ivy

The newest version!
package ca.ibodrov.mica.api.kinds;

import ca.ibodrov.mica.api.model.PartialEntity;
import ca.ibodrov.mica.api.model.ViewLike;
import ca.ibodrov.mica.api.validation.ValidName;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.TextNode;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
 * @implNote when adding or removing fields here, make sure to update
 *           {@link #toPartialEntity(ObjectMapper)}
 */
public record MicaViewV1(@ValidName String name,
        @NotNull Selector selector,
        @NotNull Data data,
        @NotNull Optional validation,
        @NotNull Optional parameters,
        @NotNull Optional caching) implements ViewLike {

    public static final String MICA_VIEW_V1 = "/mica/view/v1";

    public MicaViewV1 {
        requireNonNull(name, "missing 'name'");
        requireNonNull(selector, "missing 'selector'");
        requireNonNull(data, "missing 'data'");
        requireNonNull(validation, "'validation' cannot be null");
        requireNonNull(parameters, "'parameters' cannot be null");
    }

    public record Selector(Optional> includes,
            @ValidName String entityKind,
            @NotNull Optional> namePatterns) implements ViewLike.Selector {

        public static Selector byEntityKind(String entityKind) {
            return new Selector(Optional.empty(), entityKind, Optional.empty());
        }

        public Selector withNamePatterns(List namePatterns) {
            return new Selector(this.includes, this.entityKind, Optional.of(namePatterns));
        }

        public Selector withIncludes(List includes) {
            return new Selector(Optional.of(includes), this.entityKind, this.namePatterns);
        }
    }

    public record Data(JsonNode jsonPath,
            Optional jsonPatch,
            Optional flatten,
            Optional merge,
            Optional mergeBy,
            Optional> dropProperties,
            Optional> map) implements ViewLike.Data {

        public static Data jsonPath(String jsonPath) {
            return new Data(TextNode.valueOf(jsonPath), Optional.empty(), Optional.empty(), Optional.empty(),
                    Optional.empty(), Optional.empty(), Optional.empty());
        }

        public static Data jsonPaths(ObjectMapper objectMapper, String... jsonPaths) {
            var jsonPath = objectMapper.convertValue(requireNonNull(jsonPaths), ArrayNode.class);
            return new Data(jsonPath, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(),
                    Optional.empty(),
                    Optional.empty());
        }

        public Data withMerge() {
            return new Data(this.jsonPath, this.jsonPatch, this.flatten, Optional.of(true), Optional.empty(),
                    Optional.empty(), Optional.empty());
        }

        public Data withMergeBy(String mergeBy) {
            return new Data(this.jsonPath, this.jsonPatch, this.flatten, this.merge,
                    Optional.of(TextNode.valueOf(mergeBy)),
                    Optional.empty(), Optional.empty());

        }

        public Data withDropProperties(List dropProperties) {
            return new Data(this.jsonPath, this.jsonPatch, this.flatten, this.merge, Optional.empty(),
                    Optional.of(dropProperties),
                    Optional.empty());
        }
    }

    // TODO add @NotNull
    public record Validation(String asEntityKind) implements ViewLike.Validation {

        public static Validation asEntityKind(String asEntityKind) {
            return new Validation(asEntityKind);
        }
    }

    public record Caching(@NotNull Optional enabled,
            @NotNull Optional ttl) implements ViewLike.Caching {
    }

    public PartialEntity toPartialEntity(ObjectMapper objectMapper) {
        var props = new HashMap();
        this.parameters.ifPresent(params -> props.put("parameters",
                objectMapper.convertValue(params, JsonNode.class)));
        props.put("selector", objectMapper.convertValue(this.selector, JsonNode.class));
        props.put("data", objectMapper.convertValue(this.data, JsonNode.class));
        props.put("validation", objectMapper.convertValue(this.validation, JsonNode.class));
        return PartialEntity.create(this.name, MICA_VIEW_V1, props);
    }

    public static class Builder {

        private String name;
        private Selector selector;
        private Data data;
        private Validation validation;
        private JsonNode parameters;
        private Caching caching;

        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder selector(Selector selector) {
            this.selector = selector;
            return this;
        }

        public Builder data(Data data) {
            this.data = data;
            return this;
        }

        public Builder validation(Validation validation) {
            this.validation = validation;
            return this;
        }

        public Builder parameters(JsonNode parameters) {
            this.parameters = parameters;
            return this;
        }

        public Builder caching(Caching caching) {
            this.caching = caching;
            return this;
        }

        public MicaViewV1 build() {
            return new MicaViewV1(
                    name,
                    selector,
                    data,
                    Optional.ofNullable(validation),
                    Optional.ofNullable(parameters),
                    Optional.ofNullable(caching));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy