Search in sources :

Example 1 with ReturnType

use of com.github.davidmoten.odata.client.generator.model.Action.ReturnType 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)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 ActionRequestNoReturn (com.github.davidmoten.odata.client.ActionRequestNoReturn)1 ActionRequestReturningNonCollection (com.github.davidmoten.odata.client.ActionRequestReturningNonCollection)1 ActionRequestReturningNonCollectionUnwrapped (com.github.davidmoten.odata.client.ActionRequestReturningNonCollectionUnwrapped)1 ClientException (com.github.davidmoten.odata.client.ClientException)1 CollectionPage (com.github.davidmoten.odata.client.CollectionPage)1 CollectionPageEntityRequest (com.github.davidmoten.odata.client.CollectionPageEntityRequest)1 CollectionPageNonEntityRequest (com.github.davidmoten.odata.client.CollectionPageNonEntityRequest)1 Context (com.github.davidmoten.odata.client.Context)1 ContextPath (com.github.davidmoten.odata.client.ContextPath)1