Search in sources :

Example 1 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance in project dhis2-core by dhis2.

the class NoRegistrationSingleEventServiceTest method testGetPersonsByProgramStageInstance.

@Test
public void testGetPersonsByProgramStageInstance() {
    Event event = createEvent(programA.getUid(), organisationUnitA.getUid());
    ImportSummary importSummary = eventService.addEvent(event, null);
    assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
    assertNotNull(importSummary.getReference());
    ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(importSummary.getReference());
    assertNotNull(programStageInstance);
    assertNotNull(eventService.getEvent(programStageInstance));
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Event(org.hisp.dhis.dxf2.events.event.Event) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) Test(org.junit.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 2 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method saveProgramStage.

@Override
public String saveProgramStage(org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage, int patientId, int orgUnitId) throws NotAllowedException {
    if (mobileProgramStage.isSingleEvent()) {
        TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
        ProgramStageInstance prStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
        ProgramStage programStage = programStageService.getProgramStage(prStageInstance.getProgramStage().getId());
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
        // ---------------------------------------------------------------------
        // Add a new program-instance
        //
        // ---------------------------------------------------------------------
        ProgramInstance programInstance = new ProgramInstance();
        programInstance.setEnrollmentDate(new Date());
        programInstance.setIncidentDate(new Date());
        programInstance.setProgram(programStage.getProgram());
        programInstance.setStatus(ProgramStatus.COMPLETED);
        programInstance.setEntityInstance(patient);
        programInstanceService.addProgramInstance(programInstance);
        // ---------------------------------------------------------------------
        // Add a new program-stage-instance
        //
        // ---------------------------------------------------------------------
        ProgramStageInstance programStageInstance = new ProgramStageInstance();
        programStageInstance.setProgramInstance(programInstance);
        programStageInstance.setProgramStage(programStage);
        programStageInstance.setDueDate(new Date());
        programStageInstance.setExecutionDate(new Date());
        programStageInstance.setOrganisationUnit(organisationUnit);
        programStageInstance.setStatus(EventStatus.COMPLETED);
        programStageInstanceService.addProgramStageInstance(programStageInstance);
        // ---------------------------------------------------------------------
        // Save value
        //
        // ---------------------------------------------------------------------
        List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
            DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
            String value = dataElement1.getValue();
            if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
                value = PeriodUtil.convertDateFormat(value);
            }
            TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue();
            patientDataValue.setDataElement(dataElement);
            patientDataValue.setValue(value);
            patientDataValue.setProgramStageInstance(programStageInstance);
            patientDataValue.setLastUpdated(new Date());
            dataValueService.saveTrackedEntityDataValue(patientDataValue);
        }
        return SINGLE_EVENT_UPLOADED;
    } else {
        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(mobileProgramStage.getId());
        List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement> dataElements = mobileProgramStage.getDataElements();
        try {
            OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
            programStageInstance.setOrganisationUnit(organisationUnit);
        } catch (Exception e) {
            programStageInstance.setOrganisationUnit(null);
        }
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStageDataElement dataElement1 : dataElements) {
            DataElement dataElement = dataElementService.getDataElement(dataElement1.getId());
            String value = dataElement1.getValue();
            if (value != null) {
                if (ValueType.DATE == dataElement.getValueType() && !value.trim().equals("")) {
                    value = PeriodUtil.convertDateFormat(value);
                }
                TrackedEntityDataValue previousPatientDataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
                if (previousPatientDataValue == null) {
                    TrackedEntityDataValue patientDataValue = new TrackedEntityDataValue(programStageInstance, dataElement, value);
                    dataValueService.saveTrackedEntityDataValue(patientDataValue);
                } else {
                    previousPatientDataValue.setValue(value);
                    previousPatientDataValue.setLastUpdated(new Date());
                    previousPatientDataValue.setProvidedElsewhere(false);
                    dataValueService.updateTrackedEntityDataValue(previousPatientDataValue);
                }
            }
        }
        if (DateUtils.getMediumDate(mobileProgramStage.getReportDate()) != null) {
            programStageInstance.setExecutionDate(DateUtils.getMediumDate(mobileProgramStage.getReportDate()));
        } else {
            programStageInstance.setExecutionDate(new Date());
        }
        if (programStageInstance.getProgramStage().getProgramStageDataElements().size() > dataElements.size()) {
            programStageInstanceService.updateProgramStageInstance(programStageInstance);
            return PROGRAM_STAGE_SECTION_UPLOADED;
        } else {
            if (mobileProgramStage.isCompleted()) {
                programStageInstance.setStatus(EventStatus.COMPLETED);
            }
            programStageInstanceService.updateProgramStageInstance(programStageInstance);
            // check if all belonged program stage are completed
            if (!mobileProgramStage.isRepeatable() && isAllProgramStageFinished(programStageInstance) == true) {
                ProgramInstance programInstance = programStageInstance.getProgramInstance();
                programInstance.setStatus(ProgramStatus.COMPLETED);
                programInstanceService.updateProgramInstance(programInstance);
            }
            if (mobileProgramStage.isRepeatable()) {
                Date nextDate = DateUtils.getDateAfterAddition(new Date(), mobileProgramStage.getStandardInterval());
                return PROGRAM_STAGE_UPLOADED + "$" + DateUtils.getMediumDateString(nextDate);
            } else {
                return PROGRAM_STAGE_UPLOADED;
            }
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 3 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method isAllProgramStageFinished.

private boolean isAllProgramStageFinished(ProgramStageInstance programStageInstance) {
    ProgramInstance programInstance = programStageInstance.getProgramInstance();
    Collection<ProgramStageInstance> programStageInstances = programInstance.getProgramStageInstances();
    if (programStageInstances != null) {
        if (programStageInstances.size() < programInstance.getProgram().getProgramStages().size()) {
            return false;
        }
        Iterator<ProgramStageInstance> iterator = programStageInstances.iterator();
        while (iterator.hasNext()) {
            ProgramStageInstance each = iterator.next();
            if (!each.isCompleted()) {
                return false;
            }
        }
    }
    return true;
}
Also used : ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 4 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method handleLostToFollowUp.

@SuppressWarnings("finally")
@Override
public Notification handleLostToFollowUp(LostEvent lostEvent) throws NotAllowedException {
    Notification notification = new Notification();
    try {
        ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(lostEvent.getId());
        programStageInstance.setDueDate(DateUtils.getMediumDate(lostEvent.getDueDate()));
        programStageInstance.setStatus(EventStatus.fromInt(lostEvent.getStatus()));
        if (lostEvent.getComment() != null) {
            List<MessageConversation> conversationList = new ArrayList<>();
            MessageConversation conversation = new MessageConversation(lostEvent.getName(), currentUserService.getCurrentUser(), MessageType.PRIVATE);
            conversation.addMessage(new Message(lostEvent.getComment(), null, currentUserService.getCurrentUser()));
            conversation.setRead(true);
            conversationList.add(conversation);
            programStageInstance.setMessageConversations(conversationList);
            messageService.saveMessageConversation(conversation);
        }
        programStageInstanceService.updateProgramStageInstance(programStageInstance);
        // send SMS
        if (programStageInstance.getProgramInstance().getEntityInstance().getTrackedEntityAttributeValues() != null && lostEvent.getSMS() != null) {
            List<User> recipientsList = new ArrayList<>();
            for (TrackedEntityAttributeValue attrValue : programStageInstance.getProgramInstance().getEntityInstance().getTrackedEntityAttributeValues()) {
                if (ValueType.PHONE_NUMBER == attrValue.getAttribute().getValueType()) {
                    User user = new User();
                    user.setPhoneNumber(attrValue.getValue());
                    recipientsList.add(user);
                }
            }
            Set<User> recipients = new HashSet<>();
            recipients.addAll(recipientsList);
            smsSender.sendMessage(lostEvent.getName(), lostEvent.getSMS(), null, currentUserService.getCurrentUser(), recipients, false);
        }
        notification.setMessage("Success");
    } catch (Exception e) {
        e.printStackTrace();
        notification.setMessage("Fail");
    } finally {
        return notification;
    }
}
Also used : User(org.hisp.dhis.user.User) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) Notification(org.hisp.dhis.api.mobile.model.LWUITmodel.Notification) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) HashSet(java.util.HashSet)

Example 5 with ProgramStageInstance

use of org.hisp.dhis.program.ProgramStageInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method getCurrentActivityPlan.

@Override
public ActivityPlan getCurrentActivityPlan(OrganisationUnit unit, String localeString) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, 30);
    long upperBound = cal.getTime().getTime();
    cal.add(Calendar.DATE, -60);
    long lowerBound = cal.getTime().getTime();
    List<Activity> items = new ArrayList<>();
    TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams().addOrganisationUnit(unit);
    Grid trackedEntityDrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
    List<List<Object>> entityInstanceList = trackedEntityDrid.getRows();
    for (List<Object> entityInstance : entityInstanceList) {
        TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(entityInstance.get(0).toString());
        for (ProgramStageInstance programStageInstance : programStageInstanceService.getProgramStageInstances(trackedEntityInstance, EventStatus.ACTIVE)) {
            if (programStageInstance.getDueDate().getTime() >= lowerBound && programStageInstance.getDueDate().getTime() <= upperBound) {
                items.add(getActivity(programStageInstance, false));
            }
        }
    }
    if (items.isEmpty()) {
        return null;
    }
    Collections.sort(items, activityComparator);
    return new ActivityPlan(items);
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) Grid(org.hisp.dhis.common.Grid) ActivityPlan(org.hisp.dhis.api.mobile.model.ActivityPlan) ArrayList(java.util.ArrayList) Activity(org.hisp.dhis.api.mobile.model.Activity) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) List(java.util.List) PatientList(org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList) ArrayList(java.util.ArrayList) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Aggregations

ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)23 ArrayList (java.util.ArrayList)10 ProgramInstance (org.hisp.dhis.program.ProgramInstance)9 Date (java.util.Date)8 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 ProgramStage (org.hisp.dhis.program.ProgramStage)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 Program (org.hisp.dhis.program.Program)6 DataElement (org.hisp.dhis.dataelement.DataElement)5 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)5 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)4 HashSet (java.util.HashSet)3 List (java.util.List)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)3 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)2 Activity (org.hisp.dhis.api.mobile.model.Activity)2 ActivityPlan (org.hisp.dhis.api.mobile.model.ActivityPlan)2