Search in sources :

Example 1 with ChangedFields

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

the class Serializer method serializeChangesOnly.

public <T extends ODataEntityType> String serializeChangesOnly(T entity, boolean prettyPrint) {
    try {
        ObjectMapper m = MAPPER_INCLUDE_NULLS;
        final String s;
        if (prettyPrint) {
            s = m.writerWithDefaultPrettyPrinter().writeValueAsString(entity);
        } else {
            s = m.writeValueAsString(entity);
        }
        JsonNode tree = m.readTree(s);
        ObjectNode o = (ObjectNode) tree;
        ChangedFields cf = entity.getChangedFields();
        List<String> list = new ArrayList<>();
        Iterator<String> it = o.fieldNames();
        while (it.hasNext()) {
            String name = it.next();
            if (!cf.contains(name) && !name.equals("@odata.type")) {
                list.add(name);
            }
        }
        o.remove(list);
        return o.toString();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ChangedFields(com.github.davidmoten.odata.client.internal.ChangedFields) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ChangedFields

use of com.github.davidmoten.odata.client.internal.ChangedFields 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)

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 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)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