Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Add properties from DataElement<T>
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <bluhmdj@ornl.gov>
  • Loading branch information
dbluhm committed May 13, 2020
1 parent 1c3ce0a commit 79729ff
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 12 deletions.
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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;
}
}
Expand Up @@ -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();
}

0 comments on commit 79729ff

Please sign in to comment.