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