From 79729ffe6237aaca294971134a047ffe3972bfd6 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Tue, 12 May 2020 14:52:47 -0400 Subject: [PATCH] Add properties from DataElement Signed-off-by: Daniel Bluhm --- .../main/resources/templates/DataElement.vm | 105 ++++++++++++++++-- .../java/org/eclipse/ice/renderer/Person.java | 3 +- 2 files changed, 96 insertions(+), 12 deletions(-) diff --git a/org.eclipse.ice.dev.annotations/src/main/resources/templates/DataElement.vm b/org.eclipse.ice.dev.annotations/src/main/resources/templates/DataElement.vm index d6004ce58..7c79dcf57 100644 --- a/org.eclipse.ice.dev.annotations/src/main/resources/templates/DataElement.vm +++ b/org.eclipse.ice.dev.annotations/src/main/resources/templates/DataElement.vm @@ -2,16 +2,23 @@ package $package; #end +import java.io.Serializable; import java.util.UUID; import lombok.Data; +import org.eclipse.ice.renderer.JavascriptValidator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; /** * This is an implementation of $interface that satisfies the dependencies of * the @DataElement Annotation and was auto-generated by the ICE Framework. */ -@Data public class ${class} implements ${interface} { +@Data public class ${class} implements ${interface}, Serializable { /** * Logging tool @@ -24,29 +31,29 @@ import org.slf4j.LoggerFactory; private UUID privateId; /** - * a simple name for the data name + * A simple name for the data */ - private String name; + private String name = "name"; /** - * a simple description for the data + * A simple description for the data */ - private String description; + private String description = "description"; /** - * a unique identifier + * A unique identifier */ - private String id; + private String id = "0"; /** - * a comment that annotates the data in meaningful way + * A comment that annotates the data in meaningful way */ - private String comment; + private String comment = "no comment"; /** - * the context (a tag) in which the data should be considered + * The context (a tag) in which the data should be considered */ - private String context; + private String context = "default"; /** * This value is true if the element should be regarded as a client as required. @@ -59,10 +66,86 @@ import org.slf4j.LoggerFactory; */ private boolean secret; + /** + * The validator used to check the correctness of the data + */ + private JavascriptValidator<${class}> validator; + /** * Generated from DataField annotations */ #foreach($field in $fields) protected ${field.ClassName} ${field.Name}; #end + + /** + * This operation serializes the data element to a string in verified JSON. + * + * @return a JSON string describing the element + */ + public String toJSON() { + String value = null; + // Convert to json using Jackson + ObjectMapper mapper = new ObjectMapper(); + // Set visibility so that only methods are serialized. This removes duplication + // otherwise produced due to the convenience methods. + mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); + mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); + mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE); + try { + value = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); + } catch (JsonProcessingException e) { + logger.error("Unable to write DataElement to string!", e); + } + + return value; + } + + /** + * This operation deserializes a valid JSON string and tries to load it into the + * object. + * + * @param jsonDataElement the contents of this data element as JSON + */ + public void fromJSON(final String jsonDataElement) { + + // Load the data from the string with Jackson. + ObjectMapper mapper = new ObjectMapper(); + try { + JsonNode rootNode = mapper.readTree(jsonDataElement); + + // Static Fields + #foreach($prop in ["name", "description", "id", "comment", "context"]) + // $prop + JsonNode ${prop}Node = rootNode.get("$prop"); + $prop = mapper.treeToValue(${prop}Node, String.class); + + #end + // Required and secret booleans + JsonNode requiredNode = rootNode.get("required"); + required = mapper.treeToValue(requiredNode, Boolean.class); + JsonNode secretNode = rootNode.get("secret"); + secret = mapper.treeToValue(secretNode, Boolean.class); + + // Private ids + JsonNode privateIdNode = rootNode.get("privateId"); + privateId = mapper.treeToValue(privateIdNode, UUID.class); + + // Validators + JsonNode validatorNode = rootNode.get("validator"); + validator = mapper.treeToValue(validatorNode, validator.getClass()); + + // Dynamic Fields + #foreach($field in $fields) + // ${field.Name} + JsonNode ${field.Name}Node = rootNode.get("${field.Name}"); + ${field.Name} = mapper.treeToValue(${field.Name}Node, ${field.ClassName}.class); + + #end + } catch (JsonProcessingException e) { + logger.error("Unable to read DataElement from string!", e); + } + + return; + } } diff --git a/org.eclipse.ice.renderer/src/main/java/org/eclipse/ice/renderer/Person.java b/org.eclipse.ice.renderer/src/main/java/org/eclipse/ice/renderer/Person.java index ac8e2e6ee..e7ac76825 100644 --- a/org.eclipse.ice.renderer/src/main/java/org/eclipse/ice/renderer/Person.java +++ b/org.eclipse.ice.renderer/src/main/java/org/eclipse/ice/renderer/Person.java @@ -5,7 +5,8 @@ @DataElement @DataField(fieldName = "age", fieldType = String.class) -@DataField(fieldName = "name", fieldType = String.class) +@DataField(fieldName = "firstName", fieldType = String.class) +@DataField(fieldName = "lastName", fieldType = String.class) public interface Person { //void foo(); }