Search in sources :

Example 1 with EdmStream

use of org.apache.olingo.commons.core.edm.primitivetype.EdmStream in project teiid by teiid.

the class ODataTypeManager method parseLiteral.

public static Object parseLiteral(String odataType, String value) throws TeiidException {
    EdmPrimitiveType primitiveType = EdmPrimitiveTypeFactory.getInstance(EdmPrimitiveTypeKind.valueOf(odataType.substring(4)));
    int maxLength = DataTypeManager.MAX_STRING_LENGTH;
    if (primitiveType instanceof EdmBinary || primitiveType instanceof EdmStream) {
        maxLength = DataTypeManager.MAX_VARBINARY_BYTES;
    }
    int precision = 4;
    int scale = 3;
    if (primitiveType instanceof EdmDecimal) {
        precision = 38;
        scale = 9;
    }
    Class<?> expectedClass = primitiveType.getDefaultType();
    try {
        if (EdmString.getInstance().equals(primitiveType)) {
            value = EdmString.getInstance().fromUriLiteral(value);
        }
        Object converted = primitiveType.valueOfString(value, false, maxLength, precision, scale, true, expectedClass);
        if (primitiveType instanceof EdmTimeOfDay) {
            Calendar ts = (Calendar) converted;
            return new Time(ts.getTimeInMillis());
        } else if (primitiveType instanceof EdmDate) {
            Calendar ts = (Calendar) converted;
            return new Date(ts.getTimeInMillis());
        }
        return converted;
    } catch (EdmPrimitiveTypeException e) {
        throw new TeiidException(e);
    }
}
Also used : EdmPrimitiveType(org.apache.olingo.commons.api.edm.EdmPrimitiveType) EdmBinary(org.apache.olingo.commons.core.edm.primitivetype.EdmBinary) EdmStream(org.apache.olingo.commons.core.edm.primitivetype.EdmStream) Calendar(java.util.Calendar) Time(java.sql.Time) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) Date(java.sql.Date) EdmDecimal(org.apache.olingo.commons.core.edm.primitivetype.EdmDecimal) TeiidException(org.teiid.core.TeiidException) EdmTimeOfDay(org.apache.olingo.commons.core.edm.primitivetype.EdmTimeOfDay)

Example 2 with EdmStream

use of org.apache.olingo.commons.core.edm.primitivetype.EdmStream in project teiid by teiid.

the class EntityCollectionResponse method createEntity.

static Entity createEntity(Row row, DocumentNode node, String baseURL, EntityCollectionResponse response) throws SQLException {
    List<ProjectedColumn> projected = node.getAllProjectedColumns();
    EdmEntityType entityType = node.getEdmEntityType();
    LinkedHashMap<String, Link> streamProperties = new LinkedHashMap<String, Link>();
    Entity entity = new Entity();
    entity.setType(entityType.getFullQualifiedName().getFullQualifiedNameAsString());
    boolean allNulls = true;
    for (ProjectedColumn column : projected) {
        /*
            if (!column.isVisible()) {
                continue;
            }*/
        String propertyName = Symbol.getShortName(column.getExpression());
        Object value = row.getObject(column.getOrdinal());
        if (value != null) {
            allNulls = false;
        }
        try {
            SingletonPrimitiveType type = (SingletonPrimitiveType) column.getEdmType();
            if (type instanceof EdmStream) {
                buildStreamLink(streamProperties, value, propertyName);
                if (response != null) {
                    // this will only be used for a stream response off of the first entity. In all other scenarios it will be ignored.
                    response.setStream(propertyName, value);
                }
            } else {
                Property property = buildPropery(propertyName, type, column.getPrecision(), column.getScale(), column.isCollection(), value);
                entity.addProperty(property);
            }
        } catch (IOException e) {
            throw new SQLException(e);
        } catch (TeiidProcessingException e) {
            throw new SQLException(e);
        }
    }
    if (allNulls) {
        return null;
    }
    // Build the navigation and Stream Links
    try {
        String id = EntityResponse.buildLocation(baseURL, entity, entityType.getName(), entityType);
        entity.setId(new URI(id));
        // build stream properties
        for (String name : streamProperties.keySet()) {
            Link link = streamProperties.get(name);
            link.setHref(id + "/" + name);
            entity.getMediaEditLinks().add(link);
            entity.addProperty(createPrimitive(name, EdmStream.getInstance(), new URI(link.getHref())));
        }
        // build navigations
        for (String name : entityType.getNavigationPropertyNames()) {
            Link navLink = new Link();
            navLink.setTitle(name);
            navLink.setHref(id + "/" + name);
            navLink.setRel("http://docs.oasis-open.org/odata/ns/related/" + name);
            entity.getNavigationLinks().add(navLink);
            Link assosiationLink = new Link();
            assosiationLink.setTitle(name);
            assosiationLink.setHref(id + "/" + name + "/$ref");
            assosiationLink.setRel("http://docs.oasis-open.org/odata/ns/relatedlinks/" + name);
            entity.getAssociationLinks().add(assosiationLink);
        }
    } catch (URISyntaxException e) {
        throw new SQLException(e);
    } catch (EdmPrimitiveTypeException e) {
        throw new SQLException(e);
    }
    return entity;
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) SQLException(java.sql.SQLException) EdmStream(org.apache.olingo.commons.core.edm.primitivetype.EdmStream) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) URI(java.net.URI) ProjectedColumn(org.teiid.olingo.ProjectedColumn) LinkedHashMap(java.util.LinkedHashMap) TeiidProcessingException(org.teiid.core.TeiidProcessingException) SingletonPrimitiveType(org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType) Property(org.apache.olingo.commons.api.data.Property) Link(org.apache.olingo.commons.api.data.Link)

Aggregations

EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)2 EdmStream (org.apache.olingo.commons.core.edm.primitivetype.EdmStream)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Date (java.sql.Date)1 SQLException (java.sql.SQLException)1 Time (java.sql.Time)1 Calendar (java.util.Calendar)1 LinkedHashMap (java.util.LinkedHashMap)1 Entity (org.apache.olingo.commons.api.data.Entity)1 Link (org.apache.olingo.commons.api.data.Link)1 Property (org.apache.olingo.commons.api.data.Property)1 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)1 EdmPrimitiveType (org.apache.olingo.commons.api.edm.EdmPrimitiveType)1 EdmBinary (org.apache.olingo.commons.core.edm.primitivetype.EdmBinary)1 EdmDate (org.apache.olingo.commons.core.edm.primitivetype.EdmDate)1 EdmDecimal (org.apache.olingo.commons.core.edm.primitivetype.EdmDecimal)1 EdmTimeOfDay (org.apache.olingo.commons.core.edm.primitivetype.EdmTimeOfDay)1 SingletonPrimitiveType (org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType)1