Search in sources :

Example 1 with Query

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.Query in project eclipselink by eclipse-ee4j.

the class AbstractPersistenceUnitResource method getQueryMetadataInternal.

protected Response getQueryMetadataInternal(String version, String persistenceUnit, String queryName, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "getQueryMetadataInternal", new Object[] { "GET", version, persistenceUnit, queryName, uriInfo.getRequestUri().toASCIIString() });
    try {
        PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        List<Query> returnQueries = new ArrayList<>();
        Map<String, List<DatabaseQuery>> queries = context.getServerSession().getQueries();
        if (queries.get(queryName) != null) {
            for (DatabaseQuery query : queries.get(queryName)) {
                returnQueries.add(getQuery(query, context));
            }
        }
        String mediaType = StreamingOutputMarshaller.mediaType(headers.getAcceptableMediaTypes()).toString();
        QueryList queryList = new QueryList();
        queryList.setList(returnQueries);
        String result = null;
        if (mediaType.equals(MediaType.APPLICATION_JSON)) {
            result = marshallMetadata(queryList.getList(), mediaType);
        } else {
            result = marshallMetadata(queryList, mediaType);
        }
        return Response.ok(new StreamingOutputMarshaller(null, result, headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(org.eclipse.persistence.internal.jpa.rs.metadata.model.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ArrayList(java.util.ArrayList) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) ArrayList(java.util.ArrayList) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) List(java.util.List) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 2 with Query

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.Query in project eclipselink by eclipse-ee4j.

the class AbstractPersistenceUnitResource method getQueriesMetadataInternal.

protected Response getQueriesMetadataInternal(String version, String persistenceUnit, HttpHeaders headers, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "getQueriesMetadataInternal", new Object[] { "GET", version, persistenceUnit, uriInfo.getRequestUri().toASCIIString() });
    try {
        PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        List<Query> queries = new ArrayList<>();
        addQueries(queries, context, null);
        String mediaType = StreamingOutputMarshaller.mediaType(headers.getAcceptableMediaTypes()).toString();
        QueryList queryList = new QueryList();
        queryList.setList(queries);
        String result = null;
        if (mediaType.equals(MediaType.APPLICATION_JSON)) {
            result = marshallMetadata(queryList.getList(), mediaType);
        } else {
            result = marshallMetadata(queryList, mediaType);
        }
        return Response.ok(new StreamingOutputMarshaller(null, result, headers.getAcceptableMediaTypes())).build();
    } catch (Exception ex) {
        throw JPARSException.exceptionOccurred(ex);
    }
}
Also used : ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(org.eclipse.persistence.internal.jpa.rs.metadata.model.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) ArrayList(java.util.ArrayList) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller) QueryList(org.eclipse.persistence.jpa.rs.util.list.QueryList) JPARSException(org.eclipse.persistence.jpa.rs.exceptions.JPARSException)

Example 3 with Query

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.Query in project eclipselink by eclipse-ee4j.

the class AbstractPersistenceUnitResource method getQuery.

private Query getQuery(DatabaseQuery query, PersistenceContext context) {
    String method = query.isReadQuery() ? "get" : "post";
    String jpql = query.getJPQLString() == null ? "" : query.getJPQLString();
    StringBuilder parameterString = new StringBuilder();
    Iterator<String> argumentsIterator = query.getArguments().iterator();
    while (argumentsIterator.hasNext()) {
        String argument = argumentsIterator.next();
        parameterString.append(";");
        parameterString.append(argument).append("={").append(argument).append("}");
    }
    String version = context.getVersion();
    Query returnQuery = null;
    if (version != null) {
        returnQuery = new Query(query.getName(), jpql, new LinkTemplate("execute", method, context.getBaseURI() + version + "/" + context.getName() + "/query/" + query.getName() + parameterString));
    } else {
        returnQuery = new Query(query.getName(), jpql, new LinkTemplate("execute", method, context.getBaseURI() + context.getName() + "/query/" + query.getName() + parameterString));
    }
    if (query.isReportQuery()) {
        query.checkPrepare((AbstractSession) context.getServerSession(), new DatabaseRecord());
        for (ReportItem item : ((ReportQuery) query).getItems()) {
            if (item.getMapping() != null) {
                if (item.getAttributeExpression() != null && item.getAttributeExpression().isMapEntryExpression()) {
                    if (((MapEntryExpression) item.getAttributeExpression()).shouldReturnMapEntry()) {
                        returnQuery.getReturnTypes().add(Map.Entry.class.getSimpleName());
                    } else {
                        returnQuery.getReturnTypes().add(((Class<?>) item.getMapping().getContainerPolicy().getKeyType()).getSimpleName());
                    }
                } else {
                    returnQuery.getReturnTypes().add(item.getMapping().getAttributeClassification().getSimpleName());
                }
            } else if (item.getResultType() != null) {
                returnQuery.getReturnTypes().add(item.getResultType().getSimpleName());
            } else if (item.getDescriptor() != null) {
                returnQuery.getReturnTypes().add(item.getDescriptor().getJavaClass().getSimpleName());
            } else if (item.getAttributeExpression() != null && item.getAttributeExpression().isConstantExpression()) {
                returnQuery.getReturnTypes().add(((ConstantExpression) item.getAttributeExpression()).getValue().getClass().getSimpleName());
            } else {
                // Use Object.class by default.
                returnQuery.getReturnTypes().add(ClassConstants.OBJECT.getSimpleName());
            }
        }
    } else {
        returnQuery.getReturnTypes().add(query.getReferenceClassName() == null ? "" : query.getReferenceClass().getSimpleName());
    }
    return returnQuery;
}
Also used : MapEntryExpression(org.eclipse.persistence.internal.expressions.MapEntryExpression) ReportQuery(org.eclipse.persistence.queries.ReportQuery) Query(org.eclipse.persistence.internal.jpa.rs.metadata.model.Query) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) LinkTemplate(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate) ReportItem(org.eclipse.persistence.internal.queries.ReportItem)

Aggregations

Query (org.eclipse.persistence.internal.jpa.rs.metadata.model.Query)3 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)3 ReportQuery (org.eclipse.persistence.queries.ReportQuery)3 ArrayList (java.util.ArrayList)2 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)2 JPARSException (org.eclipse.persistence.jpa.rs.exceptions.JPARSException)2 StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)2 QueryList (org.eclipse.persistence.jpa.rs.util.list.QueryList)2 List (java.util.List)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)1 LinkTemplate (org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate)1 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)1 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)1