}
private Attribute<?> getRecursiveAttribute(Entity entity, FeatureInfo featureInfo, String[] path)
throws LayerException {
String name = path[0];
AssociationAttributeInfo associationAttributeInfo = null;
// check attribute type
Set<String> names = new HashSet<String>();
// check for id (support for backwards compatibility)
if (featureInfo.getIdentifier().getName().equals(name)) {
try {
return dtoConverterService.toDto(entity == null ? null : entity.getAttribute(name),
featureInfo.getIdentifier());
} catch (GeomajasException e) {
throw new LayerException(e, ExceptionCode.CONVERSION_PROBLEM);
}
}
for (AttributeInfo attributeInfo : featureInfo.getAttributes()) {
names.add(attributeInfo.getName());
if (attributeInfo.getName().equals(name)) {
if (attributeInfo instanceof AssociationAttributeInfo) {
associationAttributeInfo = (AssociationAttributeInfo) attributeInfo;
} else if (attributeInfo instanceof PrimitiveAttributeInfo) {
// primitive, return the attribute
PrimitiveAttributeInfo primitiveAttributeInfo = (PrimitiveAttributeInfo) attributeInfo;
try {
return dtoConverterService.toDto(entity == null ? null : entity.getAttribute(name),
primitiveAttributeInfo);
} catch (GeomajasException e) {
throw new LayerException(e, ExceptionCode.CONVERSION_PROBLEM);
}
}
}
}
// association attribute
if (associationAttributeInfo == null) {
throw new LayerException(ExceptionCode.ATTRIBUTE_UNKNOWN, name, names);
} else if (path.length > 1) {
// go deeper
return getRecursiveAttribute(entity == null ? null : entity.getChild(name),
associationAttributeInfo.getFeature(), Arrays.copyOfRange(path, 1, path.length));
} else {
switch (associationAttributeInfo.getType()) {
case MANY_TO_ONE:
ManyToOneAttribute manyToOne = new ManyToOneAttribute();
if (entity != null) {
Entity oneEntity = entity.getChild(associationAttributeInfo.getName());
AssociationValue value = getAssociationValue(oneEntity, associationAttributeInfo);
manyToOne.setValue(value);
}
return manyToOne;
case ONE_TO_MANY:
OneToManyAttribute oneToMany = new OneToManyAttribute();
if (entity != null) {
EntityCollection children = entity.getChildCollection(associationAttributeInfo.getName());
List<AssociationValue> values = new ArrayList<AssociationValue>();
for (Entity manyEntity : children) {
values.add(getAssociationValue(manyEntity, associationAttributeInfo));
}
oneToMany.setValue(values);