use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.
the class TeiidServiceHandler method createEntity.
@Override
public void createEntity(DataRequest request, Entity entity, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
EdmEntityType entityType = request.getEntitySet().getEntityType();
String txn;
try {
txn = getClient().startTransaction();
} catch (SQLException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
}
boolean success = false;
try {
List<ExpandNode> expands = new ArrayList<TeiidServiceHandler.ExpandNode>();
int insertDepth = insertDepth(entityType, entity);
// don't count the root
ODataSQLBuilder.checkExpandLevel(insertDepth - 1);
UpdateResponse updateResponse = performDeepInsert(request.getODataRequest().getRawBaseUri(), request.getUriInfo(), entityType, entity, expands);
if (updateResponse != null && updateResponse.getUpdateCount() == 1) {
ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), true, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
Query query = visitor.selectWithEntityKey(entityType, entity, updateResponse.getGeneratedKeys(), expands);
// $NON-NLS-1$ //$NON-NLS-2$
LogManager.logDetail(LogConstants.CTX_ODATA, null, "created entity = ", entityType.getName(), " with key=", query.getCriteria().toString());
EntityCollectionResponse result = new EntityCollectionResponse(request.getODataRequest().getRawBaseUri(), visitor.getContext());
getClient().executeSQL(query, visitor.getParameters(), false, null, null, null, 1, result);
if (!result.getEntities().isEmpty()) {
entity = result.getEntities().get(0);
String location = EntityResponse.buildLocation(request.getODataRequest().getRawBaseUri(), entity, request.getEntitySet().getName(), entityType);
entity.setId(new URI(location));
}
response.writeCreatedEntity(request.getEntitySet(), entity);
} else {
response.writeNotModified();
}
getClient().commit(txn);
success = true;
} catch (EdmPrimitiveTypeException | TeiidException | SQLException | URISyntaxException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
} finally {
if (!success) {
try {
getClient().rollback(txn);
} catch (SQLException e1) {
// ignore
}
}
}
}
use of org.teiid.odata.api.UpdateResponse 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();
}
}
use of org.teiid.odata.api.UpdateResponse in project teiid by teiid.
the class TeiidServiceHandler method manageReference.
private void manageReference(DataRequest request, URI referenceId, NoContentResponse response, boolean delete) throws ODataApplicationException {
UpdateResponse updateResponse = null;
try {
ReferenceUpdateSQLBuilder visitor = new ReferenceUpdateSQLBuilder(getClient().getMetadataStore(), request.getODataRequest().getRawBaseUri(), this.serviceMetadata, this.odata);
visitor.visit(request.getUriInfo());
Update update = visitor.updateReference(referenceId, this.prepared, delete);
updateResponse = getClient().executeUpdate(update, visitor.getParameters());
} catch (SQLException e) {
throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
}
if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
response.writeNoContent();
} else {
response.writeNotModified();
}
}
Aggregations