Search in sources :

Example 1 with UriParameter

use of org.apache.olingo.server.api.uri.UriParameter in project teiid by teiid.

the class ReferenceUpdateSQLBuilder method visit.

@Override
public void visit(UriResourceEntitySet info) {
    Table table = DocumentNode.findTable(info.getEntitySet(), this.metadata);
    EdmEntityType type = info.getEntitySet().getEntityType();
    List<UriParameter> keys = info.getKeyPredicates();
    this.updateTable = new ScopedTable(table, type, keys);
}
Also used : Table(org.teiid.metadata.Table) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Example 2 with UriParameter

use of org.apache.olingo.server.api.uri.UriParameter 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 3 with UriParameter

use of org.apache.olingo.server.api.uri.UriParameter in project teiid by teiid.

the class DocumentNode method buildEntityKeyCriteria.

static Criteria buildEntityKeyCriteria(DocumentNode resource, UriInfo uriInfo, MetadataStore store, OData odata, UniqueNameGenerator nameGenerator, URLParseService parseService) throws TeiidException {
    List<Column> pk = getPKColumns(resource.getTable());
    if (resource.getKeyPredicates().size() == 1) {
        if (pk.size() != 1) {
            throw new TeiidException(ODataPlugin.Event.TEIID16015, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16015, resource.getTable().getFullName()));
        }
        Column column = pk.get(0);
        ODataExpressionToSQLVisitor visitor = new ODataExpressionToSQLVisitor(resource, false, uriInfo, store, odata, nameGenerator, null, parseService);
        UriParameter key = resource.getKeyPredicates().get(0);
        org.apache.olingo.server.api.uri.queryoption.expression.Expression expr = getKeyPredicateExpression(key, odata, column);
        return new CompareCriteria(new ElementSymbol(column.getName(), resource.getGroupSymbol()), CompareCriteria.EQ, visitor.getExpression(expr));
    }
    // complex (multi-keyed)
    List<Criteria> critList = new ArrayList<Criteria>();
    if (pk.size() != resource.getKeyPredicates().size()) {
        throw new TeiidException(ODataPlugin.Event.TEIID16015, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16015, resource.getTable().getFullName()));
    }
    for (UriParameter key : resource.getKeyPredicates()) {
        Column column = findColumn(resource.getTable(), key.getName());
        ODataExpressionToSQLVisitor visitor = new ODataExpressionToSQLVisitor(resource, false, uriInfo, store, odata, nameGenerator, null, parseService);
        org.apache.olingo.server.api.uri.queryoption.expression.Expression expr = getKeyPredicateExpression(key, odata, column);
        critList.add(new CompareCriteria(new ElementSymbol(column.getName(), resource.getGroupSymbol()), CompareCriteria.EQ, visitor.getExpression(expr)));
    }
    return new CompoundCriteria(CompoundCriteria.AND, critList);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) ArrayList(java.util.ArrayList) TeiidException(org.teiid.core.TeiidException) ProjectedColumn(org.teiid.olingo.ProjectedColumn) Column(org.teiid.metadata.Column) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Example 4 with UriParameter

use of org.apache.olingo.server.api.uri.UriParameter in project teiid by teiid.

the class TeiidServiceHandler method updateEntity.

@Override
public void updateEntity(DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    if (merge) {
        try {
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            Update update = visitor.update(entityType, entity, this.prepared);
            updateResponse = getClient().executeUpdate(update, visitor.getParameters());
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        }
    } else {
        // delete, then insert
        String txn = startTransaction();
        boolean success = false;
        try {
            // build insert first as it could fail to validate
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            List<UriParameter> keys = request.getKeyPredicates();
            Insert command = visitor.insert(entityType, entity, keys, this.prepared);
            // run delete
            ODataSQLBuilder deleteVisitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            deleteVisitor.visit(request.getUriInfo());
            Delete delete = deleteVisitor.delete();
            updateResponse = getClient().executeUpdate(delete, deleteVisitor.getParameters());
            // run insert
            updateResponse = getClient().executeUpdate(command, visitor.getParameters());
            commit(txn);
            success = true;
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } finally {
            if (!success) {
                rollback(txn);
            }
        }
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writeUpdatedEntity();
    } else {
        response.writeNotModified();
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) SQLException(java.sql.SQLException) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) Update(org.teiid.query.sql.lang.Update) Insert(org.teiid.query.sql.lang.Insert) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException) UpdateResponse(org.teiid.odata.api.UpdateResponse) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Aggregations

UriParameter (org.apache.olingo.server.api.uri.UriParameter)4 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)3 TeiidException (org.teiid.core.TeiidException)3 SQLException (java.sql.SQLException)2 Column (org.teiid.metadata.Column)2 Update (org.teiid.query.sql.lang.Update)2 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)2 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)1 ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)1 UriInfo (org.apache.olingo.server.api.uri.UriInfo)1 UriResourceEntitySet (org.apache.olingo.server.api.uri.UriResourceEntitySet)1 UriParserException (org.apache.olingo.server.core.uri.parser.UriParserException)1 UriValidationException (org.apache.olingo.server.core.uri.validator.UriValidationException)1 Table (org.teiid.metadata.Table)1 UpdateResponse (org.teiid.odata.api.UpdateResponse)1 ProjectedColumn (org.teiid.olingo.ProjectedColumn)1 Criteria (org.teiid.query.sql.lang.Criteria)1 Delete (org.teiid.query.sql.lang.Delete)1