4507e3d717b9548c81548a4cf86e136cb96fce75
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / Template.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;
25
26 import com.google.gson.JsonObject;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 public class Template {
31
32     /**
33      * name parameter is used as "key", in the LinkedHashMap of Templates.
34      */
35     private String name;
36     private List<Field> fields;
37
38     public Template(String name) {
39         this.name = name;
40         this.fields = new ArrayList<>();
41     }
42
43     public Template(String name, List<Field> fields) {
44         this.name = name;
45         this.fields = fields;
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<Field> getFields() {
57         return fields;
58     }
59
60     public void setFields(List<Field> fields) {
61         this.fields = fields;
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 (Field field : this.getFields()) {
72             if (field.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 Field getSpecificField(String fieldName) {
86         for (Field field : this.getFields()) {
87             if (field.getTitle().equals(fieldName)) {
88                 return field;
89             }
90         }
91         return null;
92     }
93
94     public void addField(Field field) {
95         fields.add(field);
96     }
97
98     public void removeField(Field field) {
99         fields.remove(field);
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 (Field field : this.fields) {
110             if (field.getTitle().equals(nameField)) {
111                 field.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 (Field field : this.fields) {
124             if (field.getTitle().equals(nameField)) {
125                 field.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 (Field field : this.fields) {
138             if (field.getTitle().equals(nameField)) {
139                 field.setValue(newValue);
140             }
141         }
142     }
143
144     /**
145      * Compare two templates : size and their contents.
146      *
147      * @param template the template
148      * @return a boolean
149      */
150     public boolean checkFields(Template template) {
151         boolean duplicateFields = false;
152         if (template.getFields().size() == this.getFields().size()) {
153             int countMatchingFields = 0;
154             //loop each component of first
155             for (Field templateFieldToCheck : template.getFields()) {
156                 for (Field templateField : this.getFields()) {
157                     if (templateFieldToCheck.compareWithField(templateField)) {
158                         countMatchingFields++;
159                     }
160                 }
161             }
162
163             if (template.getFields().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             Field toInject = this.getSpecificField(fieldName);
216             jsonSchema.addProperty(fieldName, (String) toInject.getValue());
217         }
218     }
219
220     @Override
221     public String toString() {
222         return " fields : " + fields;
223     }
224 }