Search in sources :

Example 11 with Attribute

use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.

the class EventController method getEventsGrid.

// -------------------------------------------------------------------------
// READ
// -------------------------------------------------------------------------
@RequestMapping(value = "/query", method = RequestMethod.GET, produces = { ContextUtils.CONTENT_TYPE_JSON, ContextUtils.CONTENT_TYPE_JAVASCRIPT })
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD') or hasRole('F_TRACKED_ENTITY_DATAVALUE_READ')")
@ResponseBody
public Grid getEventsGrid(@RequestParam(required = false) String program, @RequestParam(required = false) String programStage, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String orgUnit, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Date dueDateStart, @RequestParam(required = false) Date dueDateEnd, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date lastUpdatedStartDate, @RequestParam(required = false) Date lastUpdatedEndDate, @RequestParam(required = false) EventStatus status, @RequestParam(required = false) String attributeCc, @RequestParam(required = false) String attributeCos, @RequestParam(required = false) boolean skipMeta, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false) String order, @RequestParam(required = false) String attachment, @RequestParam(required = false, defaultValue = "false") boolean includeDeleted, @RequestParam(required = false) String event, @RequestParam(required = false) Set<String> filter, @RequestParam(required = false) Set<String> dataElement, @RequestParam Map<String, String> parameters, IdSchemes idSchemes, Model model, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    boolean allowNoAttrOptionCombo = trackedEntityInstance != null && entityInstanceService.getTrackedEntityInstance(trackedEntityInstance) != null;
    DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(attributeCc, attributeCos, allowNoAttrOptionCombo);
    if (attributeOptionCombo == null && !allowNoAttrOptionCombo) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal attribute option combo identifier: " + attributeCc + " " + attributeCos));
    }
    Set<String> eventIds = TextUtils.splitToArray(event, TextUtils.SEMICOLON);
    lastUpdatedStartDate = lastUpdatedStartDate != null ? lastUpdatedStartDate : lastUpdated;
    EventSearchParams params = eventService.getFromUrl(program, programStage, programStatus, followUp, orgUnit, ouMode, trackedEntityInstance, startDate, endDate, dueDateStart, dueDateEnd, lastUpdatedStartDate, lastUpdatedEndDate, status, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, null, getGridOrderParams(order), false, eventIds, filter, dataElement, includeDeleted);
    contextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_JSON, CacheStrategy.NO_CACHE);
    return eventService.getEventsGrid(params);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 12 with Attribute

use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.

the class EventController method getEvents.

@RequestMapping(value = "", method = RequestMethod.GET)
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD') or hasRole('F_TRACKED_ENTITY_DATAVALUE_READ')")
@ResponseBody
public RootNode getEvents(@RequestParam(required = false) String program, @RequestParam(required = false) String programStage, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String orgUnit, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false) Date dueDateStart, @RequestParam(required = false) Date dueDateEnd, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date lastUpdatedStartDate, @RequestParam(required = false) Date lastUpdatedEndDate, @RequestParam(required = false) EventStatus status, @RequestParam(required = false) String attributeCc, @RequestParam(required = false) String attributeCos, @RequestParam(required = false) boolean skipMeta, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false) String order, @RequestParam(required = false) String attachment, @RequestParam(required = false, defaultValue = "false") boolean includeDeleted, @RequestParam(required = false) String event, @RequestParam Map<String, String> parameters, IdSchemes idSchemes, Model model, HttpServletResponse response, HttpServletRequest request) throws WebMessageException {
    WebOptions options = new WebOptions(parameters);
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.addAll(Preset.ALL.getFields());
    }
    boolean allowNoAttrOptionCombo = trackedEntityInstance != null && entityInstanceService.getTrackedEntityInstance(trackedEntityInstance) != null;
    DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(attributeCc, attributeCos, allowNoAttrOptionCombo);
    if (attributeOptionCombo == null && !allowNoAttrOptionCombo) {
        throw new WebMessageException(WebMessageUtils.conflict("Illegal attribute option combo identifier: " + attributeCc + " " + attributeCos));
    }
    Set<String> eventIds = TextUtils.splitToArray(event, TextUtils.SEMICOLON);
    lastUpdatedStartDate = lastUpdatedStartDate != null ? lastUpdatedStartDate : lastUpdated;
    EventSearchParams params = eventService.getFromUrl(program, programStage, programStatus, followUp, orgUnit, ouMode, trackedEntityInstance, startDate, endDate, dueDateStart, dueDateEnd, lastUpdatedStartDate, lastUpdatedEndDate, status, attributeOptionCombo, idSchemes, page, pageSize, totalPages, skipPaging, getOrderParams(order), null, false, eventIds, null, null, includeDeleted);
    Events events = eventService.getEvents(params);
    if (hasHref(fields)) {
        events.getEvents().forEach(e -> e.setHref(ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + e.getEvent()));
    }
    if (!skipMeta && params.getProgram() != null) {
        events.setMetaData(getMetaData(params.getProgram()));
    }
    model.addAttribute("model", events);
    model.addAttribute("viewClass", options.getViewClass("detailed"));
    RootNode rootNode = NodeUtils.createMetadata();
    if (events.getPager() != null) {
        rootNode.addChild(NodeUtils.createPager(events.getPager()));
    }
    if (!StringUtils.isEmpty(attachment)) {
        response.addHeader(ContextUtils.HEADER_CONTENT_DISPOSITION, "attachment; filename=" + attachment);
        response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
    }
    rootNode.addChild(fieldFilterService.filter(Event.class, events.getEvents(), fields));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Events(org.hisp.dhis.dxf2.events.event.Events) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Event(org.hisp.dhis.dxf2.events.event.Event) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 13 with Attribute

use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.

the class AbstractEnrollmentService method checkAttributes.

private List<ImportConflict> checkAttributes(Enrollment enrollment, ImportOptions importOptions) {
    List<ImportConflict> importConflicts = new ArrayList<>();
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    Map<TrackedEntityAttribute, Boolean> mandatoryMap = Maps.newHashMap();
    Map<String, String> attributeValueMap = Maps.newHashMap();
    for (ProgramTrackedEntityAttribute programTrackedEntityAttribute : program.getProgramAttributes()) {
        mandatoryMap.put(programTrackedEntityAttribute.getAttribute(), programTrackedEntityAttribute.isMandatory());
    }
    // ignore attributes which do not belong to this program
    trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> mandatoryMap.containsKey(value.getAttribute())).forEach(value -> attributeValueMap.put(value.getAttribute().getUid(), value.getValue()));
    for (Attribute attribute : enrollment.getAttributes()) {
        attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
        importConflicts.addAll(validateAttributeType(attribute, importOptions));
    }
    TrackedEntityInstance instance = trackedEntityInstanceService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    for (TrackedEntityAttribute trackedEntityAttribute : mandatoryMap.keySet()) {
        Boolean mandatory = mandatoryMap.get(trackedEntityAttribute);
        if (mandatory && !attributeValueMap.containsKey(trackedEntityAttribute.getUid())) {
            importConflicts.add(new ImportConflict("Attribute.attribute", "Missing mandatory attribute " + trackedEntityAttribute.getUid()));
            continue;
        }
        if (trackedEntityAttribute.isUnique()) {
            OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, instance.getOrgUnit());
            importConflicts.addAll(checkScope(trackedEntityInstance, trackedEntityAttribute, attributeValueMap.get(trackedEntityAttribute.getUid()), organisationUnit, program));
        }
        attributeValueMap.remove(trackedEntityAttribute.getUid());
    }
    if (!attributeValueMap.isEmpty()) {
        importConflicts.add(new ImportConflict("Attribute.attribute", "Only program attributes is allowed for enrollment " + attributeValueMap));
    }
    return importConflicts;
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) EventService(org.hisp.dhis.dxf2.events.event.EventService) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Coordinate(org.hisp.dhis.dxf2.events.event.Coordinate) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) UserService(org.hisp.dhis.user.UserService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Set(java.util.Set) DateUtils(org.hisp.dhis.system.util.DateUtils) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) Event(org.hisp.dhis.dxf2.events.event.Event) List(java.util.List) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) IOException(java.io.IOException) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Maps(com.google.common.collect.Maps) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramService(org.hisp.dhis.program.ProgramService) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 14 with Attribute

use of org.hisp.dhis.dxf2.events.trackedentity.Attribute in project dhis2-core by dhis2.

the class AbstractEnrollmentService method updateAttributeValues.

private void updateAttributeValues(Enrollment enrollment, ImportOptions importOptions) {
    org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance = teiService.getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    Map<String, String> attributeValueMap = Maps.newHashMap();
    for (Attribute attribute : enrollment.getAttributes()) {
        attributeValueMap.put(attribute.getAttribute(), attribute.getValue());
    }
    trackedEntityInstance.getTrackedEntityAttributeValues().stream().filter(value -> attributeValueMap.containsKey(value.getAttribute().getUid())).forEach(value -> {
        String newValue = attributeValueMap.get(value.getAttribute().getUid());
        value.setValue(newValue);
        trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(value);
        attributeValueMap.remove(value.getAttribute().getUid());
    });
    for (String key : attributeValueMap.keySet()) {
        TrackedEntityAttribute attribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), key);
        if (attribute != null) {
            TrackedEntityAttributeValue value = new TrackedEntityAttributeValue();
            value.setValue(attributeValueMap.get(key));
            value.setAttribute(attribute);
            trackedEntityAttributeValueService.addTrackedEntityAttributeValue(value);
            trackedEntityInstance.addAttributeValue(value);
        }
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) EventService(org.hisp.dhis.dxf2.events.event.EventService) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ProgramStageInstanceService(org.hisp.dhis.program.ProgramStageInstanceService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Coordinate(org.hisp.dhis.dxf2.events.event.Coordinate) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) UserService(org.hisp.dhis.user.UserService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) Set(java.util.Set) DateUtils(org.hisp.dhis.system.util.DateUtils) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) Event(org.hisp.dhis.dxf2.events.event.Event) List(java.util.List) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) TrackedEntityCommentService(org.hisp.dhis.trackedentitycomment.TrackedEntityCommentService) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) I18nManager(org.hisp.dhis.i18n.I18nManager) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) TrackedEntityInstanceService(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstanceService) IdSchemes(org.hisp.dhis.common.IdSchemes) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) IOException(java.io.IOException) InvalidIdentifierReferenceException(org.hisp.dhis.common.exception.InvalidIdentifierReferenceException) Maps(com.google.common.collect.Maps) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) Note(org.hisp.dhis.dxf2.events.event.Note) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramStatus(org.hisp.dhis.program.ProgramStatus) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramService(org.hisp.dhis.program.ProgramService) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)

Example 15 with Attribute

use of org.hisp.dhis.dxf2.events.trackedentity.Attribute 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

ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)10 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)6 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)6 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)5 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 HashSet (java.util.HashSet)4 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)4 ImportStrategy (org.hisp.dhis.importexport.ImportStrategy)4 RootNode (org.hisp.dhis.node.types.RootNode)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 Lists (com.google.common.collect.Lists)3 IOException (java.io.IOException)3 List (java.util.List)3 Set (java.util.Set)3 IdSchemes (org.hisp.dhis.common.IdSchemes)3 OrganisationUnitSelectionMode (org.hisp.dhis.common.OrganisationUnitSelectionMode)3