Search in sources :

Example 1 with ResourceSchema

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

the class MetadataResource method buildEntitySchemaResponse.

private Response buildEntitySchemaResponse(String version, String persistenceUnit, String entityName, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "buildEntitySchemaResponse", new Object[] { "GET", version, persistenceUnit, uriInfo.getRequestUri().toASCIIString() });
    final String result;
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        final ClassDescriptor descriptor = context.getServerSession().getDescriptorForAlias(entityName);
        if (descriptor == null) {
            JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_entity_type", new Object[] { entityName, persistenceUnit });
            throw JPARSException.classOrClassDescriptorCouldNotBeFoundForEntity(entityName, persistenceUnit);
        } else {
            final ResourceSchema schema = new ResourceSchema();
            schema.setTitle(descriptor.getAlias());
            schema.setSchema(HrefHelper.buildEntityMetadataHref(context, descriptor.getAlias()) + "#");
            schema.addAllOf(new Reference(HrefHelper.buildBaseRestSchemaRef("#/singularResource")));
            // Properties
            for (DatabaseMapping databaseMapping : descriptor.getMappings()) {
                schema.addProperty(databaseMapping.getAttributeName(), buildProperty(context, databaseMapping));
            }
            // Links
            final String instancesHref = HrefHelper.buildEntityDescribesHref(context, descriptor.getAlias());
            schema.setLinks((new ItemLinksBuilder()).addDescribedBy(HrefHelper.buildEntityMetadataHref(context, descriptor.getAlias())).addFind(instancesHref + "/{primaryKey}").addCreate(instancesHref).addUpdate(instancesHref).addDelete(instancesHref + "/{primaryKey}").getList());
            result = marshallMetadata(schema, MediaType.APPLICATION_JSON);
        }
    } catch (JAXBException e) {
        throw JPARSException.exceptionOccurred(e);
    }
    return Response.ok(new StreamingOutputMarshaller(null, result, AbstractResource.APPLICATION_SCHEMA_JSON_TYPE)).build();
}
Also used : ItemLinksBuilder(org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder) ResourceSchema(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Reference(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference) JAXBException(jakarta.xml.bind.JAXBException) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)

Example 2 with ResourceSchema

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

Example 3 with ResourceSchema

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

the class MetadataResource method buildQuerySchemaResponse.

private Response buildQuerySchemaResponse(String version, String persistenceUnit, String queryName, UriInfo uriInfo) {
    JPARSLogger.entering(CLASS_NAME, "buildQuerySchemaResponse", new Object[] { "GET", version, persistenceUnit, uriInfo.getRequestUri().toASCIIString() });
    final String result;
    try {
        final PersistenceContext context = getPersistenceContext(persistenceUnit, null, uriInfo.getBaseUri(), version, null);
        // We need to make sure that query with given name exists
        final DatabaseQuery query = context.getServerSession().getQuery(queryName);
        if (query == null) {
            JPARSLogger.error(context.getSessionLog(), "jpars_could_not_find_query", new Object[] { queryName, persistenceUnit });
            throw JPARSException.responseCouldNotBeBuiltForNamedQueryRequest(queryName, context.getName());
        }
        final ResourceSchema querySchema = buildQuerySchema(context, query);
        result = marshallMetadata(querySchema, MediaType.APPLICATION_JSON);
    } catch (JAXBException e) {
        throw JPARSException.exceptionOccurred(e);
    }
    return Response.ok(new StreamingOutputMarshaller(null, result, AbstractResource.APPLICATION_SCHEMA_JSON_TYPE)).build();
}
Also used : ResourceSchema(org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema) DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) JAXBException(jakarta.xml.bind.JAXBException) PersistenceContext(org.eclipse.persistence.jpa.rs.PersistenceContext) StreamingOutputMarshaller(org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)

Aggregations

ResourceSchema (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.ResourceSchema)3 JAXBException (jakarta.xml.bind.JAXBException)2 Reference (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Reference)2 PersistenceContext (org.eclipse.persistence.jpa.rs.PersistenceContext)2 ItemLinksBuilder (org.eclipse.persistence.jpa.rs.features.ItemLinksBuilder)2 StreamingOutputMarshaller (org.eclipse.persistence.jpa.rs.util.StreamingOutputMarshaller)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 ConstantExpression (org.eclipse.persistence.internal.expressions.ConstantExpression)1 MapEntryExpression (org.eclipse.persistence.internal.expressions.MapEntryExpression)1 Property (org.eclipse.persistence.internal.jpa.rs.metadata.model.v2.Property)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