use of com.reprezen.kaizen.oasparser.model3.Schema in project odata-client by davidmoten.
the class Generator method printNavigationPropertyGetters.
private void printNavigationPropertyGetters(Structure<?> structure, Imports imports, Indent indent, PrintWriter p, List<TNavigationProperty> properties, Set<String> methodNames) {
// write getters
//
properties.forEach(x -> {
String typeName = toClassName(x, imports);
String methodName = Names.getGetterMethod(x.getName());
methodNames.add(methodName);
structure.printPropertyJavadoc(p, indent, x.getName(), "navigational property " + x.getName(), Collections.emptyMap());
addNavigationPropertyAnnotation(imports, indent, p, x.getName());
p.format("%s@%s\n", indent, imports.add(JsonIgnore.class));
p.format("%spublic %s %s() {\n", indent, typeName, methodName);
if (isCollection(x)) {
if (names.isEntityWithNamespace(names.getType(x))) {
p.format("%sreturn new %s(\n", indent.right(), toClassName(x, imports));
//
p.format(//
"%scontextPath.addSegment(\"%s\"), %s.getValue(unmappedFields, \"%s\"));\n", indent.right().right().right().right(), x.getName(), imports.add(RequestHelper.class), x.getName());
indent.left().left().left().left();
} else {
throw new RuntimeException("unexpected");
}
} else {
if (names.isEntityWithNamespace(names.getType(x))) {
Schema sch = names.getSchema(names.getInnerType(names.getType(x)));
//
p.format(//
"%sreturn new %s(contextPath.addSegment(\"%s\"), %s.getValue(unmappedFields, \"%s\"));\n", //
indent.right(), imports.add(names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, names.getInnerType(names.getType(x)))), //
x.getName(), //
imports.add(RequestHelper.class), x.getName());
} else {
throw new RuntimeException("unexpected");
}
}
p.format("%s}\n", indent.left());
});
}
use of com.reprezen.kaizen.oasparser.model3.Schema in project odata-client by davidmoten.
the class Generator method writeEnum.
private void writeEnum(Schema schema, TEnumType t) {
names.getDirectoryEnum(schema).mkdirs();
String simpleClassName = names.getSimpleClassNameEnum(schema, t.getName());
Imports imports = new Imports(names.getFullClassNameEnum(schema, t.getName()));
Indent indent = new Indent();
try {
StringWriter w = new StringWriter();
try (PrintWriter p = new PrintWriter(w)) {
p.format("package %s;\n\n", names.getPackageEnum(schema));
p.format(IMPORTSHERE);
p.format("public enum %s implements %s {\n", simpleClassName, imports.add(com.github.davidmoten.odata.client.Enum.class));
// add members
indent.right();
String s = //
Util.filter(t.getMemberOrAnnotation(), TEnumTypeMember.class).map(x -> //
String.format(//
"%s@%s(\"%s\")\n%s%s(\"%s\", \"%s\")", //
indent, //
imports.add(JsonProperty.class), //
x.getName(), //
indent, //
names.getEnumInstanceName(t, x.getName()), //
x.getName(), x.getValue())).collect(Collectors.joining(",\n\n"));
indent.left();
p.format("\n%s;\n\n", s);
// add fields
p.format("%sprivate final %s name;\n", indent.right(), imports.add(String.class));
p.format("%sprivate final %s value;\n\n", indent, imports.add(String.class));
// add constructor
p.format("%sprivate %s(%s name, %s value) {\n", indent, simpleClassName, imports.add(String.class), imports.add(String.class));
p.format("%sthis.name = name;\n", indent.right());
p.format("%sthis.value = value;\n", indent);
p.format("%s}\n\n", indent.left());
// add methods
p.format("%s@%s\n", indent, imports.add(Override.class));
p.format("%spublic %s enumName() {\n", indent, imports.add(String.class));
p.format("%sreturn name;\n", indent.right());
p.format("%s}\n\n", indent.left());
p.format("%s@%s\n", indent, imports.add(Override.class));
p.format("%spublic %s enumValue() {\n", indent, imports.add(String.class));
p.format("%sreturn value;\n", indent.right());
p.format("%s}\n\n", indent.left());
// close class
p.format("}\n");
}
byte[] bytes = w.toString().replace(IMPORTSHERE, imports.toString()).getBytes(StandardCharsets.UTF_8);
Files.write(names.getClassFileEnum(schema, t.getName()).toPath(), bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.reprezen.kaizen.oasparser.model3.Schema 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));
}
}
use of com.reprezen.kaizen.oasparser.model3.Schema in project odata-client by davidmoten.
the class Names method wrapCollection.
String wrapCollection(Imports imports, Class<?> collectionClass, String inner) {
if (collectionClass.equals(CollectionPageEntityRequest.class)) {
Schema sch = getSchema(inner);
// get the type without namespace
String entityRequestClass = getFullClassNameEntityRequestFromTypeWithNamespace(sch, inner);
String a = toImportedFullClassName(inner, imports, collectionClass);
return imports.add(collectionClass) + "<" + a + ", " + imports.add(entityRequestClass) + ">";
} else {
return imports.add(collectionClass) + "<" + toImportedFullClassName(inner, imports, collectionClass) + ">";
}
}
use of com.reprezen.kaizen.oasparser.model3.Schema in project odata-client by davidmoten.
the class Names method createMap.
private Map<String, String> createMap(List<Schema> schemas, Options options) {
Map<String, String> map = new HashMap<>();
for (Schema schema : schemas) {
//
Util.types(schema, TEnumType.class).forEach(x -> map.put(schema.getNamespace() + "." + x.getName(), getFullClassNameEnum(schema, x.getName())));
//
Util.types(schema, TEntityType.class).forEach(x -> map.put(schema.getNamespace() + "." + x.getName(), getFullClassNameEntity(schema, x.getName())));
//
Util.types(schema, TComplexType.class).forEach(x -> map.put(schema.getNamespace() + "." + x.getName(), getFullClassNameComplexType(schema, x.getName())));
}
return map;
}
Aggregations