Search in sources :

Example 16 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit 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;
            }
        }
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Date(java.util.Date) NotAllowedException(org.hisp.dhis.api.mobile.NotAllowedException) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 17 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class AbstractDataSetCompletenessService method getDataSetCompleteness.

@Override
@Transactional
public List<DataSetCompletenessResult> getDataSetCompleteness(int periodId, Collection<Integer> organisationUnitIds, int dataSetId, Set<Integer> groupIds) {
    final DataSet dataSet = dataSetService.getDataSet(dataSetId);
    final Period period = periodService.getPeriod(periodId);
    final List<Integer> periodsBetweenDates = getIdentifiers(periodService.getPeriodsBetweenDates(dataSet.getPeriodType(), period.getStartDate(), period.getEndDate()));
    final Map<Integer, OrganisationUnit> orgUnits = Maps.uniqueIndex(organisationUnitService.getOrganisationUnits(organisationUnitIds), OrganisationUnit::getId);
    final Set<OrganisationUnitGroup> groups = groupIds != null ? Sets.newHashSet(idObjectManager.getObjects(OrganisationUnitGroup.class, groupIds)) : null;
    final List<DataSetCompletenessResult> results = new ArrayList<>();
    for (final Integer unitId : organisationUnitIds) {
        final OrganisationUnit unit = orgUnits.get(unitId);
        final Set<Integer> children = organisationUnitService.getOrganisationUnitHierarchy().getChildren(unit.getId());
        final Set<Integer> relevantSources = getRelevantSources(dataSet, children, groups);
        final DataSetCompletenessResult result = getDataSetCompleteness(period, periodsBetweenDates, unit, relevantSources, dataSet);
        if (result.getSources() > 0) {
            results.add(result);
        }
    }
    return results;
}
Also used : OrganisationUnitGroup(org.hisp.dhis.organisationunit.OrganisationUnitGroup) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DataSetCompletenessResult(org.hisp.dhis.completeness.DataSetCompletenessResult) DataSet(org.hisp.dhis.dataset.DataSet) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class ActivityReportingServiceImpl method generateRepeatableEvent.

@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Patient generateRepeatableEvent(int orgUnitId, String eventInfo) throws NotAllowedException {
    OrganisationUnit orgUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
    String[] keys = eventInfo.split("_");
    ProgramStage programStage = programStageService.getProgramStage(Integer.parseInt(keys[4]));
    int mobileProgramStageId = Integer.parseInt(keys[3]);
    String nextDueDate = keys[2];
    Program program = programService.getProgram(Integer.parseInt(keys[1]));
    TrackedEntityInstance trackedEntityInstance = entityInstanceService.getTrackedEntityInstance(Integer.parseInt(keys[0]));
    ProgramInstance programInstance = null;
    ProgramStageInstance newProgramStageInstance = null;
    if (mobileProgramStageId != 0) {
        ProgramStageInstance oldProgramStageIntance = programStageInstanceService.getProgramStageInstance(mobileProgramStageId);
        programInstance = oldProgramStageIntance.getProgramInstance();
        newProgramStageInstance = new ProgramStageInstance(programInstance, oldProgramStageIntance.getProgramStage());
        newProgramStageInstance.setDueDate(DateUtils.getMediumDate(nextDueDate));
    } else {
        programInstance = programInstanceService.getProgramInstances(new ProgramInstanceQueryParams().setTrackedEntityInstance(trackedEntityInstance).setProgram(program).setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL)).iterator().next();
        newProgramStageInstance = new ProgramStageInstance();
        newProgramStageInstance.setProgramInstance(programInstance);
        newProgramStageInstance.setProgramStage(programStage);
        newProgramStageInstance.setDueDate(DateUtils.getMediumDate(nextDueDate));
        newProgramStageInstance.setExecutionDate(DateUtils.getMediumDate(nextDueDate));
    }
    newProgramStageInstance.setOrganisationUnit(orgUnit);
    programInstance.getProgramStageInstances().add(newProgramStageInstance);
    List<ProgramStageInstance> proStageInstanceList = new ArrayList<>(programInstance.getProgramStageInstances());
    Collections.sort(proStageInstanceList, new ProgramStageInstanceVisitDateComparator());
    programInstance.getProgramStageInstances().removeAll(proStageInstanceList);
    programInstance.getProgramStageInstances().addAll(proStageInstanceList);
    programStageInstanceService.addProgramStageInstance(newProgramStageInstance);
    programInstanceService.updateProgramInstance(programInstance);
    TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance(programInstance.getEntityInstance().getId());
    org.hisp.dhis.api.mobile.model.LWUITmodel.Patient mobilePatient = getPatientModel(tei);
    return mobilePatient;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Patient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient) ArrayList(java.util.ArrayList) ProgramStageInstanceVisitDateComparator(org.hisp.dhis.program.comparator.ProgramStageInstanceVisitDateComparator) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) ProgramStage(org.hisp.dhis.program.ProgramStage) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams)

Example 19 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class MessageConversationController method postObject.

private void postObject(HttpServletResponse response, HttpServletRequest request, MessageConversation messageConversation) throws WebMessageException {
    List<User> users = new ArrayList<>(messageConversation.getUsers());
    messageConversation.getUsers().clear();
    for (OrganisationUnit ou : messageConversation.getOrganisationUnits()) {
        OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(ou.getUid());
        if (organisationUnit == null) {
            throw new WebMessageException(WebMessageUtils.conflict("Organisation Unit does not exist: " + ou.getUid()));
        }
        messageConversation.getUsers().addAll(organisationUnit.getUsers());
    }
    for (User u : users) {
        User user = userService.getUser(u.getUid());
        if (user == null) {
            throw new WebMessageException(WebMessageUtils.conflict("User does not exist: " + u.getUid()));
        }
        messageConversation.getUsers().add(user);
    }
    for (UserGroup ug : messageConversation.getUserGroups()) {
        UserGroup userGroup = userGroupService.getUserGroup(ug.getUid());
        if (userGroup == null) {
            throw new WebMessageException(WebMessageUtils.notFound("User Group does not exist: " + ug.getUid()));
        }
        messageConversation.getUsers().addAll(userGroup.getMembers());
    }
    if (messageConversation.getUsers().isEmpty()) {
        throw new WebMessageException(WebMessageUtils.conflict("No recipients selected."));
    }
    String metaData = MessageService.META_USER_AGENT + request.getHeader(ContextUtils.HEADER_USER_AGENT);
    int id = messageService.sendPrivateMessage(messageConversation.getSubject(), messageConversation.getText(), metaData, messageConversation.getUsers());
    org.hisp.dhis.message.MessageConversation conversation = messageService.getMessageConversation(id);
    response.addHeader("Location", MessageConversationSchemaDescriptor.API_ENDPOINT + "/" + conversation.getUid());
    webMessageService.send(WebMessageUtils.created("Message conversation created"), response, request);
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) ArrayList(java.util.ArrayList) UserGroup(org.hisp.dhis.user.UserGroup)

Example 20 with OrganisationUnit

use of org.hisp.dhis.organisationunit.OrganisationUnit in project dhis2-core by dhis2.

the class LockExceptionController method deleteLockException.

@RequestMapping(method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteLockException(@RequestParam("ou") String organisationUnitId, @RequestParam("pe") String periodId, @RequestParam("ds") String dataSetId, HttpServletRequest request, HttpServletResponse response) throws WebMessageException {
    User user = userService.getCurrentUser();
    DataSet dataSet = dataSetService.getDataSet(dataSetId);
    Period period = periodService.reloadPeriod(PeriodType.getPeriodFromIsoString(periodId));
    OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(organisationUnitId);
    if (!ObjectUtils.allNonNull(dataSet, period)) {
        throw new WebMessageException(WebMessageUtils.conflict("Can't find LockException with combination: dataSet=" + dataSetId + ", period=" + periodId));
    }
    if (!aclService.canDelete(user, dataSet)) {
        throw new ReadAccessDeniedException("You don't have the proper permissions to delete this object.");
    }
    if (organisationUnit != null) {
        dataSetService.deleteLockExceptionCombination(dataSet, period, organisationUnit);
    } else {
        dataSetService.deleteLockExceptionCombination(dataSet, period);
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) DataSet(org.hisp.dhis.dataset.DataSet) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) Period(org.hisp.dhis.period.Period) ReadAccessDeniedException(org.hisp.dhis.hibernate.exception.ReadAccessDeniedException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)321 Period (org.hisp.dhis.period.Period)74 Test (org.junit.Test)63 ArrayList (java.util.ArrayList)62 User (org.hisp.dhis.user.User)57 Date (java.util.Date)53 HashSet (java.util.HashSet)53 DataSet (org.hisp.dhis.dataset.DataSet)53 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)51 DataElement (org.hisp.dhis.dataelement.DataElement)50 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)39 DhisSpringTest (org.hisp.dhis.DhisSpringTest)37 List (java.util.List)36 CurrentUserService (org.hisp.dhis.user.CurrentUserService)29 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)28 MockCurrentUserService (org.hisp.dhis.mock.MockCurrentUserService)28 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)23 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)23 Program (org.hisp.dhis.program.Program)22 ClassPathResource (org.springframework.core.io.ClassPathResource)20