use of com.codename1.rad.models.Entity 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.Entity in project CodeRAD by shannah.
the class FieldNode method getPropertySelector.
/**
* Gets a property selector for this field node. If the field contained
* a PropertyNode or a Tags node, then it will construct a selector from those.
*
* Otherwise it will check for a {@link ProeprtySelectorAttribute}, and return
* a selector constructed form that, with the provided entity root.
* @param context
* @return A property selector, or null if no property or tag is set, and no property selector is set.
*/
public PropertySelector getPropertySelector(Entity context) {
if (context == null) {
return null;
}
Property prop = getProperty(context.getEntity().getEntityType());
if (prop != null) {
return new PropertySelector(context, prop);
}
Tags tags = getTags();
if (tags != null) {
return new PropertySelector(context, tags.toArray());
}
PropertySelectorAttribute selectorProvider = (PropertySelectorAttribute) findAttribute(PropertySelectorAttribute.class);
if (selectorProvider != null) {
return selectorProvider.getValue(context);
}
return null;
}
use of com.codename1.rad.models.Entity 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.Entity in project CodeRAD by shannah.
the class Node method createPropertySelector.
/**
* Creates a property selector on the given entity using (in order of decreasing precedence):
*
* 1. A {@link PropertyNode} attribute on the node.
* 2. A {@link Tags} attribute on the node.
* 3. A {@link PropertySelectorAttribute} on the node.
*
* @param entity The entity on which the property selector should be created.
* @return The property selector.
*/
public PropertySelector createPropertySelector(Entity entity) {
PropertyNode prop = findAttribute(PropertyNode.class);
if (prop != null) {
return new PropertySelector(entity, prop.getValue());
}
Tags tags = findAttribute(Tags.class);
if (tags != null) {
return new PropertySelector(entity, tags.toArray());
}
PropertySelectorAttribute att = (PropertySelectorAttribute) findAttribute(PropertySelectorAttribute.class);
if (att != null) {
return att.getValue(entity);
}
return null;
}
use of com.codename1.rad.models.Entity in project CodeRAD by shannah.
the class ViewController method getNearestViewModel.
/**
* Crawls up controller hierarchy until it finds a controller with an attached view model.
* @return The nearest view model or null.
*/
public Entity getNearestViewModel() {
Entity vm = getViewModel();
Controller parent = getParent();
while (parent != null) {
if (parent instanceof ViewController) {
ViewController parentVC = (ViewController) parent;
vm = parentVC.getViewModel();
if (vm != null) {
return vm;
}
}
parent = parent.getParent();
}
return null;
}
Aggregations