Search in sources :

Example 6 with STOREDBY

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);
    }
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) Event(org.hisp.dhis.dxf2.events.event.Event) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 7 with STOREDBY

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);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.datavalue.DataValue) AuthorizationException(org.jclouds.rest.AuthorizationException) FileResource(org.hisp.dhis.fileresource.FileResource) Period(org.hisp.dhis.period.Period) Date(java.util.Date) DataElement(org.hisp.dhis.dataelement.DataElement) FileResourceRetentionStrategy(org.hisp.dhis.fileresource.FileResourceRetentionStrategy) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo)

Example 8 with STOREDBY

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)))));
}
Also used : DataValue(org.hisp.dhis.dxf2.events.event.DataValue) WorkContext(org.hisp.dhis.dxf2.events.importer.context.WorkContext) Event(org.hisp.dhis.dxf2.events.event.Event) Test(org.junit.jupiter.api.Test)

Example 9 with STOREDBY

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");
}
Also used : DataValue(org.hisp.dhis.dxf2.datavalue.DataValue) DataSetContext(org.hisp.dhis.dxf2.datavalueset.ImportContext.DataSetContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DataValueContext(org.hisp.dhis.dxf2.datavalueset.ImportContext.DataValueContext) Test(org.junit.jupiter.api.Test)

Example 10 with STOREDBY

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IdScheme(org.hisp.dhis.common.IdScheme) Date(java.util.Date) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) HashSet(java.util.HashSet)

Aggregations

Date (java.util.Date)9 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)8 DataElement (org.hisp.dhis.dataelement.DataElement)6 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)6 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)6 Period (org.hisp.dhis.period.Period)6 DataValue (org.hisp.dhis.dxf2.events.event.DataValue)5 DataSet (org.hisp.dhis.dataset.DataSet)4 DataValue (org.hisp.dhis.datavalue.DataValue)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 IdScheme (org.hisp.dhis.common.IdScheme)3 Event (org.hisp.dhis.dxf2.events.event.Event)3 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CategoryOptionCombo (org.hisp.dhis.category.CategoryOptionCombo)2 DhisApiVersion (org.hisp.dhis.common.DhisApiVersion)2 CompleteDataSetRegistration (org.hisp.dhis.dataset.CompleteDataSetRegistration)2