From 8fb9f91d90ecaddc8a7f06dddff44018ccc4de9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Wed, 20 Nov 2019 17:36:56 +0100 Subject: Add attribute form controllers --- .../pl/edu/mimuw/cloudatlas/client/Attribute.java | 51 ++++++++++++++++++++++ .../mimuw/cloudatlas/client/ClientController.java | 11 +++++ 2 files changed, 62 insertions(+) create mode 100644 src/main/java/pl/edu/mimuw/cloudatlas/client/Attribute.java (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/client') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/Attribute.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/Attribute.java new file mode 100644 index 0000000..abaa02a --- /dev/null +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/Attribute.java @@ -0,0 +1,51 @@ +package pl.edu.mimuw.cloudatlas.client; + +import pl.edu.mimuw.cloudatlas.model.Value; + +public class Attribute { + private String zoneName; + private String attributeName; + private String valueString; + private String attributeType; + private Value value; + + public String getZoneName() { + return zoneName; + } + + public void setZoneName(String zoneName) { + this.zoneName = zoneName; + } + + public String getAttributeName() { + return attributeName; + } + + public void setAttributeName(String attributeName) { + this.attributeName = attributeName; + } + + public String getValueString() { + return valueString; + } + + public void setValueString(String valueString) { + this.valueString = valueString; + } + + public Value getValue() { + return value; + } + + public void setValue(Value value) { + this.value = value; + } + + public String getAttributeType() { + return attributeType; + } + + public void setAttributeType(String attributeType) { + this.attributeType = attributeType; + } +} 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 31d1c06..27c7ad4 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -123,6 +123,17 @@ public class ClientController { @GetMapping("/attribs") public String attribPage(Model model) { + model.addAttribute("attributeObject", new Attribute()); + return "attribForm"; + } + + @PostMapping("/attribs") + public String attribPage(@ModelAttribute Attribute attributeObject, Model model) { + return "attribForm"; + } + + @GetMapping("/values") + public String valuesPage(Model model) { return "attribChart"; } } -- cgit v1.2.3 From 75208adcf60b4b3a48b96be70ff9352683a37075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Thu, 21 Nov 2019 10:00:50 +0100 Subject: Refactor contact value parsing --- .../mimuw/cloudatlas/client/ClientController.java | 36 +++++++++++++--------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/client') 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 27c7ad4..922c357 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -85,26 +85,34 @@ public class ClientController { return "contactsForm"; } - @PostMapping("/contacts") - public String contactPage(@ModelAttribute ContactsString contactsObject, Model model) { - boolean success = true; + private Set parseContactsString(ContactsString contactsInput) throws Exception { Gson gson = new Gson(); - Map contactStrings = gson.fromJson(contactsObject.getString(), Map.class); + Map contactStrings = gson.fromJson(contactsInput.getString(), Map.class); Set contactObjects = new HashSet(); ArrayList cAddr; byte[] inetArray = new byte[4]; - try { - for (Map.Entry cursor : contactStrings.entrySet()) { - cAddr = cursor.getValue(); // gson always reads numerical values as doubles - for (int i = 0; i < 4; i++) { - inetArray[i] = (byte) cAddr.get(i).doubleValue(); - } - contactObjects.add(new ValueContact( - new PathName(cursor.getKey()), - InetAddress.getByAddress(inetArray)) - ); + for (Map.Entry cursor : contactStrings.entrySet()) { + cAddr = cursor.getValue(); // gson always reads numerical values as doubles + for (int i = 0; i < 4; i++) { + inetArray[i] = (byte) cAddr.get(i).doubleValue(); } + contactObjects.add(new ValueContact( + new PathName(cursor.getKey()), + InetAddress.getByAddress(inetArray)) + ); + } + + return contactObjects; + } + + @PostMapping("/contacts") + public String contactPage(@ModelAttribute ContactsString contactsObject, Model model) { + boolean success = true; + Set contactObjects; + + try { + contactObjects = parseContactsString(contactsObject); this.api.setFallbackContacts(contactObjects); } catch (Exception e) { success = false; -- cgit v1.2.3 From eae806191b4b018de807a6920df9cd7c04ad8404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Thu, 21 Nov 2019 10:35:08 +0100 Subject: Add attribute submission backend --- .../mimuw/cloudatlas/client/ClientController.java | 67 +++++++++++++++++++++- 1 file changed, 64 insertions(+), 3 deletions(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/client') 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 922c357..e579a38 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -1,12 +1,12 @@ package pl.edu.mimuw.cloudatlas.client; import com.google.gson.Gson; +import com.sun.jdi.BooleanValue; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; import pl.edu.mimuw.cloudatlas.api.Api; -import pl.edu.mimuw.cloudatlas.model.PathName; -import pl.edu.mimuw.cloudatlas.model.ValueContact; +import pl.edu.mimuw.cloudatlas.model.*; import java.net.InetAddress; import java.net.UnknownHostException; @@ -135,9 +135,70 @@ public class ClientController { return "attribForm"; } + private Value parseAttributeValue(Attribute attributeObject) throws Exception { + Value attributeValue = null; + + switch (attributeObject.getAttributeType()) { + case "Boolean": + attributeValue = attributeObject.getValueString().toLowerCase().equals("true") ? + new ValueBoolean(true) : + new ValueBoolean(false); + break; + case "Double": + attributeValue = new ValueDouble(Double.parseDouble(attributeObject.getValueString())); + break; + case "Int": + attributeValue = new ValueInt(Long.parseLong(attributeObject.getValueString())); + break; + case "String": + attributeValue = new ValueString(attributeObject.getValueString()); + break; + case "Time": + attributeValue = new ValueTime(Long.parseLong(attributeObject.getValueString())); + break; + case "Duration": + attributeValue = new ValueDuration(attributeObject.getValueString()); + break; + case "Contact": + ContactsString contactsString = new ContactsString(); + contactsString.setString(attributeObject.getValueString()); + attributeValue = parseContactsString(contactsString).iterator().next(); + break; + case "Query": + attributeValue = new ValueQuery(attributeObject.getValueString()); + break; + default: + String errMsg = "Value type not supported: " + attributeObject.getAttributeType(); + throw new UnsupportedOperationException(errMsg); + } + + return attributeValue; + } + @PostMapping("/attribs") public String attribPage(@ModelAttribute Attribute attributeObject, Model model) { - return "attribForm"; + boolean success = true; + Value attributeValue; + + try { + attributeValue = parseAttributeValue(attributeObject); + api.setAttributeValue( + attributeObject.getZoneName(), + attributeObject.getAttributeName(), + attributeValue); + } catch (Exception e) { + success = false; + System.err.println("Client exception:"); + e.printStackTrace(); + } + + if (success) { + model.addAttribute("homeMessage", "Attribute submitted successfully"); + } else { + model.addAttribute("homeMessage", "Attribute submission failed"); + } + + return "home"; } @GetMapping("/values") -- cgit v1.2.3 From 02f98aed224580d05deb4b40eac4a11c36d39498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Thu, 21 Nov 2019 10:43:58 +0100 Subject: Fix erroneous import --- src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java | 1 - 1 file changed, 1 deletion(-) (limited to 'src/main/java/pl/edu/mimuw/cloudatlas/client') 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 e579a38..022c665 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -1,7 +1,6 @@ package pl.edu.mimuw.cloudatlas.client; import com.google.gson.Gson; -import com.sun.jdi.BooleanValue; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; -- cgit v1.2.3