use of com.codename1.rad.models.EntityProperty 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;
}
Aggregations