use of org.apache.olingo.commons.api.data.Entity 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);
}
});
}
use of org.apache.olingo.commons.api.data.Entity in project teiid by teiid.
the class EntityCollectionResponse method addRow.
@Override
public void addRow(ResultSet rs) throws SQLException {
Entity entity = createEntity(rs, this.documentNode, this.baseURL, this);
processExpands(asRow(rs), entity, this.documentNode);
getEntities().add(entity);
}
use of org.apache.olingo.commons.api.data.Entity 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;
}
use of org.apache.olingo.commons.api.data.Entity 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);
}
}
}
use of org.apache.olingo.commons.api.data.Entity in project teiid by teiid.
the class CrossJoinResult method addRow.
@Override
public void addRow(ResultSet rs) throws SQLException {
ArrayList<ComplexReturnType> row = new ArrayList<ComplexReturnType>();
Entity entity = EntityCollectionResponse.createEntity(rs, this.documentNode, this.baseURL, null);
row.add(new ComplexReturnType(this.documentNode.getName(), this.documentNode.getEdmEntityType(), entity, this.documentNode.hasExpand()));
for (DocumentNode node : this.documentNode.getSibilings()) {
Entity sibiling = EntityCollectionResponse.createEntity(rs, node, this.baseURL, null);
row.add(new ComplexReturnType(node.getName(), this.documentNode.getEdmEntityType(), sibiling, ((CrossJoinNode) node).hasExpand()));
}
this.out.add(row);
}
Aggregations