Search in sources :

Example 1 with LinkTemplate

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

the class AbstractPersistenceUnitResource method buildDescriptor.

private Descriptor buildDescriptor(PersistenceContext context, String persistenceUnit, ClassDescriptor descriptor, String baseUri) {
    Descriptor returnDescriptor = new Descriptor();
    String name = descriptor.getAlias();
    returnDescriptor.setName(name);
    String version = context.getVersion();
    if (version != null) {
        version = version + "/";
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("find", "get", baseUri + version + persistenceUnit + "/entity/" + descriptor.getAlias() + "/{primaryKey}"));
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("persist", "put", baseUri + version + persistenceUnit + "/entity/" + descriptor.getAlias()));
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("update", "post", baseUri + version + persistenceUnit + "/entity/" + descriptor.getAlias()));
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("delete", "delete", baseUri + version + persistenceUnit + "/entity/" + descriptor.getAlias() + "/{primaryKey}"));
    } else {
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("find", "get", baseUri + persistenceUnit + "/entity/" + descriptor.getAlias() + "/{primaryKey}"));
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("persist", "put", baseUri + persistenceUnit + "/entity/" + descriptor.getAlias()));
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("update", "post", baseUri + persistenceUnit + "/entity/" + descriptor.getAlias()));
        returnDescriptor.getLinkTemplates().add(new LinkTemplate("delete", "delete", baseUri + persistenceUnit + "/entity/" + descriptor.getAlias() + "/{primaryKey}"));
    }
    if (!descriptor.getMappings().isEmpty()) {
        Iterator<DatabaseMapping> mappingIterator = descriptor.getMappings().iterator();
        while (mappingIterator.hasNext()) {
            DatabaseMapping mapping = mappingIterator.next();
            addMapping(returnDescriptor, mapping);
        }
    }
    addQueries(returnDescriptor.getQueries(), context, descriptor.getJavaClassName());
    return returnDescriptor;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Descriptor(org.eclipse.persistence.internal.jpa.rs.metadata.model.Descriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) LinkTemplate(org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate)

Example 2 with LinkTemplate

use of org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate 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

LinkTemplate (org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkTemplate)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)1 Descriptor (org.eclipse.persistence.internal.jpa.rs.metadata.model.Descriptor)1 Query (org.eclipse.persistence.internal.jpa.rs.metadata.model.Query)1 ReportItem (org.eclipse.persistence.internal.queries.ReportItem)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)1 ReportQuery (org.eclipse.persistence.queries.ReportQuery)1 DatabaseRecord (org.eclipse.persistence.sessions.DatabaseRecord)1