//put the entityNames TreeSets in a HashMap by packageName
try {
Collection<String> ec = reader.getEntityNames();
resultMap.put("numberOfEntities", ec.size());
for (String eName: ec) {
ModelEntity ent = reader.getModelEntity(eName);
//make sure the table name is in the list of all table names, if not null
if (UtilValidate.isNotEmpty(ent.getPlainTableName())) {
tableNames.add(ent.getPlainTableName());
}
TreeSet<String> entities = entitiesByPackage.get(ent.getPackageName());
if (entities == null) {
entities = new TreeSet<String>();
entitiesByPackage.put(ent.getPackageName(), entities);
packageNames.add(ent.getPackageName());
}
entities.add(eName);
}
} catch (GenericEntityException e) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "EntityImportErrorRetrievingEntityNames", locale) + e.getMessage());
}
String search = (String) context.get("search");
List<Map<String, Object>> packagesList = FastList.newInstance();
try {
for (String pName : packageNames) {
Map<String, Object> packageMap = FastMap.newInstance();
TreeSet<String> entities = entitiesByPackage.get(pName);
List<Map<String, Object>> entitiesList = FastList.newInstance();
for (String entityName: entities) {
Map<String, Object> entityMap = FastMap.newInstance();
String helperName = delegator.getEntityHelperName(entityName);
String groupName = delegator.getEntityGroupName(entityName);
if (search == null || entityName.toLowerCase().indexOf(search.toLowerCase()) != -1) {
ModelEntity entity = reader.getModelEntity(entityName);
ResourceBundle bundle = null;
if (UtilValidate.isNotEmpty(entity.getDefaultResourceName())) {
try {
bundle = UtilResourceBundle.getBundle(entity.getDefaultResourceName(), locale, loader);
} catch (Exception exception) {
Debug.logInfo(exception.getMessage(), module);
}
}
String entityDescription = null;
if (bundle != null) {
try {
entityDescription = bundle.getString("EntityDescription." + entity.getEntityName());
} catch (Exception exception) {}
}
if (UtilValidate.isEmpty(entityDescription)) {
entityDescription = entity.getDescription();
}
// fields list
List<Map<String, Object>> javaNameList = FastList.newInstance();
for (Iterator<ModelField> f = entity.getFieldsIterator(); f.hasNext();) {
Map<String, Object> javaNameMap = FastMap.newInstance();
ModelField field = f.next();
ModelFieldType type = delegator.getEntityFieldType(entity, field.getType());
javaNameMap.put("isPk", field.getIsPk());
javaNameMap.put("name", field.getName());
javaNameMap.put("colName", field.getColName());
String fieldDescription = null;
if (bundle != null) {
try {
fieldDescription = bundle.getString("FieldDescription." + entity.getEntityName() + "." + field.getName());
} catch (Exception exception) {}
}
if (UtilValidate.isEmpty(fieldDescription)) {
fieldDescription = field.getDescription();
}
if (UtilValidate.isEmpty(fieldDescription) && bundle != null) {
try {
fieldDescription = bundle.getString("FieldDescription." + field.getName());
} catch (Exception exception) {}
}
if (UtilValidate.isEmpty(fieldDescription)) {
fieldDescription = ModelUtil.javaNameToDbName(field.getName()).toLowerCase();
fieldDescription = ModelUtil.upperFirstChar(fieldDescription.replace('_', ' '));
}
javaNameMap.put("description", fieldDescription);
javaNameMap.put("type", (field.getType()) != null ? field.getType() : null);
javaNameMap.put("javaType", (field.getType() != null && type != null) ? type.getJavaType() : "Undefined");
javaNameMap.put("sqlType", (type != null && type.getSqlType() != null) ? type.getSqlType() : "Undefined");
javaNameMap.put("encrypted", field.getEncrypt());
javaNameList.add(javaNameMap);
}
// relations list
List<Map<String, Object>> relationsList = FastList.newInstance();
for (int r = 0; r < entity.getRelationsSize(); r++) {
Map<String, Object> relationMap = FastMap.newInstance();
ModelRelation relation = entity.getRelation(r);
List<Map<String, Object>> keysList = FastList.newInstance();
for (int km = 0; km < relation.getKeyMapsSize(); km++) {
Map<String, Object> keysMap = FastMap.newInstance();
ModelKeyMap keyMap = relation.getKeyMap(km);
String fieldName = null;
String relFieldName = null;
if (keyMap.getFieldName().equals(keyMap.getRelFieldName())) {
fieldName = keyMap.getFieldName();
relFieldName = "aa";
} else {
fieldName = keyMap.getFieldName();
relFieldName = keyMap.getRelFieldName();
}
keysMap.put("row", km + 1);
keysMap.put("fieldName", fieldName);
keysMap.put("relFieldName", relFieldName);
keysList.add(keysMap);
}
relationMap.put("title", relation.getTitle());
relationMap.put("description", relation.getDescription());
relationMap.put("relEntity", relation.getRelEntityName());
relationMap.put("fkName", relation.getFkName());
relationMap.put("type", relation.getType());
relationMap.put("length", relation.getType().length());
relationMap.put("keysList", keysList);
relationsList.add(relationMap);
}
// index list
List<Map<String, Object>> indexList = FastList.newInstance();
for (int r = 0; r < entity.getIndexesSize(); r++) {
List<String> fieldNameList = FastList.newInstance();
ModelIndex index = entity.getIndex(r);
for (Iterator<ModelIndex.Field> fieldIterator = index.getFieldsIterator(); fieldIterator.hasNext();) {
fieldNameList.add(fieldIterator.next().getFieldName());
}
Map<String, Object> indexMap = FastMap.newInstance();
indexMap.put("name", index.getName());
indexMap.put("description", index.getDescription());
indexMap.put("fieldNameList", fieldNameList);
indexList.add(indexMap);
}
entityMap.put("entityName", entityName);
entityMap.put("helperName", helperName);
entityMap.put("groupName", groupName);
entityMap.put("plainTableName", entity.getPlainTableName());
entityMap.put("title", entity.getTitle());
entityMap.put("description", entityDescription);
String entityLocation = entity.getLocation();
entityLocation = entityLocation.replaceFirst(System.getProperty("ofbiz.home") + "/", "");
entityMap.put("location", entityLocation);
entityMap.put("javaNameList", javaNameList);
entityMap.put("relationsList", relationsList);
entityMap.put("indexList", indexList);