use of org.hisp.dhis.dxf2.events.event.DataValue in project dhis2-core by dhis2.
the class DataValueController method getDataValue.
// ---------------------------------------------------------------------
// GET
// ---------------------------------------------------------------------
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<String> getDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, Model model, HttpServletResponse response) throws WebMessageException {
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = getAndValidateDataElement(de);
DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
Period period = getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
// ---------------------------------------------------------------------
// Get data value
// ---------------------------------------------------------------------
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
if (dataValue == null) {
throw new WebMessageException(WebMessageUtils.conflict("Data value does not exist"));
}
List<String> value = new ArrayList<>();
value.add(dataValue.getValue());
return value;
}
use of org.hisp.dhis.dxf2.events.event.DataValue in project dhis2-core by dhis2.
the class DataValueController method getDataValueFile.
// ---------------------------------------------------------------------
// GET file
// ---------------------------------------------------------------------
@RequestMapping(value = "/files", method = RequestMethod.GET)
public void getDataValueFile(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = getAndValidateDataElement(de);
if (!dataElement.isFileType()) {
throw new WebMessageException(WebMessageUtils.conflict("DataElement must be of type file"));
}
DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, false);
DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
Period period = getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
// ---------------------------------------------------------------------
// Get data value
// ---------------------------------------------------------------------
DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
if (dataValue == null) {
throw new WebMessageException(WebMessageUtils.conflict("Data value does not exist"));
}
// ---------------------------------------------------------------------
// Get file resource
// ---------------------------------------------------------------------
String uid = dataValue.getValue();
FileResource fileResource = fileResourceService.getFileResource(uid);
if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
throw new WebMessageException(WebMessageUtils.notFound("A data value file resource with id " + uid + " does not exist."));
}
FileResourceStorageStatus storageStatus = fileResource.getStorageStatus();
if (storageStatus != FileResourceStorageStatus.STORED) {
// Special case:
// The FileResource exists and has been tied to this DataValue, however, the underlying file
// content is still not stored to the (most likely external) file store provider.
// HTTP 409, for lack of a more suitable status code
WebMessage webMessage = WebMessageUtils.conflict("The content is being processed and is not available yet. Try again later.", "The content requested is in transit to the file store and will be available at a later time.");
webMessage.setResponse(new FileResourceWebMessageResponse(fileResource));
throw new WebMessageException(webMessage);
}
ByteSource content = fileResourceService.getFileResourceContent(fileResource);
if (content == null) {
throw new WebMessageException(WebMessageUtils.notFound("The referenced file could not be found"));
}
// ---------------------------------------------------------------------
// Attempt to build signed URL request for content and redirect
// ---------------------------------------------------------------------
URI signedGetUri = fileResourceService.getSignedGetFileResourceContentUri(uid);
if (signedGetUri != null) {
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
response.setHeader(HttpHeaders.LOCATION, signedGetUri.toASCIIString());
return;
}
// ---------------------------------------------------------------------
// Build response and return
// ---------------------------------------------------------------------
response.setContentType(fileResource.getContentType());
response.setContentLength(new Long(fileResource.getContentLength()).intValue());
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "filename=" + fileResource.getName());
// ---------------------------------------------------------------------
// Request signing is not available, stream content back to client
// ---------------------------------------------------------------------
InputStream inputStream = null;
try {
inputStream = content.openStream();
IOUtils.copy(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new WebMessageException(WebMessageUtils.error("Failed fetching the file from storage", "There was an exception when trying to fetch the file from the storage backend. " + "Depending on the provider the root cause could be network or file system related."));
} finally {
IOUtils.closeQuietly(inputStream);
}
}
use of org.hisp.dhis.dxf2.events.event.DataValue in project dhis2-core by dhis2.
the class AbstractEventService method updateEvent.
private ImportSummary updateEvent(Event event, User user, boolean singleValue, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(event.getEvent());
ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(event.getEvent());
if (programStageInstance == null) {
importSummary.getConflicts().add(new ImportConflict("Invalid Event ID.", event.getEvent()));
return importSummary.incrementIgnored();
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
if (organisationUnit == null) {
organisationUnit = programStageInstance.getOrganisationUnit();
}
Date executionDate = new Date();
if (event.getEventDate() != null) {
executionDate = DateUtils.parseDate(event.getEventDate());
programStageInstance.setExecutionDate(executionDate);
}
Date dueDate = new Date();
if (event.getDueDate() != null) {
dueDate = DateUtils.parseDate(event.getDueDate());
}
String storedBy = getStoredBy(event, null, user);
programStageInstance.setStoredBy(storedBy);
String completedBy = getCompletedBy(event, null, user);
if (event.getStatus() == EventStatus.ACTIVE) {
programStageInstance.setStatus(EventStatus.ACTIVE);
programStageInstance.setCompletedBy(null);
programStageInstance.setCompletedDate(null);
} else if (programStageInstance.getStatus() != event.getStatus() && event.getStatus() == EventStatus.COMPLETED) {
programStageInstance.setStatus(EventStatus.COMPLETED);
programStageInstance.setCompletedBy(completedBy);
programStageInstance.setCompletedDate(executionDate);
if (programStageInstance.isCompleted()) {
programStageInstanceService.completeProgramStageInstance(programStageInstance, importOptions.isSkipNotifications(), i18nManager.getI18nFormat());
}
} else if (event.getStatus() == EventStatus.SKIPPED) {
programStageInstance.setStatus(EventStatus.SKIPPED);
} else if (event.getStatus() == EventStatus.SCHEDULE) {
programStageInstance.setStatus(EventStatus.SCHEDULE);
}
programStageInstance.setDueDate(dueDate);
programStageInstance.setOrganisationUnit(organisationUnit);
if (!singleValue) {
if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
if (event.getCoordinate() != null && event.getCoordinate().isValid()) {
programStageInstance.setLatitude(event.getCoordinate().getLatitude());
programStageInstance.setLongitude(event.getCoordinate().getLongitude());
} else {
programStageInstance.setLatitude(null);
programStageInstance.setLongitude(null);
}
}
}
Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
validateExpiryDays(event, program, programStageInstance);
if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
if (attributeOptionCombo == null) {
importSummary.getConflicts().add(new ImportConflict("Invalid attribute option combo identifier:", event.getAttributeCategoryOptions()));
return importSummary.incrementIgnored();
}
programStageInstance.setAttributeOptionCombo(attributeOptionCombo);
}
programStageInstance.setDeleted(event.isDeleted());
programStageInstanceService.updateProgramStageInstance(programStageInstance);
updateTrackedEntityInstance(programStageInstance);
saveTrackedEntityComment(programStageInstance, event, storedBy);
Set<TrackedEntityDataValue> dataValues = new HashSet<>(dataValueService.getTrackedEntityDataValues(programStageInstance));
Map<String, TrackedEntityDataValue> existingDataValues = getDataElementDataValueMap(dataValues);
for (DataValue value : event.getDataValues()) {
DataElement dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), value.getDataElement());
TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
if (!validateDataValue(dataElement, value.getValue(), importSummary)) {
continue;
}
if (dataValue != null) {
if (StringUtils.isEmpty(value.getValue()) && dataElement.isFileType() && !StringUtils.isEmpty(dataValue.getValue())) {
fileResourceService.deleteFileResource(dataValue.getValue());
}
dataValue.setValue(value.getValue());
dataValue.setProvidedElsewhere(value.getProvidedElsewhere());
dataValueService.updateTrackedEntityDataValue(dataValue);
dataValues.remove(dataValue);
} else {
TrackedEntityDataValue existingDataValue = existingDataValues.get(value.getDataElement());
saveDataValue(programStageInstance, event.getStoredBy(), dataElement, value.getValue(), value.getProvidedElsewhere(), existingDataValue, null);
}
}
if (!singleValue) {
dataValues.forEach(dataValueService::deleteTrackedEntityDataValue);
}
importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
return importSummary;
}
use of org.hisp.dhis.dxf2.events.event.DataValue in project dhis2-core by dhis2.
the class AbstractEventService method saveEvent.
private ImportSummary saveEvent(Program program, ProgramInstance programInstance, ProgramStage programStage, ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event, User user, ImportOptions importOptions) {
Assert.notNull(program, "Program cannot be null");
Assert.notNull(programInstance, "Program instance cannot be null");
Assert.notNull(programStage, "Program stage cannot be null");
ImportSummary importSummary = new ImportSummary(event.getEvent());
if (importOptions == null) {
importOptions = new ImportOptions();
}
boolean existingEvent = programStageInstance != null;
boolean dryRun = importOptions.isDryRun();
// = new Date();
Date executionDate = null;
if (event.getEventDate() != null) {
executionDate = DateUtils.parseDate(event.getEventDate());
}
Date dueDate = new Date();
if (event.getDueDate() != null) {
dueDate = DateUtils.parseDate(event.getDueDate());
}
String storedBy = getStoredBy(event, importSummary, user);
String completedBy = getCompletedBy(event, importSummary, user);
DataElementCategoryOptionCombo aoc = null;
if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
try {
aoc = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
} catch (IllegalQueryException ex) {
importSummary.getConflicts().add(new ImportConflict(ex.getMessage(), event.getAttributeCategoryOptions()));
}
} else {
aoc = categoryService.getDefaultDataElementCategoryOptionCombo();
}
if (!dryRun) {
if (programStageInstance == null) {
programStageInstance = createProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, event.getEvent(), aoc, importOptions);
} else {
updateProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, programStageInstance, aoc, importOptions);
}
updateTrackedEntityInstance(programStageInstance);
saveTrackedEntityComment(programStageInstance, event, storedBy);
importSummary.setReference(programStageInstance.getUid());
}
Map<String, TrackedEntityDataValue> dataElementValueMap = Maps.newHashMap();
if (existingEvent) {
dataElementValueMap = getDataElementDataValueMap(dataValueService.getTrackedEntityDataValues(programStageInstance));
}
for (DataValue dataValue : event.getDataValues()) {
DataElement dataElement;
if (dataElementValueMap.containsKey(dataValue.getDataElement())) {
dataElement = dataElementValueMap.get(dataValue.getDataElement()).getDataElement();
} else {
dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), dataValue.getDataElement());
}
if (dataElement != null) {
if (validateDataValue(dataElement, dataValue.getValue(), importSummary)) {
String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
if (!dryRun) {
TrackedEntityDataValue existingDataValue = dataElementValueMap.get(dataValue.getDataElement());
saveDataValue(programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere(), existingDataValue, importSummary);
}
}
} else {
importSummary.getConflicts().add(new ImportConflict("dataElement", dataValue.getDataElement() + " is not a valid data element"));
importSummary.getImportCount().incrementIgnored();
}
}
importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
return importSummary;
}
use of org.hisp.dhis.dxf2.events.event.DataValue in project dhis2-core by dhis2.
the class PdfDataEntryFormUtil method getDataValueSet.
/**
* Creates data value set from Input Stream (PDF) for PDF data import
*/
public static DataValueSet getDataValueSet(InputStream in) {
PdfReader reader = null;
DataValueSet dataValueSet = new DataValueSet();
List<org.hisp.dhis.dxf2.datavalue.DataValue> dataValueList = new ArrayList<>();
try {
reader = new PdfReader(in);
AcroFields form = reader.getAcroFields();
if (form != null) {
// Process OrgUnitUID and PeriodID from the PDF Form
String orgUnitUid = form.getField(PdfDataEntryFormUtil.LABELCODE_ORGID).trim();
String periodId = form.getField(PdfDataEntryFormUtil.LABELCODE_PERIODID).trim();
if (periodId == null || periodId.isEmpty()) {
throw new InvalidIdentifierReferenceException(ERROR_EMPTY_PERIOD);
}
if (orgUnitUid == null || orgUnitUid.isEmpty()) {
throw new InvalidIdentifierReferenceException(ERROR_EMPTY_ORG_UNIT);
}
Period period = PeriodType.getPeriodFromIsoString(periodId);
if (period == null) {
throw new InvalidIdentifierReferenceException(ERROR_INVALID_PERIOD + periodId);
}
// Loop Through the Fields and get data.
@SuppressWarnings("unchecked") Set<String> fldNames = form.getFields().keySet();
for (String fldName : fldNames) {
if (fldName.startsWith(PdfDataEntryFormUtil.LABELCODE_DATAENTRYTEXTFIELD)) {
String[] strArrFldName = fldName.split("_");
org.hisp.dhis.dxf2.datavalue.DataValue dataValue = new org.hisp.dhis.dxf2.datavalue.DataValue();
dataValue.setDataElement(strArrFldName[1]);
dataValue.setCategoryOptionCombo(strArrFldName[2]);
dataValue.setOrgUnit(orgUnitUid);
dataValue.setPeriod(period.getIsoDate());
dataValue.setValue(fieldValueFormat(strArrFldName, form.getField(fldName)));
dataValue.setStoredBy(DATAVALUE_IMPORT_STOREBY);
dataValue.setComment(DATAVALUE_IMPORT_COMMENT);
dataValue.setFollowup(false);
dataValue.setLastUpdated(DateUtils.getMediumDateString());
dataValueList.add(dataValue);
}
}
dataValueSet.setDataValues(dataValueList);
} else {
throw new RuntimeException("Could not generate PDF AcroFields form from input");
}
} catch (Exception ex) {
throw new RuntimeException(ex);
} finally {
if (reader != null) {
reader.close();
}
}
return dataValueSet;
}
Aggregations