in ext/pojogen-maven-plugin/src/main/java/org/apache/olingo/ext/pojogen/AbstractPOJOGenMojo.java [235:401]
public void execute() throws MojoExecutionException, MojoFailureException {
if (new File(outputDirectory + File.separator + TOOL_DIR).exists()) {
getLog().info("Nothing to do because " + TOOL_DIR + " directory already exists. Clean to update.");
return;
}
Velocity.addProperty(Velocity.RESOURCE_LOADER, "class");
Velocity.addProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
try {
final Triple<XMLMetadata, String, Edm> metadata = getMetadata();
for (EdmSchema schema : metadata.getRight().getSchemas()) {
namespaces.add(schema.getNamespace().toLowerCase());
}
final Map<String, String> entityTypeNames = new HashMap<String, String>();
final Map<String, String> complexTypeNames = new HashMap<String, String>();
final Map<String, String> enumTypeNames = new HashMap<String, String>();
final Map<String, String> termNames = new HashMap<String, String>();
final Map<String, Object> objs = new HashMap<String, Object>();
for (EdmSchema schema : metadata.getRight().getSchemas()) {
createUtility(metadata.getRight(), schema, basePackage);
// write package-info for the base package
final String schemaPath = utility.getNamespace().toLowerCase().replace('.', File.separatorChar);
final File base = mkPkgDir(schemaPath);
final String pkg = StringUtils.isBlank(basePackage)
? utility.getNamespace().toLowerCase()
: basePackage + "." + utility.getNamespace().toLowerCase();
parseObj(base, pkg, "package-info", "package-info.java");
// write package-info for types package
final File typesBaseDir = mkPkgDir(schemaPath + "/types");
final String typesPkg = pkg + ".types";
parseObj(typesBaseDir, typesPkg, "package-info", "package-info.java");
for (EdmTerm term : schema.getTerms()) {
final String className = utility.capitalize(term.getName());
termNames.put(term.getFullQualifiedName().toString(), typesPkg + "." + className);
objs.clear();
objs.put("term", term);
parseObj(typesBaseDir, typesPkg, "term", className + ".java", objs);
}
for (EdmEnumType enumType : schema.getEnumTypes()) {
final String className = utility.capitalize(enumType.getName());
enumTypeNames.put(enumType.getFullQualifiedName().toString(), typesPkg + "." + className);
objs.clear();
objs.put("enumType", enumType);
parseObj(typesBaseDir, typesPkg, "enumType", className + ".java", objs);
}
final List<EdmComplexType> complexes = new ArrayList<EdmComplexType>();
for (EdmComplexType complex : schema.getComplexTypes()) {
complexes.add(complex);
final String className = utility.capitalize(complex.getName());
complexTypeNames.put(complex.getFullQualifiedName().toString(), typesPkg + "." + className);
objs.clear();
objs.put("complexType", complex);
parseObj(typesBaseDir, typesPkg,
"complexType", className + ".java", objs);
parseObj(typesBaseDir, typesPkg,
"complexTypeComposableInvoker", className + "ComposableInvoker.java", objs);
parseObj(typesBaseDir, typesPkg,
"complexCollection", className + "Collection.java", objs);
parseObj(typesBaseDir, typesPkg,
"complexCollectionComposableInvoker", className + "CollectionComposableInvoker.java", objs);
}
for (EdmEntityType entity : schema.getEntityTypes()) {
final String className = utility.capitalize(entity.getName());
entityTypeNames.put(entity.getFullQualifiedName().toString(), typesPkg + "." + className);
objs.clear();
objs.put("entityType", entity);
final Map<String, String> keys;
EdmEntityType baseType = null;
if (entity.getBaseType() == null) {
keys = getUtility().getEntityKeyType(entity);
} else {
baseType = entity.getBaseType();
objs.put("baseType", getUtility().getJavaType(baseType.getFullQualifiedName().toString()));
while (baseType.getBaseType() != null) {
baseType = baseType.getBaseType();
}
keys = getUtility().getEntityKeyType(baseType);
}
if (keys.size() > 1) {
// create compound key class
final String keyClassName = utility.capitalize(baseType == null
? entity.getName()
: baseType.getName()) + "Key";
objs.put("keyRef", keyClassName);
if (entity.getBaseType() == null) {
objs.put("keys", keys);
parseObj(typesBaseDir, typesPkg, "entityTypeKey", keyClassName + ".java", objs);
}
}
parseObj(typesBaseDir, typesPkg,
"entityType", className + ".java", objs);
parseObj(typesBaseDir, typesPkg,
"entityComposableInvoker", className + "ComposableInvoker.java", objs);
parseObj(typesBaseDir, typesPkg,
"entityCollection", className + "Collection.java", objs);
parseObj(typesBaseDir, typesPkg,
"entityCollectionComposableInvoker", className + "CollectionComposableInvoker.java", objs);
}
// write container and top entity sets into the base package
EdmEntityContainer container = schema.getEntityContainer();
if(container != null){
objs.clear();
objs.put("container", container);
objs.put("namespace", schema.getNamespace());
objs.put("complexes", complexes);
parseObj(base, pkg, "container", utility.capitalize(container.getName()) + ".java", objs);
for (EdmEntitySet entitySet : container.getEntitySets()) {
objs.clear();
objs.put("entitySet", entitySet);
objs.put("container", container);
parseObj(base, pkg, "entitySet", utility.capitalize(entitySet.getName()) + ".java", objs);
}
}
}
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final GZIPOutputStream gzos = new GZIPOutputStream(baos);
final ObjectOutputStream oos = new ObjectOutputStream(gzos);
try {
oos.writeObject(metadata.getLeft());
} finally {
oos.close();
gzos.close();
baos.close();
}
objs.clear();
objs.put("metadata", new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8"));
objs.put("metadataETag", metadata.getMiddle());
objs.put("entityTypes", entityTypeNames);
objs.put("complexTypes", complexTypeNames);
objs.put("enumTypes", enumTypeNames);
objs.put("terms", termNames);
final String actualBP = StringUtils.isBlank(basePackage)
? StringUtils.EMPTY
: basePackage;
parseObj(mkdir(actualBP.replace('.', File.separatorChar)), actualBP, "service", "Service.java", objs);
} catch (Exception t) {
getLog().error(t);
throw (t instanceof MojoExecutionException)
? (MojoExecutionException) t
: new MojoExecutionException("While executin mojo", t);
}
}