Fix the tosca converter
[clamp.git] / src / main / java / org / onap / clamp / clds / tosca / update / Template.java
index 3445906..6a531ae 100644 (file)
@@ -23,7 +23,9 @@
 
 package org.onap.clamp.clds.tosca.update;
 
+import com.google.gson.JsonObject;
 import java.util.ArrayList;
+import java.util.List;
 
 public class Template {
 
@@ -31,16 +33,16 @@ public class Template {
      * name parameter is used as "key", in the LinkedHashMap of Templates.
      */
     private String name;
-    private ArrayList<String> fields;
+    private List<TemplateField> templateFields;
 
     public Template(String name) {
         this.name = name;
-        this.fields = new ArrayList<String>();
+        this.templateFields = new ArrayList<>();
     }
 
-    public Template(String name, ArrayList<String> fields) {
+    public Template(String name, List<TemplateField> templateFields) {
         this.name = name;
-        this.fields = fields;
+        this.templateFields = templateFields;
     }
 
     public String getName() {
@@ -51,24 +53,92 @@ public class Template {
         this.name = name;
     }
 
-    public ArrayList<String> getFields() {
-        return fields;
+    public List<TemplateField> getTemplateFields() {
+        return templateFields;
     }
 
-    public void setFields(ArrayList<String> fields) {
-        this.fields = fields;
+    public void setTemplateFields(List<TemplateField> templateFields) {
+        this.templateFields = templateFields;
     }
 
-    public boolean hasFields(String name) {
-        return fields.contains(name);
+    /**
+     * Search in fields if fieldName exists.
+     *
+     * @param fieldName The field name
+     * @return Ture if it exists, false otherwise
+     */
+    public boolean hasFields(String fieldName) {
+        for (TemplateField templateField : this.getTemplateFields()) {
+            if (templateField.getTitle().equals(fieldName)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Get a specific Field.
+     *
+     * @param fieldName The field name
+     * @return THe Field found
+     */
+    public TemplateField getSpecificField(String fieldName) {
+        for (TemplateField templateField : this.getTemplateFields()) {
+            if (templateField.getTitle().equals(fieldName)) {
+                return templateField;
+            }
+        }
+        return null;
+    }
+
+    public void addField(TemplateField templateField) {
+        templateFields.add(templateField);
     }
 
-    public void addField(String field) {
-        fields.add(field);
+    public void removeField(TemplateField templateField) {
+        templateFields.remove(templateField);
+    }
+
+    /**
+     * Enable or disable the visibility.
+     *
+     * @param nameField THe field name
+     * @param state True or false
+     */
+    public void setVisibility(String nameField, boolean state) {
+        for (TemplateField templateField : this.templateFields) {
+            if (templateField.getTitle().equals(nameField)) {
+                templateField.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 (TemplateField templateField : this.templateFields) {
+            if (templateField.getTitle().equals(nameField)) {
+                templateField.setStaticValue(state);
+            }
+        }
     }
 
-    public void removeField(String field) {
-        fields.remove(field);
+    /**
+     * 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 (TemplateField templateField : this.templateFields) {
+            if (templateField.getTitle().equals(nameField)) {
+                templateField.setValue(newValue);
+            }
+        }
     }
 
     /**
@@ -78,27 +148,77 @@ public class Template {
      * @return a boolean
      */
     public boolean checkFields(Template template) {
-
         boolean duplicateFields = false;
-        if (template.getFields().size() == this.getFields().size()) {
+        if (template.getTemplateFields().size() == this.getTemplateFields().size()) {
             int countMatchingFields = 0;
             //loop each component of first
-            for (String templateField : template.getFields()) {
-                //if component.key is present in the second
-                if (this.getFields().contains(templateField)) {
-                    countMatchingFields++;
+            for (TemplateField templateFieldToCheck : template.getTemplateFields()) {
+                for (TemplateField templateField : this.getTemplateFields()) {
+                    if (templateFieldToCheck.compareWithField(templateField)) {
+                        countMatchingFields++;
+                    }
                 }
             }
 
-            if (template.getFields().size() == countMatchingFields) {
+            if (template.getTemplateFields().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)) {
+            TemplateField toInject = this.getSpecificField(fieldName);
+            jsonSchema.addProperty(fieldName, (String) toInject.getValue());
+        }
+    }
+
     @Override
     public String toString() {
-        return " fields : " + fields;
+        return " templateFields : " + templateFields;
     }
 }