use of org.oasisopen.odata.csdl.v4.TEntitySet in project odata-client by davidmoten.
the class Util method replaceAlias.
private static void replaceAlias(Schema schema, Object x) {
if (schema.getAlias() == null) {
return;
}
if (x instanceof TEntityType) {
TEntityType p = (TEntityType) x;
// mutate types to use alias
p.setBaseType(replaceAlias(schema, p.getBaseType()));
//
p.getKeyOrPropertyOrNavigationProperty().forEach(y -> replaceAlias(schema, y));
} else if (x instanceof TComplexType) {
TComplexType p = (TComplexType) x;
// mutate types to use alias
p.setBaseType(replaceAlias(schema, p.getBaseType()));
//
p.getPropertyOrNavigationPropertyOrAnnotation().forEach(y -> replaceAlias(schema, y));
} else if (x instanceof TAction) {
TAction a = (TAction) x;
//
a.getParameterOrAnnotationOrReturnType().forEach(y -> replaceAlias(schema, y));
} else if (x instanceof TFunction) {
TFunction a = (TFunction) x;
//
a.getParameterOrAnnotation().forEach(y -> replaceAlias(schema, y));
replaceAlias(schema, a.getReturnType());
} else if (x instanceof TActionFunctionParameter) {
TActionFunctionParameter p = (TActionFunctionParameter) x;
replaceAlias(schema, p.getType());
} else if (x instanceof TActionFunctionReturnType) {
TActionFunctionReturnType p = (TActionFunctionReturnType) x;
replaceAlias(schema, p.getType());
} else if (x instanceof TProperty) {
TProperty p = (TProperty) x;
replaceAlias(schema, p.getType());
} else if (x instanceof TNavigationProperty) {
TNavigationProperty p = (TNavigationProperty) x;
replaceAlias(schema, p.getType());
} else if (x instanceof TAnnotations) {
TAnnotations a = (TAnnotations) x;
a.setTarget(replaceAlias(schema, a.getTarget()));
} else if (x instanceof TEntityContainer) {
TEntityContainer a = (TEntityContainer) x;
Util.filter(a.getEntitySetOrActionImportOrFunctionImport(), TEntitySet.class).forEach(y -> replaceAlias(schema, y));
} else if (x instanceof TEntitySet) {
TEntitySet a = (TEntitySet) x;
a.setEntityType(replaceAlias(schema, a.getEntityType()));
}
}
use of org.oasisopen.odata.csdl.v4.TEntitySet in project odata-client by davidmoten.
the class Generator method writeEntitySet.
private void writeEntitySet(Schema schema, Pair<TEntityContainer, TEntitySet> pair) {
EntitySet t = new EntitySet(schema, pair.a, pair.b, names);
t.getDirectoryEntitySet().mkdirs();
Imports imports = new Imports(t.getFullClassNameEntitySet());
Indent indent = new Indent();
StringWriter w = new StringWriter();
try (PrintWriter p = new PrintWriter(w)) {
p.format("package %s;\n\n", t.getPackage());
p.format(IMPORTSHERE);
String baseCollectionClassName = t.getBaseCollectionRequestClassName(imports);
//
p.format(//
"%spublic final class %s extends %s {\n", //
indent, //
t.getSimpleClassNameEntitySet(), baseCollectionClassName);
indent.right();
//
p.format(//
"\n%spublic %s(%s contextPath) {\n", //
indent, //
t.getSimpleClassNameEntitySet(), imports.add(ContextPath.class));
p.format("%ssuper(contextPath, %s.empty());\n", indent.right(), imports.add(Optional.class));
p.format("%s}\n", indent.left());
// write navigation property bindings
Set<String> duplicateMethodNames = findDuplicateNavigationPropertyBindingMethodNames(pair, t);
//
Util.filter(//
pair.b.getNavigationPropertyBindingOrAnnotation(), //
TNavigationPropertyBinding.class).forEach(b -> {
String methodName = t.getMethodName(b);
if (duplicateMethodNames.contains(methodName)) {
methodName = t.getLongerMethodName(b);
}
Optional<EntitySet> referredEntitySet = t.getReferredEntitySet(b.getTarget());
if (referredEntitySet.isPresent()) {
String returnClassName = referredEntitySet.get().getFullClassNameEntitySet();
p.format("\n%spublic %s %s() {\n", indent, imports.add(returnClassName), methodName);
//
p.format(//
"%sreturn new %s(contextPath.addSegment(\"%s\"));\n", //
indent.right(), imports.add(//
referredEntitySet.get().getFullClassNameEntitySet()), b.getPath());
p.format("%s}\n", indent.left());
} else {
log(//
"WARN: EntitySet '" + b.getTarget() + //
"' not found for navigation property binding " + t.getSimpleClassNameEntitySet() + "." + b.getPath());
}
});
p.format("%s}\n", indent.left());
writeToFile(imports, w, t.getClassFile());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of org.oasisopen.odata.csdl.v4.TEntitySet in project odata-client by davidmoten.
the class Generator method generate.
public void generate() {
log("-----------------------------------");
log("Generating code for namespaces:");
//
schemas.forEach(s -> log(" " + s.getNamespace()));
log("Types:");
//
schemas.stream().flatMap(s -> Util.filter(s.getComplexTypeOrEntityTypeOrTypeDefinition(), TEntityType.class).map(//
t -> new SchemaAndType<TEntityType>(s, t))).map(//
x -> names.toTypeWithNamespace(x.schema, x.type.getName())).forEach(x -> log(" " + x));
log("-----------------------------------");
log("Replacing aliases");
Util.replaceAliases(schemas);
log("Finding collection types");
Set<String> collectionTypes = findTypesUsedInCollections(names, schemas);
for (Schema schema : schemas) {
log("Generating for namespace=" + schema.getNamespace());
log(" creating maps");
Map<String, List<Action>> typeActions = createTypeActions(schema, names, false);
log(" entity actions count = " + typeActions.size());
Map<String, List<Function>> typeFunctions = createTypeFunctions(schema, names, false);
log(" entity functions count = " + typeFunctions.size());
Map<String, List<Action>> collectionTypeActions = createTypeActions(schema, names, true);
log(" collection actions count = " + collectionTypeActions.size());
Map<String, List<Function>> collectionTypeFunctions = createTypeFunctions(schema, names, true);
System.out.println(" collection functions count = " + collectionTypeFunctions.size());
log(" checking entities have keys");
//
Util.types(schema, TEntityType.class).map(//
x -> new EntityType(x, names)).filter(//
x -> !x.hasKey()).forEach(x -> log(" " + x.getFullType() + " has no keys"));
log(" writing schema info");
writeSchemaInfo(schema);
// write enums
log(" writing enums");
//
Util.types(schema, TEnumType.class).forEach(x -> writeEnum(schema, x));
// write entityTypes
log(" writing entities");
//
Util.types(schema, TEntityType.class).forEach(x -> writeEntity(x, typeActions, typeFunctions));
// write complexTypes
log(" writing complex types");
//
Util.types(schema, TComplexType.class).forEach(x -> writeComplexType(schema, x));
// write entity collection requests
log(" writing entity collection requests");
//
Util.types(schema, TEntityType.class).forEach(x -> writeEntityCollectionRequest(schema, x, collectionTypeActions, collectionTypeFunctions, collectionTypes));
log("writing entity set requests");
//
Util.types(schema, TEntityContainer.class).flatMap(c -> //
Util.filter(c.getEntitySetOrActionImportOrFunctionImport(), //
TEntitySet.class).map(//
x -> new Pair<TEntityContainer, TEntitySet>(c, x))).forEach(x -> writeEntitySet(schema, x));
// write containers
log(" writing container");
//
Util.types(schema, TEntityContainer.class).forEach(x -> writeContainer(schema, x));
// write single requests
log(" writing entity requests");
//
Util.types(schema, TEntityType.class).forEach(x -> writeEntityRequest(schema, x, typeActions, typeFunctions));
log(" writing complex type requests");
//
Util.types(schema, TComplexType.class).forEach(x -> writeComplexTypeRequest(schema, x));
}
}
Aggregations