List<String> entityIncludes = PersistenceTools.getIncludes(extConfig, nodePath);
List<String> entityExcludes = PersistenceTools.getExcludes(extConfig, nodePath);
List<String> entityJoins = PersistenceTools.getJoins(extConfig, nodePath);
AdaptrexEntityType adaptrexEntity = apm.getAdaptrexEntity(entityClazz.getSimpleName());
Map<String,AdaptrexFieldType> adaptrexFields = adaptrexEntity.getFields();
Map<String,AdaptrexCollectionType> adaptrexCollections = adaptrexEntity.getCollections();
Map<String,AdaptrexAssociationType> adaptrexAssociations = adaptrexEntity.getAssociations();
/*
* Add ID field
*/
entityData.put(AdaptrexEntityType.ENTITY_ID_NAME, adaptrexEntity.getEntityIdFrom(entity));
/*
* Add data fields
*/
for (String fieldName : adaptrexFields.keySet()) {
if (doInclude(entityClazz, fieldName, entityIncludes, entityExcludes)) {
AdaptrexFieldType adaptrexField = adaptrexFields.get(fieldName);
Object fieldValue = adaptrexField.getValueFrom(entity);
entityData.put(fieldName, ExtTypeFormatter.format(fieldValue));
}
}
/*
* Add collections
*/
for (String fieldName : adaptrexCollections.keySet()) {
AdaptrexCollectionType adaptrexCollection = adaptrexEntity.getCollection(fieldName);
String assocIdsName = StringUtilities.singularize(fieldName) +
StringUtils.capitalize(AdaptrexEntityType.COLLECTION_IDS_NAME);
boolean includeAssoc = entityJoins.contains(StringUtilities.capitalize(fieldName));
boolean includeAssocIds = doInclude(entityClazz, assocIdsName, entityIncludes, entityExcludes);
if (!includeAssoc && !includeAssocIds) continue;
Object fieldValue = adaptrexCollection.getCollectionFrom(entity);
List<Object> associatedIds = new ArrayList<Object>();
List<Map<String, Object>> associatedData = new ArrayList<Map<String, Object>>();
List<Object> assocObjList = fieldValue == null
? new ArrayList<Object>()
: new ArrayList<Object>((Collection<? extends Object>) fieldValue);
for (Object assocObj : assocObjList) {
if (includeAssoc)
associatedData.add(getObjectGraph(assocObj, fieldName, entity, entityName));
if (includeAssocIds)
associatedIds.add(adaptrexEntity.getEntityIdFrom(assocObj));
}
if (includeAssoc) entityData.put(fieldName, associatedData);
if (includeAssocIds) entityData.put(assocIdsName, associatedIds);
}
/*
* Add association
*/
for (String fieldName : adaptrexAssociations.keySet()) {
AdaptrexAssociationType adaptrexAssociation = adaptrexEntity.getAssociation(fieldName);
String assocIdName = fieldName + StringUtils.capitalize(AdaptrexEntityType.ENTITY_ID_NAME);
boolean includeAssoc = entityJoins.contains(StringUtilities.capitalize(fieldName));
boolean includeAssocId = doInclude(entityClazz, assocIdName, entityIncludes, entityExcludes);
if (!includeAssoc && !includeAssocId) continue;
Object fieldValue = adaptrexAssociation.getAssociationFrom(entity);
if (includeAssoc) {
Map<String,Object> assoc = fieldValue == null
? null
: getObjectGraph(fieldValue, fieldName, entity, entityName);
entityData.put(fieldName, assoc);
}
if (includeAssocId) {
Object assocId = fieldValue == null ? null : adaptrexEntity.getEntityIdFrom(fieldValue);
entityData.put(assocIdName, assocId);
}
}
return entityData;