use of org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder in project eclipselink by eclipse-ee4j.
the class ReferenceAdapterV2 method marshal.
/**
* Marshal just passes through.
*/
@Override
public T marshal(T o) throws Exception {
if (o == null) {
return null;
}
// Add canonical link
final String href = HrefHelper.buildEntityHref(context, o.getClass().getSimpleName(), IdHelper.stringifyId(o, o.getClass().getSimpleName(), context));
if (o._persistence_getLinks() == null) {
final ItemLinks itemLinks = (new ItemLinksBuilder()).addCanonical(href).build();
o._persistence_setLinks(itemLinks);
} else {
final ItemLinks itemLinks = o._persistence_getLinks();
final LinkV2 canonicalLink = itemLinks.getCanonicalLink();
if (canonicalLink == null) {
o._persistence_getLinks().addLink(new LinkV2(ReservedWords.JPARS_REL_CANONICAL, href));
}
}
return o;
}
use of org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder in project eclipselink by eclipse-ee4j.
the class SelfLinksResponseBuilder method generateLinksForRelationships.
private void generateLinksForRelationships(PersistenceContext context, PersistenceWeavedRest entity) {
final ClassDescriptor classDescriptor = context.getServerSession().getProject().getDescriptor(entity.getClass());
final String entityClassName = classDescriptor.getAlias();
final String entityId = IdHelper.stringifyId(entity, entityClassName, context);
for (final Field field : entity.getClass().getDeclaredFields()) {
if (PersistenceWeavedRest.class.isAssignableFrom(field.getType())) {
final PersistenceWeavedRest obj = (PersistenceWeavedRest) callGetterForProperty(entity, field.getName());
if (obj != null) {
final String fieldClassName = context.getJAXBDescriptorForClass(field.getType()).getAlias();
final String fieldId = IdHelper.stringifyId(obj, fieldClassName, context);
final ItemLinks links = (new ItemLinksBuilder()).addSelf(HrefHelper.buildEntityFieldHref(context, entityClassName, entityId, field.getName())).addCanonical(HrefHelper.buildEntityHref(context, fieldClassName, fieldId)).build();
obj._persistence_setLinks(links);
}
}
}
}
use of org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder in project eclipselink by eclipse-ee4j.
the class MetadataResource method buildEntitySchemaResponse.
private Response buildEntitySchemaResponse(String version, String persistenceUnit, String entityName, UriInfo uriInfo) {
JPARSLogger.entering(CLASS_NAME, "buildEntitySchemaResponse", new Object[] { "GET", version, persistenceUnit, uriInfo.getRequestUri().toASCIIString() });
final String result;
try {
final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
final ClassDescriptor descriptor = context.getServerSession().getDescriptorForAlias(entityName);
if (descriptor == null) {
JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_entity_type", new Object[] { entityName, persistenceUnit });
throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(entityName, persistenceUnit);
} else {
final ResourceSchema schema = new ResourceSchema();
schema.setTitle(descriptor.getAlias());
schema.setSchema(HrefHelper.buildEntityMetadataHref(context, descriptor.getAlias()) + "#");
schema.addAllOf(new Reference(HrefHelper.buildBaseRestSchemaRef("#/singularResource")));
// Properties
for (DatabaseMapping databaseMapping : descriptor.getMappings()) {
schema.addProperty(databaseMapping.getAttributeName(), buildProperty(context, databaseMapping));
}
// Links
final String instancesHref = HrefHelper.buildEntityDescribesHref(context, descriptor.getAlias());
schema.setLinks((new ItemLinksBuilder()).addDescribedBy(HrefHelper.buildEntityMetadataHref(context, descriptor.getAlias())).addFind(instancesHref + "/{primaryKey}").addCreate(instancesHref).addUpdate(instancesHref).addDelete(instancesHref + "/{primaryKey}").getList());
result = marshallMetadata(schema, MediaType.APPLICATION_JSON);
}
} catch (JAXBException e) {
throw JPARSException.exceptionOccurred(e);
}
return Response.ok(new StreamingOutputMarshaller(null, result, AbstractResource.APPLICATION_SCHEMA_JSON_TYPE)).build();
}
use of org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder in project eclipselink by eclipse-ee4j.
the class PagingResponseBuilder method populatePagedReadAllQueryItemLinks.
private Object populatePagedReadAllQueryItemLinks(PersistenceContext context, Object result) {
// populate links for the entity
ClassDescriptor descriptor = context.getJAXBDescriptorForClass(result.getClass());
if ((result instanceof PersistenceWeavedRest) && (descriptor != null)) {
final PersistenceWeavedRest entity = (PersistenceWeavedRest) result;
final String href = HrefHelper.buildEntityHref(context, descriptor.getAlias(), IdHelper.stringifyId(result, descriptor.getAlias(), context));
final ItemLinks itemLinks = (new ItemLinksBuilder()).addSelf(href).addCanonical(href).build();
entity._persistence_setLinks(itemLinks);
return entity;
}
return result;
}
use of org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder in project eclipselink by eclipse-ee4j.
the class PagingResponseBuilder method populatePagedCollectionLinks.
private PageableCollection<?> populatePagedCollectionLinks(Map<String, Object> queryParams, UriInfo uriInfo, PageableCollection<?> resultCollection) {
// populate links for entire response
final ItemLinksBuilder itemLinksBuilder = new ItemLinksBuilder();
final int limit = Integer.parseInt((String) queryParams.get(QueryParameters.JPARS_PAGING_LIMIT));
final int offset = Integer.parseInt((String) queryParams.get(QueryParameters.JPARS_PAGING_OFFSET));
final UriBuilder uriBuilder = UriBuilder.fromUri(uriInfo.getRequestUri());
if (resultCollection.getItems() != null) {
final int actualCount = resultCollection.getItems().size();
if (actualCount > limit) {
// Remove the last item from collection. It was artificially added to indicate there are more records or not.
resultCollection.getItems().remove(actualCount - 1);
resultCollection.setCount(actualCount - 1);
// next link
// The uri might have other query/matrix parameters, just replace the limit and offset
// for next and prev links and leave the rest untouched
uriBuilder.replaceQueryParam(QueryParameters.JPARS_PAGING_OFFSET, String.valueOf(limit + offset));
itemLinksBuilder.addNext(uriBuilder.build().toString());
resultCollection.setHasMore(true);
} else {
resultCollection.setHasMore(false);
resultCollection.setCount(actualCount);
}
} else {
resultCollection.setCount(0);
}
if (offset != 0) {
if (offset > limit) {
uriBuilder.replaceQueryParam(QueryParameters.JPARS_PAGING_OFFSET, String.valueOf(offset - limit));
} else {
uriBuilder.replaceQueryParam(QueryParameters.JPARS_PAGING_OFFSET, "0");
}
if (resultCollection.getItems() != null && !resultCollection.getItems().isEmpty()) {
itemLinksBuilder.addPrev(uriBuilder.build().toString());
}
}
itemLinksBuilder.addSelf(uriInfo.getRequestUri().toString());
resultCollection.setLinks(itemLinksBuilder.build().getLinks());
resultCollection.setOffset(offset);
resultCollection.setLimit(limit);
return resultCollection;
}
Aggregations