use of org.hisp.dhis.program.ProgramInstance 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;
}
use of org.hisp.dhis.program.ProgramInstance in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method completeProgramInstance.
@Override
public String completeProgramInstance(int programId) throws NotAllowedException {
ProgramInstance programInstance = programInstanceService.getProgramInstance(programId);
programInstance.setStatus(ProgramStatus.COMPLETED);
programInstanceService.updateProgramInstance(programInstance);
return PROGRAM_COMPLETED;
}
use of org.hisp.dhis.program.ProgramInstance 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);
}
use of org.hisp.dhis.program.ProgramInstance in project dhis2-core by dhis2.
the class SingleEventListener method registerEvent.
// -------------------------------------------------------------------------
// Supportive Methods
// -------------------------------------------------------------------------
private void registerEvent(Map<String, String> commandValuePairs, SMSCommand smsCommand, IncomingSms sms) {
OrganisationUnit orgUnit = getOrganisationUnits(sms).iterator().next();
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(smsCommand.getProgram(), ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
ProgramInstance pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(smsCommand.getProgram());
pi.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(pi);
programInstances.add(pi);
} else if (programInstances.size() > 1) {
update(sms, SmsMessageStatus.FAILED, false);
sendFeedback("Multiple active program instances exists for program: " + smsCommand.getProgram().getUid(), sms.getOriginator(), ERROR);
return;
}
ProgramInstance programInstance = null;
programInstance = programInstances.get(0);
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setOrganisationUnit(orgUnit);
programStageInstance.setProgramStage(smsCommand.getProgramStage());
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setExecutionDate(sms.getSentDate());
programStageInstance.setDueDate(sms.getSentDate());
programStageInstance.setAttributeOptionCombo(dataElementCategoryService.getDefaultDataElementCategoryOptionCombo());
programStageInstance.setCompletedBy("DHIS 2");
programStageInstanceService.addProgramStageInstance(programStageInstance);
for (SMSCode smsCode : smsCommand.getCodes()) {
TrackedEntityDataValue dataValue = new TrackedEntityDataValue();
dataValue.setAutoFields();
dataValue.setDataElement(smsCode.getDataElement());
dataValue.setProgramStageInstance(programStageInstance);
dataValue.setValue(commandValuePairs.get(smsCode.getCode()));
trackedEntityDataValueService.saveTrackedEntityDataValue(dataValue);
}
update(sms, SmsMessageStatus.PROCESSED, true);
sendFeedback(StringUtils.defaultIfEmpty(smsCommand.getSuccessMessage(), EVENT_REGISTERED), sms.getOriginator(), INFO);
}
Aggregations