private List<FieldDefinition> fields = new ArrayList<FieldDefinition>();
private String idProperty;
public JPAModelDefinition(ExtConfig config) {
boolean isRoot = config.getParentConfig() == null;
ORMPersistenceManager orm = config.getORMPersistenceManager();
List<String> includes = config.getIncludes();
List<String> excludes = config.getExcludes();
List<String> entityClassIncludes = new ArrayList<String>();
List<String> entityClassExcludes = new ArrayList<String>();
/*
* Determine list of included/excluded fields for this model
*/
if (isRoot) {
/*
* Loop each include/exclude/join config and determine if they
* are set for the root entity.
*/
for (String incl : includes) {
if (!incl.contains(".")) entityClassIncludes.add(incl);
}
for (String excl : excludes) {
if (!excl.contains(".")) entityClassExcludes.add(excl);
}
} else {
/*
* Loop each include/exclude/join config and determine if they are set for
* the current entity at this position in the tree. If the full path to a
* specific field on this entity is set, we want to include it in the entity
* specific config. We only need the field portion when testing against
* the current entity so that gets split out before adding to the list
*/
for (String incl : includes) {
if (incl.contains(config.getModelName() + ".")) {
entityClassIncludes.add(incl.split("\\.")[1]);
}
}
for (String excl : excludes) {
if (excl.contains(config.getModelName() + ".")) {
entityClassExcludes.add(excl.split("\\.")[1]);
}
}
}
/*
* Get the class for this model
*/
Class<?> clazz = config.getEntityClass();
/*
* Create and add the fields for this model
*/
for (java.lang.reflect.Field field : clazz.getDeclaredFields()) {
/*
* Static fields don't get returned
*/
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
/*
* Get the field name
*/
String fieldName = field.getName();
/*
* Identify id field
*/
if (orm.isIdField(clazz, fieldName)) {
this.fields.add(new FieldDefinition(field, config));
this.idProperty = fieldName;
continue;
}
/*
* Create the field names for association id fields
*/
String extFieldName = field.getName();
if (orm.isOneToMany(clazz, fieldName) || orm.isManyToMany(clazz, fieldName)) {
extFieldName = Inflector.getInstance().singularize(fieldName) + "Ids";
} else if (orm.isManyToOne(clazz, fieldName)) {
extFieldName = fieldName + "Id";
}
/*
* Is this field included or excluded?
*/
if (isRoot) {
if (includes.size() > 0
&& (!includes.contains("*") && !includes.contains(extFieldName))) {
continue;
}
if (excludes.contains("*") || excludes.contains(extFieldName)) {
continue;
}
} else {
if (includes.size() > 0) {
if (entityClassIncludes.isEmpty()
|| (!entityClassIncludes.contains("*") && !entityClassIncludes.contains(extFieldName))) {
continue;
}
}
if (excludes.size() > 0) {
if (entityClassExcludes.contains("*") || entityClassExcludes.contains(extFieldName)) {
continue;
}
}
}
/*
* If we have a one to many association, add a field to hold an array
* of ID's for that association
*/
if (orm.isOneToMany(clazz, fieldName) || orm.isManyToMany(clazz, fieldName) || orm.isManyToOne(clazz, fieldName)) {
this.fields.add(new FieldDefinition(extFieldName, "auto"));
} else {
this.fields.add(new FieldDefinition(field, config));
}
}