Search in sources :

Example 6 with MapEntryExpression

use of org.eclipse.persistence.internal.expressions.MapEntryExpression in project eclipselink by eclipse-ee4j.

the class MapEntryNode method generateExpression.

/**
 * INTERNAL
 * Generate the a new EclipseLink TableEntryExpression for this node.
 */
@Override
public Expression generateExpression(GenerationContext context) {
    Expression owningExpression = getLeft().generateExpression(context);
    MapEntryExpression whereClause = new MapEntryExpression(owningExpression);
    whereClause.returnMapEntry();
    return whereClause;
}
Also used : MapEntryExpression(org.eclipse.persistence.internal.expressions.MapEntryExpression) Expression(org.eclipse.persistence.expressions.Expression) MapEntryExpression(org.eclipse.persistence.internal.expressions.MapEntryExpression)

Example 7 with MapEntryExpression

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

Example 8 with MapEntryExpression

use of org.eclipse.persistence.internal.expressions.MapEntryExpression in project eclipselink by eclipse-ee4j.

the class MetadataResource method buildQuerySchema.

private ResourceSchema buildQuerySchema(PersistenceContext context, DatabaseQuery query) {
    final ResourceSchema schema = new ResourceSchema();
    schema.setTitle(query.getName());
    schema.setSchema(HrefHelper.buildQueryMetadataHref(context, query.getName()) + "#");
    schema.addAllOf(new Reference(HrefHelper.buildBaseRestSchemaRef("#/collectionBaseResource")));
    // Link
    final String method = query.isReadQuery() ? "GET" : "POST";
    schema.setLinks((new ItemLinksBuilder()).addExecute(HrefHelper.buildQueryHref(context, query.getName(), getQueryParamString(query)), method).getList());
    // Definitions
    if (query.isReportQuery()) {
        // In case of report query we need to define a returned type
        final ResourceSchema returnType = new ResourceSchema();
        query.checkPrepare((AbstractSession) context.getServerSession(), new DatabaseRecord());
        for (ReportItem item : ((ReportQuery) query).getItems()) {
            final Property property;
            if (item.getMapping() != null) {
                if (item.getAttributeExpression() != null && item.getAttributeExpression().isMapEntryExpression()) {
                    if (((MapEntryExpression) item.getAttributeExpression()).shouldReturnMapEntry()) {
                        property = buildProperty(context, Map.Entry.class);
                    } else {
                        property = buildProperty(context, ((Class<?>) item.getMapping().getContainerPolicy().getKeyType()));
                    }
                } else {
                    property = buildProperty(context, item.getMapping().getAttributeClassification());
                }
            } else if (item.getResultType() != null) {
                property = buildProperty(context, item.getResultType());
            } else if (item.getDescriptor() != null) {
                property = buildProperty(context, item.getDescriptor().getJavaClass());
            } else if (item.getAttributeExpression() != null && item.getAttributeExpression().isConstantExpression()) {
                property = buildProperty(context, ((ConstantExpression) item.getAttributeExpression()).getValue().getClass());
            } else {
                // Use Object.class by default.
                property = buildProperty(context, Object.class);
            }
            returnType.addProperty(item.getName(), property);
        }
        schema.addDefinition("result", returnType);
        final Property items = new Property();
        items.setType("array");
        items.setItems(new Property("#/definitions/result"));
        schema.addProperty("items", items);
    } else {
        // Read all query. Each item is an entity. Make a JSON pointer.
        if (query.getReferenceClassName() != null) {
            final Property items = new Property();
            items.setType("array");
            items.setItems(new Property(HrefHelper.buildEntityMetadataHref(context, query.getReferenceClass().getSimpleName()) + "#"));
            schema.addProperty("items", items);
        }
    }
    return schema;
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) MapEntryExpression(org.eclipse.persistence.internal.expressions.MapEntryExpression) ResourceSchema(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema) DatabaseRecord(org.eclipse.persistence.sessions.DatabaseRecord) Reference(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference) ReportQuery(org.eclipse.persistence.queries.ReportQuery) ConstantExpression(org.eclipse.persistence.internal.expressions.ConstantExpression) ReportItem(org.eclipse.persistence.internal.queries.ReportItem) Property(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Property)

Aggregations

MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)8 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)3 Map (java.util.Map)2 Expression (org.eclipse.persistence.expressions.Expression)2 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)2 ReportQuery (org.eclipse.persistence.queries.ReportQuery)2 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)2 PrivilegedActionException (java.security.PrivilegedActionException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkTemplate (org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate)1 Query (org.eclipse.persistence.internal.jpa.rs.metadata.model.Query)1 Property (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Property)1 Reference (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference)1 ResourceSchema (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema)1 PrivilegedGetConstructorFor (org.eclipse.persistence.internal.security.PrivilegedGetConstructorFor)1 ItemLinksBuilder (org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1