Search in sources :

Example 11 with CREATED

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

the class AbstractEnrollmentService method updateEnrollment.

private ImportSummary updateEnrollment(Enrollment enrollment, ImportOptions importOptions, boolean handleEvents) {
    importOptions = updateImportOptions(importOptions);
    if (enrollment == null || StringUtils.isEmpty(enrollment.getEnrollment())) {
        return new ImportSummary(ImportStatus.ERROR, "No enrollment or enrollment ID was supplied").incrementIgnored();
    }
    ProgramInstance programInstance = programInstanceService.getProgramInstance(enrollment.getEnrollment());
    List<String> errors = trackerAccessManager.canUpdate(importOptions.getUser(), programInstance, false);
    if (programInstance == null) {
        return new ImportSummary(ImportStatus.ERROR, "ID " + enrollment.getEnrollment() + " doesn't point to a valid enrollment.").incrementIgnored();
    }
    if (!errors.isEmpty()) {
        return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
    }
    ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
    checkAttributes(programInstance.getEntityInstance(), enrollment, importOptions, importSummary);
    if (importSummary.hasConflicts()) {
        importSummary.setStatus(ImportStatus.ERROR).incrementIgnored();
        return importSummary;
    }
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    if (!program.isRegistration()) {
        String descMsg = "Provided program " + program.getUid() + " is a program without registration. An enrollment cannot be created into program without registration.";
        return new ImportSummary(ImportStatus.ERROR, descMsg).incrementIgnored();
    }
    programInstance.setProgram(program);
    if (enrollment.getIncidentDate() != null) {
        programInstance.setIncidentDate(enrollment.getIncidentDate());
    }
    if (enrollment.getEnrollmentDate() != null) {
        programInstance.setEnrollmentDate(enrollment.getEnrollmentDate());
    }
    if (enrollment.getOrgUnit() != null) {
        OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
        programInstance.setOrganisationUnit(organisationUnit);
    }
    programInstance.setFollowup(enrollment.getFollowup());
    if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
        return new ImportSummary(ImportStatus.ERROR, "DisplayIncidentDate is true but IncidentDate is null").incrementIgnored();
    }
    updateFeatureType(program, enrollment, programInstance);
    if (EnrollmentStatus.fromProgramStatus(programInstance.getStatus()) != enrollment.getStatus()) {
        Date endDate = enrollment.getCompletedDate();
        String user = enrollment.getCompletedBy();
        if (enrollment.getCompletedDate() == null) {
            endDate = new Date();
        }
        if (user == null) {
            user = importOptions.getUser().getUsername();
        }
        if (EnrollmentStatus.CANCELLED == enrollment.getStatus()) {
            programInstance.setEndDate(endDate);
            programInstanceService.cancelProgramInstanceStatus(programInstance);
        } else if (EnrollmentStatus.COMPLETED == enrollment.getStatus()) {
            programInstance.setEndDate(endDate);
            programInstance.setCompletedBy(user);
            programInstanceService.completeProgramInstanceStatus(programInstance);
        } else if (EnrollmentStatus.ACTIVE == enrollment.getStatus()) {
            programInstanceService.incompleteProgramInstanceStatus(programInstance);
        }
    }
    validateProgramInstance(program, programInstance, enrollment, importSummary);
    if (importSummary.getStatus() != ImportStatus.SUCCESS) {
        return importSummary;
    }
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
    teiService.updateTrackedEntityInstance(programInstance.getEntityInstance(), importOptions.getUser());
    saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
    importSummary = new ImportSummary(enrollment.getEnrollment()).incrementUpdated();
    importSummary.setReference(enrollment.getEnrollment());
    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) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) 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)

Example 12 with CREATED

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

the class EventImportTest method testVerifyEventCanBeUpdatedUsingProgramOnly.

@Test
void testVerifyEventCanBeUpdatedUsingProgramOnly() throws IOException {
    // CREATE A NEW EVENT
    InputStream is = createEventJsonInputStream(programB.getUid(), programStageB.getUid(), organisationUnitB.getUid(), null, dataElementB, "10");
    ImportSummaries importSummaries = eventService.addEventsJson(is, null);
    String uid = importSummaries.getImportSummaries().get(0).getReference();
    assertEquals(ImportStatus.SUCCESS, importSummaries.getStatus());
    // FETCH NEWLY CREATED EVENT
    ProgramStageInstance psi = programStageInstanceService.getProgramStageInstance(uid);
    // UPDATE EVENT (no actual changes, except for empty data value)
    // USE ONLY PROGRAM
    Event event = new Event();
    event.setEvent(uid);
    event.setProgram(programB.getUid());
    event.setStatus(EventStatus.COMPLETED);
    assertEquals(ImportStatus.SUCCESS, eventService.updateEvent(event, false, ImportOptions.getDefaultImportOptions(), false).getStatus());
    cleanSession();
    ProgramStageInstance psi2 = programStageInstanceService.getProgramStageInstance(uid);
    assertThat(psi.getLastUpdated(), DateMatchers.before(psi2.getLastUpdated()));
    assertThat(psi.getCreated(), is(psi2.getCreated()));
    assertThat(psi.getProgramInstance().getUid(), is(psi2.getProgramInstance().getUid()));
    assertThat(psi.getProgramStage().getUid(), is(psi2.getProgramStage().getUid()));
    assertThat(psi.getOrganisationUnit().getUid(), is(psi2.getOrganisationUnit().getUid()));
    assertThat(psi.getAttributeOptionCombo().getUid(), is(psi2.getAttributeOptionCombo().getUid()));
    assertThat(psi.getStatus().getValue(), is(psi2.getStatus().getValue()));
    assertThat(psi.getExecutionDate(), is(psi2.getExecutionDate()));
    assertThat(psi.getCompletedDate(), is(psi2.getCompletedDate()));
    assertThat(psi.getCompletedBy(), is(psi2.getCompletedBy()));
    assertThat(psi.isDeleted(), is(psi2.isDeleted()));
    assertThat(psi.getEventDataValues().size(), is(1));
    assertThat(psi2.getEventDataValues().size(), is(0));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Event(org.hisp.dhis.dxf2.events.event.Event) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 13 with CREATED

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

the class EventImportTest method testVerifyEventUncompleteSetsCompletedDateToNull.

@Test
void testVerifyEventUncompleteSetsCompletedDateToNull() throws IOException {
    // CREATE A NEW EVENT
    InputStream is = createEventJsonInputStream(programB.getUid(), programStageB.getUid(), organisationUnitB.getUid(), null, dataElementB, "10");
    ImportSummaries importSummaries = eventService.addEventsJson(is, null);
    String uid = importSummaries.getImportSummaries().get(0).getReference();
    assertEquals(ImportStatus.SUCCESS, importSummaries.getStatus());
    // FETCH NEWLY CREATED EVENT
    ProgramStageInstance psi = programStageInstanceService.getProgramStageInstance(uid);
    // UPDATE EVENT (no actual changes, except for empty data value and
    // status
    // change)
    Event event = new Event();
    event.setEvent(uid);
    event.setProgram(programB.getUid());
    event.setStatus(EventStatus.ACTIVE);
    assertEquals(ImportStatus.SUCCESS, eventService.updateEvent(event, false, ImportOptions.getDefaultImportOptions(), false).getStatus());
    cleanSession();
    ProgramStageInstance psi2 = programStageInstanceService.getProgramStageInstance(uid);
    assertThat(psi.getLastUpdated(), DateMatchers.before(psi2.getLastUpdated()));
    assertThat(psi.getCreated(), is(psi2.getCreated()));
    assertThat(psi.getProgramInstance().getUid(), is(psi2.getProgramInstance().getUid()));
    assertThat(psi.getProgramStage().getUid(), is(psi2.getProgramStage().getUid()));
    assertThat(psi.getOrganisationUnit().getUid(), is(psi2.getOrganisationUnit().getUid()));
    assertThat(psi.getAttributeOptionCombo().getUid(), is(psi2.getAttributeOptionCombo().getUid()));
    assertThat(psi2.getStatus(), is(EventStatus.ACTIVE));
    assertThat(psi.getExecutionDate(), is(psi2.getExecutionDate()));
    assertThat(psi2.getCompletedDate(), is(nullValue()));
    assertThat(psi.getCompletedBy(), is(psi2.getCompletedBy()));
    assertThat(psi.isDeleted(), is(psi2.isDeleted()));
    assertThat(psi.getEventDataValues().size(), is(1));
    assertThat(psi2.getEventDataValues().size(), is(0));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Event(org.hisp.dhis.dxf2.events.event.Event) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TransactionalIntegrationTest(org.hisp.dhis.TransactionalIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 14 with CREATED

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

the class EventController method getEvents.

// -------------------------------------------------------------------------
// Object Read
// -------------------------------------------------------------------------
@GetMapping
@ResponseBody
public RootNode getEvents(EventCriteria eventCriteria, @RequestParam Map<String, String> parameters, Model model, HttpServletResponse response, HttpServletRequest request) {
    WebOptions options = new WebOptions(parameters);
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.add("event,uid,program,programType,status,assignedUser,orgUnit,orgUnitName,eventDate,orgUnit,orgUnitName,created,lastUpdated,followup");
    }
    EventSearchParams params = requestToSearchParamsMapper.map(eventCriteria);
    setParamBasedOnFieldParameters(params, fields);
    Events events = eventService.getEvents(params);
    if (hasHref(fields, eventCriteria.getSkipEventId())) {
        events.getEvents().forEach(e -> e.setHref(ContextUtils.getRootPath(request) + RESOURCE_PATH + "/" + e.getEvent()));
    }
    if (!eventCriteria.isSkipMeta() && params.getProgram() != null) {
        events.setMetaData(getMetaData(params.getProgram()));
    }
    model.addAttribute("model", events);
    model.addAttribute("viewClass", options.getViewClass("detailed"));
    RootNode rootNode = NodeUtils.createMetadata();
    addPager(params, events, rootNode);
    if (!StringUtils.isEmpty(eventCriteria.getAttachment())) {
        response.addHeader(ContextUtils.HEADER_CONTENT_DISPOSITION, "attachment; filename=" + eventCriteria.getAttachment());
        response.addHeader(ContextUtils.HEADER_CONTENT_TRANSFER_ENCODING, "binary");
    }
    rootNode.addChild(fieldFilterService.toCollectionNode(Event.class, new FieldFilterParams(events.getEvents(), fields)));
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) Events(org.hisp.dhis.dxf2.events.event.Events) EventSearchParams(org.hisp.dhis.dxf2.events.event.EventSearchParams) Event(org.hisp.dhis.dxf2.events.event.Event) FieldFilterParams(org.hisp.dhis.fieldfilter.FieldFilterParams) WebOptions(org.hisp.dhis.webapi.webdomain.WebOptions) GetMapping(org.springframework.web.bind.annotation.GetMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 15 with CREATED

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

the class CompleteDataSetRegistrationController method getCompleteRegistrationsXml.

// -------------------------------------------------------------------------
// GET
// -------------------------------------------------------------------------
@GetMapping(produces = CONTENT_TYPE_XML)
public void getCompleteRegistrationsXml(@RequestParam Set<String> dataSet, @RequestParam(required = false) Set<String> period, @RequestParam(required = false) Date startDate, @RequestParam(required = false) Date endDate, @RequestParam(required = false, name = "children") boolean includeChildren, @RequestParam(required = false) Set<String> orgUnit, @RequestParam(required = false) Set<String> orgUnitGroup, @RequestParam(required = false) Date created, @RequestParam(required = false) String createdDuration, @RequestParam(required = false) Integer limit, IdSchemes idSchemes, HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType(CONTENT_TYPE_XML);
    ExportParams params = registrationExchangeService.paramsFromUrl(dataSet, orgUnit, orgUnitGroup, period, startDate, endDate, includeChildren, created, createdDuration, limit, idSchemes);
    registrationExchangeService.writeCompleteDataSetRegistrationsXml(params, response.getOutputStream());
}
Also used : ExportParams(org.hisp.dhis.dxf2.dataset.ExportParams) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)12 Test (org.junit.jupiter.api.Test)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 ArrayList (java.util.ArrayList)8 MetadataVersion (org.hisp.dhis.metadata.version.MetadataVersion)7 Date (java.util.Date)6 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)6 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)6 AvailabilityStatus (org.hisp.dhis.dxf2.synch.AvailabilityStatus)6 DhisHttpResponse (org.hisp.dhis.system.util.DhisHttpResponse)6 HttpUtils (org.hisp.dhis.system.util.HttpUtils)6 User (org.hisp.dhis.user.User)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 List (java.util.List)5 Map (java.util.Map)5 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)4 Event (org.hisp.dhis.dxf2.events.event.Event)4 HashMap (java.util.HashMap)3