Search in sources :

Example 1 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 saveDataValue.

// ---------------------------------------------------------------------
// POST
// ---------------------------------------------------------------------
@PreAuthorize("hasRole('ALL') or hasRole('F_DATAVALUE_ADD')")
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public void saveDataValue(@RequestParam String de, @RequestParam(required = false) String co, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String value, @RequestParam(required = false) String comment, @RequestParam(required = false) boolean followUp, HttpServletResponse response) throws WebMessageException {
    boolean strictPeriods = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_PERIODS);
    boolean strictCategoryOptionCombos = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_CATEGORY_OPTION_COMBOS);
    boolean strictOrgUnits = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_STRICT_ORGANISATION_UNITS);
    boolean requireCategoryOptionCombo = (Boolean) systemSettingManager.getSystemSetting(SettingKey.DATA_IMPORT_REQUIRE_CATEGORY_OPTION_COMBO);
    // ---------------------------------------------------------------------
    // Input validation
    // ---------------------------------------------------------------------
    DataElement dataElement = getAndValidateDataElement(de);
    DataElementCategoryOptionCombo categoryOptionCombo = getAndValidateCategoryOptionCombo(co, requireCategoryOptionCombo);
    DataElementCategoryOptionCombo attributeOptionCombo = getAndValidateAttributeOptionCombo(cc, cp);
    Period period = getAndValidatePeriod(pe);
    OrganisationUnit organisationUnit = getAndValidateOrganisationUnit(ou);
    validateInvalidFuturePeriod(period, dataElement);
    validateAttributeOptionComboWithOrgUnitAndPeriod(attributeOptionCombo, organisationUnit, period);
    String valueValid = ValidationUtils.dataValueIsValid(value, dataElement);
    if (valueValid != null) {
        throw new WebMessageException(WebMessageUtils.conflict("Invalid value: " + value + ", must match data element type: " + dataElement.getValueType()));
    }
    String commentValid = ValidationUtils.commentIsValid(comment);
    if (commentValid != null) {
        throw new WebMessageException(WebMessageUtils.conflict("Invalid comment: " + comment));
    }
    OptionSet optionSet = dataElement.getOptionSet();
    if (!Strings.isNullOrEmpty(value) && optionSet != null && !optionSet.getOptionCodesAsSet().contains(value)) {
        throw new WebMessageException(WebMessageUtils.conflict("Data value is not a valid option of the data element option set: " + dataElement.getUid()));
    }
    if (strictPeriods && !dataElement.getPeriodTypes().contains(period.getPeriodType())) {
        throw new WebMessageException(WebMessageUtils.conflict("Period type of period: " + period.getIsoDate() + " not valid for data element: " + dataElement.getUid()));
    }
    if (strictCategoryOptionCombos && !dataElement.getCategoryOptionCombos().contains(categoryOptionCombo)) {
        throw new WebMessageException(WebMessageUtils.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(WebMessageUtils.conflict("Data element: " + dataElement.getUid() + " must be assigned through data sets to organisation unit: " + organisationUnit.getUid()));
    }
    // ---------------------------------------------------------------------
    // Locking validation
    // ---------------------------------------------------------------------
    validateDataSetNotLocked(dataElement, period, organisationUnit, attributeOptionCombo);
    // ---------------------------------------------------------------------
    // Period validation
    // ---------------------------------------------------------------------
    validateDataInputPeriodForDataElementAndPeriod(dataElement, period);
    // ---------------------------------------------------------------------
    // Assemble and save data value
    // ---------------------------------------------------------------------
    String storedBy = currentUserService.getCurrentUsername();
    Date now = new Date();
    DataValue dataValue = dataValueService.getDataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo);
    FileResource fileResource = null;
    if (dataValue == null) {
        if (dataElement.getValueType() == ValueType.FILE_RESOURCE) {
            if (value != null) {
                fileResource = fileResourceService.getFileResource(value);
                if (fileResource == null || fileResource.getDomain() != FileResourceDomain.DATA_VALUE) {
                    throw new WebMessageException(WebMessageUtils.notFound(FileResource.class, value));
                }
                if (fileResource.isAssigned()) {
                    throw new WebMessageException(WebMessageUtils.conflict("File resource already assigned or linked to another data value"));
                }
                fileResource.setAssigned(true);
            } else {
                throw new WebMessageException(WebMessageUtils.conflict("Missing parameter 'value'"));
            }
        }
        dataValue = new DataValue(dataElement, period, organisationUnit, categoryOptionCombo, attributeOptionCombo, StringUtils.trimToNull(value), storedBy, now, StringUtils.trimToNull(comment));
        dataValueService.addDataValue(dataValue);
    } else {
        if (value == null && ValueType.TRUE_ONLY.equals(dataElement.getValueType())) {
            if (comment == null) {
                dataValueService.deleteDataValue(dataValue);
                return;
            } else {
                value = "false";
            }
        }
        if (dataElement.isFileType()) {
            fileResourceService.deleteFileResource(dataValue.getValue());
        }
        if (value != null) {
            dataValue.setValue(StringUtils.trimToNull(value));
        }
        if (comment != null) {
            dataValue.setComment(StringUtils.trimToNull(comment));
        }
        if (followUp) {
            dataValue.toggleFollowUp();
        }
        dataValue.setLastUpdated(now);
        dataValue.setStoredBy(storedBy);
        dataValueService.updateDataValue(dataValue);
    }
    if (fileResource != null) {
        fileResourceService.updateFileResource(fileResource);
    }
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) DataValue(org.hisp.dhis.datavalue.DataValue) FileResource(org.hisp.dhis.fileresource.FileResource) Period(org.hisp.dhis.period.Period) OptionSet(org.hisp.dhis.option.OptionSet) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with STOREDBY

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.

the class CompleteDataSetRegistrationController method registerCompleteDataSet.

private CompleteDataSetRegistration registerCompleteDataSet(DataSet dataSet, Period period, OrganisationUnit orgUnit, DataElementCategoryOptionCombo attributeOptionCombo, String storedBy, Date completionDate) throws WebMessageException {
    I18nFormat format = i18nManager.getI18nFormat();
    if (dataSet == null) {
        throw new WebMessageException(WebMessageUtils.conflict("dataSet can not be null."));
    }
    if (period == null) {
        throw new WebMessageException(WebMessageUtils.conflict("period can not be null"));
    }
    if (orgUnit == null) {
        throw new WebMessageException(WebMessageUtils.conflict("organisationUnit can not be null"));
    }
    if (attributeOptionCombo == null) {
        throw new WebMessageException(WebMessageUtils.conflict("attributeOptionCombo can not be null"));
    }
    CompleteDataSetRegistration registration = registrationService.getCompleteDataSetRegistration(dataSet, period, orgUnit, attributeOptionCombo);
    if (registration == null) {
        registration = new CompleteDataSetRegistration();
        registration.setDataSet(dataSet);
        registration.setPeriod(period);
        registration.setSource(orgUnit);
        registration.setAttributeOptionCombo(attributeOptionCombo);
        registration.setDate(completionDate != null ? completionDate : new Date());
        registration.setStoredBy(storedBy != null ? storedBy : currentUserService.getCurrentUsername());
        registration.setPeriodName(format.formatPeriod(registration.getPeriod()));
        registrationService.saveCompleteDataSetRegistration(registration);
    } else {
        registration.setDate(completionDate != null ? completionDate : new Date());
        registration.setStoredBy(storedBy != null ? storedBy : currentUserService.getCurrentUsername());
        registration.setPeriodName(format.formatPeriod(registration.getPeriod()));
        registrationService.updateCompleteDataSetRegistration(registration);
    }
    return registration;
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) I18nFormat(org.hisp.dhis.i18n.I18nFormat) Date(java.util.Date)

Example 3 with STOREDBY

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.

the class CompleteDataSetRegistrationController method saveCompleteDataSetRegistration.

// Legacy (<= V25)
@ApiVersion({ DhisApiVersion.V23, DhisApiVersion.V24, DhisApiVersion.V25 })
@RequestMapping(method = RequestMethod.POST, produces = "text/plain")
public void saveCompleteDataSetRegistration(@RequestParam String ds, @RequestParam String pe, @RequestParam String ou, @RequestParam(required = false) String cc, @RequestParam(required = false) String cp, @RequestParam(required = false) Date cd, @RequestParam(required = false) String sb, @RequestParam(required = false) boolean multiOu, HttpServletResponse response) throws WebMessageException {
    DataSet dataSet = dataSetService.getDataSet(ds);
    if (dataSet == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal data set identifier: " + ds));
    }
    Period period = PeriodType.getPeriodFromIsoString(pe);
    if (period == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal period identifier: " + pe));
    }
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou);
    if (organisationUnit == null) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal organisation unit identifier: " + ou));
    }
    DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(cc, cp, false);
    if (attributeOptionCombo == null) {
        return;
    }
    if (dataSetService.isLocked(dataSet, period, organisationUnit, attributeOptionCombo, null, multiOu)) {
        throw new WebMessageException(WebMessageUtils.conflict("Data set is locked: " + ds));
    }
    // ---------------------------------------------------------------------
    // Register as completed data set
    // ---------------------------------------------------------------------
    Set<OrganisationUnit> children = organisationUnit.getChildren();
    String storedBy = (sb == null) ? currentUserService.getCurrentUsername() : sb;
    Date completionDate = (cd == null) ? new Date() : cd;
    List<CompleteDataSetRegistration> registrations = new ArrayList<>();
    if (!multiOu) {
        CompleteDataSetRegistration completeDataSetRegistration = registerCompleteDataSet(dataSet, period, organisationUnit, attributeOptionCombo, storedBy, completionDate);
        if (completeDataSetRegistration != null) {
            registrations.add(completeDataSetRegistration);
        }
    } else {
        addRegistrationsForOrgUnits(registrations, Sets.union(children, Sets.newHashSet(organisationUnit)), dataSet, period, attributeOptionCombo, storedBy, completionDate);
    }
    registrationService.saveCompleteDataSetRegistrations(registrations, true);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) CompleteDataSetRegistration(org.hisp.dhis.dataset.CompleteDataSetRegistration) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) Date(java.util.Date) DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with STOREDBY

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.

the class AbstractEnrollmentService method saveTrackedEntityComment.

private void saveTrackedEntityComment(ProgramInstance programInstance, Enrollment enrollment) {
    String storedBy = currentUserService.getCurrentUsername();
    for (Note note : enrollment.getNotes()) {
        TrackedEntityComment comment = new TrackedEntityComment();
        comment.setCreator(storedBy);
        comment.setCreatedDate(new Date());
        comment.setCommentText(note.getValue());
        commentService.addTrackedEntityComment(comment);
        programInstance.getComments().add(comment);
        programInstanceService.updateProgramInstance(programInstance);
    }
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Note(org.hisp.dhis.dxf2.events.event.Note) Date(java.util.Date)

Example 5 with STOREDBY

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.STOREDBY in project dhis2-core by dhis2.

the class AbstractEnrollmentService method addEnrollment.

private ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions, org.hisp.dhis.trackedentity.TrackedEntityInstance daoTrackedEntityInstance, boolean handleEvents) {
    importOptions = updateImportOptions(importOptions);
    String storedBy = !StringUtils.isEmpty(enrollment.getStoredBy()) && enrollment.getStoredBy().length() < 31 ? enrollment.getStoredBy() : (importOptions.getUser() == null || StringUtils.isEmpty(importOptions.getUser().getUsername()) ? "system-process" : importOptions.getUser().getUsername());
    if (daoTrackedEntityInstance == null) {
        daoTrackedEntityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance(), importOptions.getUser());
    }
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
    ImportSummary importSummary = validateRequest(program, daoTrackedEntityInstance, enrollment, organisationUnit, importOptions);
    if (importSummary.getStatus() != ImportStatus.SUCCESS) {
        return importSummary;
    }
    List<String> errors = trackerAccessManager.canCreate(importOptions.getUser(), new ProgramInstance(program, daoTrackedEntityInstance, organisationUnit), false);
    if (!errors.isEmpty()) {
        return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
    }
    if (enrollment.getStatus() == null) {
        enrollment.setStatus(EnrollmentStatus.ACTIVE);
    }
    ProgramStatus programStatus = enrollment.getStatus() == EnrollmentStatus.ACTIVE ? ProgramStatus.ACTIVE : enrollment.getStatus() == EnrollmentStatus.COMPLETED ? ProgramStatus.COMPLETED : ProgramStatus.CANCELLED;
    ProgramInstance programInstance = programInstanceService.prepareProgramInstance(daoTrackedEntityInstance, program, programStatus, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
    if (programStatus == ProgramStatus.COMPLETED || programStatus == ProgramStatus.CANCELLED) {
        Date date = enrollment.getCompletedDate();
        if (date == null) {
            date = new Date();
        }
        String user = enrollment.getCompletedBy();
        if (user == null) {
            user = importOptions.getUser().getUsername();
        }
        programInstance.setCompletedBy(user);
        programInstance.setEndDate(date);
    }
    programInstanceService.addProgramInstance(programInstance, importOptions.getUser());
    importSummary = validateProgramInstance(program, programInstance, enrollment, importSummary);
    if (importSummary.getStatus() != ImportStatus.SUCCESS) {
        return importSummary;
    }
    // -----------------------------------------------------------------
    // Send enrollment notifications (if any)
    // -----------------------------------------------------------------
    eventPublisher.publishEvent(new ProgramEnrollmentNotificationEvent(this, programInstance.getId()));
    eventPublisher.publishEvent(new EnrollmentEvaluationEvent(this, programInstance.getId()));
    eventPublisher.publishEvent(new TrackerEnrollmentWebHookEvent(this, programInstance.getUid()));
    updateFeatureType(program, enrollment, programInstance);
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstance.setFollowup(enrollment.getFollowup());
    programInstance.setStoredBy(storedBy);
    programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
    trackerOwnershipAccessManager.assignOwnership(daoTrackedEntityInstance, program, organisationUnit, true, true);
    saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
    importSummary.setReference(programInstance.getUid());
    enrollment.setEnrollment(programInstance.getUid());
    importSummary.getImportCount().incrementImported();
    if (handleEvents) {
        importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
    } else {
        for (Event event : enrollment.getEvents()) {
            event.setEnrollment(enrollment.getEnrollment());
            event.setProgram(programInstance.getProgram().getUid());
            event.setTrackedEntityInstance(enrollment.getTrackedEntityInstance());
        }
    }
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) EnrollmentEvaluationEvent(org.hisp.dhis.programrule.engine.EnrollmentEvaluationEvent) Event(org.hisp.dhis.dxf2.events.event.Event) TrackerEnrollmentWebHookEvent(org.hisp.dhis.programrule.engine.TrackerEnrollmentWebHookEvent) ProgramEnrollmentNotificationEvent(org.hisp.dhis.program.notification.event.ProgramEnrollmentNotificationEvent) Date(java.util.Date) ProgramStatus(org.hisp.dhis.program.ProgramStatus)

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