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

com.zuunr.forms.Form 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.json.JsonArray;
import com.zuunr.json.JsonObject;
import com.zuunr.json.JsonValue;
import com.zuunr.json.TypedObject;

public class Form extends TypedObject
{ public static final Form EMPTY = JsonObject.EMPTY .put("value", JsonArray.EMPTY) .jsonValue() .as(Form.class); private JsonObject formFieldsByName; private Form(JsonObject jsonObject) { super(jsonObject); } private static final JsonObject fromSource(JsonValue source) { JsonObject sourceJsonObject = source.getValue(JsonObject.class); JsonObject.JsonObjectBuilder builder = sourceJsonObject.builder(); builder.put("exclusive", sourceJsonObject.get("exclusive", JsonValue.TRUE).getValue(Boolean.class)); builder.put("value", sourceJsonObject.get("value", JsonArray.EMPTY).as(FormFields.class)); return builder.build(); } private Form(JsonValue jsonValue) { super(fromSource(jsonValue)); } public FormBuilder builder() { return new FormBuilder(jsonObject); } public FormFields formFields() { return jsonObject.get("value", JsonValue.NULL).as(FormFields.class); } public JsonArray value() { return jsonObject.get("value", JsonValue.NULL).getValue(JsonArray.class); } public Boolean required() { return jsonObject.get("required", JsonValue.FALSE).getValue(Boolean.class); } public Boolean mustBeNull() { return jsonObject.get("mustBeNull", JsonValue.FALSE).getValue(Boolean.class); } public Boolean nullable() { return jsonObject.get("nullable", JsonValue.FALSE).getValue(Boolean.class); } public Boolean exclusive() { return jsonObject.get("exclusive", JsonValue.TRUE).getValue(Boolean.class); } public FormField formField(String fieldName) { return formFieldsByName().get(fieldName, JsonValue.NULL).as(FormField.class); } public JsonObject formFieldsByName() { if (formFieldsByName == null) { JsonObject.JsonObjectBuilder formFieldsCacheBuilder = JsonObject.EMPTY.builder(); for (FormField formField : formFields().asList()) { formFieldsCacheBuilder = formFieldsCacheBuilder.put(formField.name(), formField); } formFieldsByName = formFieldsCacheBuilder.build(); } return formFieldsByName; } @Override protected Form createNew(JsonObject jsonObject) { return new Form(jsonObject); } @Override public String toString() { return jsonObject.toString(); } public static class FormBuilder { JsonObject.JsonObjectBuilder jsonObjectBuilder; private FormBuilder(JsonObject jsonObject) { jsonObjectBuilder = jsonObject.builder(); } public FormBuilder value(FormFields formFields) { jsonObjectBuilder.put("value", formFields); return this; } public FormBuilder value(FormFields.FormFieldsBuilder formFieldsBuilder) { jsonObjectBuilder.put("value", formFieldsBuilder.build()); return this; } public FormBuilder exclusive(Boolean exclusive) { jsonObjectBuilder.put("exclusive", exclusive); return this; } public Form build() { return new Form(jsonObjectBuilder.build().jsonValue()); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy