Search in sources :

Example 1 with TComplexType

use of org.oasisopen.odata.csdl.v4.TComplexType 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()));
    }
}
Also used : TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) TNavigationProperty(org.oasisopen.odata.csdl.v4.TNavigationProperty) Collection(java.util.Collection) TActionFunctionParameter(org.oasisopen.odata.csdl.v4.TActionFunctionParameter) Collectors(java.util.stream.Collectors) VisibleForTesting(com.github.davidmoten.guavamini.annotations.VisibleForTesting) TEntityContainer(org.oasisopen.odata.csdl.v4.TEntityContainer) List(java.util.List) Stream(java.util.stream.Stream) TAnnotations(org.oasisopen.odata.csdl.v4.TAnnotations) TActionFunctionReturnType(org.oasisopen.odata.csdl.v4.TActionFunctionReturnType) TFunction(org.oasisopen.odata.csdl.v4.TFunction) TAction(org.oasisopen.odata.csdl.v4.TAction) TComplexType(org.oasisopen.odata.csdl.v4.TComplexType) TProperty(org.oasisopen.odata.csdl.v4.TProperty) Pattern(java.util.regex.Pattern) Schema(org.oasisopen.odata.csdl.v4.Schema) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) TAnnotations(org.oasisopen.odata.csdl.v4.TAnnotations) TFunction(org.oasisopen.odata.csdl.v4.TFunction) TAction(org.oasisopen.odata.csdl.v4.TAction) TProperty(org.oasisopen.odata.csdl.v4.TProperty) TNavigationProperty(org.oasisopen.odata.csdl.v4.TNavigationProperty) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) TComplexType(org.oasisopen.odata.csdl.v4.TComplexType) TActionFunctionParameter(org.oasisopen.odata.csdl.v4.TActionFunctionParameter) TEntityContainer(org.oasisopen.odata.csdl.v4.TEntityContainer) TActionFunctionReturnType(org.oasisopen.odata.csdl.v4.TActionFunctionReturnType) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet)

Example 2 with TComplexType

use of org.oasisopen.odata.csdl.v4.TComplexType 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

TComplexType (org.oasisopen.odata.csdl.v4.TComplexType)2 VisibleForTesting (com.github.davidmoten.guavamini.annotations.VisibleForTesting)1 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 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Schema (org.oasisopen.odata.csdl.v4.Schema)1 TAction (org.oasisopen.odata.csdl.v4.TAction)1 TActionFunctionParameter (org.oasisopen.odata.csdl.v4.TActionFunctionParameter)1 TActionFunctionReturnType (org.oasisopen.odata.csdl.v4.TActionFunctionReturnType)1 TAnnotations (org.oasisopen.odata.csdl.v4.TAnnotations)1 TEntityContainer (org.oasisopen.odata.csdl.v4.TEntityContainer)1