use of org.hisp.dhis.program.ProgramInstance in project dhis2-core by dhis2.
the class AbstractEnrollmentService method addEnrollment.
@Override
public ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(entityInstance);
Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
params.setSkipPaging(true);
params.setProgram(program);
params.setTrackedEntityInstance(entityInstance);
params.setProgramStatus(ProgramStatus.ACTIVE);
List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
if (!enrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have an active enrollment in program " + program.getUid());
importSummary.incrementIgnored();
return importSummary;
}
if (program.getOnlyEnrollOnce()) {
params.setProgramStatus(ProgramStatus.COMPLETED);
enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
if (!enrollments.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have a completed enrollment in program " + program.getUid() + ", and this program is" + " configured to only allow enrolling one time.");
importSummary.incrementIgnored();
return importSummary;
}
}
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkAttributes(enrollment, importOptions));
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.incrementIgnored();
return importSummary;
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
if (programInstance == null) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance() + " into program " + enrollment.getProgram());
importSummary.incrementIgnored();
return importSummary;
}
if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
importSummary.incrementIgnored();
return importSummary;
}
if (program.getCaptureCoordinates()) {
if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
} else {
programInstance.setLatitude(null);
programInstance.setLongitude(null);
}
}
updateAttributeValues(enrollment, importOptions);
updateDateFields(enrollment, programInstance);
programInstance.setFollowup(enrollment.getFollowup());
programInstanceService.updateProgramInstance(programInstance);
manager.update(programInstance.getEntityInstance());
saveTrackedEntityComment(programInstance, enrollment);
importSummary.setReference(programInstance.getUid());
importSummary.getImportCount().incrementImported();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
return importSummary;
}
use of org.hisp.dhis.program.ProgramInstance in project dhis2-core by dhis2.
the class AddProgramAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
displayIncidentDate = (displayIncidentDate == null) ? false : displayIncidentDate;
ignoreOverdueEvents = (ignoreOverdueEvents == null) ? false : ignoreOverdueEvents;
onlyEnrollOnce = (onlyEnrollOnce == null) ? false : onlyEnrollOnce;
selectEnrollmentDatesInFuture = (selectEnrollmentDatesInFuture == null) ? false : selectEnrollmentDatesInFuture;
selectIncidentDatesInFuture = (selectIncidentDatesInFuture == null) ? false : selectIncidentDatesInFuture;
Program program = new Program();
program.setName(StringUtils.trimToNull(name));
program.setShortName(StringUtils.trimToNull(shortName));
program.setDescription(StringUtils.trimToNull(description));
program.setVersion(1);
program.setEnrollmentDateLabel(StringUtils.trimToNull(enrollmentDateLabel));
program.setIncidentDateLabel(StringUtils.trimToNull(incidentDateLabel));
program.setProgramType(programType);
program.setDisplayIncidentDate(displayIncidentDate);
program.setOnlyEnrollOnce(onlyEnrollOnce);
program.setSelectEnrollmentDatesInFuture(selectEnrollmentDatesInFuture);
program.setSelectIncidentDatesInFuture(selectIncidentDatesInFuture);
program.setSkipOffline(skipOffline);
program.setDisplayFrontPageList(displayFrontPageList);
program.setUseFirstStageDuringRegistration(useFirstStageDuringRegistration);
program.setCaptureCoordinates(captureCoordinates);
program.setExpiryDays(expiryDays);
program.setCompleteEventsExpiryDays(completeEventsExpiryDays);
if (programType == ProgramType.WITH_REGISTRATION) {
program.setIgnoreOverdueEvents(ignoreOverdueEvents);
} else {
program.setIgnoreOverdueEvents(false);
}
periodTypeName = StringUtils.trimToNull(periodTypeName);
if (periodTypeName != null) {
PeriodType periodType = PeriodType.getPeriodTypeByName(periodTypeName);
program.setExpiryPeriodType(periodService.getPeriodTypeByClass(periodType.getClass()));
} else {
program.setExpiryPeriodType(null);
}
if (relationshipTypeId != null) {
RelationshipType relationshipType = relationshipTypeService.getRelationshipType(relationshipTypeId);
program.setRelationshipType(relationshipType);
program.setRelationshipFromA(relationshipFromA);
program.setRelationshipText(relationshipText);
Program relatedProgram = programService.getProgram(relatedProgramId);
program.setRelatedProgram(relatedProgram);
} else {
program.setRelationshipType(null);
program.setRelationshipFromA(null);
program.setRelationshipText(null);
program.setRelatedProgram(null);
}
if (trackedEntityId != null) {
TrackedEntity trackedEntity = trackedEntityService.getTrackedEntity(trackedEntityId);
program.setTrackedEntity(trackedEntity);
}
if (categoryComboId != null) {
program.setCategoryCombo(categoryService.getDataElementCategoryCombo(categoryComboId));
}
if (workflowId != null && workflowId > 0) {
program.setWorkflow(dataApprovalService.getWorkflow(workflowId));
}
programService.addProgram(program);
int index = 0;
for (String selectedPropertyId : selectedPropertyIds) {
String[] ids = selectedPropertyId.split("_");
if (ids[0].equals(TrackedEntityInstance.PREFIX_TRACKED_ENTITY_ATTRIBUTE)) {
TrackedEntityAttribute attribute = trackedEntityAttributeService.getTrackedEntityAttribute(Integer.parseInt(ids[1]));
ProgramTrackedEntityAttribute programAttribute = new ProgramTrackedEntityAttribute(program, attribute, personDisplayNames.get(index), mandatory.get(index), allowFutureDate.get(index));
programAttribute.setRenderOptionsAsRadio(renderOptionsAsRadios.get(index));
programAttribute.setAutoFields();
program.getProgramAttributes().add(programAttribute);
}
index++;
}
if (jsonAttributeValues != null) {
attributeService.updateAttributeValues(program, jsonAttributeValues);
}
programService.updateProgram(program);
if (program.isWithoutRegistration()) {
ProgramStage programStage = new ProgramStage();
programStage.setName(name);
programStage.setDescription(description);
programStage.setProgram(program);
programStage.setMinDaysFromStart(0);
programStage.setAutoGenerateEvent(true);
programStage.setExecutionDateLabel(REPORT_DATE_DESCRIPTION);
programStageService.saveProgramStage(programStage);
}
if (program.isWithoutRegistration()) {
// Add a new program-instance
ProgramInstance programInstance = new ProgramInstance();
programInstance.setEnrollmentDate(new Date());
programInstance.setIncidentDate(new Date());
programInstance.setProgram(program);
programInstance.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(programInstance);
}
return SUCCESS;
}
use of org.hisp.dhis.program.ProgramInstance in project dhis2-core by dhis2.
the class PdfDataEntryFormImportUtil method addNewProgramStageInstance.
private int addNewProgramStageInstance(int programStageId, int organisationUnitId, Date executionDate) throws Exception {
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
if (organisationUnit == null) {
throw new Exception("Invalid organisation unit identifier: " + organisationUnitId);
}
ProgramStage programStage = programStageService.getProgramStage(programStageId);
Program program = programStage.getProgram();
ProgramInstance programInstance = programInstanceService.getProgramInstances(program).iterator().next();
ProgramStageInstance programStageInstance = new ProgramStageInstance();
programStageInstance.setProgramInstance(programInstance);
programStageInstance.setProgramStage(programStage);
programStageInstance.setDueDate(executionDate);
programStageInstance.setExecutionDate(executionDate);
programStageInstance.setOrganisationUnit(organisationUnit);
return programStageInstanceService.addProgramStageInstance(programStageInstance);
}
use of org.hisp.dhis.program.ProgramInstance in project dhis2-core by dhis2.
the class DefaultProgramNotificationService method resolveProgramMessageRecipients.
private ProgramMessageRecipients resolveProgramMessageRecipients(ProgramNotificationTemplate template, OrganisationUnit organisationUnit, ProgramInstance programInstance) {
ProgramMessageRecipients recipients = new ProgramMessageRecipients();
TrackedEntityInstance trackedEntityInstance = programInstance.getEntityInstance();
ProgramNotificationRecipient recipientType = template.getNotificationRecipient();
if (recipientType == ProgramNotificationRecipient.ORGANISATION_UNIT_CONTACT) {
recipients.setOrganisationUnit(organisationUnit);
} else if (recipientType == ProgramNotificationRecipient.TRACKED_ENTITY_INSTANCE) {
recipients.setTrackedEntityInstance(trackedEntityInstance);
} else if (recipientType == ProgramNotificationRecipient.PROGRAM_ATTRIBUTE && template.getRecipientProgramAttribute() != null) {
List<String> recipientList = programInstance.getEntityInstance().getTrackedEntityAttributeValues().stream().filter(av -> template.getRecipientProgramAttribute().getUid().equals(av.getAttribute().getUid())).map(av -> av.getPlainValue()).collect(Collectors.toList());
if (template.getDeliveryChannels().contains(DeliveryChannel.SMS)) {
recipients.getPhoneNumbers().addAll(recipientList);
} else if (template.getDeliveryChannels().contains(DeliveryChannel.EMAIL)) {
recipients.getEmailAddresses().addAll(recipientList);
}
}
return recipients;
}
use of org.hisp.dhis.program.ProgramInstance in project dhis2-core by dhis2.
the class DefaultProgramMessageService method hasAccess.
@Override
public void hasAccess(ProgramMessageQueryParams params, User user) {
ProgramInstance programInstance = null;
Set<Program> programs = new HashSet<>();
if (params.hasProgramInstance()) {
programInstance = params.getProgramInstance();
}
if (params.hasProgramStageInstance()) {
programInstance = params.getProgramStageInstance().getProgramInstance();
}
programs = programService.getUserPrograms(user);
if (user != null && !programs.contains(programInstance.getProgram())) {
throw new IllegalQueryException("User does not have access to the required program");
}
}
Aggregations