use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.
the class EventAnalyticsServiceTest method parseEventData.
// -------------------------------------------------------------------------
// Internal Logic
// -------------------------------------------------------------------------
private void parseEventData(List<String[]> lines) {
String storedBy = "johndoe";
for (String[] line : lines) {
Event event = new Event();
event.setProgram(line[0]);
event.setProgramStage(line[1]);
DataValue dataValue = new DataValue();
dataValue.setDataElement(line[2]);
dataValue.setValue(line[6]);
dataValue.setStoredBy(storedBy);
event.setEventDate(line[3]);
event.setOrgUnit(line[4]);
event.setDataValues(Sets.newHashSet(dataValue));
event.setCompletedDate(line[3]);
event.setTrackedEntityInstance(line[5]);
event.setStatus(EventStatus.COMPLETED);
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.
the class DataValueController method saveDataValueInternal.
private void saveDataValueInternal(String de, String co, String cc, String cp, String pe, String ou, String ds, String value, String comment, Boolean followUp, boolean force, User currentUser) throws WebMessageException {
boolean strictPeriods = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS);
boolean strictCategoryOptionCombos = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS);
boolean strictOrgUnits = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS);
boolean requireCategoryOptionCombo = systemSettingManager.getBoolSetting(SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO);
FileResourceRetentionStrategy retentionStrategy = systemSettingManager.getSystemSetting(SettingKey.FILE_RESOURCE_RETENTION_STRATEGY, FileResourceRetentionStrategy.class);
// ---------------------------------------------------------------------
// Input validation
// ---------------------------------------------------------------------
DataElement dataElement = dataValueValidation.getAndValidateDataElement(de);
CategoryOptionCombo categoryOptionCombo = dataValueValidation.getAndValidateCategoryOptionCombo(co, requireCategoryOptionCombo);
CategoryOptionCombo attributeOptionCombo = dataValueValidation.getAndValidateAttributeOptionCombo(cc, cp);
Period period = dataValueValidation.getAndValidatePeriod(pe);
OrganisationUnit organisationUnit = dataValueValidation.getAndValidateOrganisationUnit(ou);
dataValueValidation.validateOrganisationUnitPeriod(organisationUnit, period);
DataSet dataSet = dataValueValidation.getAndValidateOptionalDataSet(ds, dataElement);
dataValueValidation.validateInvalidFuturePeriod(period, dataElement);
dataValueValidation.validateAttributeOptionCombo(attributeOptionCombo, period, dataSet, dataElement);
value = dataValueValidation.validateAndNormalizeDataValue(value, dataElement);
dataValueValidation.validateComment(comment);
dataValueValidation.validateOptionSet(value, dataElement.getOptionSet(), dataElement);
dataValueValidation.checkCategoryOptionComboAccess(currentUser, categoryOptionCombo);
dataValueValidation.checkCategoryOptionComboAccess(currentUser, attributeOptionCombo);
if (strictPeriods && !dataElement.getPeriodTypes().contains(period.getPeriodType())) {
throw new WebMessageException(conflict("Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid()));
}
if (strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains(categoryOptionCombo)) {
throw new WebMessageException(conflict("Category option combo: " + categoryOptionCombo.getUid() + " must be part of category combo of data element: " + dataElement.getUid()));
}
if (strictOrgUnits && !organisationUnit.hasDataElement(dataElement)) {
throw new WebMessageException(conflict("Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid()));
}
if (!inputUtils.canForceDataInput(currentUser, force)) {
dataValueValidation.validateDataSetNotLocked(currentUser, dataElement, period, dataSet, organisationUnit, attributeOptionCombo);
}
// ---------------------------------------------------------------------
// Period validation
// ---------------------------------------------------------------------
dataValueValidation.validateDataInputPeriodForDataElementAndPeriod(dataElement, dataSet, period);
// ---------------------------------------------------------------------
// Assemble and save data value
// ---------------------------------------------------------------------
String storedBy = currentUser.getUsername();
Date now = new Date();
DataValue persistedDataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
FileResource fileResource = null;
if (persistedDataValue == null) {
if (dataElement.getValueType().isFile()) {
fileResource = dataValueValidation.validateAndSetAssigned(value, dataElement.getValueType(), dataElement.getValueTypeOptions());
}
DataValue newValue = new DataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, StringUtils.trimToNull(value), storedBy, now, StringUtils.trimToNull(comment));
newValue.setFollowup(followUp);
dataValueService.addDataValue(newValue);
} else {
if (value == null && comment == null && followUp == null && ValueType.TRUE_ONLY.equals(dataElement.getValueType())) {
dataValueService.deleteDataValue(persistedDataValue);
return;
}
if (dataElement.getValueType().isFile()) {
fileResource = dataValueValidation.validateAndSetAssigned(value, dataElement.getValueType(), dataElement.getValueTypeOptions());
}
if (dataElement.isFileType() && retentionStrategy == FileResourceRetentionStrategy.NONE) {
try {
fileResourceService.deleteFileResource(persistedDataValue.getValue());
} catch (AuthorizationException exception) {
// If we fail to delete the fileResource now, mark it as
// unassigned for removal later
fileResourceService.getFileResource(persistedDataValue.getValue()).setAssigned(false);
}
persistedDataValue.setValue(StringUtils.EMPTY);
}
if (value != null) {
persistedDataValue.setValue(StringUtils.trimToNull(value));
}
if (comment != null) {
persistedDataValue.setComment(StringUtils.trimToNull(comment));
}
if (followUp != null) {
persistedDataValue.toggleFollowUp();
}
persistedDataValue.setLastUpdated(now);
persistedDataValue.setStoredBy(storedBy);
dataValueService.updateDataValue(persistedDataValue);
}
if (fileResource != null) {
fileResourceService.updateFileResource(fileResource);
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.
the class EventStoredByPreProcessorTest method t1.
@Test
void t1() {
WorkContext ctx = WorkContext.builder().importOptions(ImportOptions.getDefaultImportOptions()).build();
Event event = new Event();
event.setDataValues(Sets.newHashSet(new DataValue("aaa", "one"), new DataValue("bbb", "two")));
preProcessor.process(event, ctx);
assertThat(event.getStoredBy(), is(FALLBACK_USERNAME));
assertThat(event.getDataValues(), hasItems(allOf(Matchers.hasProperty("storedBy", is(FALLBACK_USERNAME)))));
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.
the class DataValueSetImportValidatorTest method testCheckDataValueStoredByIsValid.
@Test
void testCheckDataValueStoredByIsValid() {
DataValue dataValue = createRandomDataValue();
char[] chars = new char[300];
Arrays.fill(chars, 'x');
String storedBy = new String(chars);
dataValue.setStoredBy(storedBy);
DataValueContext valueContext = createDataValueContext(dataValue).build();
DataSetContext dataSetContext = createMinimalDataSetContext().build();
ImportContext context = createMinimalImportContext(valueContext).build();
assertTrue(validator.skipDataValue(dataValue, context, dataSetContext, valueContext));
assertConflict(ErrorCode.E7637, "Invalid storedBy: stored_by_length_greater_than_max_length", context, "stored_by_length_greater_than_max_length");
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY 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;
}
Aggregations