From f5be9e9bc2ea02713a32df99ef49d1a7e98ee450 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= <mag.grodzinska@gmail.com>
Date: Sun, 24 Nov 2019 20:16:37 +0100
Subject: Add complex type handling for attribute submissions to client

---
 .../mimuw/cloudatlas/client/AttributeInput.java    |  9 ++++
 .../mimuw/cloudatlas/client/ClientController.java  | 54 +++++++++++++++++++++-
 2 files changed, 61 insertions(+), 2 deletions(-)

(limited to 'src/main')

diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/AttributeInput.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/AttributeInput.java
index 58e1a30..b34468b 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/client/AttributeInput.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/AttributeInput.java
@@ -7,6 +7,7 @@ public class AttributeInput {
     private String attributeName;
     private String valueString;
     private String attributeType;
+    private String attributeComplexType;
     private Value value;
 
     public String getZoneName() {
@@ -48,4 +49,12 @@ public class AttributeInput {
     public void setAttributeType(String attributeType) {
         this.attributeType = attributeType;
     }
+
+    public String getAttributeComplexType() {
+        return attributeComplexType;
+    }
+
+    public void setAttributeComplexType(String attributeComplexType) {
+        this.attributeComplexType = attributeComplexType;
+    }
 }
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java
index ae649c0..80a331e 100644
--- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java
+++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java
@@ -142,7 +142,43 @@ public class ClientController {
         return "attribForm";
     }
 
+    private List<Value> parseCollectionAttributeValue(List values, ArrayList<String> types) throws Exception {
+        List<Value> resultValue = new ArrayList<Value>();
+        String currentTypeString = types.get(1);
+        AttributeInput attributeInput = new AttributeInput();
+        ArrayList<String> newTypes = new ArrayList<>(types.subList(1, types.size()));
+
+        for (int i = 0; i < values.size(); i++) {
+            if (currentTypeString.equals("List")) {
+                resultValue.add(parseListAttributeValue((List) values.get(i), newTypes));
+            } else if (currentTypeString.equals("Set")) {
+                resultValue.add(parseSetAttributeValue((List) values.get(i), newTypes));
+            } else {
+                attributeInput.setAttributeType(currentTypeString);
+                attributeInput.setValueString(values.get(i).toString());
+                resultValue.add(parseAttributeValue(attributeInput));
+            }
+        }
+
+        return resultValue;
+    }
+
+    private Value parseListAttributeValue(List values, ArrayList<String> types) throws Exception {
+        List<Value> listResultValue = parseCollectionAttributeValue(values, types);
+        ArrayList<Value> resultValue = new ArrayList<>(listResultValue);
+
+        return new ValueList(resultValue, resultValue.iterator().next().getType());
+    }
+
+    private Value parseSetAttributeValue(List values, ArrayList<String> types) throws Exception {
+        List<Value> listResultValue = parseCollectionAttributeValue(values, types);
+        HashSet<Value> resultValue = new HashSet<>(listResultValue);
+
+        return new ValueSet(resultValue, resultValue.iterator().next().getType());
+    }
+
     private Value parseAttributeValue(AttributeInput attributeObject) throws Exception {
+        Gson gson = new Gson();
         Value attributeValue = null;
 
         switch (attributeObject.getAttributeType()) {
@@ -155,7 +191,8 @@ public class ClientController {
                 attributeValue = new ValueDouble(Double.parseDouble(attributeObject.getValueString()));
                 break;
             case "Int":
-                attributeValue = new ValueInt(Long.parseLong(attributeObject.getValueString()));
+                Double tempDouble = Double.parseDouble(attributeObject.getValueString());
+                attributeValue = new ValueInt(tempDouble.longValue());
                 break;
             case "String":
                 attributeValue = new ValueString(attributeObject.getValueString());
@@ -172,8 +209,21 @@ public class ClientController {
                 attributeValue = parseContactsString(contactsString).iterator().next();
                 break;
             case "Query":
+                Map parsedQuery = gson.fromJson(attributeObject.getValueString(), Map.class);
                 attributeValue = new ValueQuery(attributeObject.getValueString());
                 break;
+            case "List":
+                List parsedListValue = gson.fromJson(attributeObject.getValueString(), List.class);
+                ArrayList<String> parsedListTypes = new ArrayList<>(Arrays.asList(
+                        attributeObject.getAttributeComplexType().replaceAll("\\s","").split(",")));
+                attributeValue = parseListAttributeValue(parsedListValue, parsedListTypes);
+                break;
+            case "Set":
+                List parsedSetValue = gson.fromJson(attributeObject.getValueString(), List.class);
+                ArrayList<String> parsedSetTypes = new ArrayList<>(Arrays.asList(
+                        attributeObject.getAttributeComplexType(). replaceAll("\\s","").split(",")));
+                attributeValue = parseSetAttributeValue(parsedSetValue, parsedSetTypes);
+                break;
             default:
                 String errMsg = "Value type not supported: " + attributeObject.getAttributeType();
                 throw new UnsupportedOperationException(errMsg);
@@ -329,7 +379,7 @@ public class ClientController {
         String jsonAttributes = "";
         Gson gson = new Gson();
         jsonAttributes = gson.toJson(valuesTable);
-        System.out.println(jsonAttributes);
+        // System.out.println(jsonAttributes);
         return jsonAttributes;
     }
 
-- 
cgit v1.2.3