use of com.github.davidmoten.odata.client.ContextPath 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);
}
}
use of com.github.davidmoten.odata.client.ContextPath 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);
}
}
use of com.github.davidmoten.odata.client.ContextPath 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);
}
}
use of com.github.davidmoten.odata.client.ContextPath 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());
}
use of com.github.davidmoten.odata.client.ContextPath in project odata-client by davidmoten.
the class Generator method writeEntitySet.
private void writeEntitySet(Schema schema, Pair<TEntityContainer, TEntitySet> pair) {
EntitySet t = new EntitySet(schema, pair.a, pair.b, names);
t.getDirectoryEntitySet().mkdirs();
Imports imports = new Imports(t.getFullClassNameEntitySet());
Indent indent = new Indent();
StringWriter w = new StringWriter();
try (PrintWriter p = new PrintWriter(w)) {
p.format("package %s;\n\n", t.getPackage());
p.format(IMPORTSHERE);
String baseCollectionClassName = t.getBaseCollectionRequestClassName(imports);
//
p.format(//
"%spublic final class %s extends %s {\n", //
indent, //
t.getSimpleClassNameEntitySet(), baseCollectionClassName);
indent.right();
//
p.format(//
"\n%spublic %s(%s contextPath) {\n", //
indent, //
t.getSimpleClassNameEntitySet(), imports.add(ContextPath.class));
p.format("%ssuper(contextPath, %s.empty());\n", indent.right(), imports.add(Optional.class));
p.format("%s}\n", indent.left());
// write navigation property bindings
Set<String> duplicateMethodNames = findDuplicateNavigationPropertyBindingMethodNames(pair, t);
//
Util.filter(//
pair.b.getNavigationPropertyBindingOrAnnotation(), //
TNavigationPropertyBinding.class).forEach(b -> {
String methodName = t.getMethodName(b);
if (duplicateMethodNames.contains(methodName)) {
methodName = t.getLongerMethodName(b);
}
Optional<EntitySet> referredEntitySet = t.getReferredEntitySet(b.getTarget());
if (referredEntitySet.isPresent()) {
String returnClassName = referredEntitySet.get().getFullClassNameEntitySet();
p.format("\n%spublic %s %s() {\n", indent, imports.add(returnClassName), methodName);
//
p.format(//
"%sreturn new %s(contextPath.addSegment(\"%s\"));\n", //
indent.right(), imports.add(//
referredEntitySet.get().getFullClassNameEntitySet()), b.getPath());
p.format("%s}\n", indent.left());
} else {
log(//
"WARN: EntitySet '" + b.getTarget() + //
"' not found for navigation property binding " + t.getSimpleClassNameEntitySet() + "." + b.getPath());
}
});
p.format("%s}\n", indent.left());
writeToFile(imports, w, t.getClassFile());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
Aggregations