Search in sources :

Example 1 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException 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 2 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException 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 3 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method savePatient.

@Override
public Patient savePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
    TrackedEntityInstance patientWeb = new TrackedEntityInstance();
    patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
    Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributesMobile != null) {
        for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
            TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
            patientAttributeSet.add(patientAttribute);
            TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
            patientAttributeValue.setEntityInstance(patientWeb);
            patientAttributeValue.setAttribute(patientAttribute);
            patientAttributeValue.setValue(paAtt.getValue());
            patientAttributeValues.add(patientAttributeValue);
        }
    }
    patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
    patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
    TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
    String errorMsg = null;
    try {
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
            Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
            enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
        }
        Program program = programService.getProgram(Integer.parseInt(programIdText));
        String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
        int code = Integer.parseInt(errorCode[0]);
        if (code >= 1) {
            entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
            if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
                errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
            } else {
                errorMsg = "Validation error";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (errorMsg != null) {
        throw new NotAllowedException(errorMsg);
    }
    return getPatientModel(newTrackedEntityInstance);
}
Also used : Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) HashSet(java.util.HashSet)

Example 4 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method registerRelative.

@Override
public Patient registerRelative(Patient patient, int orgUnitId, String programId) throws NotAllowedException {
    TrackedEntityInstance patientWeb = new TrackedEntityInstance();
    patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
    Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributesMobile != null) {
        for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
            TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
            patientAttributeSet.add(patientAttribute);
            TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
            patientAttributeValue.setEntityInstance(patientWeb);
            patientAttributeValue.setAttribute(patientAttribute);
            patientAttributeValue.setValue(paAtt.getValue());
            patientAttributeValues.add(patientAttributeValue);
        }
    }
    patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
    if (patient.getIdToAddRelative() != 0) {
        TrackedEntityInstance relative = entityInstanceService.getTrackedEntityInstance(patient.getIdToAddRelative());
        if (relative == null) {
            throw new NotAllowedException("relative does not exist");
        }
        patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, relative.getUid(), patient.getRelTypeIdToAdd(), patientAttributeValues);
    } else {
        patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
    }
    TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
    String errorMsg = null;
    try {
        for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
            Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
            enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
        }
        Program program = programService.getProgram(Integer.parseInt(programId));
        String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
        int code = Integer.parseInt(errorCode[0]);
        if (code >= 1) {
            entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
            if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
                errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
            } else {
                errorMsg = "Validation error";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (errorMsg != null) {
        throw new NotAllowedException(errorMsg);
    }
    if (patient.getEnrollmentRelationship() != null) {
        org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship enrollmentRelationship = patient.getEnrollmentRelationship();
        enrollmentRelationship.setPersonBId(newTrackedEntityInstance.getId());
        addRelationship(enrollmentRelationship, orgUnitId);
    }
    return getPatientModel(newTrackedEntityInstance);
}
Also used : NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) HashSet(java.util.HashSet) Program(org.hisp.dhis.program.Program) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException)

Example 5 with NotAllowedException

use of org.hisp.dhis.api.mobile.NotAllowedException in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method getAllProgramByOrgUnit.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Program getAllProgramByOrgUnit(int orgUnitId, String type) throws NotAllowedException {
    String programsInfo = "";
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
    Set<Program> tempPrograms = null;
    ProgramType programType = ProgramType.fromValue(type);
    if (programType == ProgramType.WITHOUT_REGISTRATION) {
        tempPrograms = programService.getUserPrograms(ProgramType.WITHOUT_REGISTRATION);
    } else // ProgramType.WITH_REGISTRATION
    {
        tempPrograms = programService.getUserPrograms(ProgramType.WITH_REGISTRATION);
    }
    List<Program> programs = new ArrayList<>();
    for (Program program : tempPrograms) {
        if (program.getOrganisationUnits().contains(organisationUnit)) {
            programs.add(program);
        }
    }
    if (programs.size() != 0) {
        if (programs.size() == 1) {
            Program program = programs.get(0);
            return getMobileProgramWithoutData(program);
        } else {
            for (Program program : programs) {
                if (program.getOrganisationUnits().contains(organisationUnit)) {
                    programsInfo += program.getId() + "/" + program.getName() + "$";
                }
            }
            throw new NotAllowedException(programsInfo);
        }
    } else {
        throw NotAllowedException.NO_PROGRAM_FOUND;
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) ArrayList(java.util.ArrayList) ProgramType(org.hisp.dhis.program.ProgramType)

Aggregations

NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)8 Date (java.util.Date)5 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)5 ArrayList (java.util.ArrayList)4 Program (org.hisp.dhis.program.Program)4 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)4 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)4 HashSet (java.util.HashSet)3 PatientAttribute (org.hisp.dhis.api.mobile.model.PatientAttribute)3 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)2 Calendar (java.util.Calendar)1 GregorianCalendar (java.util.GregorianCalendar)1 List (java.util.List)1 Notification (org.hisp.dhis.api.mobile.model.LWUITmodel.Notification)1 PatientList (org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList)1 Grid (org.hisp.dhis.common.Grid)1 QueryItem (org.hisp.dhis.common.QueryItem)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 EventStatus (org.hisp.dhis.event.EventStatus)1