use of org.hisp.dhis.webapi.webdomain.WebMetadata in project dhis2-core by dhis2.
the class AbstractFullReadOnlyController method getObjectList.
// --------------------------------------------------------------------------
// GET Full
// --------------------------------------------------------------------------
@GetMapping
@ResponseBody
public RootNode getObjectList(@RequestParam Map<String, String> rpParameters, OrderParams orderParams, HttpServletResponse response, @CurrentUser User currentUser) throws QueryParserException {
List<Order> orders = orderParams.getOrders(getSchema());
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
forceFiltering(filters);
if (fields.isEmpty()) {
fields.addAll(Preset.defaultPreset().getFields());
}
WebOptions options = new WebOptions(rpParameters);
WebMetadata metadata = new WebMetadata();
if (!aclService.canRead(currentUser, getEntityClass())) {
throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
}
List<T> entities = getEntityList(metadata, options, filters, orders);
Pager pager = metadata.getPager();
if (options.hasPaging() && pager == null) {
long totalCount;
if (options.getOptions().containsKey("query")) {
totalCount = entities.size();
long skip = (long) (options.getPage() - 1) * options.getPageSize();
entities = entities.stream().skip(skip).limit(options.getPageSize()).collect(toList());
} else {
totalCount = countTotal(options, filters, orders);
}
pager = new Pager(options.getPage(), totalCount, options.getPageSize());
}
postProcessResponseEntities(entities, options, rpParameters);
handleLinksAndAccess(entities, fields, false);
handleAttributeValues(entities, fields);
linkService.generatePagerLinks(pager, getEntityClass());
RootNode rootNode = NodeUtils.createMetadata();
rootNode.getConfig().setInclusionStrategy(getInclusionStrategy(rpParameters.get("inclusionStrategy")));
if (pager != null) {
rootNode.addChild(NodeUtils.createPager(pager));
}
FieldFilterParams defaults1 = new FieldFilterParams(entities, fields, Defaults.valueOf(options.get("defaults", DEFAULTS)));
CollectionNode defaults = fieldFilterService.toCollectionNode(getEntityClass(), defaults1);
rootNode.addChild(defaults);
cachePrivate(response);
return rootNode;
}
use of org.hisp.dhis.webapi.webdomain.WebMetadata in project dhis2-core by dhis2.
the class OrganisationUnitController method getEntityList.
@GetMapping("/{uid}/parents")
@ResponseBody
public List<OrganisationUnit> getEntityList(@PathVariable("uid") String uid, @RequestParam Map<String, String> parameters, Model model, TranslateParams translateParams, HttpServletRequest request, HttpServletResponse response) throws Exception {
setUserContext(translateParams);
OrganisationUnit organisationUnit = manager.get(getEntityClass(), uid);
List<OrganisationUnit> organisationUnits = Lists.newArrayList();
if (organisationUnit != null) {
OrganisationUnit organisationUnitParent = organisationUnit.getParent();
while (organisationUnitParent != null) {
organisationUnits.add(organisationUnitParent);
organisationUnitParent = organisationUnitParent.getParent();
}
}
WebMetadata metadata = new WebMetadata();
metadata.setOrganisationUnits(organisationUnits);
return organisationUnits;
}
use of org.hisp.dhis.webapi.webdomain.WebMetadata in project dhis2-core by dhis2.
the class ProgramDataElementController method getObjectList.
@GetMapping
@SuppressWarnings("unchecked")
@ResponseBody
public RootNode getObjectList(@RequestParam Map<String, String> rpParameters, OrderParams orderParams) throws QueryParserException {
Schema schema = schemaService.getDynamicSchema(ProgramDataElementDimensionItem.class);
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
List<Order> orders = orderParams.getOrders(schema);
if (fields.isEmpty()) {
fields.addAll(Preset.ALL.getFields());
}
WebOptions options = new WebOptions(rpParameters);
WebMetadata metadata = new WebMetadata();
List<ProgramDataElementDimensionItem> programDataElements;
Query query = queryService.getQueryFromUrl(ProgramDataElementDimensionItem.class, filters, orders, PaginationUtils.getPaginationData(options), options.getRootJunction());
query.setDefaultOrder();
if (options.contains("program")) {
String programUid = options.get("program");
programDataElements = programService.getGeneratedProgramDataElements(programUid);
query.setObjects(programDataElements);
}
programDataElements = (List<ProgramDataElementDimensionItem>) queryService.query(query);
Pager pager = metadata.getPager();
if (options.hasPaging() && pager == null) {
pager = new Pager(options.getPage(), programDataElements.size(), options.getPageSize());
programDataElements = PagerUtils.pageCollection(programDataElements, pager);
}
RootNode rootNode = NodeUtils.createMetadata();
if (pager != null) {
rootNode.addChild(NodeUtils.createPager(pager));
}
rootNode.addChild(fieldFilterService.toCollectionNode(ProgramDataElementDimensionItem.class, new FieldFilterParams(programDataElements, fields)));
return rootNode;
}
use of org.hisp.dhis.webapi.webdomain.WebMetadata in project dhis2-core by dhis2.
the class AbstractFullReadOnlyController method getObjectListCsv.
@GetMapping(produces = "application/csv")
public void getObjectListCsv(@RequestParam Map<String, String> rpParameters, OrderParams orderParams, @CurrentUser User currentUser, @RequestParam(defaultValue = ",") char separator, @RequestParam(defaultValue = "false") boolean skipHeader, HttpServletResponse response) throws IOException {
List<Order> orders = orderParams.getOrders(getSchema());
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<String> filters = Lists.newArrayList(contextService.getParameterValues("filter"));
WebOptions options = new WebOptions(rpParameters);
WebMetadata metadata = new WebMetadata();
if (fields.isEmpty()) {
fields.addAll(Preset.defaultPreset().getFields());
}
// only support metadata
if (!getSchema().isMetadata()) {
throw new HttpClientErrorException(HttpStatus.NOT_FOUND);
}
if (!aclService.canRead(currentUser, getEntityClass())) {
throw new ReadAccessDeniedException("You don't have the proper permissions to read objects of this type.");
}
List<T> entities = getEntityList(metadata, options, filters, orders);
CsvSchema schema;
CsvSchema.Builder schemaBuilder = CsvSchema.builder();
List<Property> properties = new ArrayList<>();
for (String field : fields) {
// then the group[id] part is simply ignored.
for (String splitField : field.split(",")) {
Property property = getSchema().getProperty(splitField);
if (property == null || !property.isSimple()) {
continue;
}
schemaBuilder.addColumn(property.getName());
properties.add(property);
}
}
schema = schemaBuilder.build().withColumnSeparator(separator);
if (!skipHeader) {
schema = schema.withHeader();
}
CsvMapper csvMapper = new CsvMapper();
csvMapper.configure(JsonGenerator.Feature.IGNORE_UNKNOWN, true);
List<Map<String, Object>> csvObjects = entities.stream().map(e -> {
Map<String, Object> map = new HashMap<>();
for (Property property : properties) {
Object value = ReflectionUtils.invokeMethod(e, property.getGetterMethod());
map.put(property.getName(), value);
}
return map;
}).collect(toList());
csvMapper.writer(schema).writeValue(response.getWriter(), csvObjects);
response.flushBuffer();
}
use of org.hisp.dhis.webapi.webdomain.WebMetadata 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;
}
Aggregations