/* * Convert result set into a collection of entities. */ private CollectionResource<Entity> buildCollectionResource(String entityType, SqlRowSet rowSet) { List<EntityResource<Entity>> results = new ArrayList<EntityResource<Entity>>(); // Extract the returned column names. May be a subset of the ones // requested. String[] columnNames = rowSet.getMetaData().getColumnNames(); // For all rows returned add an entity to the collection. while (rowSet.next()) { EntityProperties properties = new EntityProperties(); // For all columns in this row. for (String columnName : columnNames) { Object value = rowSet.getObject(columnName); // Only return non null values if (null != value) { // Add object to the property. getObject() returns an object // with the correct java type for each sql type. So we don't // need to cast. properties.setProperty(new EntityProperty(columnName, value)); } } // Create entity. // Note: Despite the variable name the first arg of both these is // the entity type name. Not it's key. Entity entity = new Entity(entityType, properties); results.add(new EntityResource<Entity>(entity.getName(), entity)); } // Note: This line looks a bit odd but the {} at the end is required. return new CollectionResource<Entity>(results) {}; }
/** * To handle generic case where user do not know the type, here we will be looking for a type and * then generate EntityResource<E> * * @param entity * @return EntityResource<E> */ public static <E> EntityResource<E> createEntityResource(E entity) { GenericEntity<E> ge = new GenericEntity<E>(entity) {}; Type t = com.temenos.interaction.core.command.CommandHelper.getEffectiveGenericType( ge.getType(), entity); if (ResourceTypeHelper.isType(ge.getRawType(), t, OEntity.class, OEntity.class)) { OEntity te = (OEntity) entity; String entityName = te != null && te.getEntityType() != null ? te.getEntityType().getName() : null; return com.temenos.interaction.core.command.CommandHelper.createEntityResource( entityName, entity, OEntity.class); } else if (ResourceTypeHelper.isType(ge.getRawType(), t, Entity.class, Entity.class)) { Entity te = (Entity) entity; String entityName = te != null ? te.getName() : null; return com.temenos.interaction.core.command.CommandHelper.createEntityResource( entityName, entity, Entity.class); } else { // Call the generic and lets see what happens return com.temenos.interaction.core.command.CommandHelper.createEntityResource(entity, null); } }
private String getAbsoluteId( String baseUri, String entitySetName, Entity entity, EntityMetadata entityMetadata) { String absId = ""; for (String key : entityMetadata.getIdFields()) { EntityProperty prop = entity.getProperties().getProperty(entityMetadata.getSimplePropertyName(key)); if (prop != null) { absId += absId.isEmpty() ? (!baseUri.endsWith("/") ? baseUri + "/" : baseUri) + entitySetName : ","; if (entityMetadata.isPropertyNumber(prop.getFullyQualifiedName())) { absId += "(" + entityMetadata.getPropertyValueAsString(prop) + ")"; } else { absId += "('" + entityMetadata.getPropertyValueAsString(prop) + "')"; } } } return absId; }
protected void writeEntry( StreamWriter writer, String entityName, Entity entity, Collection<Link> entityLinks, Map<Transition, RESTResource> embeddedResources, String baseUri, String absoluteId, String updated) { assert (entityName != null); // entity name could be different between entity resource and underlying entity // e.g., for Errors entity, entity resource would have the request entity name String entityMetadataName = entity != null ? entity.getName() : entityName; EntityMetadata entityMetadata = metadata.getEntityMetadata(entityMetadataName); String modelName = metadata.getModelName(); writer.writeId(absoluteId); OAtomEntity oae = getAtomInfo(entity); writer.writeTitle(oae.getAtomEntityTitle()); String summary = oae.getAtomEntitySummary(); if (!summary.isEmpty()) { writer.writeSummary(summary); } LocalDateTime updatedTime = oae.getAtomEntityUpdated(); if (updatedTime != null) { updated = InternalUtil.toString(updatedTime.toDateTime(DateTimeZone.UTC)); } writer.writeUpdated(updated); writer.writeAuthor(oae.getAtomEntityAuthor()); if (entityLinks != null) { for (Link link : entityLinks) { String type = (link.getTransition().getTarget() instanceof CollectionResourceState) ? atom_feed_content_type : atom_entry_content_type; String href = link.getRelativeHref(baseUri); String rel = link.getRel(); writer.startLink(href, rel); if ("self".equals(link.getRel())) { ResourceState target = link.getTransition().getTarget(); writer.writeAttribute("profile", target.getRel()); } if (!"self".equals(link.getRel()) && !"edit".equals(link.getRel())) { writer.writeAttribute("type", type); } writer.writeAttribute("title", link.getTitle()); if (embeddedResources != null && embeddedResources.get(link.getTransition()) != null) { String embeddedAbsoluteId = link.getHref(); writeLinkInline( writer, metadata, link, embeddedResources.get(link.getTransition()), link.getHref(), baseUri, embeddedAbsoluteId, updated); } String linkId = link.getLinkId(); if (linkId != null && linkId.length() > 0) { writer.writeAttribute("id", linkId); } writer.endLink(); } } writer.writeCategory(modelName + Metadata.MODEL_SUFFIX + "." + entityName, scheme); writer.flush(); writer.startContent(MediaType.APPLICATION_XML); writer.startElement(new QName(m, "properties", "m")); if (entity != null) { writeProperties(writer, entityMetadata, entity.getProperties(), modelName); } writer.endElement(); writer.endContent(); }