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;
}
}
}
}
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;
}
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;
}
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);
}
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);
}
}
Aggregations