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;
}
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();
}
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);
}
}
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;
}
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);
}
Aggregations