Merge "Fix the tosca converter"
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / templates / JsonTemplate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2020 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.tosca.update.templates;
25
26 import com.google.gson.JsonObject;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 public class JsonTemplate {
31
32     /**
33      * name parameter is used as "key", in the LinkedHashMap of Templates.
34      */
35     private String name;
36     private List<JsonTemplateField> jsonTemplateFields;
37
38     public JsonTemplate(String name) {
39         this.name = name;
40         this.jsonTemplateFields = new ArrayList<>();
41     }
42
43     public JsonTemplate(String name, List<JsonTemplateField> jsonTemplateFields) {
44         this.name = name;
45         this.jsonTemplateFields = jsonTemplateFields;
46     }
47
48     public String getName() {
49         return name;
50     }
51
52     public void setName(String name) {
53         this.name = name;
54     }
55
56     public List<JsonTemplateField> getJsonTemplateFields() {
57         return jsonTemplateFields;
58     }
59
60     public void setJsonTemplateFields(List<JsonTemplateField> jsonTemplateFields) {
61         this.jsonTemplateFields = jsonTemplateFields;
62     }
63
64     /**
65      * Search in fields if fieldName exists.
66      *
67      * @param fieldName The field name
68      * @return Ture if it exists, false otherwise
69      */
70     public boolean hasFields(String fieldName) {
71         for (JsonTemplateField jsonTemplateField : this.getJsonTemplateFields()) {
72             if (jsonTemplateField.getTitle().equals(fieldName)) {
73                 return true;
74             }
75         }
76         return false;
77     }
78
79     /**
80      * Get a specific Field.
81      *
82      * @param fieldName The field name
83      * @return THe Field found
84      */
85     public JsonTemplateField getSpecificField(String fieldName) {
86         for (JsonTemplateField jsonTemplateField : this.getJsonTemplateFields()) {
87             if (jsonTemplateField.getTitle().equals(fieldName)) {
88                 return jsonTemplateField;
89             }
90         }
91         return null;
92     }
93
94     public void addField(JsonTemplateField jsonTemplateField) {
95         jsonTemplateFields.add(jsonTemplateField);
96     }
97
98     public void removeField(JsonTemplateField jsonTemplateField) {
99         jsonTemplateFields.remove(jsonTemplateField);
100     }
101
102     /**
103      * Enable or disable the visibility.
104      *
105      * @param nameField THe field name
106      * @param state True or false
107      */
108     public void setVisibility(String nameField, boolean state) {
109         for (JsonTemplateField jsonTemplateField : this.jsonTemplateFields) {
110             if (jsonTemplateField.getTitle().equals(nameField)) {
111                 jsonTemplateField.setVisible(state);
112             }
113         }
114     }
115
116     /**
117      * This method defines if a field is static or not.
118      *
119      * @param nameField The name of the field
120      * @param state true or false
121      */
122     public void setStatic(String nameField, boolean state) {
123         for (JsonTemplateField jsonTemplateField : this.jsonTemplateFields) {
124             if (jsonTemplateField.getTitle().equals(nameField)) {
125                 jsonTemplateField.setStaticValue(state);
126             }
127         }
128     }
129
130     /**
131      * This method updates the value of a specfic field.
132      *
133      * @param nameField The name of the field
134      * @param newValue The new value as Object
135      */
136     public void updateValueField(String nameField, Object newValue) {
137         for (JsonTemplateField jsonTemplateField : this.jsonTemplateFields) {
138             if (jsonTemplateField.getTitle().equals(nameField)) {
139                 jsonTemplateField.setValue(newValue);
140             }
141         }
142     }
143
144     /**
145      * Compare two templates : size and their contents.
146      *
147      * @param jsonTemplate the template
148      * @return a boolean
149      */
150     public boolean checkFields(JsonTemplate jsonTemplate) {
151         boolean duplicateFields = false;
152         if (jsonTemplate.getJsonTemplateFields().size() == this.getJsonTemplateFields().size()) {
153             int countMatchingFields = 0;
154             //loop each component of first
155             for (JsonTemplateField jsonTemplateFieldToCheck : jsonTemplate.getJsonTemplateFields()) {
156                 for (JsonTemplateField jsonTemplateField : this.getJsonTemplateFields()) {
157                     if (jsonTemplateFieldToCheck.compareWithField(jsonTemplateField)) {
158                         countMatchingFields++;
159                     }
160                 }
161             }
162
163             if (jsonTemplate.getJsonTemplateFields().size() == countMatchingFields) {
164                 duplicateFields = true;
165             }
166         }
167         return duplicateFields;
168     }
169
170     /**
171      * This method gets the specific field status.
172      *
173      * @param field The field name
174      * @return true or false
175      */
176     public boolean fieldStaticStatus(String field) {
177         if (this.hasFields(field) && this.getSpecificField(field).getStaticValue().equals(true)
178                 && this.getSpecificField(field).getValue() != null) {
179             return true;
180         }
181         return false;
182     }
183
184     public boolean isVisible(String field) {
185         return this.getSpecificField(field).getVisible();
186     }
187
188     /**
189      * Set the value of a property of the Field in the json.
190      *
191      * @param jsonSchema The Json schema
192      * @param fieldName The Field name
193      * @param value The value
194      */
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);
200             }
201             else {
202                 jsonSchema.addProperty(fieldName, value);
203             }
204         }
205     }
206
207     /**
208      * Inject a static value in the json.
209      *
210      * @param jsonSchema The json schema object
211      * @param fieldName The field name
212      */
213     public void injectStaticValue(JsonObject jsonSchema, String fieldName) {
214         if (isVisible(fieldName)) {
215             JsonTemplateField toInject = this.getSpecificField(fieldName);
216             jsonSchema.addProperty(fieldName, (String) toInject.getValue());
217         }
218     }
219
220     @Override
221     public String toString() {
222         return " templateFields : " + jsonTemplateFields;
223     }
224 }