use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class ProgramMessageStoreTest method setUpTest.
// -------------------------------------------------------------------------
// Prerequisite
// -------------------------------------------------------------------------
@Override
public void setUpTest() {
ouA = createOrganisationUnit('A');
ouB = createOrganisationUnit('B');
orgUnitService.addOrganisationUnit(ouA);
orgUnitService.addOrganisationUnit(ouB);
programA = createProgram('A', new HashSet<>(), ouA);
programService.addProgram(programA);
ProgramStage stageA = new ProgramStage("StageA", programA);
stageA.setSortOrder(1);
programStageService.saveProgramStage(stageA);
Set<ProgramStage> programStages = new HashSet<>();
programStages.add(stageA);
programA.setProgramStages(programStages);
programService.updateProgram(programA);
entityInstanceA = createTrackedEntityInstance('A', ouA);
entityInstanceService.addTrackedEntityInstance(entityInstanceA);
TrackedEntityInstance entityInstanceB = createTrackedEntityInstance('B', ouA);
entityInstanceService.addTrackedEntityInstance(entityInstanceB);
DateTime testDate1 = DateTime.now();
testDate1.withTimeAtStartOfDay();
testDate1 = testDate1.minusDays(70);
incidentDate = testDate1.toDate();
DateTime testDate2 = DateTime.now();
testDate2.withTimeAtStartOfDay();
enrollmentDate = testDate2.toDate();
programInstanceA = new ProgramInstance(enrollmentDate, incidentDate, entityInstanceA, programA);
programInstanceA.setUid("UID-A");
programStageInstanceA = new ProgramStageInstance(programInstanceA, stageA);
programStageInstanceA.setDueDate(enrollmentDate);
programStageInstanceA.setUid("UID-A");
Set<OrganisationUnit> ouSet = new HashSet<>();
ouSet.add(ouA);
Set<String> ouUids = new HashSet<>();
ouUids.add(ouA.getUid());
// ouSet.add( ouB );
teiA = createTrackedEntityInstance('Z', ouA);
teiService.addTrackedEntityInstance(teiA);
recipientsA = new ProgramMessageRecipients();
recipientsA.setOrganisationUnit(ouA);
recipientsA.setTrackedEntityInstance(teiA);
recipientsB = new ProgramMessageRecipients();
recipientsB.setOrganisationUnit(ouA);
recipientsB.setTrackedEntityInstance(teiA);
recipientsC = new ProgramMessageRecipients();
recipientsC.setOrganisationUnit(ouA);
recipientsC.setTrackedEntityInstance(teiA);
Set<String> phoneNumberListA = new HashSet<>();
phoneNumberListA.add(msisdn);
recipientsA.setPhoneNumbers(phoneNumberListA);
Set<String> phoneNumberListB = new HashSet<>();
phoneNumberListB.add(msisdn);
recipientsB.setPhoneNumbers(phoneNumberListB);
Set<String> phoneNumberListC = new HashSet<>();
phoneNumberListC.add(msisdn);
recipientsC.setPhoneNumbers(phoneNumberListC);
channels.add(DeliveryChannel.SMS);
pmsgA = new ProgramMessage();
pmsgA.setText(text);
pmsgA.setSubject(subject);
pmsgA.setRecipients(recipientsA);
pmsgA.setMessageStatus(messageStatus);
pmsgA.setDeliveryChannels(channels);
pmsgB = new ProgramMessage();
pmsgB.setText(text);
pmsgB.setSubject(subject);
pmsgB.setRecipients(recipientsB);
pmsgB.setMessageStatus(messageStatus);
pmsgB.setDeliveryChannels(channels);
pmsgC = new ProgramMessage();
pmsgC.setText(text);
pmsgC.setSubject(subject);
pmsgC.setRecipients(recipientsC);
pmsgC.setMessageStatus(messageStatus);
pmsgC.setDeliveryChannels(channels);
uidA = CodeGenerator.generateCode(10);
uidB = CodeGenerator.generateCode(10);
uidC = CodeGenerator.generateCode(10);
pmsgA.setUid(uidA);
pmsgB.setUid(uidB);
pmsgC.setUid(uidC);
params = new ProgramMessageQueryParams();
params.setOrganisationUnit(ouUids);
}
use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AuditController method getTrackedEntityAttributeValueAudit.
@RequestMapping(value = "trackedEntityAttributeValue", method = RequestMethod.GET)
@ResponseBody
public RootNode getTrackedEntityAttributeValueAudit(@RequestParam(required = false, defaultValue = "") List<String> tea, @RequestParam(required = false, defaultValue = "") List<String> tei, @RequestParam(required = false) AuditType auditType, @RequestParam(required = false) boolean skipPaging, @RequestParam(required = false, defaultValue = "50") int pageSize, @RequestParam(required = false, defaultValue = "1") int page) throws WebMessageException {
List<String> fields = Lists.newArrayList(contextService.getParameterValues("fields"));
List<TrackedEntityAttribute> trackedEntityAttributes = getTrackedEntityAttributes(tea);
List<TrackedEntityInstance> trackedEntityInstances = getTrackedEntityInstances(tei);
List<TrackedEntityAttributeValueAudit> attributeValueAudits;
Pager pager = null;
if (skipPaging) {
attributeValueAudits = trackedEntityAttributeValueAuditService.getTrackedEntityAttributeValueAudits(trackedEntityAttributes, trackedEntityInstances, auditType);
} else {
int total = trackedEntityAttributeValueAuditService.countTrackedEntityAttributeValueAudits(trackedEntityAttributes, trackedEntityInstances, auditType);
pager = new Pager(page, total, pageSize);
attributeValueAudits = trackedEntityAttributeValueAuditService.getTrackedEntityAttributeValueAudits(trackedEntityAttributes, trackedEntityInstances, auditType, pager.getOffset(), pager.getPageSize());
}
RootNode rootNode = NodeUtils.createMetadata();
if (pager != null) {
rootNode.addChild(NodeUtils.createPager(pager));
}
CollectionNode trackedEntityAttributeValueAudits = rootNode.addChild(new CollectionNode("trackedEntityAttributeValueAudits", true));
trackedEntityAttributeValueAudits.addChildren(fieldFilterService.filter(TrackedEntityAttributeValueAudit.class, attributeValueAudits, fields).getChildren());
return rootNode;
}
use of org.hisp.dhis.trackedentity.TrackedEntityInstance 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.trackedentity.TrackedEntityInstance 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);
}
use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method findVisitSchedule.
@Override
public String findVisitSchedule(int orgUnitId, int programId, String info) throws NotAllowedException {
String status = info.substring(0, info.indexOf("$"));
String fromDays = info.substring(info.indexOf("$") + 1, info.indexOf("/"));
String toDays = info.substring(info.indexOf("/") + 1);
// Event Status
EventStatus eventStatus = null;
if (status.equals("Schedule in future")) {
eventStatus = EventStatus.SCHEDULE;
} else if (status.equals("Overdue")) {
eventStatus = EventStatus.OVERDUE;
} else if (status.equals("Incomplete")) {
eventStatus = EventStatus.VISITED;
} else if (status.equals("Completed")) {
eventStatus = EventStatus.COMPLETED;
} else if (status.equals("Skipped")) {
eventStatus = EventStatus.SKIPPED;
}
// From/To Date
Date fromDate = getDate(-1, fromDays);
Date toDate = getDate(1, toDays);
TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
List<TrackedEntityAttribute> trackedEntityAttributeList = new ArrayList<>(attributeService.getTrackedEntityAttributesByDisplayOnVisitSchedule(true));
for (TrackedEntityAttribute trackedEntityAttribute : trackedEntityAttributeList) {
QueryItem queryItem = new QueryItem(trackedEntityAttribute);
param.addAttribute(queryItem);
}
param.setProgram(programService.getProgram(programId));
param.addOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
param.setEventStatus(eventStatus);
param.setEventStartDate(fromDate);
param.setEventEndDate(toDate);
Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
List<List<Object>> listOfListProgramStageInstance = programStageInstanceGrid.getRows();
if (listOfListProgramStageInstance.size() == 0) {
throw NotAllowedException.NO_EVENT_FOUND;
}
String eventsInfo = "";
for (List<Object> row : listOfListProgramStageInstance) {
TrackedEntityInstance instance = entityInstanceService.getTrackedEntityInstance((String) row.get(0));
Collection<TrackedEntityAttribute> displayAttributes = attributeService.getTrackedEntityAttributesDisplayInList();
eventsInfo += instance.getId() + "/";
String displayName = "";
for (TrackedEntityAttribute displayAttribute : displayAttributes) {
TrackedEntityAttributeValue value = attValueService.getTrackedEntityAttributeValue(instance, displayAttribute);
if (value != null) {
displayName += value.getValue() + " ";
}
}
eventsInfo += displayName.trim() + "$";
}
return eventsInfo;
}
Aggregations