Search in sources :

Example 21 with CompleteDataSetRegistration

use of org.hisp.dhis.dataset.CompleteDataSetRegistration 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)

Example 22 with CompleteDataSetRegistration

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

the class MarkComplete method execute.

// -------------------------------------------------------------------------
// Action Implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    Validate.notNull(organisationUnitId);
    Validate.notNull(isoPeriod);
    Validate.notNull(dataSetId);
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
    Period period = periodService.getPeriod(isoPeriod);
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    //TODO
    DataElementCategoryOptionCombo optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, organisationUnit, optionCombo);
    if (registration == null) {
        registration = new CompleteDataSetRegistration();
        registration.setDataSet(dataSet);
        registration.setPeriod(period);
        registration.setSource(organisationUnit);
        registration.setDate(new Date());
        registration.setStoredBy(currentUserService.getCurrentUsername());
        registrationService.saveCompleteDataSetRegistration(registration);
    }
    return SUCCESS;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) Period(org.hisp.dhis.period.Period) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date)

Example 23 with CompleteDataSetRegistration

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

the class DefaultDataValueSetService method getCompleteDate.

private Date getCompleteDate(DataExportParams params) {
    if (params.isSingleDataValueSet()) {
        //TODO
        DataElementCategoryOptionCombo optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
        CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(params.getFirstDataSet(), params.getFirstPeriod(), params.getFirstOrganisationUnit(), optionCombo);
        return registration != null ? registration.getDate() : null;
    }
    return null;
}
Also used : CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 24 with CompleteDataSetRegistration

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

the class DefaultDataValueSetService method handleComplete.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
private void handleComplete(DataSet dataSet, Date completeDate, Period period, OrganisationUnit orgUnit, DataElementCategoryOptionCombo attributeOptionCombo, ImportSummary summary) {
    if (orgUnit == null) {
        summary.getConflicts().add(new ImportConflict(OrganisationUnit.class.getSimpleName(), ERROR_OBJECT_NEEDED_TO_COMPLETE));
        return;
    }
    if (period == null) {
        summary.getConflicts().add(new ImportConflict(Period.class.getSimpleName(), ERROR_OBJECT_NEEDED_TO_COMPLETE));
        return;
    }
    period = periodService.reloadPeriod(period);
    CompleteDataSetRegistration completeAlready = registrationService.getCompleteDataSetRegistration(dataSet, period, orgUnit, attributeOptionCombo);
    String username = currentUserService.getCurrentUsername();
    if (completeAlready != null) {
        completeAlready.setStoredBy(username);
        completeAlready.setDate(completeDate);
        registrationService.updateCompleteDataSetRegistration(completeAlready);
    } else {
        CompleteDataSetRegistration registration = new CompleteDataSetRegistration(dataSet, period, orgUnit, attributeOptionCombo, completeDate, username);
        registrationService.saveCompleteDataSetRegistration(registration);
    }
    summary.setDataSetComplete(DateUtils.getMediumDateString(completeDate));
}
Also used : CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 25 with CompleteDataSetRegistration

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

the class DefaultCompleteDataSetRegistrationExchangeService method batchImport.

/**
     * @return total number of processed CompleteDataSetRegistration objects
     */
private int batchImport(CompleteDataSetRegistrations completeRegistrations, ImportConfig config, ImportSummary summary, MetaDataCallables mdCallables, MetaDataCaches mdCaches) {
    final String currentUser = currentUserService.getCurrentUsername();
    final Set<OrganisationUnit> userOrgUnits = currentUserService.getCurrentUserOrganisationUnits();
    final I18n i18n = i18nManager.getI18n();
    BatchHandler<CompleteDataSetRegistration> batchHandler = batchHandlerFactory.createBatchHandler(CompleteDataSetRegistrationBatchHandler.class).init();
    int importCount = 0, updateCount = 0, deleteCount = 0, totalCount = 0;
    Date now = new Date();
    while (completeRegistrations.hasNextCompleteDataSetRegistration()) {
        org.hisp.dhis.dxf2.dataset.CompleteDataSetRegistration cdsr = completeRegistrations.getNextCompleteDataSetRegistration();
        totalCount++;
        // ---------------------------------------------------------------------
        // Init meta-data properties against meta-data cache
        // ---------------------------------------------------------------------
        MetaDataProperties mdProps = initMetaDataProperties(cdsr, mdCallables, mdCaches);
        heatCaches(mdCaches, config);
        // ---------------------------------------------------------------------
        // Meta-data validation
        // ---------------------------------------------------------------------
        String storedBy;
        try {
            // Validate CDSR meta-data properties
            mdProps.validate(cdsr, config);
            validateOrgUnitInUserHierarchy(mdCaches, mdProps, userOrgUnits, currentUser);
            if (config.strictAttrOptionCombos) {
                validateAocMatchesDataSetCc(mdProps);
            }
            validateAttrOptCombo(mdProps, mdCaches, config);
            if (config.strictPeriods) {
                validateHasMatchingPeriodTypes(mdProps);
            }
            if (config.strictOrgUnits) {
                validateDataSetIsAssignedToOrgUnit(mdProps);
            }
            storedBy = cdsr.getStoredBy();
            validateStoredBy(storedBy, i18n);
            storedBy = StringUtils.isBlank(storedBy) ? currentUser : storedBy;
        // TODO Check if Period is within range of data set?
        } catch (ImportConflictException ic) {
            summary.getConflicts().add(ic.getImportConflict());
            continue;
        }
        // -----------------------------------------------------------------
        // Create complete data set registration
        // -----------------------------------------------------------------
        CompleteDataSetRegistration internalCdsr = createCompleteDataSetRegistration(cdsr, mdProps, now, storedBy);
        CompleteDataSetRegistration existingCdsr = config.skipExistingCheck ? null : batchHandler.findObject(internalCdsr);
        ImportStrategy strategy = config.strategy;
        boolean isDryRun = config.dryRun;
        if (!config.skipExistingCheck && existingCdsr != null) {
            if (strategy.isCreateAndUpdate() || strategy.isUpdate()) {
                // Update existing CDSR
                updateCount++;
                if (!isDryRun) {
                    batchHandler.updateObject(internalCdsr);
                }
            } else if (strategy.isDelete()) {
                // TODO Does 'delete' even make sense for CDSR?
                // Replace existing CDSR
                deleteCount++;
                if (!isDryRun) {
                    batchHandler.deleteObject(internalCdsr);
                }
            }
        } else {
            if (strategy.isCreateAndUpdate() || strategy.isCreate()) {
                if (existingCdsr != null) {
                    // Already exists -> update
                    importCount++;
                    if (!isDryRun) {
                        batchHandler.updateObject(internalCdsr);
                    }
                } else {
                    // Does not exist -> add new CDSR
                    boolean added = false;
                    if (!isDryRun) {
                        added = batchHandler.addObject(internalCdsr);
                    }
                    if (isDryRun || added) {
                        importCount++;
                    }
                }
            }
        }
    }
    batchHandler.flush();
    finalizeSummary(summary, totalCount, importCount, updateCount, deleteCount);
    return totalCount;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Date(java.util.Date) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) CompleteDataSetRegistrationBatchHandler(org.hisp.dhis.jdbc.batchhandler.CompleteDataSetRegistrationBatchHandler) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) I18n(org.hisp.dhis.i18n.I18n)

Aggregations

CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)31 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)12 Test (org.junit.Test)12 DhisSpringTest (org.hisp.dhis.DhisSpringTest)11 Period (org.hisp.dhis.period.Period)10 Date (java.util.Date)9 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 DataSet (org.hisp.dhis.dataset.DataSet)6 DataValue (org.hisp.dhis.datavalue.DataValue)4 DataValueAudit (org.hisp.dhis.datavalue.DataValueAudit)3 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)3 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)3 ClassPathResource (org.springframework.core.io.ClassPathResource)3 ArrayList (java.util.ArrayList)2 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)2 ApiVersion (org.hisp.dhis.webapi.mvc.annotation.ApiVersion)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 HashSet (java.util.HashSet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Criteria (org.hibernate.Criteria)1