Search in sources :

Example 6 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ProgramMessageStoreTest method setUpTest.

// -------------------------------------------------------------------------
// Prerequisite
// -------------------------------------------------------------------------
@Override
public void setUpTest() {
    ouA = createOrganisationUnit('A');
    ouB = createOrganisationUnit('B');
    orgUnitService.addOrganisationUnit(ouA);
    orgUnitService.addOrganisationUnit(ouB);
    programA = createProgram('A', new HashSet<>(), ouA);
    programService.addProgram(programA);
    ProgramStage stageA = new ProgramStage("StageA", programA);
    stageA.setSortOrder(1);
    programStageService.saveProgramStage(stageA);
    Set<ProgramStage> programStages = new HashSet<>();
    programStages.add(stageA);
    programA.setProgramStages(programStages);
    programService.updateProgram(programA);
    entityInstanceA = createTrackedEntityInstance('A', ouA);
    entityInstanceService.addTrackedEntityInstance(entityInstanceA);
    TrackedEntityInstance entityInstanceB = createTrackedEntityInstance('B', ouA);
    entityInstanceService.addTrackedEntityInstance(entityInstanceB);
    DateTime testDate1 = DateTime.now();
    testDate1.withTimeAtStartOfDay();
    testDate1 = testDate1.minusDays(70);
    incidentDate = testDate1.toDate();
    DateTime testDate2 = DateTime.now();
    testDate2.withTimeAtStartOfDay();
    enrollmentDate = testDate2.toDate();
    programInstanceA = new ProgramInstance(enrollmentDate, incidentDate, entityInstanceA, programA);
    programInstanceA.setUid("UID-A");
    programStageInstanceA = new ProgramStageInstance(programInstanceA, stageA);
    programStageInstanceA.setDueDate(enrollmentDate);
    programStageInstanceA.setUid("UID-A");
    Set<OrganisationUnit> ouSet = new HashSet<>();
    ouSet.add(ouA);
    Set<String> ouUids = new HashSet<>();
    ouUids.add(ouA.getUid());
    // ouSet.add( ouB );
    teiA = createTrackedEntityInstance('Z', ouA);
    teiService.addTrackedEntityInstance(teiA);
    recipientsA = new ProgramMessageRecipients();
    recipientsA.setOrganisationUnit(ouA);
    recipientsA.setTrackedEntityInstance(teiA);
    recipientsB = new ProgramMessageRecipients();
    recipientsB.setOrganisationUnit(ouA);
    recipientsB.setTrackedEntityInstance(teiA);
    recipientsC = new ProgramMessageRecipients();
    recipientsC.setOrganisationUnit(ouA);
    recipientsC.setTrackedEntityInstance(teiA);
    Set<String> phoneNumberListA = new HashSet<>();
    phoneNumberListA.add(msisdn);
    recipientsA.setPhoneNumbers(phoneNumberListA);
    Set<String> phoneNumberListB = new HashSet<>();
    phoneNumberListB.add(msisdn);
    recipientsB.setPhoneNumbers(phoneNumberListB);
    Set<String> phoneNumberListC = new HashSet<>();
    phoneNumberListC.add(msisdn);
    recipientsC.setPhoneNumbers(phoneNumberListC);
    channels.add(DeliveryChannel.SMS);
    pmsgA = new ProgramMessage();
    pmsgA.setText(text);
    pmsgA.setSubject(subject);
    pmsgA.setRecipients(recipientsA);
    pmsgA.setMessageStatus(messageStatus);
    pmsgA.setDeliveryChannels(channels);
    pmsgB = new ProgramMessage();
    pmsgB.setText(text);
    pmsgB.setSubject(subject);
    pmsgB.setRecipients(recipientsB);
    pmsgB.setMessageStatus(messageStatus);
    pmsgB.setDeliveryChannels(channels);
    pmsgC = new ProgramMessage();
    pmsgC.setText(text);
    pmsgC.setSubject(subject);
    pmsgC.setRecipients(recipientsC);
    pmsgC.setMessageStatus(messageStatus);
    pmsgC.setDeliveryChannels(channels);
    uidA = CodeGenerator.generateCode(10);
    uidB = CodeGenerator.generateCode(10);
    uidC = CodeGenerator.generateCode(10);
    pmsgA.setUid(uidA);
    pmsgB.setUid(uidB);
    pmsgC.setUid(uidC);
    params = new ProgramMessageQueryParams();
    params.setOrganisationUnit(ouUids);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) DateTime(org.joda.time.DateTime) ProgramMessageQueryParams(org.hisp.dhis.program.message.ProgramMessageQueryParams) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) HashSet(java.util.HashSet)

Example 7 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class AuditController method getTrackedEntityAttributeValueAudit.

@RequestMapping(value = "trackedEntityAttributeValue", method = RequestMethod.GET)
@ResponseBody
public RootNode getTrackedEntityAttributeValueAudit(@RequestParam(required = false, defaultValue = "") List<String> tea, @RequestParam(required = false, defaultValue = "") List<String> tei, @RequestParam(required = false) AuditType auditType, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false, defaultValue = "50") int pageSize, @RequestParam(required = false, defaultValue = "1") int page) throws WebMessageException {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    List<TrackedEntityAttribute> trackedEntityAttributes = getTrackedEntityAttributes(tea);
    List<TrackedEntityInstance> trackedEntityInstances = getTrackedEntityInstances(tei);
    List<TrackedEntityAttributeValueAudit> attributeValueAudits;
    Pager pager = null;
    if (skipPaging) {
        attributeValueAudits = trackedEntityAttributeValueAuditService.getTrackedEntityAttributeValueAudits(trackedEntityAttributes, trackedEntityInstances, auditType);
    } else {
        int total = trackedEntityAttributeValueAuditService.countTrackedEntityAttributeValueAudits(trackedEntityAttributes, trackedEntityInstances, auditType);
        pager = new Pager(page, total, pageSize);
        attributeValueAudits = trackedEntityAttributeValueAuditService.getTrackedEntityAttributeValueAudits(trackedEntityAttributes, trackedEntityInstances, auditType, pager.getOffset(), pager.getPageSize());
    }
    RootNode rootNode = NodeUtils.createMetadata();
    if (pager != null) {
        rootNode.addChild(NodeUtils.createPager(pager));
    }
    CollectionNode trackedEntityAttributeValueAudits = rootNode.addChild(new CollectionNode("trackedEntityAttributeValueAudits", true));
    trackedEntityAttributeValueAudits.addChildren(fieldFilterService.filter(TrackedEntityAttributeValueAudit.class, attributeValueAudits, fields).getChildren());
    return rootNode;
}
Also used : RootNode(org.hisp.dhis.node.types.RootNode) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Pager(org.hisp.dhis.common.Pager) TrackedEntityAttributeValueAudit(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueAudit) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) CollectionNode(org.hisp.dhis.node.types.CollectionNode) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 8 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance 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 9 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance 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)

Example 10 with TrackedEntityInstance

use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method findVisitSchedule.

@Override
public String findVisitSchedule(int orgUnitId, int programId, String info) throws NotAllowedException {
    String status = info.substring(0, info.indexOf("$"));
    String fromDays = info.substring(info.indexOf("$") + 1, info.indexOf("/"));
    String toDays = info.substring(info.indexOf("/") + 1);
    // Event Status
    EventStatus eventStatus = null;
    if (status.equals("Schedule in future")) {
        eventStatus = EventStatus.SCHEDULE;
    } else if (status.equals("Overdue")) {
        eventStatus = EventStatus.OVERDUE;
    } else if (status.equals("Incomplete")) {
        eventStatus = EventStatus.VISITED;
    } else if (status.equals("Completed")) {
        eventStatus = EventStatus.COMPLETED;
    } else if (status.equals("Skipped")) {
        eventStatus = EventStatus.SKIPPED;
    }
    // From/To Date
    Date fromDate = getDate(-1, fromDays);
    Date toDate = getDate(1, toDays);
    TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
    List<TrackedEntityAttribute> trackedEntityAttributeList = new ArrayList<>(attributeService.getTrackedEntityAttributesByDisplayOnVisitSchedule(true));
    for (TrackedEntityAttribute trackedEntityAttribute : trackedEntityAttributeList) {
        QueryItem queryItem = new QueryItem(trackedEntityAttribute);
        param.addAttribute(queryItem);
    }
    param.setProgram(programService.getProgram(programId));
    param.addOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
    param.setEventStatus(eventStatus);
    param.setEventStartDate(fromDate);
    param.setEventEndDate(toDate);
    Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
    List<List<Object>> listOfListProgramStageInstance = programStageInstanceGrid.getRows();
    if (listOfListProgramStageInstance.size() == 0) {
        throw NotAllowedException.NO_EVENT_FOUND;
    }
    String eventsInfo = "";
    for (List<Object> row : listOfListProgramStageInstance) {
        TrackedEntityInstance instance = entityInstanceService.getTrackedEntityInstance((String) row.get(0));
        Collection<TrackedEntityAttribute> displayAttributes = attributeService.getTrackedEntityAttributesDisplayInList();
        eventsInfo += instance.getId() + "/";
        String displayName = "";
        for (TrackedEntityAttribute displayAttribute : displayAttributes) {
            TrackedEntityAttributeValue value = attValueService.getTrackedEntityAttributeValue(instance, displayAttribute);
            if (value != null) {
                displayName += value.getValue() + " ";
            }
        }
        eventsInfo += displayName.trim() + "$";
    }
    return eventsInfo;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) Grid(org.hisp.dhis.common.Grid) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) EventStatus(org.hisp.dhis.event.EventStatus) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) List(java.util.List) PatientList(org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList) ArrayList(java.util.ArrayList) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)

Aggregations

TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)33 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)11 Date (java.util.Date)10 HashSet (java.util.HashSet)9 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)8 ArrayList (java.util.ArrayList)7 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)7 Program (org.hisp.dhis.program.Program)6 ProgramInstance (org.hisp.dhis.program.ProgramInstance)6 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)6 List (java.util.List)5 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)5 PatientList (org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList)5 PatientAttribute (org.hisp.dhis.api.mobile.model.PatientAttribute)4 Grid (org.hisp.dhis.common.Grid)4 TrackedEntityInstanceQueryParams (org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)4 Activity (org.hisp.dhis.api.mobile.model.Activity)3 Patient (org.hisp.dhis.api.mobile.model.LWUITmodel.Patient)3 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)3 QueryItem (org.hisp.dhis.common.QueryItem)3