Search in sources :

Example 1 with ComplexType

use of com.github.davidmoten.odata.client.generator.model.ComplexType in project odata-client by davidmoten.

the class Generator method writeComplexType.

private void writeComplexType(Schema schema, TComplexType complexType) {
    ComplexType t = new ComplexType(complexType, names);
    t.getDirectoryComplexType().mkdirs();
    String simpleClassName = t.getSimpleClassName();
    Imports imports = new Imports(t.getFullClassName());
    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);
        t.printJavadoc(p, indent);
        printPropertyOrder(imports, p, t.getProperties());
        printJsonIncludesNonNull(indent, imports, p);
        p.format("public class %s%s implements %s {\n", simpleClassName, t.getExtendsClause(imports), imports.add(ODataType.class));
        indent.right();
        if (!t.hasBaseType()) {
            addContextPathInjectableField(imports, indent, p);
        }
        if (!t.hasBaseType()) {
            addUnmappedFieldsField(imports, indent, p);
        }
        // write fields from properties
        printPropertyFields(imports, indent, p, t.getProperties(), t.hasBaseType());
        // write constructor
        writeNoArgsConstructor(simpleClassName, indent, p, t.hasBaseType());
        p.format("\n%s@%s\n", indent, imports.add(Override.class));
        p.format("%spublic String odataTypeName() {\n", indent);
        p.format("%sreturn \"%s\";\n", indent.right(), t.getFullType());
        p.format("%s}\n", indent.left());
        Set<String> methodNames = new HashSet<>();
        printPropertyGetterAndSetters(t, imports, indent, p, simpleClassName, t.getFullType(), t.getProperties(), false, methodNames);
        addUnmappedFieldsSetterAndGetter(imports, indent, p, methodNames);
        p.format("\n%s@%s\n", indent, imports.add(Override.class));
        p.format("%spublic void postInject(boolean addKeysToContextPath) {\n", indent);
        p.format("%s// do nothing;\n", indent.right());
        p.format("%s}\n", indent.left());
        writeBuilder(t, simpleClassName, imports, indent, p);
        // write copy method
        writeCopyMethod(t, simpleClassName, imports, indent, p, false);
        // write toString
        writeToString(t, simpleClassName, imports, indent, p);
        p.format("\n}\n");
        writeToFile(imports, w, t.getClassFile());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) ODataType(com.github.davidmoten.odata.client.ODataType) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) TComplexType(org.oasisopen.odata.csdl.v4.TComplexType) ComplexType(com.github.davidmoten.odata.client.generator.model.ComplexType) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Aggregations

ODataType (com.github.davidmoten.odata.client.ODataType)1 ComplexType (com.github.davidmoten.odata.client.generator.model.ComplexType)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 UncheckedIOException (java.io.UncheckedIOException)1 HashSet (java.util.HashSet)1 TComplexType (org.oasisopen.odata.csdl.v4.TComplexType)1