String datasourceName = (String) context.get("datasourceName");
String entityNamePrefix = (String) context.get("entityNamePrefix");
if (datasourceName == null) datasourceName = "localderby";
ModelReader reader = dctx.getDelegator().getModelReader();
try {
if (!eomodeldFullPath.endsWith(".eomodeld")) {
eomodeldFullPath = eomodeldFullPath + ".eomodeld";
}
File outdir = new File(eomodeldFullPath);
if (!outdir.exists()) {
outdir.mkdir();
}
if (!outdir.isDirectory()) {
return ServiceUtil.returnError("eomodel Full Path is not a directory: " + eomodeldFullPath);
}
if (!outdir.canWrite()) {
return ServiceUtil.returnError("eomodel Full Path is not write-able: " + eomodeldFullPath);
}
Set<String> entityNames = new TreeSet<String>();
if (UtilValidate.isNotEmpty(entityPackageNameOrig)) {
Set<String> entityPackageNameSet = FastSet.newInstance();
entityPackageNameSet.addAll(StringUtil.split(entityPackageNameOrig, ","));
Debug.logInfo("Exporting with entityPackageNameSet: " + entityPackageNameSet, module);
Map<String, TreeSet<String>> entitiesByPackage = reader.getEntitiesByPackage(entityPackageNameSet, null);
for (Map.Entry<String, TreeSet<String>> entitiesByPackageMapEntry: entitiesByPackage.entrySet()) {
entityNames.addAll(entitiesByPackageMapEntry.getValue());
}
} else if (UtilValidate.isNotEmpty(entityGroupId)) {
Debug.logInfo("Exporting entites from the Group: " + entityGroupId, module);
entityNames.addAll(EntityGroupUtil.getEntityNamesByGroup(entityGroupId, dctx.getDelegator(), false));
} else {
entityNames.addAll(reader.getEntityNames());
}
Debug.logInfo("Exporting the following entities: " + entityNames, module);
// remove all view-entity
Iterator<String> filterEntityNameIter = entityNames.iterator();
while (filterEntityNameIter.hasNext()) {
String entityName = filterEntityNameIter.next();
ModelEntity modelEntity = reader.getModelEntity(entityName);
if (modelEntity instanceof ModelViewEntity) {
filterEntityNameIter.remove();
}
}
// write the index.eomodeld file
Map<String, Object> topLevelMap = FastMap.newInstance();
topLevelMap.put("EOModelVersion", "\"2.1\"");
List<Map<String, Object>> entitiesMapList = FastList.newInstance();
topLevelMap.put("entities", entitiesMapList);
for (String entityName: entityNames) {
Map<String, Object> entitiesMap = FastMap.newInstance();
entitiesMapList.add(entitiesMap);
entitiesMap.put("className", "EOGenericRecord");
entitiesMap.put("name", entityName);
}
UtilPlist.writePlistFile(topLevelMap, eomodeldFullPath, "index.eomodeld", true);
// write each <EntityName>.plist file
for (String curEntityName: entityNames) {
ModelEntity modelEntity = reader.getModelEntity(curEntityName);
UtilPlist.writePlistFile(modelEntity.createEoModelMap(entityNamePrefix, datasourceName, entityNames, reader), eomodeldFullPath, curEntityName +".plist", true);
}
return ServiceUtil.returnSuccess("Exported eomodeld file for " + entityNames.size() + " entities to: " + eomodeldFullPath);
} catch (UnsupportedEncodingException e) {