Search in sources :

Example 1 with FieldsFilteringValidator

use of org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator in project eclipselink by eclipse-ee4j.

the class AbstractEntityResource method findInternal.

protected Response findInternal(String version, String persistenceUnit, String type, String id, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "findInternal", new Object[] { "GET", version, persistenceUnit, type, id, uriInfo.getRequestUri().toASCIIString() });
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
        final Map<String, String> discriminators = getMatrixParameters(uriInfo, persistenceUnit);
        final Object entityId = IdHelper.buildId(context, type, id);
        final Object entity = context.find(discriminators, type, entityId, getQueryParameters(uriInfo));
        if (entity == null) {
            JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_entity_for_key", new Object[] { type, id, persistenceUnit });
            throw JPARSException.entityNotFound(type, id, persistenceUnit);
        }
        // Fields filtering
        if (context.getSupportedFeatureSet().isSupported(Feature.FIELDS_FILTERING)) {
            final FieldsFilteringValidator fieldsFilteringValidator = new FieldsFilteringValidator(uriInfo);
            if (fieldsFilteringValidator.isFeatureApplicable()) {
                final StreamingOutputMarshaller marshaller = new StreamingOutputMarshaller(context, singleEntityResponse(context, entity, uriInfo), headers.getAcceptableMediaTypes(), fieldsFilteringValidator.getFilter());
                return Response.ok(marshaller).build();
            }
        }
        return Response.ok(new StreamingOutputMarshaller(context, singleEntityResponse(context, entity, uriInfo), headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) FieldsFilteringValidator(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 2 with FieldsFilteringValidator

use of org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator in project eclipselink by eclipse-ee4j.

the class AbstractEntityResource method findAttributeInternal.

protected Response findAttributeInternal(String version, String persistenceUnit, String type, String id, String attribute, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "findAttributeInternal", new Object[] { "GET", version, persistenceUnit, type, id, attribute, uriInfo.getRequestUri().toASCIIString() });
    EntityManager em = null;
    try {
        PersistenceContext context = getPersistenceContext(persistenceUnit, type, uriInfo.getBaseUri(), version, null);
        Object entityId = IdHelper.buildId(context, type, id);
        em = context.getEmf().createEntityManager(getMatrixParameters(uriInfo, persistenceUnit));
        Object entity = em.find(context.getClass(type), entityId, getQueryParameters(uriInfo));
        DatabaseSession serverSession = context.getServerSession();
        ClassDescriptor descriptor = serverSession.getClassDescriptor(context.getClass(type));
        if (descriptor == null) {
            throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(type, persistenceUnit);
        }
        DatabaseMapping attributeMapping = descriptor.getMappingForAttributeName(attribute);
        if ((attributeMapping == null) || (entity == null)) {
            throw JPARSException.databaseMappingCouldNotBeFoundForEntityAttribute(attribute, type, id, persistenceUnit);
        }
        if (!attributeMapping.isCollectionMapping()) {
            Object result = attributeMapping.getRealAttributeValueFromAttribute(attributeMapping.getAttributeValueFromObject(entity), entity, (AbstractSession) serverSession);
            if (result == null) {
                JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_entity_for_attribute", new Object[] { attribute, type, id, persistenceUnit });
                throw JPARSException.attributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
            }
            final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
            return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, getQueryParameters(uriInfo), headers, uriInfo, responseBuilder, null);
        }
        ReadQuery query = (ReadQuery) ((((ForeignReferenceMapping) attributeMapping).getSelectionQuery()).clone());
        if (query == null) {
            throw JPARSException.selectionQueryForAttributeCouldNotBeFoundForEntity(attribute, type, id, persistenceUnit);
        }
        final FeatureSet featureSet = context.getSupportedFeatureSet();
        final AbstractSession clientSession = context.getClientSession(em);
        // Fields filtering
        FieldsFilter fieldsFilter = null;
        if (context.getSupportedFeatureSet().isSupported(Feature.FIELDS_FILTERING)) {
            final FieldsFilteringValidator fieldsFilteringValidator = new FieldsFilteringValidator(uriInfo);
            if (fieldsFilteringValidator.isFeatureApplicable()) {
                fieldsFilter = fieldsFilteringValidator.getFilter();
            }
        }
        // Pagination
        if (featureSet.isSupported(Feature.PAGING)) {
            final PageableFieldValidator validator = new PageableFieldValidator(entity.getClass(), attribute, uriInfo);
            if (validator.isFeatureApplicable()) {
                // Adding extra one to detect are there more rows or not. It will be removed later
                // on in response processor.
                query.setMaxRows(validator.getLimit() + validator.getOffset() + 1);
                query.setFirstResult(validator.getOffset());
                // We need to add limit and offset to query parameters because request builder reads it from there
                final Map<String, Object> queryParams = getQueryParameters(uriInfo);
                queryParams.put(QueryParameters.JPARS_PAGING_LIMIT, String.valueOf(validator.getLimit()));
                queryParams.put(QueryParameters.JPARS_PAGING_OFFSET, String.valueOf(validator.getOffset()));
                // check orderBy, and generate a warning if there is none
                checkOrderBy(context, query);
                final Object result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
                final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.PAGING);
                return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, queryParams, headers, uriInfo, responseBuilder, fieldsFilter);
            }
        }
        final Object result = clientSession.executeQuery(query, descriptor.getObjectBuilder().buildRow(entity, clientSession, WriteType.INSERT));
        final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
        return findAttributeResponse(context, attribute, type, id, persistenceUnit, result, getQueryParameters(uriInfo), headers, uriInfo, responseBuilder, fieldsFilter);
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    } finally {
        if (em != null) {
            if (em.isOpen()) {
                em.close();
            }
        }
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) FieldsFilteringValidator(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) PageableFieldValidator(org.eclipse.persistence.jpa.rs.features.paging.PageableFieldValidator) FeatureResponseBuilder(org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilder) EntityManager(jakarta.persistence.EntityManager) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) FieldsFilter(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilter) FeatureSet(org.eclipse.persistence.jpa.rs.features.FeatureSet) ReadQuery(org.eclipse.persistence.queries.ReadQuery) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 3 with FieldsFilteringValidator

use of org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator in project eclipselink by eclipse-ee4j.

the class AbstractQueryResource method namedQueryResponse.

@SuppressWarnings("unchecked")
private Response namedQueryResponse(PersistenceContext context, String queryName, DatabaseQuery dbQuery, Query query, HttpHeaders headers, UriInfo uriInfo, FeatureResponseBuilder responseBuilder) {
    // We need to add limit and offset to query parameters because request builder reads it from there
    final Map<String, Object> queryParams = getQueryParameters(uriInfo);
    if (query.getMaxResults() != Integer.MAX_VALUE) {
        queryParams.put(QueryParameters.JPARS_PAGING_LIMIT, String.valueOf(query.getMaxResults() - 1));
        queryParams.put(QueryParameters.JPARS_PAGING_OFFSET, String.valueOf(query.getFirstResult()));
    }
    // Fields filtering
    FieldsFilter fieldsFilter = null;
    if (context.getSupportedFeatureSet().isSupported(Feature.FIELDS_FILTERING)) {
        final FieldsFilteringValidator fieldsFilteringValidator = new FieldsFilteringValidator(uriInfo);
        if (fieldsFilteringValidator.isFeatureApplicable()) {
            fieldsFilter = fieldsFilteringValidator.getFilter();
        }
    }
    if (dbQuery instanceof ReportQuery) {
        // simple types selected : select u.name, u.age from employee
        List<ReportItem> reportItems = ((ReportQuery) dbQuery).getItems();
        List<Object[]> queryResults = query.getResultList();
        if ((queryResults != null) && (!queryResults.isEmpty())) {
            Object list = responseBuilder.buildReportQueryResponse(context, queryParams, queryResults, reportItems, uriInfo);
            if (list != null) {
                return Response.ok(new StreamingOutputMarshaller(context, list, headers.getAcceptableMediaTypes(), fieldsFilter)).build();
            } else {
                // something is wrong with the descriptors
                throw JPARSException.responseCouldNotBeBuiltForNamedQueryRequest(queryName, context.getName());
            }
        }
        return Response.ok(new StreamingOutputMarshaller(context, queryResults, headers.getAcceptableMediaTypes(), fieldsFilter)).build();
    }
    List<Object> results = query.getResultList();
    if (results != null) {
        Object list = responseBuilder.buildReadAllQueryResponse(context, queryParams, results, uriInfo);
        return Response.ok(new StreamingOutputMarshaller(context, list, headers.getAcceptableMediaTypes(), fieldsFilter)).build();
    }
    return Response.ok(new StreamingOutputMarshaller(context, null, headers.getAcceptableMediaTypes(), fieldsFilter)).build();
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) FieldsFilter(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilter) FieldsFilteringValidator(org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Aggregations

FieldsFilteringValidator (org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilteringValidator)3 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)2 JPARSException (org.eclipse.persistence.jpa.rs.exceptions.JPARSException)2 FieldsFilter (org.eclipse.persistence.jpa.rs.features.fieldsfiltering.FieldsFilter)2 StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)2 EntityManager (jakarta.persistence.EntityManager)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 FeatureResponseBuilder (org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilder)1 FeatureSet (org.eclipse.persistence.jpa.rs.features.FeatureSet)1 PageableFieldValidator (org.eclipse.persistence.jpa.rs.features.paging.PageableFieldValidator)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)1 ReadQuery (org.eclipse.persistence.queries.ReadQuery)1 ReportQuery (org.eclipse.persistence.queries.ReportQuery)1 DatabaseSession (org.eclipse.persistence.sessions.DatabaseSession)1