Search in sources :

Example 1 with Link

use of org.apache.olingo.commons.api.data.Link in project teiid by teiid.

the class TeiidServiceHandler method performDeepInsert.

private UpdateResponse performDeepInsert(String rawURI, UriInfo uriInfo, EdmEntityType entityType, Entity entity, List<ExpandNode> expandNodes) throws SQLException, TeiidException {
    UpdateResponse response = performInsert(rawURI, uriInfo, entityType, entity);
    for (String navigationName : entityType.getNavigationPropertyNames()) {
        EdmNavigationProperty navProperty = entityType.getNavigationProperty(navigationName);
        Link navLink = entity.getNavigationLink(navigationName);
        if (navLink != null && navLink.getInlineEntity() != null) {
            ExpandNode node = new ExpandNode();
            node.navigationProperty = navProperty;
            expandNodes.add(node);
            performDeepInsert(rawURI, uriInfo, navProperty.getType(), navLink.getInlineEntity(), node.children);
        } else if (navLink != null && navLink.getInlineEntitySet() != null && !navLink.getInlineEntitySet().getEntities().isEmpty()) {
            ExpandNode node = new ExpandNode();
            node.navigationProperty = navProperty;
            expandNodes.add(node);
            for (Entity inlineEntity : navLink.getInlineEntitySet().getEntities()) {
                performDeepInsert(rawURI, uriInfo, navProperty.getType(), inlineEntity, node.children);
            }
        }
    }
    return response;
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) Entity(org.apache.olingo.commons.api.data.Entity) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) Link(org.apache.olingo.commons.api.data.Link)

Example 2 with Link

use of org.apache.olingo.commons.api.data.Link in project teiid by teiid.

the class TeiidServiceHandler method insertDepth.

private int insertDepth(EdmEntityType entityType, Entity entity) throws SQLException, TeiidException {
    int depth = 1;
    int childDepth = 0;
    for (String navigationName : entityType.getNavigationPropertyNames()) {
        EdmNavigationProperty navProperty = entityType.getNavigationProperty(navigationName);
        Link navLink = entity.getNavigationLink(navigationName);
        if (navLink != null && navLink.getInlineEntity() != null) {
            childDepth = Math.max(childDepth, insertDepth(navProperty.getType(), navLink.getInlineEntity()));
        } else if (navLink != null && navLink.getInlineEntitySet() != null && !navLink.getInlineEntitySet().getEntities().isEmpty()) {
            for (Entity inlineEntity : navLink.getInlineEntitySet().getEntities()) {
                childDepth = Math.max(childDepth, insertDepth(navProperty.getType(), inlineEntity));
            }
        }
    }
    return depth + childDepth;
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) Link(org.apache.olingo.commons.api.data.Link)

Example 3 with Link

use of org.apache.olingo.commons.api.data.Link in project teiid by teiid.

the class EntityCollectionResponse method buildStreamLink.

private static void buildStreamLink(LinkedHashMap<String, Link> streamProperties, Object value, String propName) {
    if (value != null) {
        // read link
        Link streamLink = new Link();
        streamLink.setTitle(propName);
        if (value instanceof SQLXML) {
            streamLink.setType("application/xml");
        } else if (value instanceof Clob) {
            streamLink.setType("application/json");
        } else if (value instanceof Blob) {
            streamLink.setType("application/octet-stream");
        }
        streamLink.setRel("http://docs.oasis-open.org/odata/ns/mediaresource/" + propName);
        streamProperties.put(propName, streamLink);
    }
}
Also used : SQLXML(java.sql.SQLXML) Blob(java.sql.Blob) Clob(java.sql.Clob) Link(org.apache.olingo.commons.api.data.Link)

Example 4 with Link

use of org.apache.olingo.commons.api.data.Link 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)

Example 5 with Link

use of org.apache.olingo.commons.api.data.Link in project teiid by teiid.

the class EntityCollectionResponse method processExpands.

private void processExpands(Row vals, Entity entity, DocumentNode node) throws SQLException {
    if (node.getExpands() == null || node.getExpands().isEmpty()) {
        return;
    }
    for (ExpandDocumentNode expandNode : node.getExpands()) {
        Object[] expandedVals = vals.getArray(expandNode.getColumnIndex());
        if (expandedVals == null) {
            continue;
        }
        for (Object o : expandedVals) {
            Object[] expandedVal = (Object[]) o;
            Entity expandEntity = createEntity(expandedVal, expandNode, this.baseURL, this);
            Link link = entity.getNavigationLink(expandNode.getNavigationName());
            if (expandNode.isCollection()) {
                if (link.getInlineEntitySet() == null) {
                    link.setInlineEntitySet(new EntityCollectionResponse());
                }
                EntityCollectionResponse expandResponse = (EntityCollectionResponse) link.getInlineEntitySet();
                boolean addEntity = expandResponse.processOptions(expandNode.getSkip(), expandNode.getTop(), expandEntity);
                if (addEntity) {
                    link.getInlineEntitySet().getEntities().add(expandEntity);
                }
            } else {
                link.setInlineEntity(expandEntity);
            }
            processExpands(asRow(expandedVal), expandEntity, expandNode);
        }
    }
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) Link(org.apache.olingo.commons.api.data.Link)

Aggregations

Link (org.apache.olingo.commons.api.data.Link)5 Entity (org.apache.olingo.commons.api.data.Entity)4 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)2 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Blob (java.sql.Blob)1 Clob (java.sql.Clob)1 SQLException (java.sql.SQLException)1 SQLXML (java.sql.SQLXML)1 LinkedHashMap (java.util.LinkedHashMap)1 Property (org.apache.olingo.commons.api.data.Property)1 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)1 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)1 EdmStream (org.apache.olingo.commons.core.edm.primitivetype.EdmStream)1 SingletonPrimitiveType (org.apache.olingo.commons.core.edm.primitivetype.SingletonPrimitiveType)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 UpdateResponse (org.teiid.odata.api.UpdateResponse)1 ProjectedColumn (org.teiid.olingo.ProjectedColumn)1