Search in sources :

Example 1 with ProgramInstanceQueryParams

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

the class ActivityReportingServiceImpl method generateRepeatableEvent.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient generateRepeatableEvent(int orgUnitId, String eventInfo) throws NotAllowedException {
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
    String[] keys = eventInfo.split("_");
    ProgramStage programStage = programStageService.getProgramStage(Integer.parseInt(keys[4]));
    int mobileProgramStageId = Integer.parseInt(keys[3]);
    String nextDueDate = keys[2];
    Program program = programService.getProgram(Integer.parseInt(keys[1]));
    TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(Integer.parseInt(keys[0]));
    ProgramInstance programInstance = null;
    ProgramStageInstance newProgramStageInstance = null;
    if (mobileProgramStageId != 0) {
        ProgramStageInstance oldProgramStageIntance = programStageInstanceService.getProgramStageInstance(mobileProgramStageId);
        programInstance = oldProgramStageIntance.getProgramInstance();
        newProgramStageInstance = new ProgramStageInstance(programInstance, oldProgramStageIntance.getProgramStage());
        newProgramStageInstance.setDueDate(DateUtils.getMediumDate(nextDueDate));
    } else {
        programInstance = programInstanceService.getProgramInstances(new ProgramInstanceQueryParams().setTrackedEntityInstance(trackedEntityInstance).setProgram(program).setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL)).iterator().next();
        newProgramStageInstance = new ProgramStageInstance();
        newProgramStageInstance.setProgramInstance(programInstance);
        newProgramStageInstance.setProgramStage(programStage);
        newProgramStageInstance.setDueDate(DateUtils.getMediumDate(nextDueDate));
        newProgramStageInstance.setExecutionDate(DateUtils.getMediumDate(nextDueDate));
    }
    newProgramStageInstance.setOrganisationUnit(orgUnit);
    programInstance.getProgramStageInstances().add(newProgramStageInstance);
    List<ProgramStageInstance> proStageInstanceList = new ArrayList<>(programInstance.getProgramStageInstances());
    Collections.sort(proStageInstanceList, new ProgramStageInstanceVisitDateComparator());
    programInstance.getProgramStageInstances().removeAll(proStageInstanceList);
    programInstance.getProgramStageInstances().addAll(proStageInstanceList);
    programStageInstanceService.addProgramStageInstance(newProgramStageInstance);
    programInstanceService.updateProgramInstance(programInstance);
    TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance(programInstance.getEntityInstance().getId());
    org.hisp.dhis.api.mobile.model.LWUITmodel.Patient mobilePatient = getPatientModel(tei);
    return mobilePatient;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) ArrayList(java.util.ArrayList) ProgramStageInstanceVisitDateComparator(org.hisp.dhis.program.comparator.ProgramStageInstanceVisitDateComparator) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams)

Example 2 with ProgramInstanceQueryParams

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

the class EnrollmentController method getEnrollments.

// -------------------------------------------------------------------------
// READ
// -------------------------------------------------------------------------
@RequestMapping(value = "", method = RequestMethod.GET)
@PreAuthorize("hasRole('ALL') or hasRole('F_PROGRAM_ENROLLMENT') or hasRole('F_PROGRAM_UNENROLLMENT') or hasRole('F_PROGRAM_ENROLLMENT_READ')")
@ResponseBody
public RootNode getEnrollments(@RequestParam(required = false) String ou, @RequestParam(required = false) OrganisationUnitSelectionMode ouMode, @RequestParam(required = false) String program, @RequestParam(required = false) ProgramStatus programStatus, @RequestParam(required = false) Boolean followUp, @RequestParam(required = false) Date lastUpdated, @RequestParam(required = false) Date programStartDate, @RequestParam(required = false) Date programEndDate, @RequestParam(required = false) String trackedEntity, @RequestParam(required = false) String trackedEntityInstance, @RequestParam(required = false) String enrollment, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) boolean totalPages, @RequestParam(required = false) boolean skipPaging) {
    List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
    if (fields.isEmpty()) {
        fields.add("enrollment,created,lastUpdated,trackedEntity,trackedEntityInstance,program,status,orgUnit,orgUnitName,enrollmentDate,incidentDate,followup");
    }
    Set<String> orgUnits = TextUtils.splitToArray(ou, TextUtils.SEMICOLON);
    List<Enrollment> enrollments;
    if (enrollment == null) {
        ProgramInstanceQueryParams params = programInstanceService.getFromUrl(orgUnits, ouMode, lastUpdated, program, programStatus, programStartDate, programEndDate, trackedEntity, trackedEntityInstance, followUp, page, pageSize, totalPages, skipPaging);
        enrollments = new ArrayList<>(enrollmentService.getEnrollments(programInstanceService.getProgramInstances(params)));
    } else {
        Set<String> enrollmentIds = TextUtils.splitToArray(enrollment, TextUtils.SEMICOLON);
        enrollments = enrollmentIds != null ? enrollmentIds.stream().map(enrollmentId -> enrollmentService.getEnrollment(enrollmentId)).collect(Collectors.toList()) : null;
    }
    RootNode rootNode = NodeUtils.createMetadata();
    rootNode.addChild(fieldFilterService.filter(Enrollment.class, enrollments, fields));
    return rootNode;
}
Also used : DhisApiVersion(org.hisp.dhis.common.DhisApiVersion) ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Controller(org.springframework.stereotype.Controller) StreamUtils(org.hisp.dhis.commons.util.StreamUtils) ApiVersion(org.hisp.dhis.webapi.mvc.annotation.ApiVersion) WebMessageService(org.hisp.dhis.webapi.service.WebMessageService) ArrayList(java.util.ArrayList) NodeUtils(org.hisp.dhis.node.NodeUtils) Model(org.springframework.ui.Model) HttpServletRequest(javax.servlet.http.HttpServletRequest) Lists(com.google.common.collect.Lists) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) Map(java.util.Map) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) WebMessageUtils(org.hisp.dhis.dxf2.webmessage.WebMessageUtils) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) ContextUtils(org.hisp.dhis.webapi.utils.ContextUtils) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) NotFoundException(org.hisp.dhis.webapi.controller.exception.NotFoundException) ContextService(org.hisp.dhis.webapi.service.ContextService) OrganisationUnitSelectionMode(org.hisp.dhis.common.OrganisationUnitSelectionMode) FieldFilterService(org.hisp.dhis.fieldfilter.FieldFilterService) MediaType(org.springframework.http.MediaType) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IOException(java.io.IOException) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) Collectors(java.util.stream.Collectors) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ProgramStatus(org.hisp.dhis.program.ProgramStatus) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) RootNode(org.hisp.dhis.node.types.RootNode) TextUtils(org.hisp.dhis.commons.util.TextUtils) InputStream(java.io.InputStream) RootNode(org.hisp.dhis.node.types.RootNode) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with ProgramInstanceQueryParams

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

the class AbstractEnrollmentService method addEnrollment.

@Override
public ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(entityInstance);
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
    params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    params.setSkipPaging(true);
    params.setProgram(program);
    params.setTrackedEntityInstance(entityInstance);
    params.setProgramStatus(ProgramStatus.ACTIVE);
    List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
    if (!enrollments.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have an active enrollment in program " + program.getUid());
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getOnlyEnrollOnce()) {
        params.setProgramStatus(ProgramStatus.COMPLETED);
        enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
        if (!enrollments.isEmpty()) {
            importSummary.setStatus(ImportStatus.ERROR);
            importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have a completed enrollment in program " + program.getUid() + ", and this program is" + " configured to only allow enrolling one time.");
            importSummary.incrementIgnored();
            return importSummary;
        }
    }
    Set<ImportConflict> importConflicts = new HashSet<>();
    importConflicts.addAll(checkAttributes(enrollment, importOptions));
    importSummary.setConflicts(importConflicts);
    if (!importConflicts.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.incrementIgnored();
        return importSummary;
    }
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
    ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
    if (programInstance == null) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance() + " into program " + enrollment.getProgram());
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getCaptureCoordinates()) {
        if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
            programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
            programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
        } else {
            programInstance.setLatitude(null);
            programInstance.setLongitude(null);
        }
    }
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstance.setFollowup(enrollment.getFollowup());
    programInstanceService.updateProgramInstance(programInstance);
    manager.update(programInstance.getEntityInstance());
    saveTrackedEntityComment(programInstance, enrollment);
    importSummary.setReference(programInstance.getUid());
    importSummary.getImportCount().incrementImported();
    importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
    importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
    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) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 4 with ProgramInstanceQueryParams

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

the class ActivityReportingServiceImpl method getPatientModel.

// get patient model for LWUIT
private org.hisp.dhis.api.mobile.model.LWUITmodel.Patient getPatientModel(TrackedEntityInstance patient) {
    org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patientModel = new org.hisp.dhis.api.mobile.model.LWUITmodel.Patient();
    List<org.hisp.dhis.api.mobile.model.PatientAttribute> patientAtts = new ArrayList<>();
    List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance> mobileProgramInstanceList = new ArrayList<>();
    List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance> mobileCompletedProgramInstanceList = new ArrayList<>();
    patientModel.setId(patient.getId());
    if (patient.getOrganisationUnit() != null) {
        patientModel.setOrganisationUnitName(patient.getOrganisationUnit().getName());
    }
    if (patient.getTrackedEntity() != null) {
        patientModel.setTrackedEntityName(patient.getTrackedEntity().getName());
    } else {
        patientModel.setTrackedEntityName("");
    }
    List<TrackedEntityAttributeValue> atts = new ArrayList<>(patient.getTrackedEntityAttributeValues());
    for (TrackedEntityAttributeValue value : atts) {
        if (value != null) {
            org.hisp.dhis.api.mobile.model.PatientAttribute patientAttribute = new org.hisp.dhis.api.mobile.model.PatientAttribute(value.getAttribute().getName(), value.getValue(), null, false, value.getAttribute().getDisplayInListNoProgram(), new OptionSet());
            patientAttribute.setType(value.getAttribute().getValueType());
            patientAtts.add(patientAttribute);
        }
    }
    patientModel.setAttributes(patientAtts);
    List<ProgramInstance> listOfProgramInstance = new ArrayList<>(programInstanceService.getProgramInstances(new ProgramInstanceQueryParams().setTrackedEntityInstance(patient).setProgramStatus(ProgramStatus.ACTIVE).setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL)));
    if (listOfProgramInstance.size() > 0) {
        for (ProgramInstance each : listOfProgramInstance) {
            mobileProgramInstanceList.add(getMobileProgramInstance(each));
        }
    }
    patientModel.setEnrollmentPrograms(mobileProgramInstanceList);
    List<ProgramInstance> listOfCompletedProgramInstance = new ArrayList<>(programInstanceService.getProgramInstances(new ProgramInstanceQueryParams().setTrackedEntityInstance(patient).setProgramStatus(ProgramStatus.COMPLETED).setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL)));
    if (listOfCompletedProgramInstance.size() > 0) {
        for (ProgramInstance each : listOfCompletedProgramInstance) {
            mobileCompletedProgramInstanceList.add(getMobileProgramInstance(each));
        }
    }
    patientModel.setCompletedPrograms(mobileCompletedProgramInstanceList);
    // Set Relationship
    Collection<Relationship> relationships = relationshipService.getRelationshipsForTrackedEntityInstance(patient);
    List<org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship> relationshipList = new ArrayList<>();
    for (Relationship eachRelationship : relationships) {
        org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship relationshipMobile = new org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship();
        relationshipMobile.setId(eachRelationship.getId());
        if (eachRelationship.getEntityInstanceA().getId() == patient.getId()) {
            relationshipMobile.setName(eachRelationship.getRelationshipType().getaIsToB());
            relationshipMobile.setaIsToB(eachRelationship.getRelationshipType().getaIsToB());
            relationshipMobile.setbIsToA(eachRelationship.getRelationshipType().getbIsToA());
            relationshipMobile.setPersonBId(eachRelationship.getEntityInstanceB().getId());
        } else {
            relationshipMobile.setName(eachRelationship.getRelationshipType().getbIsToA());
            relationshipMobile.setaIsToB(eachRelationship.getRelationshipType().getbIsToA());
            relationshipMobile.setbIsToA(eachRelationship.getRelationshipType().getaIsToB());
            relationshipMobile.setPersonBId(eachRelationship.getEntityInstanceA().getId());
        }
        // get relative's name
        TrackedEntityInstance relative = entityInstanceService.getTrackedEntityInstance(relationshipMobile.getPersonBId());
        List<TrackedEntityAttributeValue> attributes = new ArrayList<>(relative.getTrackedEntityAttributeValues());
        List<TrackedEntityAttributeValue> attributesInList = new ArrayList<>();
        for (TrackedEntityAttributeValue value : attributes) {
            if (value != null && value.getAttribute().getDisplayInListNoProgram()) {
                attributesInList.add(value);
            }
        }
        Collections.sort(attributesInList, new TrackedEntityAttributeValueSortOrderComparator());
        String relativeName = "";
        for (TrackedEntityAttributeValue value : attributesInList) {
            if (value != null && value.getAttribute().getDisplayInListNoProgram()) {
                relativeName += value.getValue() + " ";
            }
        }
        relationshipMobile.setPersonBName(relativeName);
        relationshipList.add(relationshipMobile);
    }
    patientModel.setRelationships(relationshipList);
    return patientModel;
}
Also used : Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityAttributeValueSortOrderComparator(org.hisp.dhis.api.mobile.model.comparator.TrackedEntityAttributeValueSortOrderComparator) ArrayList(java.util.ArrayList) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Relationship(org.hisp.dhis.relationship.Relationship) OptionSet(org.hisp.dhis.api.mobile.model.OptionSet) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams)

Aggregations

ProgramInstanceQueryParams (org.hisp.dhis.program.ProgramInstanceQueryParams)4 ArrayList (java.util.ArrayList)3 ProgramInstance (org.hisp.dhis.program.ProgramInstance)3 Patient (org.hisp.dhis.api.mobile.model.LWUITmodel.Patient)2 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)2 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 Program (org.hisp.dhis.program.Program)2 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 OptionSet (org.hisp.dhis.api.mobile.model.OptionSet)1