Search in sources :

Example 1 with ReportItem

use of org.eclipse.persistence.internal.queries.ReportItem in project eclipselink by eclipse-ee4j.

the class ConstructorSelectionImpl method translate.

public ConstructorReportItem translate() {
    ConstructorReportItem item = new ConstructorReportItem(this.getAlias());
    item.setResultType(this.getJavaType());
    item.setConstructor(constructor);
    for (Selection selection : this.getCompoundSelectionItems()) {
        if (selection.isCompoundSelection()) {
            item.addItem(((ConstructorSelectionImpl) selection).translate());
        } else {
            ReportItem reportItem = new ReportItem(item.getName() + item.getReportItems().size(), ((SelectionImpl) selection).getCurrentNode());
            // bug: 297385 - set type here because the selection already knows the type
            reportItem.setResultType(selection.getJavaType());
            item.addItem(reportItem);
        }
    }
    return item;
}
Also used : ConstructorReportItem(org.eclipse.persistence.queries.ConstructorReportItem) Selection(jakarta.persistence.criteria.Selection) ConstructorReportItem(org.eclipse.persistence.queries.ConstructorReportItem) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Example 2 with ReportItem

use of org.eclipse.persistence.internal.queries.ReportItem 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)

Example 3 with ReportItem

use of org.eclipse.persistence.internal.queries.ReportItem in project eclipselink by eclipse-ee4j.

the class AbstractSingleResultQueryResource method namedQuerySingleResultInternal.

/**
 * Named query single result.
 *
 * @param version the version
 * @param persistenceUnit the persistence unit
 * @param queryName the query name
 * @param headers the http headers
 * @param uriInfo the uriInfo
 * @return the response
 */
@SuppressWarnings("rawtypes")
protected Response namedQuerySingleResultInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "namedQuerySingleResultInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        final Query query = context.buildQuery(getMatrixParameters(uriInfo, persistenceUnit), queryName, getMatrixParameters(uriInfo, queryName), getQueryParameters(uriInfo));
        final DatabaseQuery dbQuery = ((EJBQueryImpl<?>) query).getDatabaseQuery();
        final FeatureResponseBuilder responseBuilder = context.getSupportedFeatureSet().getResponseBuilder(Feature.NO_PAGING);
        if (dbQuery instanceof ReportQuery) {
            final List<ReportItem> reportItems = ((ReportQuery) dbQuery).getItems();
            final Object queryResults = query.getSingleResult();
            final Object response = responseBuilder.buildSingleResultQueryResponse(context, getQueryParameters(uriInfo), queryResults, reportItems, uriInfo);
            if (response != null && response instanceof SingleResultQuery) {
                final SingleResultQuery singleResultQuery = (SingleResultQuery) response;
                final List<JAXBElement<?>> item = singleResultQuery.getFields();
                if ((item != null) && (item.size() == 1)) {
                    // Fix for Bug 393320 - JPA-RS: Respect the Accept Header for a singleResultQuery
                    // If there is only one item in the select clause and if value of that item is binary, we will create a response with
                    // that binary data without converting its to Base64.
                    JAXBElement element = item.get(0);
                    Object elementValue = element.getValue();
                    if (elementValue instanceof byte[]) {
                        List<MediaType> acceptableMediaTypes = headers.getAcceptableMediaTypes();
                        if (acceptableMediaTypes.contains(MediaType.APPLICATION_OCTET_STREAM_TYPE)) {
                            return Response.ok(new StreamingOutputMarshaller(context, elementValue, headers.getAcceptableMediaTypes())).build();
                        }
                    }
                }
                return Response.ok(new StreamingOutputMarshaller(context, response, headers.getAcceptableMediaTypes())).build();
            } else {
                // something went wrong with the descriptors, return error
                throw JPARSException.responseCouldNotBeBuiltForNamedQueryRequest(queryName, context.getName());
            }
        }
        final Object queryResult = query.getSingleResult();
        return Response.ok(new StreamingOutputMarshaller(context, responseBuilder.buildSingleEntityResponse(context, getQueryParameters(uriInfo), queryResult, uriInfo), headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) Query(jakarta.persistence.Query) SingleResultQuery(org.eclipse.persistence.jpa.rs.util.list.SingleResultQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ReportQuery(org.eclipse.persistence.queries.ReportQuery) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) SingleResultQuery(org.eclipse.persistence.jpa.rs.util.list.SingleResultQuery) JAXBElement(jakarta.xml.bind.JAXBElement) ReportItem(org.eclipse.persistence.internal.queries.ReportItem) EJBQueryImpl(org.eclipse.persistence.internal.jpa.EJBQueryImpl) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException) FeatureResponseBuilder(org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilder) MediaType(jakarta.ws.rs.core.MediaType) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)

Example 4 with ReportItem

use of org.eclipse.persistence.internal.queries.ReportItem in project eclipselink by eclipse-ee4j.

the class FeatureResponseBuilderImpl method createShellJAXBElementList.

/**
 * Creates the shell jaxb element list.
 *
 * @param reportItems the report items
 * @param record the record
 * @return the list. Returns an empty list if reportItems is null or empty.
 */
public List<JAXBElement<?>> createShellJAXBElementList(List<ReportItem> reportItems, Object record) {
    if (reportItems == null || reportItems.size() == 0) {
        return Collections.emptyList();
    }
    List<JAXBElement<?>> jaxbElements = new ArrayList<>(reportItems.size());
    for (int index = 0; index < reportItems.size(); index++) {
        ReportItem reportItem = reportItems.get(index);
        Object reportItemValue = record;
        if (record instanceof Object[]) {
            reportItemValue = ((Object[]) record)[index];
        }
        if (reportItemValue != null) {
            JAXBElement element = new JAXBElement(new QName(reportItem.getName()), reportItemValue.getClass(), reportItemValue);
            jaxbElements.add(reportItem.getResultIndex(), element);
        }
    }
    return jaxbElements;
}
Also used : QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) JAXBElement(jakarta.xml.bind.JAXBElement) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Example 5 with ReportItem

use of org.eclipse.persistence.internal.queries.ReportItem in project eclipselink by eclipse-ee4j.

the class ExistsNode method generateExpression.

/**
 * INTERNAL
 * Generate the EclipseLink expression for this node
 */
@Override
public Expression generateExpression(GenerationContext context) {
    SubqueryNode subqueryNode = (SubqueryNode) getLeft();
    ReportQuery reportQuery = subqueryNode.getReportQuery(context);
    // Replace the SELECT clause of the exists subquery by SELECT 1 to
    // avoid problems with databases not supporting multiple columns in the
    // subquery SELECT clause in SQL.
    // The original select clause expressions might include relationship
    // navigations which should result in FK joins in the generated SQL,
    // e.g. ... EXISTS (SELECT o.customer FROM Order o ...). Add the
    // select clause expressions as non fetch join attributes to the
    // ReportQuery representing the subquery. This make sure the FK joins
    // get generated.
    List<ReportItem> items = reportQuery.getItems();
    for (Iterator<ReportItem> i = items.iterator(); i.hasNext(); ) {
        ReportItem item = i.next();
        Expression expr = item.getAttributeExpression();
        reportQuery.addNonFetchJoinedAttribute(expr);
    }
    reportQuery.clearItems();
    Expression one = new ConstantExpression(1, new ExpressionBuilder());
    reportQuery.addItem("one", one);
    reportQuery.dontUseDistinct();
    Expression expr = context.getBaseExpression();
    return notIndicated() ? expr.notExists(reportQuery) : expr.exists(reportQuery);
}
Also used : ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Aggregations

ReportItem (org.eclipse.persistence.internal.queries.ReportItem)20 ReportQuery (org.eclipse.persistence.queries.ReportQuery)11 ArrayList (java.util.ArrayList)6 Expression (org.eclipse.persistence.expressions.Expression)6 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)4 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)4 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)4 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 JAXBElement (jakarta.xml.bind.JAXBElement)2 PrivilegedActionException (java.security.PrivilegedActionException)2 Map (java.util.Map)2 ExpressionBuilder (org.eclipse.persistence.expressions.ExpressionBuilder)2 FunctionExpression (org.eclipse.persistence.internal.expressions.FunctionExpression)2 ParameterExpression (org.eclipse.persistence.internal.expressions.ParameterExpression)2 StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)2 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)2 Query (jakarta.persistence.Query)1 Selection (jakarta.persistence.criteria.Selection)1 MediaType (jakarta.ws.rs.core.MediaType)1 StringWriter (java.io.StringWriter)1