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);
}
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);
}
}
}
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());
}
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;
}
Aggregations