use of org.hisp.dhis.program.ProgramStageInstance in project dhis2-core by dhis2.
the class NoRegistrationSingleEventServiceTest method testGetPersonsByProgramStageInstance.
@Test
public void testGetPersonsByProgramStageInstance() {
Event event = createEvent(programA.getUid(), organisationUnitA.getUid());
ImportSummary importSummary = eventService.addEvent(event, null);
assertEquals(ImportStatus.SUCCESS, importSummary.getStatus());
assertNotNull(importSummary.getReference());
ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(importSummary.getReference());
assertNotNull(programStageInstance);
assertNotNull(eventService.getEvent(programStageInstance));
}
use of org.hisp.dhis.program.ProgramStageInstance 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;
}
}
}
}
use of org.hisp.dhis.program.ProgramStageInstance in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method isAllProgramStageFinished.
private boolean isAllProgramStageFinished(ProgramStageInstance programStageInstance) {
ProgramInstance programInstance = programStageInstance.getProgramInstance();
Collection<ProgramStageInstance> programStageInstances = programInstance.getProgramStageInstances();
if (programStageInstances != null) {
if (programStageInstances.size() < programInstance.getProgram().getProgramStages().size()) {
return false;
}
Iterator<ProgramStageInstance> iterator = programStageInstances.iterator();
while (iterator.hasNext()) {
ProgramStageInstance each = iterator.next();
if (!each.isCompleted()) {
return false;
}
}
}
return true;
}
use of org.hisp.dhis.program.ProgramStageInstance 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;
}
}
use of org.hisp.dhis.program.ProgramStageInstance 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);
}
Aggregations