Search in sources :

Example 1 with DataValue

use of org.hisp.dhis.api.mobile.model.DataValue in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method saveActivityReport.

// -------------------------------------------------------------------------
// DataValueService
// -------------------------------------------------------------------------
@Override
public void saveActivityReport(OrganisationUnit unit, ActivityValue activityValue, Integer programStageSectionId) throws NotAllowedException {
    ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(activityValue.getProgramInstanceId());
    if (programStageInstance == null) {
        throw NotAllowedException.INVALID_PROGRAM_STAGE;
    }
    programStageInstance.getProgramStage();
    List<org.hisp.dhis.dataelement.DataElement> dataElements = new ArrayList<>();
    ProgramStageSection programStageSection = programStageSectionService.getProgramStageSection(programStageSectionId);
    if (programStageSectionId != 0) {
        dataElements.addAll(programStageSection.getDataElements());
    } else {
        for (ProgramStageDataElement de : programStageInstance.getProgramStage().getProgramStageDataElements()) {
            dataElements.add(de.getDataElement());
        }
    }
    programStageInstance.getProgramStage().getProgramStageDataElements();
    Collection<Integer> dataElementIds = new ArrayList<>(activityValue.getDataValues().size());
    for (DataValue dv : activityValue.getDataValues()) {
        dataElementIds.add(dv.getId());
    }
    if (dataElements.size() != dataElementIds.size()) {
        throw NotAllowedException.INVALID_PROGRAM_STAGE;
    }
    Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = new HashMap<>();
    for (org.hisp.dhis.dataelement.DataElement dataElement : dataElements) {
        if (!dataElementIds.contains(dataElement.getId())) {
            throw NotAllowedException.INVALID_PROGRAM_STAGE;
        }
        dataElementMap.put(dataElement.getId(), dataElement);
    }
    // Set ProgramStageInstance to completed
    if (programStageSectionId == 0) {
        programStageInstance.setStatus(EventStatus.COMPLETED);
        programStageInstanceService.updateProgramStageInstance(programStageInstance);
    }
    // Everything is fine, hence save
    saveDataValues(activityValue, programStageInstance, dataElementMap);
}
Also used : ProgramStageSection(org.hisp.dhis.program.ProgramStageSection) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) DataValue(org.hisp.dhis.api.mobile.model.DataValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 2 with DataValue

use of org.hisp.dhis.api.mobile.model.DataValue in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method saveDataValues.

private void saveDataValues(ActivityValue activityValue, ProgramStageInstance programStageInstance, Map<Integer, DataElement> dataElementMap) {
    org.hisp.dhis.dataelement.DataElement dataElement;
    String value;
    for (DataValue dv : activityValue.getDataValues()) {
        value = dv.getValue();
        if (value != null && value.trim().length() == 0) {
            value = null;
        }
        if (value != null) {
            value = value.trim();
        }
        dataElement = dataElementMap.get(dv.getId());
        TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
        if (dataValue == null) {
            if (value != null) {
                if (programStageInstance.getExecutionDate() == null) {
                    programStageInstance.setExecutionDate(new Date());
                    programStageInstanceService.updateProgramStageInstance(programStageInstance);
                }
                dataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
                dataValueService.saveTrackedEntityDataValue(dataValue);
            }
        } else {
            if (programStageInstance.getExecutionDate() == null) {
                programStageInstance.setExecutionDate(new Date());
                programStageInstanceService.updateProgramStageInstance(programStageInstance);
            }
            dataValue.setValue(value);
            dataValue.setLastUpdated(new Date());
            dataValueService.updateTrackedEntityDataValue(dataValue);
        }
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) DataValue(org.hisp.dhis.api.mobile.model.DataValue) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) Date(java.util.Date)

Example 3 with DataValue

use of org.hisp.dhis.api.mobile.model.DataValue in project dhis2-core by dhis2.

the class FacilityReportingServiceImpl method saveDataSetValues.

@Override
public void saveDataSetValues(OrganisationUnit unit, DataSetValue dataSetValue) throws NotAllowedException {
    org.hisp.dhis.dataset.DataSet dataSet = dataSetService.getDataSet(dataSetValue.getId());
    if (!dataSetAssociatedWithOrgUnit(unit, dataSet)) {
        log.info("Failed to save data value set for: " + unit.getName() + ", " + dataSet.getName() + " - Org unit and data set not associated.");
        throw NotAllowedException.INVALID_DATASET_ASSOCIATION;
    }
    Period period = getPeriod(dataSetValue.getPeriodName(), dataSet.getPeriodType());
    if (period == null) {
        log.info("Failed to save data value set for: " + unit.getName() + ", " + dataSet.getName() + " - Period not found.");
        throw NotAllowedException.INVALID_PERIOD;
    }
    log.info("Recieved data value set for: " + unit.getName() + ", " + dataSet.getName() + ", " + period.getIsoDate());
    Map<Integer, org.hisp.dhis.dataelement.DataElement> dataElementMap = getDataElementIdMapping(dataSet);
    for (DataValue dataValue : dataSetValue.getDataValues()) {
        org.hisp.dhis.dataelement.DataElement dataElement = dataElementMap.get(dataValue.getId());
        if (dataElement == null) {
            log.info("Data value submitted for data element " + dataValue.getId() + ", that is not in data set '" + dataSet.getName() + "'");
            continue;
        }
        if (StringUtils.isEmpty(dataValue.getValue())) {
            log.debug("Empty data value for data element " + dataValue.getId() + " not saved");
            continue;
        }
        saveValue(unit, period, dataElement, dataValue);
    }
    DataElementCategoryOptionCombo optionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
    CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, unit, optionCombo);
    if (registration != null) {
        registrationService.deleteCompleteDataSetRegistration(registration);
    }
    registration = new CompleteDataSetRegistration();
    registration.setDataSet(dataSet);
    registration.setPeriod(period);
    registration.setSource(unit);
    registration.setDate(new Date());
    registration.setStoredBy(currentUserService.getCurrentUser().getUsername());
    registrationService.saveCompleteDataSetRegistration(registration);
    log.info("Saved and registered data value set as complete: " + unit.getName() + ", " + dataSet.getName() + ", " + period.getIsoDate());
}
Also used : DataValue(org.hisp.dhis.api.mobile.model.DataValue) Period(org.hisp.dhis.period.Period) Date(java.util.Date) DataElement(org.hisp.dhis.api.mobile.model.DataElement) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 4 with DataValue

use of org.hisp.dhis.api.mobile.model.DataValue in project dhis2-core by dhis2.

the class FacilityReportingServiceImpl method getDataSetValues.

@Override
public DataSetValueList getDataSetValues(OrganisationUnit unit, DataSetList dataSetList) throws NotAllowedException {
    DataSetValueList dataSetValueList = new DataSetValueList();
    List<DataSet> dataSets = dataSetList.getCurrentDataSets();
    for (DataSet dataSet : dataSets) {
        log.info("Getting DataSetValue for: " + dataSet.getName());
        org.hisp.dhis.dataset.DataSet apiDataSet = dataSetService.getDataSet(dataSet.getId());
        Vector<String> periods = PeriodUtil.generatePeriods(dataSet.getPeriodType());
        if (periods != null) {
            for (int i = 0; i < periods.size(); i++) {
                Period period = getPeriod(periods.elementAt(i), apiDataSet.getPeriodType());
                if (period != null) {
                    Set<org.hisp.dhis.dataelement.DataElement> dataElements = apiDataSet.getDataElements();
                    Collection<org.hisp.dhis.datavalue.DataValue> dataValues = dataValueService.getDataValues(new DataExportParams().setDataElements(dataElements).setPeriods(Sets.newHashSet(period)).setOrganisationUnits(Sets.newHashSet(unit)));
                    if (dataValues != null && !dataValues.isEmpty()) {
                        DataSetValue dataSetValue = new DataSetValue();
                        dataSetValue.setId(dataSet.getId());
                        dataSetValue.setName(dataSet.getName());
                        dataSetValue.setPeriodName(periods.elementAt(i));
                        dataSetValue.setCompleted(true);
                        for (org.hisp.dhis.datavalue.DataValue dataValue : dataValues) {
                            DataValue dv = new DataValue();
                            dv.setCategoryOptComboID(dataValue.getCategoryOptionCombo().getId());
                            dv.setClientVersion(dataSet.getClientVersion());
                            dv.setId(dataValue.getDataElement().getId());
                            dv.setValue(dataValue.getValue());
                            dataSetValue.getDataValues().add(dv);
                        }
                        dataSetValueList.getDataSetValues().add(dataSetValue);
                    }
                }
            }
        }
    }
    log.info("Retrieved Data value set: " + unit.getName() + ", " + dataSetList.getName());
    return dataSetValueList;
}
Also used : DataSet(org.hisp.dhis.api.mobile.model.DataSet) DataValue(org.hisp.dhis.api.mobile.model.DataValue) DataSetValue(org.hisp.dhis.api.mobile.model.DataSetValue) Period(org.hisp.dhis.period.Period) DataElement(org.hisp.dhis.api.mobile.model.DataElement) DataExportParams(org.hisp.dhis.datavalue.DataExportParams) DataSetValueList(org.hisp.dhis.api.mobile.model.DataSetValueList)

Aggregations

DataValue (org.hisp.dhis.api.mobile.model.DataValue)4 Date (java.util.Date)2 DataElement (org.hisp.dhis.api.mobile.model.DataElement)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 Period (org.hisp.dhis.period.Period)2 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DataSet (org.hisp.dhis.api.mobile.model.DataSet)1 DataSetValue (org.hisp.dhis.api.mobile.model.DataSetValue)1 DataSetValueList (org.hisp.dhis.api.mobile.model.DataSetValueList)1 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)1 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)1 DataExportParams (org.hisp.dhis.datavalue.DataExportParams)1 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)1 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)1 ProgramStageSection (org.hisp.dhis.program.ProgramStageSection)1