Search in sources :

Example 1 with StreamUploader

use of com.github.davidmoten.odata.client.StreamUploader in project odata-client by davidmoten.

the class Generator method printPropertyGetterAndSetters.

private void printPropertyGetterAndSetters(Structure<?> structure, Imports imports, Indent indent, PrintWriter p, String simpleClassName, String fullType, List<TProperty> properties, boolean ofEntity, Set<String> methodNames) {
    // write getters and setters
    // 
    properties.forEach(x -> {
        String fieldName = Names.getIdentifier(x.getName());
        String t = names.getType(x);
        boolean isCollection = isCollection(x);
        structure.printPropertyJavadoc(p, indent, x.getName(), "property " + x.getName(), Collections.emptyMap());
        addPropertyAnnotation(imports, indent, p, x.getName());
        p.format("\n%s@%s\n", indent, imports.add(JsonIgnore.class));
        String methodName = Names.getGetterMethod(x.getName());
        methodNames.add(methodName);
        if (isCollection) {
            String inner = names.getInnerType(t);
            String importedInnerType = names.toImportedTypeNonCollection(inner, imports);
            boolean isEntity = names.isEntityWithNamespace(inner);
            {
                String options = String.format("%s.EMPTY", imports.add(HttpRequestOptions.class));
                p.format("%spublic %s<%s> %s() {\n", indent, imports.add(CollectionPage.class), importedInnerType, methodName);
                writePropertyGetterCollectionBody(imports, indent, p, fieldName, inner, importedInnerType, isEntity, options);
                p.format("%s}\n", indent.left());
            }
            // add a mutator for a collection if is collection of complex type and owner is an entity
            if (!isEntity && ofEntity) {
                Map<String, String> map = new LinkedHashMap<>();
                map.put(fieldName, "new value of {@code " + x.getName() + "} field (as defined in service metadata)");
                structure.printMutatePropertyJavadoc(p, indent, x.getName(), map);
                String classSuffix = "";
                String withMethodName = Names.getWithMethod(x.getName());
                methodNames.add(withMethodName);
                // 
                p.format(// 
                "\n%spublic %s%s %s(%s<%s> %s) {\n", // 
                indent, // 
                simpleClassName, // 
                classSuffix, withMethodName, imports.add(List.class), importedInnerType, fieldName);
                // use _x as identifier so doesn't conflict with any field name
                p.format("%s%s _x = _copy();\n", indent.right(), simpleClassName);
                if (ofEntity) {
                    p.format("%s_x.changedFields = changedFields.add(\"%s\");\n", indent, x.getName());
                }
                // 
                p.format(// 
                "%s_x.odataType = %s.nvl(odataType, \"%s\");\n", // 
                indent, // 
                imports.add(com.github.davidmoten.odata.client.Util.class), fullType);
                p.format("%s_x.%s = %s;\n", indent, fieldName, fieldName);
                p.format("%sreturn _x;\n", indent);
                p.format("%s}\n", indent.left());
            }
            {
                String options = "options";
                Map<String, String> parameterDoc = new HashMap<>();
                parameterDoc.put("options", "specify connect and read timeouts");
                structure.printPropertyJavadoc(p, indent, x.getName(), "property " + x.getName(), parameterDoc);
                addPropertyAnnotation(imports, indent, p, x.getName());
                p.format("\n%s@%s\n", indent, imports.add(JsonIgnore.class));
                p.format("%spublic %s<%s> %s(%s options) {\n", indent, imports.add(CollectionPage.class), importedInnerType, methodName, imports.add(HttpRequestOptions.class));
                writePropertyGetterCollectionBody(imports, indent, p, fieldName, inner, importedInnerType, isEntity, options);
                p.format("%s}\n", indent.left());
            }
        } else {
            boolean isStream = isStream(x);
            if (isStream) {
                p.format("%spublic %s<%s> %s() {\n", indent, imports.add(Optional.class), imports.add(StreamProvider.class), methodName);
                p.format("%sreturn %s.createStreamForEdmStream(contextPath, this, \"%s\", %s);\n", indent.right(), imports.add(RequestHelper.class), x.getName(), fieldName);
                p.format("%s}\n", indent.left());
                for (HttpMethod method : HttpMethod.createOrUpdateMethods()) {
                    String putMethodName = Names.getPutMethod(x.getName(), method);
                    methodNames.add(putMethodName);
                    p.format("\n%s/**", indent);
                    p.format("\n%s * If metadata indicate that the stream is editable then returns", indent);
                    p.format("\n%s * a {@link StreamUploader} which can be used to upload the stream", indent);
                    p.format("\n%s * to the {@code %s} property, using HTTP %s.", indent, x.getName(), method);
                    p.format("\n%s *", indent);
                    p.format("\n%s * @return a StreamUploader if upload permitted", indent);
                    p.format("\n%s */", indent);
                    addPropertyAnnotation(imports, indent, p, x.getName());
                    p.format("\n%spublic %s<%s> %s() {\n", indent, // 
                    imports.add(Optional.class), // 
                    imports.add(StreamUploaderSingleCall.class), putMethodName);
                    // 
                    p.format(// 
                    "%sreturn %s(%s.singleCall());\n", // 
                    indent.right(), // 
                    putMethodName, // 
                    imports.add(UploadStrategy.class));
                    p.format("%s}\n", indent.left());
                    String putChunkedMethodName = Names.getPutChunkedMethod(x.getName(), method);
                    methodNames.add(putChunkedMethodName);
                    p.format("\n%s/**", indent);
                    p.format("\n%s * If metadata indicate that the stream is editable then returns", indent);
                    p.format("\n%s * a {@link StreamUploaderChunked} which can be used to upload the stream", indent);
                    p.format("\n%s * to the {@code %s} property, using HTTP %s.", indent, x.getName(), method);
                    p.format("\n%s *", indent);
                    p.format("\n%s * @return a StreamUploaderChunked if upload permitted", indent);
                    p.format("\n%s */", indent);
                    addPropertyAnnotation(imports, indent, p, x.getName());
                    p.format("\n%spublic %s<%s> %s() {\n", indent, // 
                    imports.add(Optional.class), // 
                    imports.add(StreamUploaderChunked.class), putChunkedMethodName);
                    // 
                    p.format(// 
                    "%sreturn %s(%s.chunked());\n", // 
                    indent.right(), // 
                    putMethodName, // 
                    imports.add(UploadStrategy.class));
                    p.format("%s}\n", indent.left());
                    addPropertyAnnotation(imports, indent, p, x.getName());
                    p.format(// 
                    "\n%spublic <T extends %s<T>> Optional<T> %s(%s<T> strategy) {\n", // 
                    indent, // 
                    imports.add(StreamUploader.class), // 
                    putMethodName, imports.add(UploadStrategy.class));
                    p.format(// 
                    "%sreturn strategy.builder(contextPath.addSegment(\"%s\"), this, \"%s\", %s.%s);\n", // 
                    indent.right(), // 
                    x.getName(), // 
                    x.getName(), // 
                    imports.add(HttpMethod.class), method.name());
                    p.format("%s}\n", indent.left());
                }
            } else {
                final String importedType = names.toImportedTypeNonCollection(t, imports);
                String importedTypeWithOptional = imports.add(Optional.class) + "<" + importedType + ">";
                p.format("%spublic %s %s() {\n", indent, importedTypeWithOptional, methodName);
                p.format("%sreturn %s.ofNullable(%s);\n", indent.right(), imports.add(Optional.class), fieldName);
                p.format("%s}\n", indent.left());
                Map<String, String> map = new LinkedHashMap<>();
                map.put(fieldName, "new value of {@code " + x.getName() + "} field (as defined in service metadata)");
                structure.printMutatePropertyJavadoc(p, indent, x.getName(), map);
                String classSuffix = "";
                String withMethodName = Names.getWithMethod(x.getName());
                methodNames.add(withMethodName);
                p.format("\n%spublic %s%s %s(%s %s) {\n", indent, simpleClassName, classSuffix, withMethodName, importedType, fieldName);
                if (x.isUnicode() != null && !x.isUnicode()) {
                    p.format("%s%s.checkIsAscii(%s);\n", indent.right(), imports.add(Checks.class), fieldName);
                    indent.left();
                }
                // use _x as identifier so doesn't conflict with any field name
                p.format("%s%s _x = _copy();\n", indent.right(), simpleClassName);
                if (ofEntity) {
                    p.format("%s_x.changedFields = changedFields.add(\"%s\");\n", indent, x.getName());
                }
                // 
                p.format(// 
                "%s_x.odataType = %s.nvl(odataType, \"%s\");\n", // 
                indent, // 
                imports.add(com.github.davidmoten.odata.client.Util.class), fullType);
                p.format("%s_x.%s = %s;\n", indent, fieldName, fieldName);
                p.format("%sreturn _x;\n", indent);
                p.format("%s}\n", indent.left());
            }
            // add special convenience method to write stream to upload url. Supports Ms Graph createUploadSession.
            if (structure.getSimpleClassName().equals("UploadSession") && x.getName().equals("uploadUrl")) {
                addPropertyAnnotation(imports, indent, p, x.getName());
                // 
                p.format(// 
                "\n%spublic <T extends %s<T>> T put(%s<T> strategy) {\n", // 
                indent, // 
                imports.add(StreamUploader.class), imports.add(UploadStrategy.class));
                p.format("%sthis.unmappedFields.put(\"uploadUrl@odata.mediaEditLink\", uploadUrl);\n", indent.right());
                // 
                p.format(// 
                "%sreturn strategy.builder(new %s(contextPath.context(), new %s(uploadUrl, contextPath.context().service().getBasePath().style())), this, \"uploadUrl\", %s.%s).get();\n", // 
                indent, // 
                imports.add(ContextPath.class), // 
                imports.add(Path.class), // 
                imports.add(HttpMethod.class), HttpMethod.PUT.name());
                p.format("%s}\n", indent.left());
                addPropertyAnnotation(imports, indent, p, x.getName());
                p.format("\n%spublic %s putChunked() {\n", indent, imports.add(StreamUploaderChunked.class));
                p.format("%sreturn put(%s.chunked());\n", indent.right(), imports.add(UploadStrategy.class));
                p.format("%s}\n", indent.left());
                addPropertyAnnotation(imports, indent, p, x.getName());
                p.format("\n%spublic %s put() {\n", indent, imports.add(StreamUploaderSingleCall.class));
                p.format("%sreturn put(%s.singleCall());\n", indent.right(), imports.add(UploadStrategy.class));
                p.format("%s}\n", indent.left());
            }
        }
    });
    // add unmapped fields mutator withUnmappedField
    String method = Names.getWithMethod("unmappedField");
    // 
    p.format(// 
    "\n%spublic %s %s(%s name, %s value) {\n", // 
    indent, // 
    simpleClassName, // 
    method, imports.add(String.class), imports.add(String.class));
    p.format("%s%s _x = _copy();\n", indent.right(), simpleClassName);
    p.format("%s_x.setUnmappedField(name, value);\n", indent);
    p.format("%sreturn _x;\n", indent);
    p.format("%s}\n", indent.left());
}
Also used : StreamUploaderChunked(com.github.davidmoten.odata.client.StreamUploaderChunked) ContextPath(com.github.davidmoten.odata.client.ContextPath) Path(com.github.davidmoten.odata.client.Path) StreamProvider(com.github.davidmoten.odata.client.StreamProvider) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) Optional(java.util.Optional) StreamUploaderSingleCall(com.github.davidmoten.odata.client.StreamUploaderSingleCall) StreamUploader(com.github.davidmoten.odata.client.StreamUploader) LinkedHashMap(java.util.LinkedHashMap) ContextPath(com.github.davidmoten.odata.client.ContextPath) Checks(com.github.davidmoten.odata.client.internal.Checks) RequestHelper(com.github.davidmoten.odata.client.internal.RequestHelper) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ParameterMap(com.github.davidmoten.odata.client.internal.ParameterMap) HashMap(java.util.HashMap) HttpMethod(com.github.davidmoten.odata.client.HttpMethod) UploadStrategy(com.github.davidmoten.odata.client.UploadStrategy)

Aggregations

JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 ContextPath (com.github.davidmoten.odata.client.ContextPath)1 HttpMethod (com.github.davidmoten.odata.client.HttpMethod)1 Path (com.github.davidmoten.odata.client.Path)1 StreamProvider (com.github.davidmoten.odata.client.StreamProvider)1 StreamUploader (com.github.davidmoten.odata.client.StreamUploader)1 StreamUploaderChunked (com.github.davidmoten.odata.client.StreamUploaderChunked)1 StreamUploaderSingleCall (com.github.davidmoten.odata.client.StreamUploaderSingleCall)1 UploadStrategy (com.github.davidmoten.odata.client.UploadStrategy)1 Checks (com.github.davidmoten.odata.client.internal.Checks)1 ParameterMap (com.github.davidmoten.odata.client.internal.ParameterMap)1 RequestHelper (com.github.davidmoten.odata.client.internal.RequestHelper)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1