Search in sources :

Example 11 with TrackedEntity

use of org.hisp.dhis.trackedentity.TrackedEntity 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 12 with TrackedEntity

use of org.hisp.dhis.trackedentity.TrackedEntity in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method updatePatient.

@Override
public Patient updatePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
    TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    TrackedEntityInstance tempTEI = entityInstance;
    TrackedEntity trackedEntity = null;
    Program program = programService.getProgram(Integer.parseInt(programIdText));
    trackedEntity = program.getTrackedEntity();
    entityInstance.setTrackedEntity(trackedEntity);
    // get attributes to be saved/updated/deleted
    Collection<TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes();
    List<TrackedEntityAttributeValue> valuesForSave = new ArrayList<>();
    List<TrackedEntityAttributeValue> valuesForUpdate = new ArrayList<>();
    Collection<TrackedEntityAttributeValue> valuesForDelete = null;
    TrackedEntityAttributeValue attributeValue = null;
    Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
    if (attributes != null && attributes.size() > 0) {
        valuesForDelete = attValueService.getTrackedEntityAttributeValues(entityInstance);
        tempTEI.getTrackedEntityAttributeValues().clear();
        for (TrackedEntityAttribute attribute : attributes) {
            String value = getAttributeValue(attributesMobile, attribute.getName());
            if (value != null) {
                attributeValue = attValueService.getTrackedEntityAttributeValue(entityInstance, attribute);
                if (attributeValue == null) {
                    attributeValue = new TrackedEntityAttributeValue();
                    attributeValue.setEntityInstance(entityInstance);
                    attributeValue.setAttribute(attribute);
                    attributeValue.setValue(value.trim());
                    valuesForSave.add(attributeValue);
                } else {
                    attributeValue.setValue(value.trim());
                    valuesForUpdate.add(attributeValue);
                    valuesForDelete.remove(attributeValue);
                }
                tempTEI.getTrackedEntityAttributeValues().add(attributeValue);
            }
        }
    }
    // validate
    String[] errorCode = entityInstanceService.validateTrackedEntityInstance(tempTEI, program).split("_");
    int code = Integer.parseInt(errorCode[0]);
    if (code >= 1) {
        if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
            throw new NotAllowedException("Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName());
        } else {
            throw new NotAllowedException("Validation error");
        }
    }
    entityInstanceService.updateTrackedEntityInstance(entityInstance, null, null, valuesForSave, valuesForUpdate, valuesForDelete);
    enrollProgram(patient.getId() + "-" + programIdText, null, new Date());
    entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
    entityInstance.setTrackedEntity(trackedEntity);
    return getPatientModel(entityInstance);
}
Also used : Program(org.hisp.dhis.program.Program) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) PatientAttribute(org.hisp.dhis.api.mobile.model.PatientAttribute) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date)

Example 13 with TrackedEntity

use of org.hisp.dhis.trackedentity.TrackedEntity in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method findPatientInAdvanced.

/**
     * keyword is on format of
     * {attribute-id1}:{operator1}:{filter-value1};{attribute
     * -id2}:{operator2}:{filter-value2}
     */
@Override
public String findPatientInAdvanced(String keyword, int orgUnitId, int programId) throws NotAllowedException {
    TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
    List<TrackedEntityAttribute> displayAttributes = new ArrayList<>(attributeService.getTrackedEntityAttributesDisplayInList());
    for (TrackedEntityAttribute trackedEntityAttribute : displayAttributes) {
        QueryItem queryItem = new QueryItem(trackedEntityAttribute);
        param.addAttribute(queryItem);
    }
    if (programId != 0) {
        param.setProgram(programService.getProgram(programId));
    }
    if (orgUnitId != 0) {
        param.addOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
        param.setOrganisationUnitMode(OrganisationUnitSelectionMode.SELECTED);
    } else {
        param.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    }
    String[] items = keyword.split(";");
    if (items == null) {
        items = new String[1];
        items[0] = keyword;
    }
    for (int i = 0; i < items.length; i++) {
        String[] split = keyword.split(":");
        if (split == null || (split.length != 3 && split.length != 2)) {
            throw NotAllowedException.INVALID_FILTER;
        }
        if (split.length == 2) {
            QueryOperator operator = QueryOperator.fromString(split[0]);
            param.setQuery(new QueryFilter(operator, split[1]));
        } else {
            TrackedEntityAttribute at = attributeService.getTrackedEntityAttributeByName(split[0]);
            QueryItem queryItem = new QueryItem(at, at.getLegendSets().get(0), at.getValueType(), at.getAggregationType(), at.getOptionSet());
            QueryOperator operator = QueryOperator.fromString(split[1]);
            queryItem.getFilters().add(new QueryFilter(operator, split[2]));
            param.getFilters().add(queryItem);
        }
    }
    Grid trackedEntityInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
    List<List<Object>> listOfTrackedEntityInstance = trackedEntityInstanceGrid.getRows();
    if (listOfTrackedEntityInstance.size() == 0) {
        throw NotAllowedException.NO_BENEFICIARY_FOUND;
    }
    /**
         * Grid columns: 0 = instance 1 = created 2 = lastupdated 3 = ou 4 = te
         * 5 onwards = attributes
         */
    int instanceIndex = 0;
    int teIndex = 4;
    List<Integer> attributesIndex = new ArrayList<>();
    List<GridHeader> headers = trackedEntityInstanceGrid.getHeaders();
    int index = 0;
    for (GridHeader header : headers) {
        if (header.getName().equals("instance")) {
            instanceIndex = index;
        } else if (header.getName().equals("te")) {
            teIndex = index;
        } else if (!header.getName().equals("created") && !header.getName().equals("lastupdated") && !header.getName().equals("ou")) {
            attributesIndex.add(new Integer(index));
        }
        index++;
    }
    String instanceInfo = "";
    String trackedEntityName = "";
    for (List<Object> row : listOfTrackedEntityInstance) {
        TrackedEntity te = trackedEntityService.getTrackedEntity((String) row.get(teIndex));
        if (!trackedEntityName.equals(te.getDisplayName())) {
            trackedEntityName = te.getDisplayName();
            instanceInfo += te.getDisplayName() + "$";
        }
        // NOTE: this line should be here but because the mobile client uses
        // the int TEI id, we will temprarily get the int id for now.
        // instanceInfo += (String) row.get( instanceIndex ) + "/";
        TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance((String) row.get(instanceIndex));
        instanceInfo += tei.getId() + "/";
        // end of temproary fix
        String attText = "";
        for (Integer attIndex : attributesIndex) {
            if (row.get(attIndex.intValue()) != null) {
                attText += (String) row.get(attIndex.intValue()) + " ";
            }
        }
        instanceInfo += attText.trim() + "$";
    }
    return instanceInfo;
}
Also used : QueryItem(org.hisp.dhis.common.QueryItem) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntity(org.hisp.dhis.trackedentity.TrackedEntity) Grid(org.hisp.dhis.common.Grid) ArrayList(java.util.ArrayList) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) GridHeader(org.hisp.dhis.common.GridHeader) QueryFilter(org.hisp.dhis.common.QueryFilter) List(java.util.List) PatientList(org.hisp.dhis.api.mobile.model.LWUITmodel.PatientList) ArrayList(java.util.ArrayList) QueryOperator(org.hisp.dhis.common.QueryOperator) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams)

Aggregations

TrackedEntity (org.hisp.dhis.trackedentity.TrackedEntity)13 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 Program (org.hisp.dhis.program.Program)4 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 TrackedEntityInstance (org.hisp.dhis.trackedentity.TrackedEntityInstance)3 Date (java.util.Date)2 List (java.util.List)2 DataElement (org.hisp.dhis.dataelement.DataElement)2 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)2 PeriodType (org.hisp.dhis.period.PeriodType)2 ProgramStage (org.hisp.dhis.program.ProgramStage)2 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)2 ProgramTrackedEntityAttribute (org.hisp.dhis.program.ProgramTrackedEntityAttribute)2 RelationshipType (org.hisp.dhis.relationship.RelationshipType)2 HashMap (java.util.HashMap)1 DhisSpringTest (org.hisp.dhis.DhisSpringTest)1 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)1 NotAllowedException (org.hisp.dhis.api.mobile.NotAllowedException)1