Search in sources :

Example 26 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class DataElementOperandController method getObjectList.

@GetMapping
@SuppressWarnings("unchecked")
@ResponseBody
public RootNode getObjectList(@RequestParam Map<String, String> rpParameters, OrderParams orderParams, @CurrentUser User currentUser) throws QueryParserException {
    Schema schema = schemaService.getDynamicSchema(DataElementOperand.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<DataElementOperand> dataElementOperands;
    if (options.isTrue("persisted")) {
        dataElementOperands = Lists.newArrayList(manager.getAll(DataElementOperand.class));
    } else {
        boolean totals = options.isTrue("totals");
        String deg = CollectionUtils.popStartsWith(filters, "dataElement.dataElementGroups.id:eq:");
        deg = deg != null ? deg.substring("dataElement.dataElementGroups.id:eq:".length()) : null;
        String ds = options.get("dataSet");
        if (deg != null) {
            DataElementGroup dataElementGroup = manager.get(DataElementGroup.class, deg);
            dataElementOperands = dataElementCategoryService.getOperands(dataElementGroup.getMembers(), totals);
        } else if (ds != null) {
            DataSet dataSet = manager.get(DataSet.class, ds);
            dataElementOperands = dataElementCategoryService.getOperands(dataSet, totals);
        } else {
            List<DataElement> dataElements = new ArrayList<>(manager.getAllSorted(DataElement.class));
            dataElementOperands = dataElementCategoryService.getOperands(dataElements, totals);
        }
    }
    // This is needed for two reasons:
    // 1) We are doing in-memory paging;
    // 2) We have to count all items respecting the filtering and the
    // initial universe of elements. In this case, the variable
    // "dataElementOperands".
    Query queryForCount = queryService.getQueryFromUrl(DataElementOperand.class, filters, orders);
    queryForCount.setObjects(dataElementOperands);
    List<DataElementOperand> totalOfItems = (List<DataElementOperand>) queryService.query(queryForCount);
    Query query = queryService.getQueryFromUrl(DataElementOperand.class, filters, orders, PaginationUtils.getPaginationData(options), options.getRootJunction());
    query.setDefaultOrder();
    query.setObjects(dataElementOperands);
    dataElementOperands = (List<DataElementOperand>) queryService.query(query);
    Pager pager = metadata.getPager();
    if (options.hasPaging() && pager == null) {
        final long countTotal = isNotEmpty(totalOfItems) ? totalOfItems.size() : 0;
        // fetch the count for the current query from a short-lived cache
        long cachedCountTotal = paginationCountCache.computeIfAbsent(calculatePaginationCountKey(currentUser, filters, options), () -> countTotal);
        pager = new Pager(options.getPage(), cachedCountTotal, options.getPageSize());
        linkService.generatePagerLinks(pager, DataElementOperand.class);
    }
    RootNode rootNode = NodeUtils.createMetadata();
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    rootNode.addChild(fieldFilterService.toCollectionNode(DataElementOperand.class, new FieldFilterParams(dataElementOperands, fields)));
    return rootNode;
}
Also used : Order(org.hisp.dhis.query.Order) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) RootNode(org.hisp.dhis.node.types.RootNode) Query(org.hisp.dhis.query.Query) DataSet(org.hisp.dhis.dataset.DataSet) Schema(org.hisp.dhis.schema.Schema) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) WebMetadata(org.hisp.dhis.webapi.webdomain.WebMetadata) Pager(org.hisp.dhis.common.Pager) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) ArrayList(java.util.ArrayList) List(java.util.List) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 27 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class ConfigurationController method setInfrastructuralDataElements.

@PreAuthorize("hasRole('ALL') or hasRole('F_SYSTEM_SETTING')")
@PostMapping("/infrastructuralDataElements")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void setInfrastructuralDataElements(@RequestBody String uid) throws NotFoundException {
    uid = trim(uid);
    DataElementGroup group = identifiableObjectManager.get(DataElementGroup.class, uid);
    if (group == null) {
        throw new NotFoundException("Data element group", uid);
    }
    Configuration configuration = configurationService.getConfiguration();
    configuration.setInfrastructuralDataElements(group);
    configurationService.setConfiguration(configuration);
}
Also used : Configuration(org.hisp.dhis.configuration.Configuration) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 28 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testObjectBundleShouldAddToObjectAndPreheat.

@Test
void testObjectBundleShouldAddToObjectAndPreheat() {
    DataElementGroup dataElementGroup = fromJson("dxf2/degAUidRef.json", DataElementGroup.class);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.VALIDATE);
    params.addObject(dataElementGroup);
    ObjectBundle bundle = objectBundleService.create(params);
    bundle.getPreheat().put(bundle.getPreheatIdentifier(), dataElementGroup);
    assertTrue(StreamSupport.stream(bundle.getObjects(DataElementGroup.class).spliterator(), false).anyMatch(dataElementGroup::equals));
    assertTrue(bundle.getPreheat().containsKey(PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid()));
}
Also used : DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 29 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class GetDataElementsAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    canReadType(DataElement.class);
    if (id != null && id != ALL) {
        DataElementGroup dataElementGroup = dataElementService.getDataElementGroup(id);
        if (dataElementGroup != null) {
            dataElements = new ArrayList<>(dataElementGroup.getMembers());
        }
    } else if (categoryComboId != null && categoryComboId != ALL) {
        CategoryCombo categoryCombo = categoryService.getCategoryCombo(categoryComboId);
        if (categoryCombo != null) {
            dataElements = new ArrayList<>(dataElementService.getDataElementByCategoryCombo(categoryCombo));
        }
    } else if (dataSetId != null) {
        DataSet dataset = dataSetService.getDataSet(dataSetId);
        if (dataset != null) {
            dataElements = new ArrayList<>(dataset.getDataElements());
        }
    } else if (periodTypeName != null) {
        PeriodType periodType = periodService.getPeriodTypeByName(periodTypeName);
        if (periodType != null) {
            dataElements = new ArrayList<>(dataElementService.getDataElementsByPeriodType(periodType));
        }
    } else if (domain != null) {
        if (domain.equals(DataElementDomain.AGGREGATE.getValue())) {
            dataElements = new ArrayList<>(dataElementService.getDataElementsByDomainType(DataElementDomain.AGGREGATE));
        } else {
            dataElements = new ArrayList<>(dataElementService.getDataElementsByDomainType(DataElementDomain.TRACKER));
        }
    } else {
        dataElements = new ArrayList<>(dataElementService.getAllDataElements());
        ContextUtils.clearIfNotModified(ServletActionContext.getRequest(), ServletActionContext.getResponse(), dataElements);
    }
    if (key != null) {
        dataElements = IdentifiableObjectUtils.filterNameByKey(dataElements, key, true);
    }
    if (dataElements == null) {
        dataElements = new ArrayList<>();
    }
    Collections.sort(dataElements);
    if (aggregate) {
        FilterUtils.filter(dataElements, new AggregatableDataElementFilter());
    }
    User currentUser = currentUserService.getCurrentUser();
    dataElements.forEach(instance -> canReadInstance(instance, currentUser));
    if (usePaging) {
        this.paging = createPaging(dataElements.size());
        dataElements = dataElements.subList(paging.getStartPos(), paging.getEndPos());
    }
    return SUCCESS;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) User(org.hisp.dhis.user.User) CategoryCombo(org.hisp.dhis.category.CategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) ArrayList(java.util.ArrayList) AggregatableDataElementFilter(org.hisp.dhis.system.filter.AggregatableDataElementFilter)

Example 30 with DataElementGroup

use of org.hisp.dhis.dataelement.DataElementGroup in project dhis2-core by dhis2.

the class PreheatServiceTest method testPreheatAllUID.

@Test
@SuppressWarnings("unchecked")
public void testPreheatAllUID() {
    DataElementGroup dataElementGroup = new DataElementGroup("DataElementGroupA");
    dataElementGroup.setAutoFields();
    DataElement de1 = createDataElement('A');
    DataElement de2 = createDataElement('B');
    DataElement de3 = createDataElement('C');
    manager.save(de1);
    manager.save(de2);
    manager.save(de3);
    User user = createUser('A');
    manager.save(user);
    dataElementGroup.addDataElement(de1);
    dataElementGroup.addDataElement(de2);
    dataElementGroup.addDataElement(de3);
    dataElementGroup.setUser(user);
    manager.save(dataElementGroup);
    PreheatParams params = new PreheatParams();
    params.setPreheatMode(PreheatMode.ALL);
    params.setClasses(Sets.newHashSet(DataElement.class, DataElementGroup.class, User.class));
    preheatService.validate(params);
    Preheat preheat = preheatService.preheat(params);
    assertFalse(preheat.isEmpty());
    assertFalse(preheat.isEmpty(PreheatIdentifier.UID));
    assertFalse(preheat.isEmpty(PreheatIdentifier.UID, DataElement.class));
    assertFalse(preheat.isEmpty(PreheatIdentifier.UID, DataElementGroup.class));
    assertFalse(preheat.isEmpty(PreheatIdentifier.UID, User.class));
    assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElement.class, de1.getUid()));
    assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElement.class, de2.getUid()));
    assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElement.class, de3.getUid()));
    assertTrue(preheat.containsKey(PreheatIdentifier.UID, DataElementGroup.class, dataElementGroup.getUid()));
    assertTrue(preheat.containsKey(PreheatIdentifier.UID, User.class, user.getUid()));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) User(org.hisp.dhis.user.User) DataElementGroup(org.hisp.dhis.dataelement.DataElementGroup) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Aggregations

DataElementGroup (org.hisp.dhis.dataelement.DataElementGroup)55 DataElement (org.hisp.dhis.dataelement.DataElement)31 Test (org.junit.jupiter.api.Test)29 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)17 ArrayList (java.util.ArrayList)15 User (org.hisp.dhis.user.User)15 DataSet (org.hisp.dhis.dataset.DataSet)9 List (java.util.List)8 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)8 Set (java.util.Set)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)7 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)6 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)6 DataElementGroupSet (org.hisp.dhis.dataelement.DataElementGroupSet)6 DimensionalObject (org.hisp.dhis.common.DimensionalObject)5 LegendSet (org.hisp.dhis.legend.LegendSet)5 OptionSet (org.hisp.dhis.option.OptionSet)5 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)5 PeriodType (org.hisp.dhis.period.PeriodType)5 DimensionItemKeywords (org.hisp.dhis.common.DimensionItemKeywords)4