Search in sources :

Example 1 with JsonSerializer

use of org.apache.olingo.client.core.serialization.JsonSerializer in project teiid by teiid.

the class ODataUpdateQuery method getPayload.

public String getPayload(Entity parentEntity) throws TranslatorException {
    JsonSerializer serializer = new JsonSerializer(false, ContentType.APPLICATION_JSON);
    StringWriter writer = new StringWriter();
    try {
        if (!this.complexTables.isEmpty()) {
            Table table = this.complexTables.get(0).getTable();
            Property complexProperty = new Property();
            complexProperty.setName(getName(table));
            complexProperty.setType(table.getProperty(ODataMetadataProcessor.NAME_IN_SCHEMA, false));
            ComplexValue value = new ComplexValue();
            for (Property p : this.payloadProperties) {
                value.getValue().add(p);
            }
            if (this.complexTables.get(0).isCollection()) {
                complexProperty.setValue(ValueType.COLLECTION_COMPLEX, Arrays.asList(value));
            } else {
                complexProperty.setValue(ValueType.COMPLEX, value);
            }
            serializer.write(writer, complexProperty);
        } else if (!this.expandTables.isEmpty()) {
            Table table = this.expandTables.get(0).getTable();
            Entity entity = new Entity();
            entity.setType(table.getProperty(ODataMetadataProcessor.NAME_IN_SCHEMA, false));
            for (Property p : this.payloadProperties) {
                entity.addProperty(p);
            }
            serializer.write(writer, entity);
        } else {
            Entity entity = new Entity();
            entity.setType(this.rootDocument.getTable().getProperty(ODataMetadataProcessor.NAME_IN_SCHEMA, false));
            for (Property p : this.payloadProperties) {
                entity.addProperty(p);
            }
            // for updates
            if (parentEntity != null) {
                // add all the key properties.
                List<Column> keys = this.rootDocument.getTable().getPrimaryKey().getColumns();
                for (Column key : keys) {
                    entity.addProperty(parentEntity.getProperty(key.getName()));
                }
            }
            serializer.write(writer, entity);
        }
    } catch (ODataSerializerException e) {
        throw new TranslatorException(e);
    }
    return writer.toString();
}
Also used : ComplexValue(org.apache.olingo.commons.api.data.ComplexValue) Entity(org.apache.olingo.commons.api.data.Entity) ODataSerializerException(org.apache.olingo.client.api.serialization.ODataSerializerException) Table(org.teiid.metadata.Table) StringWriter(java.io.StringWriter) Column(org.teiid.metadata.Column) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) JsonSerializer(org.apache.olingo.client.core.serialization.JsonSerializer) Property(org.apache.olingo.commons.api.data.Property)

Aggregations

StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ODataSerializerException (org.apache.olingo.client.api.serialization.ODataSerializerException)1 JsonSerializer (org.apache.olingo.client.core.serialization.JsonSerializer)1 ComplexValue (org.apache.olingo.commons.api.data.ComplexValue)1 Entity (org.apache.olingo.commons.api.data.Entity)1 Property (org.apache.olingo.commons.api.data.Property)1 Column (org.teiid.metadata.Column)1 Table (org.teiid.metadata.Table)1 TranslatorException (org.teiid.translator.TranslatorException)1