Search in sources :

Example 1 with Action

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

the class Generator method writeContainer.

private void writeContainer(Schema schema, TEntityContainer t) {
    names.getDirectoryContainer(schema).mkdirs();
    String simpleClassName = names.getSimpleClassNameContainer(schema, t.getName());
    Imports imports = new Imports(names.getFullClassNameContainer(schema, t.getName()));
    Indent indent = new Indent();
    StringWriter w = new StringWriter();
    try (PrintWriter p = new PrintWriter(w)) {
        p.format("package %s;\n\n", names.getPackageContainer(schema));
        p.format(IMPORTSHERE);
        final String extension;
        if (t.getExtends() != null) {
            extension = " extends " + imports.add(names.getFullClassNameFromTypeWithNamespace(t.getExtends()));
        } else {
            extension = "";
        }
        p.format("public final class %s%s implements %s {\n\n", simpleClassName, extension, imports.add(HasContext.class));
        // TODO handle container extension
        // write fields
        p.format("%sprivate final %s contextPath;\n\n", indent.right(), imports.add(ContextPath.class));
        // write constructor
        p.format("%spublic %s(%s context) {\n", indent, simpleClassName, imports.add(Context.class));
        p.format("%sthis.contextPath = new %s(context, context.service().getBasePath());\n", indent.right(), imports.add(ContextPath.class));
        p.format("%s}\n", indent.left());
        p.format("\n%s@%s\n", indent, imports.add(Override.class));
        p.format("%spublic %s _context() {\n", indent, imports.add(Context.class));
        p.format("%sreturn contextPath.context();\n", indent.right());
        p.format("%s}\n", indent.left());
        p.format("\n%spublic %s _service() {\n", indent, imports.add(HttpService.class));
        p.format("%sreturn contextPath.context().service();\n", indent.right());
        p.format("%s}\n", indent.left());
        // write static testing method
        p.format("\n%sstatic final class ContainerBuilderImpl extends %s<%s> {\n", indent, imports.add(ContainerBuilder.class), simpleClassName);
        p.format("\n%s@%s\n", indent.right(), imports.add(Override.class));
        p.format("%spublic %s _create(%s context) {\n", indent, simpleClassName, imports.add(Context.class));
        p.format("%sreturn new %s(context);\n", indent.right(), simpleClassName);
        p.format("%s}\n", indent.left());
        p.format("%s}\n", indent.left());
        p.format("\n%spublic static %s<%s<%s>, %s> test() {\n", indent, imports.add(BuilderBase.class), imports.add(ContainerBuilder.class), simpleClassName, simpleClassName);
        p.format("%sreturn new ContainerBuilderImpl();\n", indent.right());
        p.format("%s}\n", indent.left());
        // write get methods from properties
        // 
        Util.filter(t.getEntitySetOrActionImportOrFunctionImport(), TEntitySet.class).forEach(x -> {
            Schema sch = names.getSchema(x.getEntityType());
            boolean addedWithEmptyKeys = false;
            if (names.isEntityWithNamespace(x.getEntityType())) {
                String entityRequestType = names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, x.getEntityType());
                EntityType et = names.getEntityType(x.getEntityType());
                KeyInfo k = getKeyInfo(et, imports);
                p.format("\n%spublic %s %s(%s) {\n", indent, imports.add(entityRequestType), Names.getIdentifier(x.getName()), k.typedParams);
                p.format("%sreturn new %s(contextPath.addSegment(\"%s\")%s, %s.empty());\n", indent.right(), imports.add(entityRequestType), x.getName(), k.addKeys, imports.add(Optional.class));
                p.format("%s}\n", indent.left());
                addedWithEmptyKeys = k.isEmpty();
            }
            if (!addedWithEmptyKeys) {
                // don't add a collection method if there were no keys for the entity
                EntitySet es = new EntitySet(schema, t, x, names);
                p.format("\n%spublic %s %s() {\n", indent, imports.add(es.getFullClassNameEntitySet()), Names.getIdentifier(x.getName()));
                p.format("%sreturn new %s(\n", indent.right(), imports.add(es.getFullClassNameEntitySet()));
                p.format("%scontextPath.addSegment(\"%s\"));\n", indent.right().right().right().right(), x.getName());
                p.format("%s}\n", indent.left().left().left().left().left());
            }
        });
        // 
        Util.filter(t.getEntitySetOrActionImportOrFunctionImport(), // 
        TSingleton.class).forEach(x -> {
            String importedType = toClassName(x, imports);
            p.format("\n%spublic %s %s() {\n", indent, importedType, Names.getIdentifier(x.getName()));
            p.format("%sreturn new %s(contextPath.addSegment(\"%s\"), %s.empty());\n", indent.right(), importedType, x.getName(), imports.add(Optional.class));
            p.format("%s}\n", indent.left());
        });
        // write unbound actions
        Set<String> methodNames = new HashSet<>();
        // 
        Util.types(schema, // 
        TAction.class).filter(x -> !x.isIsBound()).forEach(x -> writeAction(imports, indent, p, new Action(x, names), methodNames));
        // 
        Util.types(schema, // 
        TFunction.class).filter(x -> !x.isIsBound()).forEach(x -> writeFunction(imports, indent, p, new Function(x, names), methodNames));
        p.format("\n}\n");
        File classFile = names.getClassFileContainer(schema, t.getName());
        writeToFile(imports, w, classFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
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) Action(com.github.davidmoten.odata.client.generator.model.Action) TAction(org.oasisopen.odata.csdl.v4.TAction) Schema(org.oasisopen.odata.csdl.v4.Schema) Function(com.github.davidmoten.odata.client.generator.model.Function) TFunction(org.oasisopen.odata.csdl.v4.TFunction) ContextPath(com.github.davidmoten.odata.client.ContextPath) TEntitySet(org.oasisopen.odata.csdl.v4.TEntitySet) EntitySet(com.github.davidmoten.odata.client.generator.model.EntitySet) ContainerBuilder(com.github.davidmoten.odata.client.TestingService.ContainerBuilder) StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) HasContext(com.github.davidmoten.odata.client.HasContext) Context(com.github.davidmoten.odata.client.Context) TSingleton(org.oasisopen.odata.csdl.v4.TSingleton) Optional(java.util.Optional) BuilderBase(com.github.davidmoten.odata.client.TestingService.BuilderBase) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) 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) HttpService(com.github.davidmoten.odata.client.HttpService) HasContext(com.github.davidmoten.odata.client.HasContext) File(java.io.File)

Example 2 with Action

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

the class Generator method writeEntityRequest.

private void writeEntityRequest(Schema schema, TEntityType entityType, Map<String, List<Action>> typeActions, Map<String, List<Function>> typeFunctions) {
    EntityType t = new EntityType(entityType, names);
    names.getDirectoryEntityRequest(schema).mkdirs();
    // TODO only write out those requests needed
    String simpleClassName = t.getSimpleClassNameEntityRequest();
    Imports imports = new Imports(t.getFullClassNameEntityRequest());
    Indent indent = new Indent();
    StringWriter w = new StringWriter();
    try (PrintWriter p = new PrintWriter(w)) {
        p.format("package %s;\n\n", t.getPackageEntityRequest());
        p.format(IMPORTSHERE);
        p.format("@%s\n", imports.add(JsonIgnoreType.class));
        // don't make class final because can get extended by EntitySet
        p.format("public class %s extends %s {\n\n", simpleClassName, imports.add(EntityRequest.class) + "<" + imports.add(t.getFullClassNameEntity()) + ">");
        indent.right();
        // add constructor
        // 
        p.format(// 
        "%spublic %s(%s contextPath, %s<%s> value) {\n", // 
        indent, // 
        simpleClassName, // 
        imports.add(ContextPath.class), imports.add(Optional.class), imports.add(Object.class));
        // 
        p.format(// 
        "%ssuper(%s.class, contextPath, value, %s);\n", // 
        indent.right(), // 
        imports.add(t.getFullClassNameEntity()), t.isMediaEntityOrHasStreamProperty());
        p.format("%s}\n", indent.left());
        indent.left();
        if (t.hasStream()) {
            p.format("\n%s/**\n", indent);
            p.format("%s * If returning a stream without using object metadata is not supported then", indent);
            p.format("%s * returns {@code Optional.empty()}. Otherwise, returns a stream provider\n", indent);
            p.format("%s * where the location of the stream is assumed to be the current path + {@code /$value}.\n", indent);
            p.format("%s *\n", indent);
            p.format("%s * @return StreamProvider if suitable metadata found otherwise returns\n", indent);
            p.format("%s *         {@code Optional.empty()}\n", indent);
            p.format("%s */\n", indent);
            p.format("%s@%s\n", indent, imports.add(JsonIgnore.class));
            p.format("%spublic %s<%s> getStreamCurrentPath() {\n", indent, imports.add(Optional.class), imports.add(StreamProvider.class));
            p.format("%sreturn %s.createStream(contextPath, null);\n", indent.right(), imports.add(RequestHelper.class));
            p.format("%s}\n", indent.left());
        }
        // TODO also support navigation properties with complexTypes?
        // 
        t.getNavigationProperties().stream().filter(x -> {
            boolean isEntity = names.isEntityWithNamespace(names.getInnerType(names.getType(x)));
            if (!isEntity) {
                log("Unexpected entity with non-entity navigation property type: " + simpleClassName + "." + x.getName() + ". If you get this message then raise an issue on the github project for odata-client.");
            }
            return isEntity;
        }).forEach(x -> {
            indent.right();
            final String returnClass;
            String y = x.getType().get(0);
            Schema sch = names.getSchema(names.getInnerType(y));
            if (Names.isCollection(y)) {
                returnClass = toClassName(x, imports);
            } else {
                returnClass = imports.add(names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, y));
            }
            // if collection then add with id method (if has ids)
            if (Names.isCollection(y)) {
                // TODO use actual key name from metadata
                String inner = names.getInnerType(y);
                // TODO remove redundant check
                if (names.isEntityWithNamespace(inner)) {
                    String entityRequestType = names.getFullClassNameEntityRequestFromTypeWithNamespace(sch, inner);
                    EntityType et = names.getEntityType(inner);
                    KeyInfo k = getKeyInfo(et, imports);
                    if (!k.isEmpty()) {
                        p.format("\n%spublic %s %s(%s) {\n", indent, imports.add(entityRequestType), Names.getIdentifier(x.getName()), k.typedParams);
                        p.format("%sreturn new %s(contextPath.addSegment(\"%s\")%s, %s.empty());\n", indent.right(), imports.add(entityRequestType), x.getName(), k.addKeys, imports.add(Optional.class));
                        p.format("%s}\n", indent.left());
                    }
                }
            }
            // 
            p.format(// 
            "\n%spublic %s %s() {\n", // 
            indent, // 
            returnClass, Names.getGetterMethodWithoutGet(x.getName()));
            if (isCollection(x)) {
                p.format("%sreturn new %s(\n", indent.right(), toClassName(x, imports));
                p.format("%scontextPath.addSegment(\"%s\"), %s.empty());\n", indent.right().right().right().right(), x.getName(), imports.add(Optional.class));
                indent.left().left().left().left();
            } else {
                p.format("%sreturn new %s(contextPath.addSegment(\"%s\"), %s.empty());\n", indent.right(), returnClass, x.getName(), imports.add(Optional.class));
            }
            p.format("%s}\n", indent.left());
            indent.left();
        });
        indent.right();
        Set<String> methodNames = new HashSet<>();
        writeBoundActionMethods(t, typeActions, imports, indent, p, methodNames);
        writeBoundFunctionMethods(t, typeFunctions, imports, indent, p, methodNames);
        indent.left();
        p.format("\n}\n");
        writeToFile(imports, w, t.getClassFileEntityRequest());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
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) StreamProvider(com.github.davidmoten.odata.client.StreamProvider) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Optional(java.util.Optional) JsonIgnoreType(com.fasterxml.jackson.annotation.JsonIgnoreType) Schema(org.oasisopen.odata.csdl.v4.Schema) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) EntityType(com.github.davidmoten.odata.client.generator.model.EntityType) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) ContextPath(com.github.davidmoten.odata.client.ContextPath) StringWriter(java.io.StringWriter) RequestHelper(com.github.davidmoten.odata.client.internal.RequestHelper) TypedObject(com.github.davidmoten.odata.client.internal.TypedObject) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 3 with Action

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

the class Generator method writeEntity.

private void writeEntity(TEntityType entityType, Map<String, List<Action>> typeActions, Map<String, List<Function>> typeFunctions) {
    EntityType t = new EntityType(entityType, names);
    t.getDirectoryEntity().mkdirs();
    String simpleClassName = t.getSimpleClassName();
    Imports imports = new Imports(t.getFullClassNameEntity());
    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(ODataEntityType.class));
        indent.right();
        if (!t.hasBaseType()) {
            addContextPathInjectableField(imports, indent, p);
            addUnmappedFieldsField(imports, indent, p);
            addChangedFieldsField(imports, indent, p);
        }
        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());
        // add other fields
        printPropertyFields(imports, indent, p, t.getProperties(), t.hasBaseType());
        // write constructor
        writeNoArgsConstructor(simpleClassName, indent, p, t.hasBaseType());
        writeBuilder(t, simpleClassName, imports, indent, p);
        p.format("\n%s@%s\n", indent, imports.add(Override.class));
        p.format("%s@%s\n", indent, imports.add(JsonIgnore.class));
        p.format("%spublic %s getChangedFields() {\n", indent, imports.add(ChangedFields.class));
        p.format("%sreturn changedFields;\n", indent.right());
        p.format("%s}\n", indent.left());
        String nullCheck = // 
        fieldNames(t).stream().map(// 
        f -> f + " != null").collect(Collectors.joining(" && "));
        if (!nullCheck.isEmpty()) {
            nullCheck = " && " + nullCheck;
        }
        p.format("\n%s@%s\n", indent, imports.add(Override.class));
        p.format("%spublic void postInject(boolean addKeysToContextPath) {\n", indent);
        p.format("%sif (addKeysToContextPath%s) {\n", indent.right(), nullCheck);
        p.format("%scontextPath = contextPath.clearQueries()%s;\n", indent.right(), getAddKeys(t, imports));
        p.format("%s}\n", indent.left());
        p.format("%s}\n", indent.left());
        Set<String> methodNames = new HashSet<>();
        // write property getter and setters
        printPropertyGetterAndSetters(t, imports, indent, p, simpleClassName, t.getFullType(), t.getProperties(), true, methodNames);
        addInheritedPropertyNames(t, methodNames);
        printNavigationPropertyGetters(t, imports, indent, p, t.getNavigationProperties(), methodNames);
        addUnmappedFieldsSetterAndGetter(imports, indent, p, methodNames);
        if (t.hasStream()) {
            p.format("\n%s/**\n", indent);
            p.format("%s * If suitable metadata found a StreamProvider is returned otherwise returns\n", indent);
            p.format("%s * {@code Optional.empty()}. Normally for a stream to be available this entity\n", indent);
            p.format("%s * needs to have been hydrated with full metadata. Consider calling the builder\n", indent);
            p.format("%s * method {@code .metadataFull()} when getting this instance (either directly or\n", indent);
            p.format("%s * as part of a collection).\n", indent);
            p.format("%s *\n", indent);
            p.format("%s * @return StreamProvider if suitable metadata found otherwise returns\n", indent);
            p.format("%s *         {@code Optional.empty()}\n", indent);
            p.format("%s */\n", indent);
            p.format("%s@%s\n", indent, imports.add(JsonIgnore.class));
            p.format("%spublic %s<%s> getStream() {\n", indent, imports.add(Optional.class), imports.add(StreamProvider.class));
            p.format("%sreturn %s.createStream(contextPath, this);\n", indent.right(), imports.add(RequestHelper.class));
            p.format("%s}\n", indent.left());
        }
        // write Patched class
        writePatchAndPutMethods(t, simpleClassName, imports, indent, p);
        writeCopyMethod(t, simpleClassName, imports, indent, p, true);
        writeBoundActionMethods(t, typeActions, imports, indent, p, methodNames);
        writeBoundFunctionMethods(t, typeFunctions, imports, indent, p, methodNames);
        // write toString
        writeToString(t, simpleClassName, imports, indent, p);
        p.format("%s}\n", indent.left());
        writeToFile(imports, w, t.getClassFile());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
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) StreamProvider(com.github.davidmoten.odata.client.StreamProvider) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Optional(java.util.Optional) ChangedFields(com.github.davidmoten.odata.client.internal.ChangedFields) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) EntityType(com.github.davidmoten.odata.client.generator.model.EntityType) ODataEntityType(com.github.davidmoten.odata.client.ODataEntityType) TEntityType(org.oasisopen.odata.csdl.v4.TEntityType) StringWriter(java.io.StringWriter) RequestHelper(com.github.davidmoten.odata.client.internal.RequestHelper) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 4 with Action

use of com.github.davidmoten.odata.client.generator.model.Action 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)

Example 5 with Action

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

the class Generator method writeAction.

private void writeAction(Imports imports, Indent indent, PrintWriter p, Action action, Set<String> methodNames) {
    // 
    p.format(// 
    "\n%s@%s(name = \"%s\")\n", // 
    indent, // 
    imports.add(com.github.davidmoten.odata.client.annotation.Action.class), action.getName());
    p.format("%s@%s\n", indent, imports.add(JsonIgnore.class));
    List<Parameter> parameters = action.getParametersUnbound(imports);
    String paramsDeclaration = // 
    parameters.stream().map(// 
    x -> String.format("%s %s", x.importedFullClassName, x.nameJava())).collect(Collectors.joining(", "));
    String methodName = disambiguateMethodName(action.getActionMethodName(), methodNames, "_Action");
    if (action.hasReturnType()) {
        ReturnType returnType = action.getReturnType(imports);
        boolean isNonCollectionEdm = !returnType.isCollection && returnType.innerType.startsWith("Edm.");
        final String typ;
        if (returnType.isStream()) {
            typ = imports.add(FunctionRequestReturningStream.class);
        } else {
            typ = (returnType.isCollection ? imports.add(CollectionPageNonEntityRequest.class) : isNonCollectionEdm ? // 
            imports.add(ActionRequestReturningNonCollection.class) : // 
            imports.add(ActionRequestReturningNonCollectionUnwrapped.class)) + // 
            "<" + // 
            action.getReturnType(imports).innerImportedFullClassName + ">";
        }
        // 
        p.format(// 
        "%spublic %s %s(%s) {\n", // 
        indent, // 
        typ, // 
        methodName, paramsDeclaration);
        writeActionParameterMapAndNullChecksAndAsciiChecks(imports, indent, p, parameters);
        if (returnType.isCollection) {
            p.format(// 
            "%sreturn %s.forAction(this.contextPath.addActionOrFunctionSegment(\"%s\"), %s.class, _parameters);\n", // 
            indent, // 
            imports.add(CollectionPageNonEntityRequest.class), // 
            action.getFullType(), returnType.innerImportedFullClassName);
        } else if (returnType.isStream()) {
            p.format(// 
            "%sthrow new %s(\"Actions that return a stream are not supported yet. If you want this raise an issue at the project home\");\n", // 
            indent, // 
            imports.add(UnsupportedOperationException.class), action.getFullType());
        } else
            p.format(// 
            "%sreturn new %s<%s>(this.contextPath.addActionOrFunctionSegment(\"%s\"), %s.class, _parameters);\n", // 
            indent, isNonCollectionEdm ? imports.add(ActionRequestReturningNonCollection.class) : // 
            imports.add(ActionRequestReturningNonCollectionUnwrapped.class), // 
            returnType.innerImportedFullClassName, // 
            action.getFullType(), returnType.innerImportedFullClassName);
    } else {
        // 
        p.format(// 
        "%spublic %s %s(%s) {\n", // 
        indent, // 
        imports.add(ActionRequestNoReturn.class), methodName, paramsDeclaration);
        writeActionParameterMapAndNullChecksAndAsciiChecks(imports, indent, p, parameters);
        p.format(// 
        "%sreturn new %s(this.contextPath.addActionOrFunctionSegment(\"%s\"), _parameters);\n", // 
        indent, // 
        imports.add(ActionRequestNoReturn.class), action.getFullType());
    }
    p.format("%s}\n", indent.left());
}
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) Action(com.github.davidmoten.odata.client.generator.model.Action) TAction(org.oasisopen.odata.csdl.v4.TAction) ActionRequestReturningNonCollection(com.github.davidmoten.odata.client.ActionRequestReturningNonCollection) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ReturnType(com.github.davidmoten.odata.client.generator.model.Action.ReturnType) TActionFunctionReturnType(org.oasisopen.odata.csdl.v4.TActionFunctionReturnType) ActionRequestReturningNonCollectionUnwrapped(com.github.davidmoten.odata.client.ActionRequestReturningNonCollectionUnwrapped) CollectionPageNonEntityRequest(com.github.davidmoten.odata.client.CollectionPageNonEntityRequest) FunctionRequestReturningStream(com.github.davidmoten.odata.client.FunctionRequestReturningStream) Parameter(com.github.davidmoten.odata.client.generator.model.Action.Parameter) TActionFunctionParameter(org.oasisopen.odata.csdl.v4.TActionFunctionParameter) ActionRequestNoReturn(com.github.davidmoten.odata.client.ActionRequestNoReturn)

Aggregations

JacksonInject (com.fasterxml.jackson.annotation.JacksonInject)5 JsonAnyGetter (com.fasterxml.jackson.annotation.JsonAnyGetter)5 JsonAnySetter (com.fasterxml.jackson.annotation.JsonAnySetter)5 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)5 JsonIgnoreType (com.fasterxml.jackson.annotation.JsonIgnoreType)5 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)5 Include (com.fasterxml.jackson.annotation.JsonInclude.Include)5 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)5 JsonPropertyOrder (com.fasterxml.jackson.annotation.JsonPropertyOrder)5 Lists (com.github.davidmoten.guavamini.Lists)5 Preconditions (com.github.davidmoten.guavamini.Preconditions)5 ActionRequestNoReturn (com.github.davidmoten.odata.client.ActionRequestNoReturn)5 ActionRequestReturningNonCollection (com.github.davidmoten.odata.client.ActionRequestReturningNonCollection)5 ActionRequestReturningNonCollectionUnwrapped (com.github.davidmoten.odata.client.ActionRequestReturningNonCollectionUnwrapped)5 ClientException (com.github.davidmoten.odata.client.ClientException)5 CollectionPage (com.github.davidmoten.odata.client.CollectionPage)5 CollectionPageEntityRequest (com.github.davidmoten.odata.client.CollectionPageEntityRequest)5 CollectionPageNonEntityRequest (com.github.davidmoten.odata.client.CollectionPageNonEntityRequest)5 Context (com.github.davidmoten.odata.client.Context)5 ContextPath (com.github.davidmoten.odata.client.ContextPath)5