aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/tosca/update/Template.java
blob: 4507e3d717b9548c81548a4cf86e136cb96fce75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END============================================
 * ===================================================================
 *
 */

package org.onap.clamp.clds.tosca.update;

import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;

public class Template {

    /**
     * name parameter is used as "key", in the LinkedHashMap of Templates.
     */
    private String name;
    private List<Field> fields;

    public Template(String name) {
        this.name = name;
        this.fields = new ArrayList<>();
    }

    public Template(String name, List<Field> fields) {
        this.name = name;
        this.fields = fields;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Field> getFields() {
        return fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

    /**
     * Search in fields if fieldName exists.
     *
     * @param fieldName The field name
     * @return Ture if it exists, false otherwise
     */
    public boolean hasFields(String fieldName) {
        for (Field field : this.getFields()) {
            if (field.getTitle().equals(fieldName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Get a specific Field.
     *
     * @param fieldName The field name
     * @return THe Field found
     */
    public Field getSpecificField(String fieldName) {
        for (Field field : this.getFields()) {
            if (field.getTitle().equals(fieldName)) {
                return field;
            }
        }
        return null;
    }

    public void addField(Field field) {
        fields.add(field);
    }

    public void removeField(Field field) {
        fields.remove(field);
    }

    /**
     * Enable or disable the visibility.
     *
     * @param nameField THe field name
     * @param state True or false
     */
    public void setVisibility(String nameField, boolean state) {
        for (Field field : this.fields) {
            if (field.getTitle().equals(nameField)) {
                field.setVisible(state);
            }
        }
    }

    /**
     * This method defines if a field is static or not.
     *
     * @param nameField The name of the field
     * @param state true or false
     */
    public void setStatic(String nameField, boolean state) {
        for (Field field : this.fields) {
            if (field.getTitle().equals(nameField)) {
                field.setStaticValue(state);
            }
        }
    }

    /**
     * This method updates the value of a specfic field.
     *
     * @param nameField The name of the field
     * @param newValue The new value as Object
     */
    public void updateValueField(String nameField, Object newValue) {
        for (Field field : this.fields) {
            if (field.getTitle().equals(nameField)) {
                field.setValue(newValue);
            }
        }
    }

    /**
     * Compare two templates : size and their contents.
     *
     * @param template the template
     * @return a boolean
     */
    public boolean checkFields(Template template) {
        boolean duplicateFields = false;
        if (template.getFields().size() == this.getFields().size()) {
            int countMatchingFields = 0;
            //loop each component of first
            for (Field templateFieldToCheck : template.getFields()) {
                for (Field templateField : this.getFields()) {
                    if (templateFieldToCheck.compareWithField(templateField)) {
                        countMatchingFields++;
                    }
                }
            }

            if (template.getFields().size() == countMatchingFields) {
                duplicateFields = true;
            }
        }
        return duplicateFields;
    }

    /**
     * This method gets the specific field status.
     *
     * @param field The field name
     * @return true or false
     */
    public boolean fieldStaticStatus(String field) {
        if (this.hasFields(field) && this.getSpecificField(field).getStaticValue().equals(true)
                && this.getSpecificField(field).getValue() != null) {
            return true;
        }
        return false;
    }

    public boolean isVisible(String field) {
        return this.getSpecificField(field).getVisible();
    }

    /**
     * Set the value of a property of the Field in the json.
     *
     * @param jsonSchema The Json schema
     * @param fieldName The Field name
     * @param value The value
     */
    public void setValue(JsonObject jsonSchema, String fieldName, String value) {
        if (isVisible(fieldName)) {
            if (fieldStaticStatus(fieldName)) {
                String defaultValue = (String) this.getSpecificField(fieldName).getValue();
                jsonSchema.addProperty(fieldName, defaultValue);
            }
            else {
                jsonSchema.addProperty(fieldName, value);
            }
        }
    }

    /**
     * Inject a static value in the json.
     *
     * @param jsonSchema The json schema object
     * @param fieldName The field name
     */
    public void injectStaticValue(JsonObject jsonSchema, String fieldName) {
        if (isVisible(fieldName)) {
            Field toInject = this.getSpecificField(fieldName);
            jsonSchema.addProperty(fieldName, (String) toInject.getValue());
        }
    }

    @Override
    public String toString() {
        return " fields : " + fields;
    }
}