Search in sources :

Example 16 with ProgramInstance

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 17 with ProgramInstance

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;
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) Program(org.hisp.dhis.program.Program) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) ProgramTrackedEntityAttribute(org.hisp.dhis.program.ProgramTrackedEntityAttribute) ProgramInstance(org.hisp.dhis.program.ProgramInstance) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ProgramStage(org.hisp.dhis.program.ProgramStage) Date(java.util.Date)

Example 18 with ProgramInstance

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);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 19 with ProgramInstance

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;
}
Also used : ProgramMessageService(org.hisp.dhis.program.message.ProgramMessageService) NotificationMessageRenderer(org.hisp.dhis.notification.NotificationMessageRenderer) Date(java.util.Date) ProgramMessage(org.hisp.dhis.program.message.ProgramMessage) BatchResponseStatus(org.hisp.dhis.outboundmessage.BatchResponseStatus) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) MessageService(org.hisp.dhis.message.MessageService) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) ProgramInstance(org.hisp.dhis.program.ProgramInstance) User(org.hisp.dhis.user.User) Nullable(javax.annotation.Nullable) MessageType(org.hisp.dhis.message.MessageType) ProgramInstanceStore(org.hisp.dhis.program.ProgramInstanceStore) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Clock(org.hisp.dhis.system.util.Clock) List(java.util.List) DeliveryChannel(org.hisp.dhis.common.DeliveryChannel) ProgramStageInstanceStore(org.hisp.dhis.program.ProgramStageInstanceStore) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) NotificationMessage(org.hisp.dhis.notification.NotificationMessage) Transactional(org.springframework.transaction.annotation.Transactional) ProgramMessageRecipients(org.hisp.dhis.program.message.ProgramMessageRecipients) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance)

Example 20 with ProgramInstance

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");
    }
}
Also used : Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) HashSet(java.util.HashSet)

Aggregations

ProgramInstance (org.hisp.dhis.program.ProgramInstance)24 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)10 Date (java.util.Date)9 Program (org.hisp.dhis.program.Program)9 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)7 ProgramStage (org.hisp.dhis.program.ProgramStage)7 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)7 HashSet (java.util.HashSet)5 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)5 ArrayList (java.util.ArrayList)4 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 ProgramInstanceQueryParams (org.hisp.dhis.program.ProgramInstanceQueryParams)3 Patient (org.hisp.dhis.api.mobile.model.LWUITmodel.Patient)2 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)2 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)2 Relationship (org.hisp.dhis.relationship.Relationship)2 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)2 TrackedEntityAttributeValue (org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue)2 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)2 Lists (com.google.common.collect.Lists)1