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