Search in sources :

Example 31 with TrackedEntityInstance

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

the class ActivityReportingServiceImpl method findPatients.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList findPatients(String patientIds) throws NotAllowedException {
    PatientList patientlist = new PatientList();
    while (patientIds.length() > 0) {
        int patientId = Integer.parseInt(patientIds.substring(0, patientIds.indexOf("$")));
        TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
        patientlist.getPatientList().add(getPatientModel(patient));
        patientIds = patientIds.substring(patientIds.indexOf("$") + 1, patientIds.length());
    }
    return patientlist;
}
Also used : PatientList(org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 32 with TrackedEntityInstance

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

the class ActivityReportingServiceImpl method enrollProgram.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient enrollProgram(String enrollInfo, List<org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage> mobileProgramStageList, Date incidentDate) throws NotAllowedException {
    String[] enrollProgramInfo = enrollInfo.split("-");
    int patientId = Integer.parseInt(enrollProgramInfo[0]);
    int programId = Integer.parseInt(enrollProgramInfo[1]);
    TrackedEntityInstance patient = entityInstanceService.getTrackedEntityInstance(patientId);
    Program program = programService.getProgram(programId);
    ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(patient, program, new Date(), incidentDate, patient.getOrganisationUnit());
    Iterator<ProgramStage> programStagesIterator = program.getProgramStages().iterator();
    for (int i = 0; i < program.getProgramStages().size(); i++) {
        ProgramStage programStage = programStagesIterator.next();
        if (programStage.getAutoGenerateEvent()) {
            ProgramStageInstance programStageInstance = programStageInstanceService.createProgramStageInstance(programInstance, programStage, new Date(), incidentDate, patient.getOrganisationUnit());
            int programStageInstanceId = programStageInstance.getId();
            // Inject Datavalue avaiable on-the-fly
            if (mobileProgramStageList != null && mobileProgramStageList.size() > 0) {
                org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramStage mobileProgramStage = mobileProgramStageList.get(i);
                if (mobileProgramStage != null && mobileProgramStage.getDataElements().size() > 0) {
                    mobileProgramStage.setId(programStageInstanceId);
                    if (mobileProgramStage.isSingleEvent()) {
                        this.saveProgramStage(mobileProgramStage, patientId, patient.getOrganisationUnit().getId());
                    } else {
                        this.saveProgramStage(mobileProgramStage, patientId, 0);
                    }
                }
            }
            programInstance.getProgramStageInstances().add(programStageInstance);
        }
    }
    programInstanceService.updateProgramInstance(programInstance);
    patient.getProgramInstances().add(programInstance);
    entityInstanceService.updateTrackedEntityInstance(patient);
    patient = entityInstanceService.getTrackedEntityInstance(patientId);
    return getPatientModel(patient);
}
Also used : Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 33 with TrackedEntityInstance

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

the class TrackedEntityRegistrationSMSListener method receive.

@Transactional
@Override
public void receive(IncomingSms sms) {
    String message = sms.getText();
    SMSCommand smsCommand = smsCommandService.getSMSCommand(SmsUtils.getCommandString(sms), ParserType.TRACKED_ENTITY_REGISTRATION_PARSER);
    Map<String, String> parsedMessage = this.parse(message, smsCommand);
    Date date = SmsUtils.lookForDate(message);
    String senderPhoneNumber = StringUtils.replace(sms.getOriginator(), "+", "");
    Collection<OrganisationUnit> orgUnits = SmsUtils.getOrganisationUnitsByPhoneNumber(senderPhoneNumber, userService.getUsersByPhoneNumber(senderPhoneNumber));
    if (orgUnits == null || orgUnits.size() == 0) {
        if (StringUtils.isEmpty(smsCommand.getNoUserMessage())) {
            throw new SMSParserException(SMSCommand.NO_USER_MESSAGE);
        } else {
            throw new SMSParserException(smsCommand.getNoUserMessage());
        }
    }
    OrganisationUnit orgUnit = SmsUtils.selectOrganisationUnit(orgUnits, parsedMessage, smsCommand);
    TrackedEntityInstance trackedEntityInstance = new TrackedEntityInstance();
    trackedEntityInstance.setOrganisationUnit(orgUnit);
    trackedEntityInstance.setTrackedEntity(trackedEntityService.getTrackedEntityByName("Person"));
    Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
    for (SMSCode code : smsCommand.getCodes()) {
        if (parsedMessage.containsKey(code.getCode().toUpperCase())) {
            TrackedEntityAttributeValue trackedEntityAttributeValue = this.createTrackedEntityAttributeValue(parsedMessage, code, smsCommand, trackedEntityInstance);
            patientAttributeValues.add(trackedEntityAttributeValue);
        }
    }
    int trackedEntityInstanceId = 0;
    if (patientAttributeValues.size() > 0) {
        trackedEntityInstanceId = trackedEntityInstanceService.createTrackedEntityInstance(trackedEntityInstance, null, null, patientAttributeValues);
    }
    programInstanceService.enrollTrackedEntityInstance(trackedEntityInstanceService.getTrackedEntityInstance(trackedEntityInstanceId), smsCommand.getProgram(), new Date(), date, orgUnit);
    smsSender.sendMessage(null, "Entity Registered Successfully ", senderPhoneNumber);
    sms.setStatus(SmsMessageStatus.PROCESSED);
    sms.setParsed(true);
    incomingSmsService.update(sms);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) SMSCommand(org.hisp.dhis.sms.command.SMSCommand) SMSParserException(org.hisp.dhis.sms.parse.SMSParserException) SMSCode(org.hisp.dhis.sms.command.code.SMSCode) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

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