Person.java
, PersonImplementation.java
, and PersonPersistenceHandler.java
are all generated from PersonSpec.java
through the @DataElement
and @DataField
Annotations.
Last active
June 22, 2020 21:05
-
-
Save dbluhm/fdb6454319a6a8ac911e238015a99388 to your computer and use it in GitHub Desktop.
Eclipse ICE @dataelement Generation Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.ice.renderer; | |
import org.eclipse.ice.dev.annotations.IDataElement; | |
/** | |
* This interface satisfies the dependencies of the @DataElement Annotation and | |
* was auto-generated by the ICE Framework. | |
*/ | |
public interface Person extends IDataElement<Person> { | |
/** | |
* Get age. | |
* @return age | |
*/ | |
public int getAge(); | |
/** | |
* Set age. | |
*/ | |
public void setAge(int age); | |
/** | |
* Get firstName. | |
* @return firstName | |
*/ | |
public java.lang.String getFirstName(); | |
/** | |
* Set firstName. | |
*/ | |
public void setFirstName(java.lang.String firstName); | |
/** | |
* Get lastName. | |
* @return lastName | |
*/ | |
public java.lang.String getLastName(); | |
/** | |
* Set lastName. | |
*/ | |
public void setLastName(java.lang.String lastName); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.ice.renderer; | |
import java.io.Serializable; | |
import java.util.Collections; | |
import java.util.Map; | |
import java.util.HashMap; | |
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; | |
import lombok.AccessLevel; | |
import lombok.Data; | |
import lombok.Getter; | |
import lombok.NoArgsConstructor; | |
import lombok.NonNull; | |
import lombok.Setter; | |
/** | |
* This is an implementation of Person that satisfies the dependencies of | |
* the @DataElement Annotation and was auto-generated by the ICE Framework. | |
*/ | |
@Data | |
@NoArgsConstructor | |
public class PersonImplementation implements Person, Serializable { | |
/** | |
* Logging tool | |
*/ | |
private static final Logger logger = LoggerFactory.getLogger(PersonImplementation.class); | |
/** | |
* The private UUID of this element. This field is left out of matches(). | |
*/ | |
@NonNull | |
@Getter(AccessLevel.NONE) | |
@Setter(AccessLevel.NONE) | |
protected java.util.UUID privateId = java.util.UUID.randomUUID(); | |
/** | |
* A unique identifier for this element. | |
*/ | |
protected long id = 0L; | |
/** | |
* A simple name for the data. | |
*/ | |
@NonNull | |
protected java.lang.String name = "name"; | |
/** | |
* A simple description of the data | |
*/ | |
@NonNull | |
protected java.lang.String description = "description"; | |
/** | |
* A comment that annotates the data in a meaningful way. | |
*/ | |
@NonNull | |
protected java.lang.String comment = "no comment"; | |
/** | |
* The context (a tag) in which the data should be considered. | |
*/ | |
@NonNull | |
protected java.lang.String context = "default"; | |
/** | |
* This value is true if the element should be regarded by the client as required. | |
*/ | |
protected boolean required = false; | |
/** | |
* This value is true if the element should be regarded as a secret by the client, such as for passwords. | |
*/ | |
protected boolean secret = false; | |
/** | |
* The validator used to check the correctness of the data. | |
*/ | |
protected org.eclipse.ice.dev.annotations.JavascriptValidator<Person> validator; | |
/** | |
* The person's age. | |
*/ | |
private int age = 42; | |
/** | |
* The person's first name. | |
*/ | |
@NonNull | |
private java.lang.String firstName = "Bob"; | |
/** | |
* The person's last name. | |
*/ | |
@NonNull | |
private java.lang.String lastName = "Builder"; | |
/** | |
* An example constant value. This one probably doesn't actually make sense. | |
*/ | |
static public final java.lang.String COLLECTION = "people"; | |
/** | |
* Get privateId by alias UUID. | |
* @return privateId | |
*/ | |
public java.util.UUID getUUID() { | |
return privateId; | |
} | |
/** | |
* Copy constructor for PersonImplementation. | |
* @param other Instance of PersonImplementation to copy | |
* @throws Exception if other is null or not of type PersonImplementation or other errors. | |
*/ | |
public PersonImplementation(PersonImplementation other) throws Exception { | |
if (other == null) { | |
throw (new Exception("PersonImplementation to copy cannot be null.")); | |
} | |
if (!(other instanceof PersonImplementation)) { | |
throw (new Exception("PersonImplementation can copy only from other instances of PersonImplementation.")); | |
} | |
this.privateId = other.privateId; | |
this.id = other.id; | |
this.name = other.name; | |
this.description = other.description; | |
this.comment = other.comment; | |
this.context = other.context; | |
this.required = other.required; | |
this.secret = other.secret; | |
this.validator = other.validator; | |
this.age = other.age; | |
this.firstName = other.firstName; | |
this.lastName = other.lastName; | |
} | |
/** | |
* This operation clones the object. Note that it differs from the base class | |
* implementation in that it will return null if it cannot create the clone to | |
* promote fast failure. See {@link java.lang.Object#clone()}; | |
*/ | |
@Override | |
public Object clone() { | |
try { | |
// Call the copy constructor to create the clone. | |
return new PersonImplementation(this); | |
} catch (Exception e) { | |
logger.error("Unable to clone PersonImplementation!", e); | |
return null; | |
} | |
} | |
/** | |
* This function checks deep equality of DataElements to see if all members are | |
* equal ("match") with the exception of fields with match set to false (such | |
* as an automatically generated UUID). This is important for checking if two | |
* objects were generated separately but are otherwise equal. | |
* | |
* @param o the other element to compare | |
* @return true if all members of the element except excluded fields match | |
* this element. | |
*/ | |
public boolean matches(Object o) { | |
boolean retval = false; | |
// Outer check for null comparisons | |
if (o != null) { | |
// Next check for shallow comparison | |
if (this != o) { | |
if (o instanceof PersonImplementation) { | |
PersonImplementation other = (PersonImplementation) o; | |
// Separate boolean checks used to enable better catching | |
// by debuggers. | |
boolean idMatch = | |
this.id == other.id; | |
boolean nameMatch = | |
this.name.equals(other.name); | |
boolean descriptionMatch = | |
this.description.equals(other.description); | |
boolean commentMatch = | |
this.comment.equals(other.comment); | |
boolean contextMatch = | |
this.context.equals(other.context); | |
boolean requiredMatch = | |
this.required == other.required; | |
boolean secretMatch = | |
this.secret == other.secret; | |
boolean validatorMatch = | |
this.validator == null ? | |
this.validator == other.validator : | |
this.validator.equals(other.validator); | |
boolean ageMatch = | |
this.age == other.age; | |
boolean firstNameMatch = | |
this.firstName.equals(other.firstName); | |
boolean lastNameMatch = | |
this.lastName.equals(other.lastName); | |
boolean COLLECTIONMatch = | |
this.COLLECTION.equals(other.COLLECTION); | |
retval = | |
idMatch && nameMatch && descriptionMatch && | |
commentMatch && contextMatch && requiredMatch && | |
secretMatch && validatorMatch && ageMatch && | |
firstNameMatch && lastNameMatch && COLLECTIONMatch; | |
} | |
} else { | |
// This should be true if they are the same because the deep comparison is | |
// performed otherwise. | |
retval = true; | |
} | |
} | |
return retval; | |
} | |
/** | |
* 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; | |
} | |
/** | |
* Load DataElement from JsonNode. | |
* @param rootNode DataElement root as JsonNode | |
* @param mapper ObjectMapper to convert values | |
*/ | |
private void fromJsonNode(final JsonNode rootNode, final ObjectMapper mapper) throws JsonProcessingException { | |
// privateId | |
JsonNode privateIdNode = rootNode.get("privateId"); | |
privateId = mapper.treeToValue(privateIdNode, java.util.UUID.class); | |
// id | |
JsonNode idNode = rootNode.get("id"); | |
id = mapper.treeToValue(idNode, long.class); | |
// name | |
JsonNode nameNode = rootNode.get("name"); | |
name = mapper.treeToValue(nameNode, java.lang.String.class); | |
// description | |
JsonNode descriptionNode = rootNode.get("description"); | |
description = mapper.treeToValue(descriptionNode, java.lang.String.class); | |
// comment | |
JsonNode commentNode = rootNode.get("comment"); | |
comment = mapper.treeToValue(commentNode, java.lang.String.class); | |
// context | |
JsonNode contextNode = rootNode.get("context"); | |
context = mapper.treeToValue(contextNode, java.lang.String.class); | |
// required | |
JsonNode requiredNode = rootNode.get("required"); | |
required = mapper.treeToValue(requiredNode, boolean.class); | |
// secret | |
JsonNode secretNode = rootNode.get("secret"); | |
secret = mapper.treeToValue(secretNode, boolean.class); | |
// validator | |
JsonNode validatorNode = rootNode.get("validator"); | |
if (rootNode.hasNonNull("validator")) { | |
validator = mapper.treeToValue(validatorNode, validator.getClass()); | |
} else { | |
validator = null; | |
} | |
// age | |
JsonNode ageNode = rootNode.get("age"); | |
age = mapper.treeToValue(ageNode, int.class); | |
// firstName | |
JsonNode firstNameNode = rootNode.get("firstName"); | |
firstName = mapper.treeToValue(firstNameNode, java.lang.String.class); | |
// lastName | |
JsonNode lastNameNode = rootNode.get("lastName"); | |
lastName = mapper.treeToValue(lastNameNode, java.lang.String.class); | |
} | |
/** | |
* 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 Person fromJSON(final String jsonDataElement) { | |
// Load the data from the string with Jackson. | |
ObjectMapper mapper = new ObjectMapper(); | |
try { | |
JsonNode rootNode = mapper.readTree(jsonDataElement); | |
fromJsonNode(rootNode, mapper); | |
} catch (JsonProcessingException e) { | |
logger.error("Unable to read DataElement from string!", e); | |
} | |
return this; | |
} | |
/** | |
* Load from a String-Object Map, skipping the String parsing step. Structures | |
* such as <code>org.bson.Document</code> implement Map<String, Object> and | |
* therefore do not need to be processed from raw String form. | |
* | |
* @param jsonDataElement the contents of this data element as a Map<String, Object> | |
*/ | |
public <T extends Map<String, Object>> Person fromJSON(final T jsonDataElement) { | |
// Load the data from the string with Jackson. | |
ObjectMapper mapper = new ObjectMapper(); | |
try { | |
JsonNode rootNode = mapper.valueToTree(jsonDataElement); | |
fromJsonNode(rootNode, mapper); | |
} catch (JsonProcessingException e) { | |
logger.error("Unable to read DataElement from string!", e); | |
} | |
return this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.ice.renderer; | |
import java.util.Map; | |
import java.util.UUID; | |
import org.bson.Document; | |
import org.eclipse.ice.dev.annotations.IDataElement; | |
import org.eclipse.ice.dev.annotations.IPersistenceHandler; | |
import org.eclipse.ice.dev.annotations.PersistenceFilters; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.mongodb.client.MongoCollection; | |
import com.mongodb.client.MongoDatabase; | |
/** | |
* This is an implementation of IPersistenceHandler<Person> | |
* that satisfies the dependencies of the {@code @Persisted} Annotation and was | |
* auto-generated by the ICE Framework. | |
*/ | |
public class PersonPersistenceHandler implements IPersistenceHandler<Person> { | |
/** | |
* The name of the collection backing this PersistenceHandler. | |
*/ | |
private final static String COLLECTION = "people"; | |
/** | |
* The collection in which implementations of Person will be stored. | |
*/ | |
private MongoCollection<Document> collection; | |
/** | |
* Convert values from Document to PersonImplementation using mapper. | |
*/ | |
private ObjectMapper mapper = new ObjectMapper(); | |
public PersonPersistenceHandler(MongoDatabase db) { | |
this.collection = db.getCollection(COLLECTION); | |
} | |
/** | |
* Save the Person. | |
* @param <T> Object extending IDataElement | |
* @param element | |
* @throws Exception | |
*/ | |
@Override | |
public void save(Person element) throws Exception { | |
this.collection.insertOne(Document.parse(element.toJSON())); | |
} | |
/** | |
* Find and retrieve all Person from the collection. | |
* @param <T> | |
* @return an iterable of the retrieved elements. | |
* @throws Exception | |
*/ | |
@Override | |
public Iterable<Person> findAll() throws Exception { | |
return this.collection.find() | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Clear all Person from the collection. | |
* @return | |
* @throws Exception | |
*/ | |
@Override | |
public long clear() throws Exception { | |
long count = this.collection.count(); | |
this.collection.drop(); | |
return count; | |
} | |
/** | |
* Find Person by UUID. | |
* @param UUID | |
* @return found element or null | |
*/ | |
@Override | |
public Person findByUUID(java.util.UUID UUID) throws Exception { | |
Document doc = this.collection | |
.find(PersistenceFilters.eq("privateId", UUID)) | |
.first(); | |
if (doc == null) { | |
return null; | |
} | |
return new PersonImplementation().fromJSON(doc); | |
} | |
/** | |
* Find Person by id. | |
* @param id | |
* @return Iterator of results | |
*/ | |
@Override | |
public Iterable<Person> findById(long id) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("id", id)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by name. | |
* @param name | |
* @return Iterator of results | |
*/ | |
@Override | |
public Iterable<Person> findByName(java.lang.String name) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("name", name)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by description. | |
* @param description | |
* @return Iterator of results | |
*/ | |
@Override | |
public Iterable<Person> findByDescription(java.lang.String description) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("description", description)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by comment. | |
* @param comment | |
* @return Iterator of results | |
*/ | |
@Override | |
public Iterable<Person> findByComment(java.lang.String comment) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("comment", comment)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by context. | |
* @param context | |
* @return Iterator of results | |
*/ | |
@Override | |
public Iterable<Person> findByContext(java.lang.String context) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("context", context)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by required. | |
* @param required | |
* @return Iterator of results | |
*/ | |
@Override | |
public Iterable<Person> findByRequired(boolean required) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("required", required)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by secret. | |
* @param secret | |
* @return Iterator of results | |
*/ | |
@Override | |
public Iterable<Person> findBySecret(boolean secret) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("secret", secret)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by age. | |
* @param age | |
* @return Iterator of results | |
*/ | |
public Iterable<Person> findByAge(int age) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("age", age)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by firstName. | |
* @param firstName | |
* @return Iterator of results | |
*/ | |
public Iterable<Person> findByFirstName(java.lang.String firstName) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("firstName", firstName)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
/** | |
* Find Person by lastName. | |
* @param lastName | |
* @return Iterator of results | |
*/ | |
public Iterable<Person> findByLastName(java.lang.String lastName) throws Exception { | |
return this.collection.find(PersistenceFilters.eq("lastName", lastName)) | |
.map(doc -> new PersonImplementation().fromJSON(doc)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.ice.renderer; | |
import org.eclipse.ice.dev.annotations.DataElement; | |
import org.eclipse.ice.dev.annotations.DataField; | |
import org.eclipse.ice.dev.annotations.Persisted; | |
@DataElement(name = "Person") | |
@Persisted(collection = "people") | |
public class PersonSpec { | |
/** | |
* The person's age. | |
*/ | |
@DataField.Default("42") | |
@DataField private int age; | |
/** | |
* The person's first name. | |
*/ | |
@DataField.Default(value = "Bob", isString = true) | |
@DataField private String firstName; | |
/** | |
* The person's last name. | |
*/ | |
@DataField.Default(value = "Builder", isString = true) | |
@DataField private String lastName; | |
/** | |
* An example constant value. This one probably doesn't actually make sense. | |
*/ | |
@DataField public static final String COLLECTION = "people"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment