com.zuunr.forms.ValueFormat Maven / Gradle / Ivy
/*
 * Copyright 2019 Zuunr AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.zuunr.forms;
import com.zuunr.forms.formfield.*;
import com.zuunr.json.JsonArray;
import com.zuunr.json.JsonObject;
import com.zuunr.json.JsonObjectSupport;
import com.zuunr.json.JsonValue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.math.BigDecimal;
/**
 * This class represents the form field in its raw format without applying any default values.
 */
public final class ValueFormat implements JsonObjectSupport {
    private static final Logger logger = LoggerFactory.getLogger(ValueFormat.class);
    private static ExplicitValueFormatCreator explicitValueFormatCreator = new ExplicitValueFormatCreator();
    public static final ValueFormat EMPTY = new ValueFormat(JsonObject.EMPTY.jsonValue());
    private JsonObject jsonObject;
    private Min min;
    private Max max;
    private MinLength minlength;
    private MaxLength maxlength;
    private MinSize minSize;
    private MaxSize maxSize;
    private Pattern pattern;
    private Boolean required;
    private ValueFormat explicitValueFormat;
    private ValueFormat(JsonValue source, boolean isExplicit) {
        this(source);
        if (isExplicit) {
            explicitValueFormat = this;
        }
    }
    public ValueFormat(JsonValue source) {
        try {
            init(source);
        } catch (Exception e) {
            logger.error("Bad form field: {}", source.asJson());
            throw new RuntimeException(e);
        }
    }
    private void init(JsonValue source) {
        JsonObject in = source.getValue(JsonObject.class);
        JsonObject.JsonObjectBuilder builder = JsonObject.EMPTY.builder();
        if (in.get("desc", JsonValue.NULL).getValue(String.class) != null) {
            builder.put("desc", in.get("desc"));
        }
        if (in.get("equals", JsonValue.NULL).as(Equals.class) != null) {
            builder.put("equals", in.get("equals"));
        }
        if (in.get("eform", JsonValue.NULL).as(Form.class) != null) {
            builder.put("eform", in.get("eform"));
        }
        if (in.get("form", JsonValue.NULL).as(Form.class) != null) {
            builder.put("form", in.get("form"));
        }
        String href = source.get("href", JsonValue.NULL).as(String.class);
        if (href != null) {
            builder.put("href", href);
        }
        Boolean mustBeNull = in.get("mustBeNull", JsonValue.NULL).getValue(Boolean.class);
        if (mustBeNull != null) {
            builder.put("mustBeNull", mustBeNull);
        }
        Boolean nullable = in.get("nullable", JsonValue.NULL).getValue(Boolean.class);
        if (nullable != null) {
            builder.put("nullable", in.get("nullable", nullable).getValue(Boolean.class));
        }
        if (in.get("pattern", JsonValue.NULL).getValue(String.class) != null) {
            pattern = new Pattern(in.get("pattern"));
            builder.put("pattern", pattern.asJsonValue());
        }
        Options options = in.get("options", JsonValue.NULL).as(Options.class);
        if (options != null) {
            builder.put("options", options);
        }
        ValueFormat element = in.get("element", JsonValue.NULL).as(ValueFormat.class);
        if (element != null) {
            builder.put("element", element);
        }
        required = in.get("required", JsonValue.NULL).getValue(Boolean.class);
        if (required != null) {
            builder.put("required", in.get("required"));
        }
        Type type = mustBeNull != null && mustBeNull ? null : in.get("type", Type.STRING).as(Type.class);
        if (type != null) {
            builder.put("type", type);
            if (Type.INTEGER.equals(type)) {
                if (in.get("max", JsonValue.NULL).asInteger() != null) {
                    max = new Max(in.get("max"));
                    builder.put("max", max.asJsonValue());
                }
                if (in.get("min", JsonValue.NULL).asInteger() != null) {
                    min = new Min(in.get("min"));
                    builder.put("min", min.asJsonValue());
                }
            } else if (Type.DECIMAL.equals(type)) {
                if (in.get("max", JsonValue.NULL).getValue(BigDecimal.class) != null) {
                    max = new Max(in.get("max"));
                    builder.put("max", max.asJsonValue());
                }
                if (in.get("min", JsonValue.NULL).getValue(BigDecimal.class) != null) {
                    min = new Min(in.get("min"));
                    builder.put("min", min.asJsonValue());
                }
            } else if (Type.STRING.equals(type)) {
                if (in.get("maxlength", JsonValue.NULL).asInteger() != null) {
                    maxlength = new MaxLength(in.get("maxlength"));
                    builder.put("maxlength", maxlength.asJsonValue());
                }
                if (in.get("minlength", JsonValue.NULL).asInteger() != null) {
                    minlength = new MinLength(in.get("minlength"));
                    builder.put("minlength", minlength.asJsonValue());
                }
                if (in.get("max", JsonValue.NULL).getValue(String.class) != null) {
                    max = new Max(in.get("max"));
                    builder.put("max", max.asJsonValue());
                }
                if (in.get("min", JsonValue.NULL).getValue(String.class) != null) {
                    min = new Min(in.get("min"));
                    builder.put("min", min.asJsonValue());
                }
            } else if (Type.ARRAY.equals(type) || Type.SET.equals(type)) {
                if (in.get("maxsize", JsonValue.NULL).asInteger() != null) {
                    maxSize = new MaxSize(in.get("maxsize"));
                    builder.put("maxsize", maxSize.asJsonValue());
                }
                if (in.get("minsize", JsonValue.NULL).asInteger() != null) {
                    minSize = new MinSize(in.get("minsize"));
                    builder.put("minsize", minSize.asJsonValue());
                }
            } else if (Type.DATE.equals(type)) {
                if (in.get("max", JsonValue.NULL).getValue(String.class) != null) {
                    max = new Max(in.get("max"));
                    builder.put("max", max.asJsonValue());
                }
                if (in.get("min", JsonValue.NULL).getValue(String.class) != null) {
                    min = new Min(in.get("min"));
                    builder.put("min", min.asJsonValue());
                }
            }
        }
        this.jsonObject = builder.build();
    }
    public Builder builder() {
        return new Builder(jsonObject);
    }
    public MinLength minlength() {
        return minlength;
    }
    public MaxLength maxlength() {
        return maxlength;
    }
    public Boolean mustBeNull() {
        return jsonObject.get("mustBeNull", JsonValue.NULL).getValue(Boolean.class);
    }
    public Max max() {
        return max;
    }
    public Min min() {
        return min;
    }
    public Boolean nullable() {
        return jsonObject.get("nullable", JsonValue.NULL).getValue(Boolean.class);
    }
    public ValueFormat element() {
        return jsonObject.get("element", JsonValue.NULL).as(ValueFormat.class);
    }
    public Options options() {
        return jsonObject.get("options", JsonValue.NULL).as(Options.class);
    }
    public Type type() {
        return jsonObject.get("type", JsonValue.NULL).as(Type.class);
    }
    public Pattern pattern() {
        return pattern;
    }
    public Form form() {
        return jsonObject.get("form", JsonValue.NULL).as(Form.class);
    }
    public String href() {
        return jsonObject.get("href", JsonValue.NULL).getValue(String.class);
    }
    public Form eform() {
        return jsonObject.get("eform", JsonValue.NULL).as(Form.class);
    }
    public Boolean required() {
        return jsonObject.get("required", JsonValue.NULL).getValue(Boolean.class);
    }
    @Override
    public JsonObject asJsonObject() {
        return jsonObject;
    }
    @Override
    public JsonValue asJsonValue() {
        return jsonObject.jsonValue();
    }
    @Override
    public String toString() {
        return jsonObject.toString();
    }
    @Override
    public boolean equals(Object obj) {
        return obj.getClass() == getClass() && jsonObject.equals(((ValueFormat) obj).jsonObject);
    }
    /**
     * @return where all defaults are applied
     */
    public ValueFormat asExplicitValueFormat() {
        if (explicitValueFormat == null) {
            explicitValueFormat = new ValueFormat(explicitValueFormatCreator.createExplicitValueFormat(this).jsonValue(), true);
        }
        return explicitValueFormat;
    }
    public static class Builder {
        JsonObject.JsonObjectBuilder jsonObjectBuilder;
        private Builder(JsonObject jsonObject) {
            jsonObjectBuilder = jsonObject.builder();
        }
        public Builder desc(String description) {
            jsonObjectBuilder.put("desc", description);
            return this;
        }
        public Builder eform(Form form) {
            jsonObjectBuilder.put("eform", form.asJsonValue());
            return this;
        }
        public Builder element(ValueFormat elementValueFormat) {
            jsonObjectBuilder.put("element", elementValueFormat.asJsonValue());
            return this;
        }
        public Builder equalTo(Equals paths) {
            jsonObjectBuilder.put("equals", paths.asJsonValue());
            return this;
        }
        public Builder form(Form form) {
            jsonObjectBuilder.put("form", form.asJsonValue());
            return this;
        }
        public Builder href(String href) {
            jsonObjectBuilder.put("href", href);
            return this;
        }
        public Builder max(Integer max) {
            jsonObjectBuilder.put("max", max);
            return this;
        }
        public Builder max(BigDecimal max) {
            jsonObjectBuilder.put("max", max);
            return this;
        }
        public Builder max(String max) {
            jsonObjectBuilder.put("max", max);
            return this;
        }
        public Builder maxlength(Integer maxlength) {
            jsonObjectBuilder.put("maxlength", maxlength);
            return this;
        }
        public Builder min(Integer min) {
            jsonObjectBuilder.put("min", min);
            return this;
        }
        public Builder min(BigDecimal min) {
            jsonObjectBuilder.put("min", min);
            return this;
        }
        public Builder min(String min) {
            jsonObjectBuilder.put("min", min);
            return this;
        }
        public Builder minlength(Integer minlength) {
            jsonObjectBuilder.put("minlength", minlength);
            return this;
        }
        public Builder mustBeNull(Boolean mustBeNull) {
            jsonObjectBuilder.put("mustBeNull", mustBeNull);
            return this;
        }
        public Builder nullable(Boolean nullable) {
            jsonObjectBuilder.put("nullable", nullable);
            return this;
        }
        public Builder pattern(String pattern) {
            jsonObjectBuilder.put("pattern", pattern);
            return this;
        }
        public Builder required(Boolean required) {
            jsonObjectBuilder.put("required", required);
            return this;
        }
        public Builder type(Type type) {
            jsonObjectBuilder.put("type", type);
            return this;
        }
        public Builder maxsize(Integer maxsize) {
            jsonObjectBuilder.put("maxsize", maxsize);
            return this;
        }
        public Builder minsize(Integer minsize) {
            jsonObjectBuilder.put("minsize", minsize);
            return this;
        }
        public Builder options(Options options) {
            jsonObjectBuilder.put("options", options);
            return this;
        }
        public ValueFormat build() {
            return new ValueFormat(jsonObjectBuilder.build().jsonValue());
        }
    }
}
    © 2015 - 2025 Weber Informatics LLC | Privacy Policy