com.zuunr.forms.FormField Maven / Gradle / Ivy
/*
 * Copyright 2018 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.FormFieldPattern;
import com.zuunr.json.JsonObject;
import com.zuunr.json.JsonObjectSupport;
import com.zuunr.json.JsonValue;
import java.math.BigDecimal;
import java.util.regex.Pattern;
public final class FormField implements JsonObjectSupport {
    private JsonObject jsonObject;
    private Integer minlength = null;
    private Integer maxlength;
    private boolean patternIsCached = false;
    private FormFieldPattern pattern;
    private String name;
    public FormField(JsonValue source) {
        JsonObject in = source.getValue(JsonObject.class);
        JsonObject.JsonObjectBuilder builder = JsonObject.EMPTY.builder().put("name", in.get("name"));
        if (in.get("desc", JsonValue.NULL).getValue(String.class) != null) {
            builder.put("desc", in.get("desc"));
        }
        if (in.get("equals", JsonValue.NULL).as(EqualsFormFieldMember.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"));
        }
        Boolean mustBeNull = in.get("mustBeNull", JsonValue.FALSE).getValue(Boolean.class);
        builder.put("mustBeNull", mustBeNull);
        builder.put("nullable", in.get("nullable", mustBeNull).getValue(Boolean.class));
        if (in.get("pattern", JsonValue.NULL).getValue(String.class) != null) {
            builder.put("pattern", in.get("pattern"));
        }
        builder.put("required", in.get("required", JsonValue.FALSE).getValue(Boolean.class));
        JsonValue defaultTypeIfNull = !mustBeNull ? Type.STRING.asJsonValue() : JsonValue.NULL;
        Type type = in.get("type", defaultTypeIfNull).as(Type.class);
        if (type != null) {
            if (!mustBeNull) { // There cannot be a type when value is null
                builder.put("type", type);
            }
            if (Type.INTEGER.equals(type)) {
                if (in.get("max", JsonValue.NULL).asInteger() != null) {
                    builder.put("max", in.get("max"));
                }
                if (in.get("min", JsonValue.NULL).asInteger() != null) {
                    builder.put("min", in.get("min"));
                }
            } else if (Type.DECIMAL.equals(type)) {
                if (in.get("max", JsonValue.NULL).getValue(BigDecimal.class) != null) {
                    builder.put("max", in.get("max"));
                }
                if (in.get("min", JsonValue.NULL).getValue(BigDecimal.class) != null) {
                    builder.put("min", in.get("min"));
                }
            } else if (Type.STRING.equals(type)) {
                if (in.get("maxlength", JsonValue.NULL).asInteger() != null) {
                    builder.put("maxlength", in.get("maxlength"));
                }
                if (in.get("minlength", JsonValue.NULL).asInteger() != null) {
                    builder.put("minlength", in.get("minlength"));
                }
            } else if (Type.ARRAY.equals(type) || Type.SET.equals(type)) {
                if (in.get("maxsize", JsonValue.NULL).asInteger() != null) {
                    builder.put("maxsize", in.get("maxsize"));
                }
                if (in.get("minsize", JsonValue.NULL).asInteger() != null) {
                    builder.put("minsize", in.get("minsize"));
                }
            }
        }
        this.jsonObject = builder.build();
    }
    public FormFieldBuilder builder() {
        return new FormFieldBuilder(jsonObject);
    }
    public static FormFieldBuilder builder(String formFieldName) {
        return new FormField(JsonObject.EMPTY.builder().put("name", formFieldName).build().jsonValue()).builder();
    }
    public Integer minlength() {
        if (minlength == null) {
            minlength = jsonObject.get("minlength", 0).asInteger();
        }
        return minlength;
    }
    public Integer maxlength() {
        if (maxlength == null) {
            maxlength = jsonObject.get("maxlength", Integer.MAX_VALUE).asInteger();
        }
        return maxlength;
    }
    public Boolean mustBeNull() {
        return jsonObject.get("mustBeNull", JsonValue.FALSE).getValue(Boolean.class);
    }
    public String name() {
        if (name == null) {
            // name can never be null
            name = jsonObject.get("name").getValue(String.class);
        }
        return name;
    }
    public Boolean nullable() {
        return jsonObject.get("nullable", JsonValue.FALSE).getValue(Boolean.class);
    }
    public Type type() {
        return jsonObject.get("type", Type.STRING).as(Type.class);
    }
    public FormFieldPattern pattern() {
        if (!patternIsCached) {
            pattern = jsonObject.get("pattern", JsonValue.NULL).as(FormFieldPattern.class);
            patternIsCached = true;
        }
        return pattern;
    }
    public Form form() {
        return jsonObject.get("form", JsonValue.NULL).as(Form.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(((FormField) obj).jsonObject);
    }
    public static class FormFieldBuilder {
        JsonObject.JsonObjectBuilder jsonObjectBuilder;
        private FormFieldBuilder(JsonObject jsonObject) {
            jsonObjectBuilder = jsonObject.builder();
        }
        public FormFieldBuilder desc(String description) {
            jsonObjectBuilder.put("desc", description);
            return this;
        }
        public FormFieldBuilder eform(Form form) {
            jsonObjectBuilder.put("eform", form.asJsonValue());
            return this;
        }
        public FormFieldBuilder equalTo(EqualsFormFieldMember paths) {
            jsonObjectBuilder.put("equals", paths.asJsonValue());
            return this;
        }
        public FormFieldBuilder form(Form form) {
            jsonObjectBuilder.put("form", form.asJsonValue());
            return this;
        }
        public FormFieldBuilder min(Integer min) {
            jsonObjectBuilder.put("min", min);
            return this;
        }
        public FormFieldBuilder min(BigDecimal min) {
            jsonObjectBuilder.put("min", min);
            return this;
        }
        public FormFieldBuilder minlength(Integer minlength) {
            jsonObjectBuilder.put("minlength", minlength);
            return this;
        }
        public FormFieldBuilder max(Integer max) {
            jsonObjectBuilder.put("max", max);
            return this;
        }
        public FormFieldBuilder max(BigDecimal max) {
            jsonObjectBuilder.put("max", max);
            return this;
        }
        public FormFieldBuilder maxlength(Integer maxlength) {
            jsonObjectBuilder.put("maxlength", maxlength);
            return this;
        }
        public FormFieldBuilder mustBeNull(Boolean mustBeNull) {
            jsonObjectBuilder.put("mustBeNull", mustBeNull);
            return this;
        }
        public FormFieldBuilder nullable(Boolean nullable) {
            jsonObjectBuilder.put("nullable", nullable);
            return this;
        }
        public FormFieldBuilder pattern(String pattern) {
            jsonObjectBuilder.put("pattern", pattern);
            return this;
        }
        public FormFieldBuilder required(Boolean required) {
            jsonObjectBuilder.put("required", required);
            return this;
        }
        public FormFieldBuilder type(Type type) {
            jsonObjectBuilder.put("type", type);
            return this;
        }
        public FormFieldBuilder maxsize(Integer maxsize) {
            jsonObjectBuilder.put("maxsize", maxsize);
            return this;
        }
        public FormFieldBuilder minsize(Integer minsize) {
            jsonObjectBuilder.put("minsize", minsize);
            return this;
        }
        public FormField build() {
            return new FormField(jsonObjectBuilder.build().jsonValue());
        }
    }
}
    © 2015 - 2025 Weber Informatics LLC | Privacy Policy