2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.tosca.update;
26 import com.google.gson.JsonObject;
27 import java.util.ArrayList;
28 import java.util.List;
30 public class Template {
33 * name parameter is used as "key", in the LinkedHashMap of Templates.
36 private List<TemplateField> templateFields;
38 public Template(String name) {
40 this.templateFields = new ArrayList<>();
43 public Template(String name, List<TemplateField> templateFields) {
45 this.templateFields = templateFields;
48 public String getName() {
52 public void setName(String name) {
56 public List<TemplateField> getTemplateFields() {
57 return templateFields;
60 public void setTemplateFields(List<TemplateField> templateFields) {
61 this.templateFields = templateFields;
65 * Search in fields if fieldName exists.
67 * @param fieldName The field name
68 * @return Ture if it exists, false otherwise
70 public boolean hasFields(String fieldName) {
71 for (TemplateField templateField : this.getTemplateFields()) {
72 if (templateField.getTitle().equals(fieldName)) {
80 * Get a specific Field.
82 * @param fieldName The field name
83 * @return THe Field found
85 public TemplateField getSpecificField(String fieldName) {
86 for (TemplateField templateField : this.getTemplateFields()) {
87 if (templateField.getTitle().equals(fieldName)) {
94 public void addField(TemplateField templateField) {
95 templateFields.add(templateField);
98 public void removeField(TemplateField templateField) {
99 templateFields.remove(templateField);
103 * Enable or disable the visibility.
105 * @param nameField THe field name
106 * @param state True or false
108 public void setVisibility(String nameField, boolean state) {
109 for (TemplateField templateField : this.templateFields) {
110 if (templateField.getTitle().equals(nameField)) {
111 templateField.setVisible(state);
117 * This method defines if a field is static or not.
119 * @param nameField The name of the field
120 * @param state true or false
122 public void setStatic(String nameField, boolean state) {
123 for (TemplateField templateField : this.templateFields) {
124 if (templateField.getTitle().equals(nameField)) {
125 templateField.setStaticValue(state);
131 * This method updates the value of a specfic field.
133 * @param nameField The name of the field
134 * @param newValue The new value as Object
136 public void updateValueField(String nameField, Object newValue) {
137 for (TemplateField templateField : this.templateFields) {
138 if (templateField.getTitle().equals(nameField)) {
139 templateField.setValue(newValue);
145 * Compare two templates : size and their contents.
147 * @param template the template
150 public boolean checkFields(Template template) {
151 boolean duplicateFields = false;
152 if (template.getTemplateFields().size() == this.getTemplateFields().size()) {
153 int countMatchingFields = 0;
154 //loop each component of first
155 for (TemplateField templateFieldToCheck : template.getTemplateFields()) {
156 for (TemplateField templateField : this.getTemplateFields()) {
157 if (templateFieldToCheck.compareWithField(templateField)) {
158 countMatchingFields++;
163 if (template.getTemplateFields().size() == countMatchingFields) {
164 duplicateFields = true;
167 return duplicateFields;
171 * This method gets the specific field status.
173 * @param field The field name
174 * @return true or false
176 public boolean fieldStaticStatus(String field) {
177 if (this.hasFields(field) && this.getSpecificField(field).getStaticValue().equals(true)
178 && this.getSpecificField(field).getValue() != null) {
184 public boolean isVisible(String field) {
185 return this.getSpecificField(field).getVisible();
189 * Set the value of a property of the Field in the json.
191 * @param jsonSchema The Json schema
192 * @param fieldName The Field name
193 * @param value The value
195 public void setValue(JsonObject jsonSchema, String fieldName, String value) {
196 if (isVisible(fieldName)) {
197 if (fieldStaticStatus(fieldName)) {
198 String defaultValue = (String) this.getSpecificField(fieldName).getValue();
199 jsonSchema.addProperty(fieldName, defaultValue);
202 jsonSchema.addProperty(fieldName, value);
208 * Inject a static value in the json.
210 * @param jsonSchema The json schema object
211 * @param fieldName The field name
213 public void injectStaticValue(JsonObject jsonSchema, String fieldName) {
214 if (isVisible(fieldName)) {
215 TemplateField toInject = this.getSpecificField(fieldName);
216 jsonSchema.addProperty(fieldName, (String) toInject.getValue());
221 public String toString() {
222 return " templateFields : " + templateFields;