Search in sources :

Example 6 with EdmProperty

use of org.apache.olingo.commons.api.edm.EdmProperty in project teiid by teiid.

the class ReferenceUpdateSQLBuilder method updateReference.

public Update updateReference(URI referenceId, boolean prepared, boolean delete) throws SQLException {
    try {
        if (referenceId != null) {
            UriInfo uriInfo = ODataSQLBuilder.buildUriInfo(referenceId, this.baseURI, this.serviceMetadata, this.odata);
            UriResourceEntitySet uriEnitytSet = (UriResourceEntitySet) uriInfo.asUriInfoResource().getUriResourceParts().get(0);
            if (this.collection) {
                this.updateTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
            } else {
                this.referenceTable.setKeyPredicates(uriEnitytSet.getKeyPredicates());
            }
        }
    } catch (UriParserException e) {
        throw new SQLException(e);
    } catch (URISyntaxException e) {
        throw new SQLException(e);
    } catch (UriValidationException e) {
        throw new SQLException(e);
    }
    try {
        Update update = new Update();
        update.setGroup(this.updateTable.getGroupSymbol());
        List<String> columnNames = DocumentNode.getColumnNames(this.updateTable.getFk().getColumns());
        for (int i = 0; i < columnNames.size(); i++) {
            Column column = this.updateTable.getFk().getColumns().get(i);
            String columnName = columnNames.get(i);
            ElementSymbol symbol = new ElementSymbol(columnName, this.updateTable.getGroupSymbol());
            EdmEntityType entityType = this.updateTable.getEdmEntityType();
            EdmProperty edmProperty = (EdmProperty) entityType.getProperty(columnName);
            // reference table keys will be null for delete scenario
            Object value = null;
            if (!delete) {
                UriParameter parameter = getParameter(this.updateTable.getFk().getReferenceColumns().get(i), this.referenceTable.getKeyPredicates());
                value = ODataTypeManager.parseLiteral(edmProperty, column.getJavaType(), parameter.getText());
            }
            if (prepared) {
                update.addChange(symbol, new Reference(i++));
                this.params.add(ODataSQLBuilder.asParam(edmProperty, value));
            } else {
                update.addChange(symbol, new Constant(ODataSQLBuilder.asParam(edmProperty, value).getValue()));
            }
        }
        Criteria criteria = DocumentNode.buildEntityKeyCriteria(this.updateTable, null, this.metadata, this.odata, null, null);
        update.setCriteria(criteria);
        return update;
    } catch (TeiidException e) {
        throw new SQLException(e);
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) SQLException(java.sql.SQLException) Reference(org.teiid.query.sql.symbol.Reference) Constant(org.teiid.query.sql.symbol.Constant) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) URISyntaxException(java.net.URISyntaxException) Criteria(org.teiid.query.sql.lang.Criteria) Update(org.teiid.query.sql.lang.Update) TeiidException(org.teiid.core.TeiidException) UriValidationException(org.apache.olingo.server.core.uri.validator.UriValidationException) Column(org.teiid.metadata.Column) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) UriResourceEntitySet(org.apache.olingo.server.api.uri.UriResourceEntitySet) UriInfo(org.apache.olingo.server.api.uri.UriInfo) UriParserException(org.apache.olingo.server.core.uri.parser.UriParserException) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Example 7 with EdmProperty

use of org.apache.olingo.commons.api.edm.EdmProperty in project teiid by teiid.

the class TeiidServiceHandler method read.

@Override
public <T extends ServiceResponse> void read(final DataRequest request, T response) throws ODataLibraryException, ODataApplicationException {
    final ODataSQLBuilder visitor = new ODataSQLBuilder(odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.visit(request.getUriInfo());
    final BaseResponse queryResponse;
    try {
        Query query = visitor.selectQuery();
        queryResponse = executeQuery(request, request.isCountRequest(), visitor, query);
    } catch (Throwable e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    response.accepts(new ServiceResponseVisior() {

        public void visit(CountResponse response) throws ODataLibraryException, ODataApplicationException {
            org.teiid.odata.api.CountResponse cr = (org.teiid.odata.api.CountResponse) queryResponse;
            response.writeCount(cr.getCount());
        }

        public void visit(PrimitiveValueResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (!entitySet.getEntities().isEmpty()) {
                Entity entity = entitySet.getEntities().get(0);
                EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
                Property property = entity.getProperty(edmProperty.getName());
                if (property == null) {
                    response.writeNotFound(true);
                } else if (property.getValue() == null) {
                    response.writeNoContent(true);
                } else {
                    response.write(property.getValue());
                }
            } else {
                response.writeNotFound(true);
            }
        }

        public void visit(PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (!entitySet.getEntities().isEmpty()) {
                Entity entity = entitySet.getEntities().get(0);
                EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
                Property property = entity.getProperty(edmProperty.getName());
                response.writeProperty(edmProperty.getType(), property);
            } else {
                response.writeNotFound(true);
            }
        }

        public void visit(StreamResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollectionResponse entitySet = (EntityCollectionResponse) queryResponse;
            EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
            Object value = entitySet.getStream(edmProperty.getName());
            if (value == null) {
                response.writeNoContent(true);
            } else {
                try {
                    handleLobResult(getClient().getProperty(Client.CHARSET), value, response);
                } catch (SQLException e) {
                    LogManager.logDetail(LogConstants.CTX_ODATA, e);
                    response.writeServerError(true);
                }
            }
        }

        public void visit(EntityResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (entitySet.getEntities().isEmpty()) {
                if (visitor.hasNavigation()) {
                    response.writeNoContent(true);
                } else {
                    response.writeNotFound(true);
                }
            } else {
                response.writeReadEntity(visitor.getContext().getEdmEntityType(), entitySet.getEntities().get(0));
            }
        }

        public void visit(EntitySetResponse response) throws ODataLibraryException, ODataApplicationException {
            sendResults(request, visitor, queryResponse, response);
        }
    });
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) Query(org.teiid.query.sql.lang.Query) SQLException(java.sql.SQLException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) BaseResponse(org.teiid.odata.api.BaseResponse) EntityCollection(org.apache.olingo.commons.api.data.EntityCollection) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Property(org.apache.olingo.commons.api.data.Property) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

Aggregations

EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)7 SQLException (java.sql.SQLException)5 Property (org.apache.olingo.commons.api.data.Property)4 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)3 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)3 TeiidException (org.teiid.core.TeiidException)3 Column (org.teiid.metadata.Column)3 Update (org.teiid.query.sql.lang.Update)3 Constant (org.teiid.query.sql.symbol.Constant)3 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)3 Reference (org.teiid.query.sql.symbol.Reference)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 UpdateResponse (org.teiid.odata.api.UpdateResponse)2 SubqueryHint (org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Entity (org.apache.olingo.commons.api.data.Entity)1 EntityCollection (org.apache.olingo.commons.api.data.EntityCollection)1