Search in sources :

Example 86 with DataSet

use of org.hisp.dhis.dataset.DataSet in project dhis2-core by dhis2.

the class ObjectBundleServiceFavoritesTest method testCreateMetadataWithReportTables1.

@Test
public void testCreateMetadataWithReportTables1() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/favorites/metadata_with_rt1.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE_AND_UPDATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    ObjectBundleValidationReport validate = objectBundleValidationService.validate(bundle);
    assertTrue(validate.getErrorReports().isEmpty());
    objectBundleService.commit(bundle);
    List<DataSet> dataSets = manager.getAll(DataSet.class);
    List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
    List<DataElement> dataElements = manager.getAll(DataElement.class);
    List<ReportTable> reportTables = manager.getAll(ReportTable.class);
    assertEquals(1, dataSets.size());
    assertEquals(1, organisationUnits.size());
    assertEquals(4, dataElements.size());
    assertEquals(3, reportTables.size());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ObjectBundleValidationReport(org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport) DataSet(org.hisp.dhis.dataset.DataSet) ReportTable(org.hisp.dhis.reporttable.ReportTable) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) List(java.util.List) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 87 with DataSet

use of org.hisp.dhis.dataset.DataSet in project dhis2-core by dhis2.

the class ObjectBundleServiceTest method testCreateSimpleMetadataUID.

@Test
public void testCreateSimpleMetadataUID() throws IOException {
    Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> metadata = renderService.fromMetadata(new ClassPathResource("dxf2/simple_metadata.json").getInputStream(), RenderFormat.JSON);
    ObjectBundleParams params = new ObjectBundleParams();
    params.setObjectBundleMode(ObjectBundleMode.COMMIT);
    params.setImportStrategy(ImportStrategy.CREATE);
    params.setObjects(metadata);
    ObjectBundle bundle = objectBundleService.create(params);
    objectBundleValidationService.validate(bundle);
    objectBundleService.commit(bundle);
    List<OrganisationUnit> organisationUnits = manager.getAll(OrganisationUnit.class);
    List<DataElement> dataElements = manager.getAll(DataElement.class);
    List<DataSet> dataSets = manager.getAll(DataSet.class);
    List<UserAuthorityGroup> userRoles = manager.getAll(UserAuthorityGroup.class);
    List<User> users = manager.getAll(User.class);
    assertFalse(organisationUnits.isEmpty());
    assertFalse(dataElements.isEmpty());
    assertFalse(dataSets.isEmpty());
    assertFalse(users.isEmpty());
    assertFalse(userRoles.isEmpty());
    Map<Class<? extends IdentifiableObject>, IdentifiableObject> defaults = manager.getDefaults();
    DataSet dataSet = dataSets.get(0);
    User user = users.get(0);
    for (DataElement dataElement : dataElements) {
        assertNotNull(dataElement.getDataElementCategoryCombo());
        assertEquals(defaults.get(DataElementCategoryCombo.class), dataElement.getDataElementCategoryCombo());
    }
    assertFalse(dataSet.getSources().isEmpty());
    assertFalse(dataSet.getDataSetElements().isEmpty());
    assertEquals(1, dataSet.getSources().size());
    assertEquals(2, dataSet.getDataSetElements().size());
    assertEquals(PeriodType.getPeriodTypeByName("Monthly"), dataSet.getPeriodType());
    assertNotNull(user.getUserCredentials());
    assertEquals("admin", user.getUserCredentials().getUsername());
    assertFalse(user.getUserCredentials().getUserAuthorityGroups().isEmpty());
    assertFalse(user.getOrganisationUnits().isEmpty());
    assertEquals("PdWlltZnVZe", user.getOrganisationUnit().getUid());
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) DataSet(org.hisp.dhis.dataset.DataSet) ClassPathResource(org.springframework.core.io.ClassPathResource) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) DataElement(org.hisp.dhis.dataelement.DataElement) UserAuthorityGroup(org.hisp.dhis.user.UserAuthorityGroup) List(java.util.List) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 88 with DataSet

use of org.hisp.dhis.dataset.DataSet in project dhis2-core by dhis2.

the class LockExceptionController method addLockException.

@RequestMapping(method = RequestMethod.POST)
public void addLockException(@RequestParam("ou") String organisationUnitId, @RequestParam("pe") String periodId, @RequestParam("ds") String dataSetId, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
    User user = userService.getCurrentUser();
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    Period period = periodService.reloadPeriod(PeriodType.getPeriodFromIsoString(periodId));
    if (dataSet == null || period == null) {
        throw new WebMessageException(WebMessageUtils.conflict(" DataSet or Period is invalid"));
    }
    if (!aclService.canUpdate(user, dataSet)) {
        throw new ReadAccessDeniedException("You don't have the proper permissions to update this object");
    }
    boolean created = false;
    List<String> listOrgUnitIds = new ArrayList<>();
    if (organisationUnitId.startsWith("[") && organisationUnitId.endsWith("]")) {
        String[] arrOrgUnitIds = organisationUnitId.substring(1, organisationUnitId.length() - 1).split(",");
        Collections.addAll(listOrgUnitIds, arrOrgUnitIds);
    } else {
        listOrgUnitIds.add(organisationUnitId);
    }
    if (listOrgUnitIds.size() == 0) {
        throw new WebMessageException(WebMessageUtils.conflict(" OrganisationUnit ID is invalid."));
    }
    for (String id : listOrgUnitIds) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(id);
        if (organisationUnit == null) {
            throw new WebMessageException(WebMessageUtils.conflict("Can't find OrganisationUnit with id =" + id));
        }
        if (organisationUnit.getDataSets().contains(dataSet)) {
            LockException lockException = new LockException();
            lockException.setOrganisationUnit(organisationUnit);
            lockException.setDataSet(dataSet);
            lockException.setPeriod(period);
            dataSetService.addLockException(lockException);
            created = true;
        }
    }
    if (created) {
        webMessageService.send(WebMessageUtils.created("LockException created successfully."), response, request);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) LockException(org.hisp.dhis.dataset.LockException) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 89 with DataSet

use of org.hisp.dhis.dataset.DataSet in project dhis2-core by dhis2.

the class LoadFormAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    dataSet = dataSetService.getDataSet(dataSetId);
    if (dataSet == null) {
        return INPUT;
    }
    FormType formType = dataSet.getFormType();
    if (formType.isCustom() && dataSet.hasDataEntryForm()) {
        dataEntryForm = dataSet.getDataEntryForm();
        customDataEntryFormCode = dataEntryFormService.prepareDataEntryFormForEntry(dataEntryForm, dataSet, i18n);
        return formType.toString();
    }
    // ---------------------------------------------------------------------
    // Section / default form
    // ---------------------------------------------------------------------
    List<DataElement> dataElements = new ArrayList<>(dataSet.getDataElements());
    if (dataElements.isEmpty()) {
        return INPUT;
    }
    Collections.sort(dataElements);
    orderedDataElements = ListMap.getListMap(dataElements, de -> de.getCategoryCombo(dataSet));
    orderedCategoryCombos = getDataElementCategoryCombos(dataElements, dataSet);
    for (DataElementCategoryCombo categoryCombo : orderedCategoryCombos) {
        List<DataElementCategoryOptionCombo> optionCombos = categoryCombo.getSortedOptionCombos();
        orderedCategoryOptionCombos.put(categoryCombo.getId(), optionCombos);
        // -----------------------------------------------------------------
        // Perform ordering of categories and their options so that they
        // could be displayed as in the paper form. Note that the total
        // number of entry cells to be generated are the multiple of options
        // from each category.
        // -----------------------------------------------------------------
        numberOfTotalColumns.put(categoryCombo.getId(), optionCombos.size());
        orderedCategories.put(categoryCombo.getId(), categoryCombo.getCategories());
        Map<Integer, List<DataElementCategoryOption>> optionsMap = new HashMap<>();
        for (DataElementCategory category : categoryCombo.getCategories()) {
            optionsMap.put(category.getId(), category.getCategoryOptions());
        }
        orderedOptionsMap.put(categoryCombo.getId(), optionsMap);
        // -----------------------------------------------------------------
        // Calculating the number of times each category should be repeated
        // -----------------------------------------------------------------
        Map<Integer, Integer> catRepeat = new HashMap<>();
        Map<Integer, Collection<Integer>> colRepeat = new HashMap<>();
        int catColSpan = optionCombos.size();
        for (DataElementCategory cat : categoryCombo.getCategories()) {
            int categoryOptionSize = cat.getCategoryOptions().size();
            if (categoryOptionSize > 0 && catColSpan >= categoryOptionSize) {
                catColSpan = catColSpan / categoryOptionSize;
                int total = optionCombos.size() / (catColSpan * categoryOptionSize);
                Collection<Integer> cols = new ArrayList<>(total);
                for (int i = 0; i < total; i++) {
                    cols.add(i);
                }
                colRepeat.put(cat.getId(), cols);
                catRepeat.put(cat.getId(), catColSpan);
            }
        }
        catColRepeat.put(categoryCombo.getId(), colRepeat);
    }
    // ---------------------------------------------------------------------
    // Get data entry form
    // ---------------------------------------------------------------------
    DataSet dsOriginal = dataSet;
    if (dataSet.getFormType().isDefault()) {
        DataSet dataSetCopy = new DataSet();
        dataSetCopy.setUid(dataSet.getUid());
        dataSetCopy.setName(dataSet.getName());
        dataSetCopy.setShortName(dataSet.getShortName());
        dataSetCopy.setRenderAsTabs(dataSet.isRenderAsTabs());
        dataSetCopy.setRenderHorizontally(dataSet.isRenderHorizontally());
        dataSetCopy.setDataElementDecoration(dataSet.isDataElementDecoration());
        dataSet = dataSetCopy;
        for (int i = 0; i < orderedCategoryCombos.size(); i++) {
            DataElementCategoryCombo categoryCombo = orderedCategoryCombos.get(i);
            String name = !categoryCombo.isDefault() ? categoryCombo.getName() : dataSetCopy.getName();
            Section section = new Section();
            section.setUid(CodeGenerator.generateUid());
            section.setId(i);
            section.setName(name);
            section.setSortOrder(i);
            section.setDataSet(dataSetCopy);
            dataSetCopy.getSections().add(section);
            section.getDataElements().addAll(orderedDataElements.get(categoryCombo));
            section.setIndicators(new ArrayList<>(dataSet.getIndicators()));
        }
        formType = FormType.SECTION;
    }
    if (CodeGenerator.isValidUid(multiOrganisationUnit)) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(multiOrganisationUnit);
        List<OrganisationUnit> organisationUnitChildren = new ArrayList<>();
        for (OrganisationUnit child : organisationUnit.getChildren()) {
            if (child.getDataSets().contains(dsOriginal)) {
                organisationUnitChildren.add(child);
            }
        }
        Collections.sort(organisationUnitChildren);
        organisationUnits.addAll(organisationUnitChildren);
        getSectionForm(dataElements, dataSet);
        formType = FormType.SECTION_MULTIORG;
    }
    getSectionForm(dataElements, dataSet);
    return formType.toString();
}
Also used : ListMap(org.hisp.dhis.common.ListMap) SectionOrderComparator(org.hisp.dhis.dataset.comparator.SectionOrderComparator) DataSet(org.hisp.dhis.dataset.DataSet) HashMap(java.util.HashMap) OrganisationUnitService(org.hisp.dhis.organisationunit.OrganisationUnitService) I18n(org.hisp.dhis.i18n.I18n) ArrayList(java.util.ArrayList) DataElement(org.hisp.dhis.dataelement.DataElement) HashSet(java.util.HashSet) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) Map(java.util.Map) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) DataEntryForm(org.hisp.dhis.dataentryform.DataEntryForm) FormType(org.hisp.dhis.dataset.FormType) Collection(java.util.Collection) Set(java.util.Set) DataEntryFormService(org.hisp.dhis.dataentryform.DataEntryFormService) DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) DataElementCategoryComboSizeComparator(org.hisp.dhis.dataelement.comparator.DataElementCategoryComboSizeComparator) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Section(org.hisp.dhis.dataset.Section) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Action(com.opensymphony.xwork2.Action) DataSetService(org.hisp.dhis.dataset.DataSetService) Collections(java.util.Collections) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataElementCategoryCombo(org.hisp.dhis.dataelement.DataElementCategoryCombo) HashMap(java.util.HashMap) DataSet(org.hisp.dhis.dataset.DataSet) FormType(org.hisp.dhis.dataset.FormType) ArrayList(java.util.ArrayList) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Section(org.hisp.dhis.dataset.Section) DataElement(org.hisp.dhis.dataelement.DataElement) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 90 with DataSet

use of org.hisp.dhis.dataset.DataSet in project dhis2-core by dhis2.

the class GetDataValuesForDataSetAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    // ---------------------------------------------------------------------
    // Validation
    // ---------------------------------------------------------------------
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    Period period = PeriodType.getPeriodFromIsoString(periodId);
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
    if (organisationUnit == null || period == null || dataSet == null) {
        log.warn("Illegal input, org unit: " + organisationUnit + ", period: " + period + ", data set: " + dataSet);
        return SUCCESS;
    }
    Set<OrganisationUnit> children = organisationUnit.getChildren();
    // ---------------------------------------------------------------------
    // Attributes
    // ---------------------------------------------------------------------
    DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(cc, cp, false);
    // ---------------------------------------------------------------------
    // Data values & Min-max data elements
    // ---------------------------------------------------------------------
    minMaxDataElements.addAll(minMaxDataElementService.getMinMaxDataElements(organisationUnit, dataSet.getDataElements()));
    if (!multiOrganisationUnit) {
        dataValues.addAll(dataValueService.getDataValues(organisationUnit, period, dataSet.getDataElements(), attributeOptionCombo));
    } else {
        for (OrganisationUnit ou : children) {
            if (ou.getDataSets().contains(dataSet)) {
                dataValues.addAll(dataValueService.getDataValues(ou, period, dataSet.getDataElements(), attributeOptionCombo));
                minMaxDataElements.addAll(minMaxDataElementService.getMinMaxDataElements(ou, dataSet.getDataElements()));
            }
        }
    }
    // ---------------------------------------------------------------------
    // File resource meta-data
    // ---------------------------------------------------------------------
    List<String> fileResourceUids = dataValues.stream().filter(dv -> dv.getDataElement().isFileType()).map(DataValue::getValue).collect(Collectors.toList());
    dataValueFileResourceMap.putAll(fileResourceService.getFileResources(fileResourceUids).stream().collect(Collectors.toMap(BaseIdentifiableObject::getUid, f -> f)));
    if (!multiOrganisationUnit) {
        CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, organisationUnit, attributeOptionCombo);
        if (registration != null) {
            complete = true;
            date = registration.getDate();
            storedBy = registration.getStoredBy();
        }
        locked = dataSetService.isLocked(dataSet, period, organisationUnit, attributeOptionCombo, null);
    } else {
        complete = true;
        for (OrganisationUnit ou : children) {
            if (ou.getDataSets().contains(dataSet)) {
                locked = dataSetService.isLocked(dataSet, period, organisationUnit, attributeOptionCombo, null);
                if (locked) {
                    break;
                }
                CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, ou, attributeOptionCombo);
                if (complete && registration == null) {
                    complete = false;
                }
            }
        }
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) DataSet(org.hisp.dhis.dataset.DataSet) Period(org.hisp.dhis.period.Period) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Aggregations

DataSet (org.hisp.dhis.dataset.DataSet)118 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)52 Period (org.hisp.dhis.period.Period)40 Test (org.junit.Test)39 DataElement (org.hisp.dhis.dataelement.DataElement)34 ArrayList (java.util.ArrayList)29 DhisSpringTest (org.hisp.dhis.DhisSpringTest)21 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)21 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 List (java.util.List)20 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)17 User (org.hisp.dhis.user.User)17 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)16 HashSet (java.util.HashSet)14 DataElementCategoryCombo (org.hisp.dhis.dataelement.DataElementCategoryCombo)14 ClassPathResource (org.springframework.core.io.ClassPathResource)14 UserAuthorityGroup (org.hisp.dhis.user.UserAuthorityGroup)13 ObjectBundleValidationReport (org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport)11 PeriodType (org.hisp.dhis.period.PeriodType)11 Date (java.util.Date)9