Search in sources :

Example 1 with TEntitySet

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()));
    }
}
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 TEntitySet

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);
    }
}
Also used : Optional(java.util.Optional) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) EntitySet(com.github.davidmoten.odata.client.generator.model.EntitySet) ContextPath(com.github.davidmoten.odata.client.ContextPath) StringWriter(java.io.StringWriter) TNavigationPropertyBinding(org.oasisopen.odata.csdl.v4.TNavigationPropertyBinding) PrintWriter(java.io.PrintWriter)

Example 3 with TEntitySet

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));
    }
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StreamProvider(com.github.davidmoten.odata.client.StreamProvider) Arrays(java.util.Arrays) HasNameJavaHasNullable(com.github.davidmoten.odata.client.generator.model.HasNameJavaHasNullable) HasContext(com.github.davidmoten.odata.client.HasContext) HttpService(com.github.davidmoten.odata.client.HttpService) TEnumType(org.oasisopen.odata.csdl.v4.TEnumType) Map(java.util.Map) Parameter(com.github.davidmoten.odata.client.generator.model.Action.Parameter) PrintWriter(java.io.PrintWriter) FunctionRequestReturningNonCollection(com.github.davidmoten.odata.client.FunctionRequestReturningNonCollection) SchemaAndType(com.github.davidmoten.odata.client.generator.Names.SchemaAndType) JacksonInject(com.fasterxml.jackson.annotation.JacksonInject) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) NameValue(com.github.davidmoten.odata.client.NameValue) RequestHelper(com.github.davidmoten.odata.client.internal.RequestHelper) Set(java.util.Set) ODataType(com.github.davidmoten.odata.client.ODataType) StandardCharsets(java.nio.charset.StandardCharsets) UncheckedIOException(java.io.UncheckedIOException) Stream(java.util.stream.Stream) Lists(com.github.davidmoten.guavamini.Lists) CollectionPageEntityRequest(com.github.davidmoten.odata.client.CollectionPageEntityRequest) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) SchemaInfo(com.github.davidmoten.odata.client.SchemaInfo) TProperty(org.oasisopen.odata.csdl.v4.TProperty) Checks(com.github.davidmoten.odata.client.internal.Checks) Files(java.nio.file.Files) TNavigationProperty(org.oasisopen.odata.csdl.v4.TNavigationProperty) Function(com.github.davidmoten.odata.client.generator.model.Function) StringWriter(java.io.StringWriter) TSingleton(org.oasisopen.odata.csdl.v4.TSingleton) HttpMethod(com.github.davidmoten.odata.client.HttpMethod) IOException(java.io.IOException) Include(com.fasterxml.jackson.annotation.JsonInclude.Include) ContainerBuilder(com.github.davidmoten.odata.client.TestingService.ContainerBuilder) File(java.io.File) ChangedFields(com.github.davidmoten.odata.client.internal.ChangedFields) NavigationProperty(com.github.davidmoten.odata.client.annotation.NavigationProperty) TNavigationPropertyBinding(org.oasisopen.odata.csdl.v4.TNavigationPropertyBinding) RequestOptions(com.github.davidmoten.odata.client.RequestOptions) ParameterMap(com.github.davidmoten.odata.client.internal.ParameterMap) TEnumTypeMember(org.oasisopen.odata.csdl.v4.TEnumTypeMember) HttpRequestOptions(com.github.davidmoten.odata.client.HttpRequestOptions) CollectionPage(com.github.davidmoten.odata.client.CollectionPage) FieldName(com.github.davidmoten.odata.client.generator.model.Structure.FieldName) ActionRequestNoReturn(com.github.davidmoten.odata.client.ActionRequestNoReturn) Action(com.github.davidmoten.odata.client.generator.model.Action) Field(com.github.davidmoten.odata.client.generator.model.Field) UploadStrategy(com.github.davidmoten.odata.client.UploadStrategy) TComplexType(org.oasisopen.odata.csdl.v4.TComplexType) JsonAnyGetter(com.fasterxml.jackson.annotation.JsonAnyGetter) ActionRequestReturningNonCollection(com.github.davidmoten.odata.client.ActionRequestReturningNonCollection) ReturnType(com.github.davidmoten.odata.client.generator.model.Action.ReturnType) UnmappedFields(com.github.davidmoten.odata.client.UnmappedFields) Collectors(java.util.stream.Collectors) EntityType(com.github.davidmoten.odata.client.generator.model.EntityType) JsonAnySetter(com.fasterxml.jackson.annotation.JsonAnySetter) Context(com.github.davidmoten.odata.client.Context) List(java.util.List) KeyElement(com.github.davidmoten.odata.client.generator.model.KeyElement) Method(com.github.davidmoten.odata.client.generator.model.Method) PropertyRef(com.github.davidmoten.odata.client.generator.model.PropertyRef) Structure(com.github.davidmoten.odata.client.generator.model.Structure) StreamUploaderChunked(com.github.davidmoten.odata.client.StreamUploaderChunked) Optional(java.util.Optional) ClientException(com.github.davidmoten.odata.client.ClientException) ContextPath(com.github.davidmoten.odata.client.ContextPath) JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) ComplexType(com.github.davidmoten.odata.client.generator.model.ComplexType) Preconditions(com.github.davidmoten.guavamini.Preconditions) TypedObject(com.github.davidmoten.odata.client.internal.TypedObject) HashMap(java.util.HashMap) TActionFunctionParameter(org.oasisopen.odata.csdl.v4.TActionFunctionParameter) HashSet(java.util.HashSet) TActionFunctionReturnType(org.oasisopen.odata.csdl.v4.TActionFunctionReturnType) TFunction(org.oasisopen.odata.csdl.v4.TFunction) EntityRequest(com.github.davidmoten.odata.client.EntityRequest) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) TAction(org.oasisopen.odata.csdl.v4.TAction) Path(com.github.davidmoten.odata.client.Path) Schema(org.oasisopen.odata.csdl.v4.Schema) StreamUploaderSingleCall(com.github.davidmoten.odata.client.StreamUploaderSingleCall) EntitySet(com.github.davidmoten.odata.client.generator.model.EntitySet) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) BuilderBase(com.github.davidmoten.odata.client.TestingService.BuilderBase) ActionRequestReturningNonCollectionUnwrapped(com.github.davidmoten.odata.client.ActionRequestReturningNonCollectionUnwrapped) TEntityContainer(org.oasisopen.odata.csdl.v4.TEntityContainer) UnmappedFieldsImpl(com.github.davidmoten.odata.client.internal.UnmappedFieldsImpl) StreamUploader(com.github.davidmoten.odata.client.StreamUploader) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) FunctionRequestReturningStream(com.github.davidmoten.odata.client.FunctionRequestReturningStream) JsonIgnoreType(com.fasterxml.jackson.annotation.JsonIgnoreType) CollectionPageNonEntityRequest(com.github.davidmoten.odata.client.CollectionPageNonEntityRequest) FunctionRequestReturningNonCollectionUnwrapped(com.github.davidmoten.odata.client.FunctionRequestReturningNonCollectionUnwrapped) Property(com.github.davidmoten.odata.client.annotation.Property) Collections(java.util.Collections) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) Schema(org.oasisopen.odata.csdl.v4.Schema) SchemaAndType(com.github.davidmoten.odata.client.generator.Names.SchemaAndType) TEnumType(org.oasisopen.odata.csdl.v4.TEnumType) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) EntityType(com.github.davidmoten.odata.client.generator.model.EntityType) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) TComplexType(org.oasisopen.odata.csdl.v4.TComplexType) TEntityContainer(org.oasisopen.odata.csdl.v4.TEntityContainer) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TEntitySet (org.oasisopen.odata.csdl.v4.TEntitySet)3 ContextPath (com.github.davidmoten.odata.client.ContextPath)2 EntitySet (com.github.davidmoten.odata.client.generator.model.EntitySet)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 UncheckedIOException (java.io.UncheckedIOException)2 Optional (java.util.Optional)2 TNavigationPropertyBinding (org.oasisopen.odata.csdl.v4.TNavigationPropertyBinding)2 JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)1 JsonAnyGetter (com.fasterxml.jackson.annotation.JsonAnyGetter)1 JsonAnySetter (com.fasterxml.jackson.annotation.JsonAnySetter)1 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonIgnoreType (com.fasterxml.jackson.annotation.JsonIgnoreType)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 Include (com.fasterxml.jackson.annotation.JsonInclude.Include)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonPropertyOrder (com.fasterxml.jackson.annotation.JsonPropertyOrder)1 Lists (com.github.davidmoten.guavamini.Lists)1 Preconditions (com.github.davidmoten.guavamini.Preconditions)1