use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.
the class ResultParserTest method dateFormatTest.
private void dateFormatTest() throws Exception {
EntityType personType = new EntityTypeBuilder().string(Person.name).string(Person.email).Date(Person.birthDate).build();
ResultParser parser = new ResultParser(personType).property("name", Person.name).property("email", Person.email).property("dob", Person.birthDate, new SimpleDateFormat("MMM d, yyyy"));
String json = "{\"name\":\"Paul\", \"email\":\"paul@example.com\", \"dob\" : \"December 27, 1978\"}";
Entity person = parser.parseRow(Result.fromContent(json, Result.JSON), personType.newInstance());
assertEqual("Paul", person.getEntity().getText(Person.name));
assertEqual("paul@example.com", person.getEntity().getText(Person.email));
}
use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.
the class ContentType method createObjectType.
public static <V> ContentType<V> createObjectType(Class<V> representationClass) {
if (representationClass == Entity.class) {
return (ContentType<V>) EntityType;
}
if (representationClass == EntityList.class) {
return (ContentType<V>) EntityListType;
}
return new ContentType<V>(new Name(representationClass.getName()), representationClass) {
private boolean isEntity;
private boolean isEntityList;
{
isEntity = Entity.class.isAssignableFrom(representationClass);
isEntityList = EntityList.class.isAssignableFrom(representationClass);
;
}
@Override
public boolean equals(Object obj) {
return obj.getClass() == this.getClass() && ((ContentType) obj).getRepresentationClass() == representationClass;
}
@Override
public boolean isEntity() {
return isEntity;
}
@Override
public boolean isEntityList() {
return isEntityList;
}
@Override
public int hashCode() {
return representationClass.hashCode();
}
@Override
public boolean canConvertFrom(ContentType otherType) {
return representationClass.isAssignableFrom(otherType.representationClass);
}
@Override
public <U> V from(ContentType<U> otherType, U data) {
if (representationClass.isAssignableFrom(otherType.representationClass)) {
return (V) data;
}
return super.from(otherType, data);
}
@Override
public <U> U to(ContentType<U> otherType, V data) {
if (otherType.representationClass.isAssignableFrom(representationClass)) {
return (U) data;
}
return super.to(otherType, data);
}
};
}
use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.
the class FieldNode method getProperty.
/**
* Gets the property for this field. First this will check for an explicit
* property setting using {@link #getProperty()}, and if none is foune, it will
* resolve the tags against the given entity type to find the appropriate property
* of the entity type.
* @param context The entity type to find the property from, in case no property is explicitly set.
* @return The property or null.
*/
public Property getProperty(EntityType context) {
PropertyNode explicitProperty = getProperty();
if (explicitProperty != null) {
return explicitProperty.getValue();
}
if (context == null) {
return null;
}
Tags tags = getTags();
if (tags != null && !tags.isEmpty()) {
for (Property prop : context) {
Tags propTags = prop.getTags();
if (tags.intersects(propTags)) {
return prop;
}
}
}
return null;
}
use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.
the class ResultParser method parseRow.
/**
* Parse a single row of a result into the given row entity.
* @param rowResult The rowResult.
* @param rowEntity The row entity.
* @return The resulting entity. Same as input rowEntity.
* @throws IOException if parsing fails.
*/
public Entity parseRow(Result rowResult, Entity rowEntity) throws IOException {
if (rowEntity.getEntity().getEntityType() != entityType) {
ResultParser matchingParser = getParserFor(rowEntity.getEntity().getEntityType());
if (matchingParser == null) {
throw new IOException("No parser found for type " + rowEntity.getEntity().getEntityType());
}
return matchingParser.parseRow(rowResult, rowEntity);
}
for (PropertyParser propertyParser : propertyParsers) {
String rs = propertyParser.resultPropertySelector;
Property prop = propertyParser.property;
if (prop == null) {
if (propertyParser.tags != null) {
prop = rowEntity.getEntity().findProperty(propertyParser.tags);
}
}
if (prop == null) {
throw new IOException("Property not found for property selector when parsing selector " + rs);
}
if (propertyParser.entityParser != null) {
// same row data - not a sub-object in the dataset.
if (prop.getContentType().isEntity()) {
EntityProperty eProp = (EntityProperty) prop;
Class cls = eProp.getRepresentationClass();
Entity e;
try {
e = createEntity(cls);
} catch (Throwable t) {
throw new IOException("Failed to create new entity instance for property " + prop + " of type " + cls);
}
e = propertyParser.entityParser.parseRow(rowResult, e);
rowEntity.getEntity().set(prop, e);
} else {
throw new IOException("Property " + prop + " is assigned an EntityParser, but the property is not an entity type.");
}
continue;
}
// This is just a simple property selector
Getter getter = propertyParser.getter;
if (getter == null && propertyParser.parserCallback == null) {
getter = createGetter(prop);
}
Object val;
if (getter == null) {
val = rowResult.get(rs);
} else {
val = getter.get(rowResult, rs);
}
if (propertyParser.parserCallback != null) {
val = propertyParser.parserCallback.parse(val);
}
if (val == null) {
rowEntity.getEntity().set(val, null);
} else if (val.getClass() == Double.class) {
rowEntity.getEntity().setDouble(prop, (Double) val);
} else if (val.getClass() == Integer.class) {
rowEntity.getEntity().setInt(prop, (Integer) val);
} else if (val.getClass() == Long.class) {
rowEntity.getEntity().setLong(prop, (Long) val);
} else if (val.getClass() == Float.class) {
rowEntity.getEntity().setFloat(prop, (Float) val);
} else if (val.getClass() == Boolean.class) {
rowEntity.getEntity().setBoolean(prop, (Boolean) val);
} else if (val.getClass() == String.class) {
rowEntity.getEntity().setText(prop, (String) val);
} else if (val.getClass() == Date.class) {
rowEntity.getEntity().setDate(prop, (Date) val);
} else if (val instanceof List) {
if (prop.getContentType().isEntityList()) {
parse((List) val, rowEntity.getEntity().getEntityListNonNull(prop));
} else {
throw new IOException("Property type mismatch. Value " + val + " for property selector " + rs + " is a list, but the property " + prop + " is not an entity list type.");
}
} else if (val instanceof Map) {
if (prop.getContentType().isEntity()) {
EntityProperty eProp = (EntityProperty) prop;
Class cls = eProp.getRepresentationClass();
Entity e;
try {
e = createEntity(cls);
} catch (Throwable t) {
throw new IOException("Failed to create new entity instance for property " + prop + " of type " + cls);
}
e = parseRow(Result.fromContent((Map) val), e);
rowEntity.getEntity().set(prop, e);
} else {
throw new IOException("Property type mismatch. Value " + val + " for property selector " + rs + " is a map, but the property " + prop + " is not an entity type.");
}
} else if (val instanceof Element) {
if (prop.getContentType().isEntity()) {
EntityProperty eProp = (EntityProperty) prop;
Class cls = eProp.getRepresentationClass();
Entity e;
try {
e = createEntity(cls);
} catch (Throwable t) {
throw new IOException("Failed to create new entity instance for property " + prop + " of type " + cls);
}
e = parseRow(Result.fromContent((Element) val), e);
rowEntity.getEntity().set(prop, e);
} else {
throw new IOException("Property type mismatch. Value " + val + " for property selector " + rs + " is a map, but the property " + prop + " is not an entity type.");
}
} else {
throw new IOException("Unsupported content type for property " + prop + ". Value was " + val + " of type " + val.getClass());
}
}
return rowEntity;
}
use of com.codename1.rad.models.EntityType in project CodeRAD by shannah.
the class ResultParserTest method manualExampleTest.
private void manualExampleTest() throws Exception {
String xml = "<?xml version=\"1.0\"?>\n" + "<catalog>\n" + " <book id=\"bk101\">\n" + " <author>Gambardella, Matthew</author>\n" + " <title>XML Developer's Guide</title>\n" + " <genre>Computer</genre>\n" + " <price>44.95</price>\n" + " <publish_date>2000-10-01</publish_date>\n" + " <description>An in-depth look at creating applications \n" + " with XML.</description>\n" + " </book>\n" + " <book id=\"bk102\">\n" + " <author>Ralls, Kim</author>\n" + " <title>Midnight Rain</title>\n" + " <genre>Fantasy</genre>\n" + " <price>5.95</price>\n" + " <publish_date>2000-12-16</publish_date>\n" + " <description>A former architect battles corporate zombies, \n" + " an evil sorceress, and her own childhood to become queen \n" + " of the world.</description>\n" + " </book>\n" + " <book id=\"bk103\">\n" + " <author>Corets, Eva</author>\n" + " <title>Maeve Ascendant</title>\n" + " <genre>Fantasy</genre>\n" + " <price>5.95</price>\n" + " <publish_date>2000-11-17</publish_date>\n" + " <description>After the collapse of a nanotechnology \n" + " society in England, the young survivors lay the \n" + " foundation for a new society.</description>\n" + " </book>\n" + " <book id=\"bk104\">\n" + " <author>Corets, Eva</author>\n" + " <title>Oberon's Legacy</title>\n" + " <genre>Fantasy</genre>\n" + " <price>5.95</price>\n" + " <publish_date>2001-03-10</publish_date>\n" + " <description>In post-apocalypse England, the mysterious \n" + " agent known only as Oberon helps to create a new life \n" + " for the inhabitants of London. Sequel to Maeve \n" + " Ascendant.</description>\n" + " </book>\n" + " <book id=\"bk105\">\n" + " <author>Corets, Eva</author>\n" + " <title>The Sundered Grail</title>\n" + " <genre>Fantasy</genre>\n" + " <price>5.95</price>\n" + " <publish_date>2001-09-10</publish_date>\n" + " <description>The two daughters of Maeve, half-sisters, \n" + " battle one another for control of England. Sequel to \n" + " Oberon's Legacy.</description>\n" + " </book>\n" + " <book id=\"bk106\">\n" + " <author>Randall, Cynthia</author>\n" + " <title>Lover Birds</title>\n" + " <genre>Romance</genre>\n" + " <price>4.95</price>\n" + " <publish_date>2000-09-02</publish_date>\n" + " <description>When Carla meets Paul at an ornithology \n" + " conference, tempers fly as feathers get ruffled.</description>\n" + " </book>\n" + " <book id=\"bk107\">\n" + " <author>Thurman, Paula</author>\n" + " <title>Splish Splash</title>\n" + " <genre>Romance</genre>\n" + " <price>4.95</price>\n" + " <publish_date>2000-11-02</publish_date>\n" + " <description>A deep sea diver finds true love twenty \n" + " thousand leagues beneath the sea.</description>\n" + " </book>\n" + " <book id=\"bk108\">\n" + " <author>Knorr, Stefan</author>\n" + " <title>Creepy Crawlies</title>\n" + " <genre>Horror</genre>\n" + " <price>4.95</price>\n" + " <publish_date>2000-12-06</publish_date>\n" + " <description>An anthology of horror stories about roaches,\n" + " centipedes, scorpions and other insects.</description>\n" + " </book>\n" + " <book id=\"bk109\">\n" + " <author>Kress, Peter</author>\n" + " <title>Paradox Lost</title>\n" + " <genre>Science Fiction</genre>\n" + " <price>6.95</price>\n" + " <publish_date>2000-11-02</publish_date>\n" + " <description>After an inadvertant trip through a Heisenberg\n" + " Uncertainty Device, James Salway discovers the problems \n" + " of being quantum.</description>\n" + " </book>\n" + " <book id=\"bk110\">\n" + " <author>O'Brien, Tim</author>\n" + " <title>Microsoft .NET: The Programming Bible</title>\n" + " <genre>Computer</genre>\n" + " <price>36.95</price>\n" + " <publish_date>2000-12-09</publish_date>\n" + " <description>Microsoft's .NET initiative is explored in \n" + " detail in this deep programmer's reference.</description>\n" + " </book>\n" + " <book id=\"bk111\">\n" + " <author>O'Brien, Tim</author>\n" + " <title>MSXML3: A Comprehensive Guide</title>\n" + " <genre>Computer</genre>\n" + " <price>36.95</price>\n" + " <publish_date>2000-12-01</publish_date>\n" + " <description>The Microsoft MSXML3 parser is covered in \n" + " detail, with attention to XML DOM interfaces, XSLT processing, \n" + " SAX and more.</description>\n" + " </book>\n" + " <book id=\"bk112\">\n" + " <author>Galos, Mike</author>\n" + " <title>Visual Studio 7: A Comprehensive Guide</title>\n" + " <genre>Computer</genre>\n" + " <price>49.95</price>\n" + " <publish_date>2001-04-16</publish_date>\n" + " <description>Microsoft Visual Studio 7 is explored in depth,\n" + " looking at how Visual Basic, Visual C++, C#, and ASP+ are \n" + " integrated into a comprehensive development \n" + " environment.</description>\n" + " </book>\n" + "</catalog>";
EntityType bookType = new EntityTypeBuilder().string(Thing.identifier).string(Thing.name).string(Thing.description).build();
class Book extends BaseEntity {
}
EntityType.register(Book.class, bookType, cls -> {
return new Book();
});
class Books extends EntityList<Book> {
}
EntityType.registerList(Books.class, Book.class, cls -> {
return new Books();
});
Tag BOOKS = new Tag("Books");
EntityType catalogType = new EntityTypeBuilder().list(Books.class, BOOKS).build();
class Catalog extends BaseEntity {
{
setEntityType(catalogType);
}
}
EntityType.register(Catalog.class, cls -> {
return new Catalog();
});
ResultParser parser = new ResultParser(catalogType).property("./book", BOOKS).entityType(bookType).property("@id", Thing.identifier).property("title", Thing.name).property("description", Thing.description);
Catalog catalog = (Catalog) parser.parseXML(xml, new Catalog());
Books books = (Books) catalog.get(BOOKS);
assertEqual("bk101", books.get(0).get(Thing.identifier));
assertEqual("bk112", books.get(books.size() - 1).get(Thing.identifier));
assertEqual("XML Developer's Guide", books.get(0).get(Thing.name));
}
Aggregations