Search in sources :

Example 1 with ItemLinksBuilder

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;
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) ItemLinks(org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks) LinkV2(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2)

Example 2 with ItemLinksBuilder

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);
            }
        }
    }
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) Field(java.lang.reflect.Field) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) ItemLinks(org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks)

Example 3 with ItemLinksBuilder

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();
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) ResourceSchema(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Reference(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference) JAXBException(jakarta.xml.bind.JAXBException) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)

Example 4 with ItemLinksBuilder

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;
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) ItemLinks(org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks)

Example 5 with ItemLinksBuilder

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;
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) UriBuilder(jakarta.ws.rs.core.UriBuilder)

Aggregations

ItemLinksBuilder (org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder)13 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)7 LinkV2 (org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2)5 PersistenceWeavedRest (org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest)5 ItemLinks (org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks)3 Resource (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Resource)3 JAXBException (jakarta.xml.bind.JAXBException)2 Reference (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference)2 ResourceSchema (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema)2 AbstractResource (org.eclipse.persistence.jpa.rs.resources.common.AbstractResource)2 StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)2 UriBuilder (jakarta.ws.rs.core.UriBuilder)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)1 ContextsCatalog (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ContextsCatalog)1 MetadataCatalog (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.MetadataCatalog)1 Property (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Property)1 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)1