use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class AbstractCrudController method partialUpdateObject.
// --------------------------------------------------------------------------
// OLD PATCH
// --------------------------------------------------------------------------
@PatchMapping(value = "/{uid}")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void partialUpdateObject(@PathVariable("uid") String pvUid, @RequestParam Map<String, String> rpParameters, @CurrentUser User currentUser, HttpServletRequest request) throws Exception {
WebOptions options = new WebOptions(rpParameters);
List<T> entities = getEntity(pvUid, options);
if (entities.isEmpty()) {
throw new WebMessageException(notFound(getEntityClass(), pvUid));
}
T persistedObject = entities.get(0);
if (!aclService.canUpdate(currentUser, persistedObject)) {
throw new UpdateAccessDeniedException("You don't have the proper permissions to update this object.");
}
Patch patch = diff(request);
prePatchEntity(persistedObject);
patchService.apply(patch, persistedObject);
validateAndThrowErrors(() -> schemaValidator.validate(persistedObject));
manager.update(persistedObject);
postPatchEntity(persistedObject);
}
use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class LockExceptionController method getLockExceptions.
// -------------------------------------------------------------------------
// Resources
// -------------------------------------------------------------------------
@GetMapping(produces = ContextUtils.CONTENT_TYPE_JSON)
@ResponseBody
public RootNode getLockExceptions(@RequestParam(required = false) String key, @RequestParam Map<String, String> rpParameters, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
if (fields.isEmpty()) {
fields.addAll(Preset.ALL.getFields());
}
List<LockException> lockExceptions = new ArrayList<>();
if (key != null) {
LockException lockException = dataSetService.getLockException(MathUtils.parseInt(key));
if (lockException == null) {
throw new WebMessageException(notFound("Cannot find LockException with key: " + key));
}
lockExceptions.add(lockException);
} else if (!filters.isEmpty()) {
lockExceptions = dataSetService.filterLockExceptions(filters);
} else {
lockExceptions = dataSetService.getAllLockExceptions();
}
WebOptions options = new WebOptions(rpParameters);
WebMetadata metadata = new WebMetadata();
Pager pager = metadata.getPager();
if (options.hasPaging() && pager == null) {
pager = new Pager(options.getPage(), lockExceptions.size(), options.getPageSize());
lockExceptions = PagerUtils.pageCollection(lockExceptions, pager);
}
RootNode rootNode = NodeUtils.createMetadata();
if (pager != null) {
rootNode.addChild(NodeUtils.createPager(pager));
}
I18nFormat format = this.i18nManager.getI18nFormat();
for (LockException lockException : lockExceptions) {
lockException.getPeriod().setName(format.formatPeriod(lockException.getPeriod()));
}
rootNode.addChild(fieldFilterService.toCollectionNode(LockException.class, new FieldFilterParams(lockExceptions, fields)));
return rootNode;
}
use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class DimensionController method getItems.
@SuppressWarnings("unchecked")
@GetMapping("/{uid}/items")
@ResponseBody
public RootNode getItems(@PathVariable String uid, @RequestParam Map<String, String> parameters, OrderParams orderParams) throws QueryParserException {
List<String> fields = newArrayList(contextService.getParameterValues("fields"));
List<String> filters = newArrayList(contextService.getParameterValues("filter"));
List<Order> orders = orderParams.getOrders(getSchema(DimensionalItemObject.class));
WebOptions options = new WebOptions(parameters);
if (fields.isEmpty()) {
fields.addAll(Preset.defaultPreset().getFields());
}
// This is the base list used in this flow. It contains only items
// allowed to the current user.
List<DimensionalItemObject> readableItems = dimensionService.getCanReadDimensionItems(uid);
// This is needed for two reasons:
// 1) We are doing in-memory paging;
// 2) We have to count all items respecting the filtering.
Query queryForCount = queryService.getQueryFromUrl(DimensionalItemObject.class, filters, orders);
queryForCount.setObjects(readableItems);
List<DimensionalItemObject> forCountItems = (List<DimensionalItemObject>) queryService.query(queryForCount);
Query query = queryService.getQueryFromUrl(DimensionalItemObject.class, filters, orders, getPaginationData(options));
query.setObjects(readableItems);
query.setDefaultOrder();
List<DimensionalItemObject> paginatedItems = (List<DimensionalItemObject>) queryService.query(query);
RootNode rootNode = NodeUtils.createMetadata();
CollectionNode collectionNode = rootNode.addChild(fieldFilterService.toCollectionNode(DimensionalItemObject.class, new FieldFilterParams(paginatedItems, fields)));
collectionNode.setName("items");
for (Node node : collectionNode.getChildren()) {
((AbstractNode) node).setName("item");
}
// Adding pagination elements to the root node.
final int totalOfItems = isNotEmpty(forCountItems) ? forCountItems.size() : 0;
dimensionItemPageHandler.addPaginationToNodeIfEnabled(rootNode, options, uid, totalOfItems);
return rootNode;
}
use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class DataItemQueryController method getDimensionalItems.
/**
* Based on the informed arguments, this method will read the URL and based
* on the give params will retrieve the respective data items.
*
* @param currentUser the logged user
* @param urlParameters the request url params
* @param orderParams the request order params
* @return the complete root node
*/
private ResponseEntity<RootNode> getDimensionalItems(final User currentUser, final Map<String, String> urlParameters, final OrderParams orderParams) {
// Defining the input params.
final Set<String> fields = newHashSet(contextService.getParameterValues(FIELDS));
final Set<String> filters = newHashSet(contextService.getParameterValues(FILTER));
final WebOptions options = new WebOptions(urlParameters);
if (fields.isEmpty()) {
fields.addAll(Preset.ALL.getFields());
}
checkNamesAndOperators(filters);
checkOrderParams(orderParams.getOrders());
// Extracting the target entities to be queried.
final Set<Class<? extends BaseIdentifiableObject>> targetEntities = dataItemServiceFacade.extractTargetEntities(filters);
// Checking if the user can read all the target entities.
checkAuthorization(currentUser, targetEntities);
// Retrieving the data items based on the input params.
final List<DataItem> dimensionalItems = dataItemServiceFacade.retrieveDataItemEntities(targetEntities, filters, options, orderParams);
// Creating the response node.
final RootNode rootNode = createMetadata();
responseHandler.addResultsToNode(rootNode, dimensionalItems, fields);
responseHandler.addPaginationToNode(rootNode, targetEntities, currentUser, options, filters);
return new ResponseEntity<>(rootNode, OK);
}
use of org.hisp.dhis.webapi.webdomain.WebOptions in project dhis2-core by dhis2.
the class DataElementGroupController method getOperandsByQuery.
@GetMapping("/{uid}/operands/query/{q}")
public String getOperandsByQuery(@PathVariable("uid") String uid, @PathVariable("q") String q, @RequestParam Map<String, String> parameters, TranslateParams translateParams, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {
WebOptions options = new WebOptions(parameters);
setUserContext(translateParams);
List<DataElementGroup> dataElementGroups = getEntity(uid, NO_WEB_OPTIONS);
if (dataElementGroups.isEmpty()) {
throw new WebMessageException(notFound("DataElementGroup not found for uid: " + uid));
}
WebMetadata metadata = new WebMetadata();
List<DataElementOperand> dataElementOperands = Lists.newArrayList();
for (DataElementOperand dataElementOperand : dataElementCategoryService.getOperands(dataElementGroups.get(0).getMembers())) {
if (dataElementOperand.getDisplayName().toLowerCase().contains(q.toLowerCase())) {
dataElementOperands.add(dataElementOperand);
}
}
metadata.setDataElementOperands(dataElementOperands);
if (options.hasPaging()) {
Pager pager = new Pager(options.getPage(), dataElementOperands.size(), options.getPageSize());
metadata.setPager(pager);
dataElementOperands = PagerUtils.pageCollection(dataElementOperands, pager);
}
metadata.setDataElementOperands(dataElementOperands);
linkService.generateLinks(metadata, false);
model.addAttribute("model", metadata);
model.addAttribute("viewClass", options.getViewClass("basic"));
return StringUtils.uncapitalize(getEntitySimpleName());
}
Aggregations