use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class AbstractCrudController method getObjectInternal.
@SuppressWarnings("unchecked")
private RootNode getObjectInternal(String uid, Map<String, String> parameters, List<String> filters, List<String> fields, User user) throws Exception {
WebOptions options = new WebOptions(parameters);
List<T> entities = getEntity(uid, options);
if (entities.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), uid));
}
Query query = queryService.getQueryFromUrl(getEntityClass(), filters, new ArrayList<>(), options.getRootJunction());
query.setUser(user);
query.setObjects(entities);
entities = (List<T>) queryService.query(query);
handleLinksAndAccess(entities, fields, true, user);
for (T entity : entities) {
postProcessEntity(entity);
postProcessEntity(entity, options, parameters);
}
CollectionNode collectionNode = fieldFilterService.filter(getEntityClass(), entities, fields);
if (options.isTrue("useWrapper") || entities.size() > 1) {
RootNode rootNode = NodeUtils.createMetadata(collectionNode);
rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
return rootNode;
} else {
List<Node> children = collectionNode.getChildren();
RootNode rootNode;
if (!children.isEmpty()) {
rootNode = NodeUtils.createRootNode(children.get(0));
} else {
rootNode = NodeUtils.createRootNode(new ComplexNode(getSchema().getSingular()));
}
rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(parameters.get("inclusionStrategy")));
return rootNode;
}
}
use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class AbstractCrudController method updateObjectProperty.
@RequestMapping(value = "/{uid}/{property}", method = { RequestMethod.PUT, RequestMethod.PATCH })
public void updateObjectProperty(@PathVariable("uid") String pvUid, @PathVariable("property") String pvProperty, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
throw new WebMessageException(WebMessageUtils.notFound(getEntityClass(), pvUid));
}
if (!getSchema().haveProperty(pvProperty)) {
throw new WebMessageException(WebMessageUtils.notFound("Property " + pvProperty + " does not exist on " + getEntityName()));
}
Property property = getSchema().getProperty(pvProperty);
T persistedObject = entities.get(0);
if (!aclService.canUpdate(currentUserService.getCurrentUser(), persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
if (!property.isWritable()) {
throw new UpdateAccessDeniedException("This property is read-only.");
}
T object = deserialize(request);
if (object == null) {
throw new WebMessageException(WebMessageUtils.badRequest("Unknown payload format."));
}
Object value = property.getGetterMethod().invoke(object);
property.getSetterMethod().invoke(persistedObject, value);
manager.update(persistedObject);
postPatchEntity(persistedObject);
}
use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class GeoFeatureController method getGeoFeaturesJson.
// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_HTML })
public void getGeoFeaturesJson(@RequestParam String ou, @RequestParam(required = false) DisplayProperty displayProperty, @RequestParam(required = false) Date relativePeriodDate, @RequestParam(required = false) String userOrgUnit, @RequestParam(defaultValue = "false", value = "includeGroupSets") boolean rpIncludeGroupSets, @RequestParam Map<String, String> parameters, DhisApiVersion apiVersion, HttpServletRequest request, HttpServletResponse response) throws IOException {
WebOptions options = new WebOptions(parameters);
boolean includeGroupSets = "detailed".equals(options.getViewClass()) || rpIncludeGroupSets;
List<GeoFeature> features = getGeoFeatures(ou, displayProperty, relativePeriodDate, userOrgUnit, request, response, includeGroupSets, apiVersion);
if (features == null) {
return;
}
ContextUtils.setCacheControl(response, GEOFEATURE_CACHE);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
renderService.toJson(response.getOutputStream(), features);
}
Aggregations