use of org.hisp.dhis.program.ProgramStage in project dhis2-core by dhis2.
the class NamebasedUtilsImpl method getProgramStage.
@Override
public ProgramStage getProgramStage(int programId, int programStageId) {
Program program = programService.getProgram(programId);
Collection<ProgramStage> stages = program.getProgramStages();
for (ProgramStage programStage : stages) {
if (programStage.getId() == programStageId) {
return programStage;
}
}
return null;
}
use of org.hisp.dhis.program.ProgramStage in project dhis2-core by dhis2.
the class AbstractEventService method addEvent.
protected ImportSummary addEvent(Event event, User user, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
ProgramStage programStage = getProgramStage(importOptions.getIdSchemes().getProgramStageIdScheme(), event.getProgramStage());
ProgramInstance programInstance;
ProgramStageInstance programStageInstance = null;
if (program == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.program does not point to a valid program: " + event.getProgram()).incrementIgnored();
}
if (programStage == null && program.isRegistration()) {
return new ImportSummary(ImportStatus.ERROR, "Event.programStage does not point to a valid programStage, and program is multi stage: " + event.getProgramStage()).incrementIgnored();
} else if (programStage == null) {
programStage = program.getProgramStageByStage(1);
}
Assert.notNull(programStage, "Program stage cannot be null");
if (!canAccess(program, user)) {
return new ImportSummary(ImportStatus.ERROR, "Current user does not have permission to access this program").incrementIgnored();
}
if (program.isRegistration()) {
if (event.getTrackedEntityInstance() == null) {
return new ImportSummary(ImportStatus.ERROR, "No Event.trackedEntityInstance was provided for registration based program").incrementIgnored();
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(event.getTrackedEntityInstance());
if (entityInstance == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.trackedEntityInstance does not point to a valid tracked entity instance: " + event.getTrackedEntityInstance()).incrementIgnored();
}
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(entityInstance, program, ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " is not enrolled in program: " + program.getUid()).incrementIgnored();
} else if (programInstances.size() > 1) {
return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance: " + entityInstance.getUid() + " have multiple active enrollments in program: " + program.getUid()).incrementIgnored();
}
programInstance = programInstances.get(0);
if (!programStage.getRepeatable()) {
programStageInstance = programStageInstanceService.getProgramStageInstance(programInstance, programStage);
if (programStageInstance != null && !programStageInstance.getUid().equals(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Program stage is not repeatable and an event already exists").incrementIgnored();
}
} else {
if (event.getEvent() != null) {
programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
if (programStageInstance == null) {
if (!CodeGenerator.isValidUid(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
}
}
}
}
} else {
List<ProgramInstance> programInstances = new ArrayList<>(programInstanceService.getProgramInstances(program, ProgramStatus.ACTIVE));
if (programInstances.isEmpty()) {
// Create PI if it doesn't exist (should only be one)
ProgramInstance pi = new ProgramInstance();
pi.setEnrollmentDate(new Date());
pi.setIncidentDate(new Date());
pi.setProgram(program);
pi.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(pi);
programInstances.add(pi);
} else if (programInstances.size() > 1) {
return new ImportSummary(ImportStatus.ERROR, "Multiple active program instances exists for program: " + program.getUid()).incrementIgnored();
}
programInstance = programInstances.get(0);
if (event.getEvent() != null) {
programStageInstance = manager.getObject(ProgramStageInstance.class, importOptions.getIdSchemes().getProgramStageInstanceIdScheme(), event.getEvent());
if (programStageInstance == null) {
if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(IdScheme.UID) && !CodeGenerator.isValidUid(event.getEvent())) {
return new ImportSummary(ImportStatus.ERROR, "Event.event did not point to a valid event: " + event.getEvent()).incrementIgnored();
}
}
}
}
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
program = programInstance.getProgram();
if (programStageInstance != null) {
programStage = programStageInstance.getProgramStage();
}
if (organisationUnit == null) {
return new ImportSummary(ImportStatus.ERROR, "Event.orgUnit does not point to a valid organisation unit: " + event.getOrgUnit()).incrementIgnored();
}
if (!programInstance.getProgram().hasOrganisationUnit(organisationUnit)) {
return new ImportSummary(ImportStatus.ERROR, "Program is not assigned to this organisation unit: " + event.getOrgUnit()).incrementIgnored();
}
validateExpiryDays(event, program, null);
return saveEvent(program, programInstance, programStage, programStageInstance, organisationUnit, event, user, importOptions);
}
use of org.hisp.dhis.program.ProgramStage in project dhis2-core by dhis2.
the class AbstractEventService method getFromUrl.
@Override
public EventSearchParams getFromUrl(String program, String programStage, ProgramStatus programStatus, Boolean followUp, String orgUnit, OrganisationUnitSelectionMode orgUnitSelectionMode, String trackedEntityInstance, Date startDate, Date endDate, Date dueDateStart, Date dueDateEnd, Date lastUpdatedStartDate, Date lastUpdatedEndDate, EventStatus status, DataElementCategoryOptionCombo attributeOptionCombo, IdSchemes idSchemes, Integer page, Integer pageSize, boolean totalPages, boolean skipPaging, List<Order> orders, List<String> gridOrders, boolean includeAttributes, Set<String> events, Set<String> filters, Set<String> dataElements, boolean includeDeleted) {
UserCredentials userCredentials = currentUserService.getCurrentUser().getUserCredentials();
EventSearchParams params = new EventSearchParams();
Program pr = programService.getProgram(program);
if (StringUtils.isNotEmpty(program) && pr == null) {
throw new IllegalQueryException("Program is specified but does not exist: " + program);
}
ProgramStage ps = programStageService.getProgramStage(programStage);
if (StringUtils.isNotEmpty(programStage) && ps == null) {
throw new IllegalQueryException("Program stage is specified but does not exist: " + programStage);
}
OrganisationUnit ou = organisationUnitService.getOrganisationUnit(orgUnit);
if (StringUtils.isNotEmpty(orgUnit) && ou == null) {
throw new IllegalQueryException("Org unit is specified but does not exist: " + orgUnit);
}
if (ou != null && !organisationUnitService.isInUserHierarchy(ou)) {
if (!userCredentials.isSuper() && !userCredentials.isAuthorized("F_TRACKED_ENTITY_INSTANCE_SEARCH_IN_ALL_ORGUNITS")) {
throw new IllegalQueryException("User has no access to organisation unit: " + ou.getUid());
}
}
if (pr != null && !userCredentials.isSuper() && !userCredentials.canAccessProgram(pr)) {
throw new IllegalQueryException("User has no access to program: " + pr.getUid());
}
TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance(trackedEntityInstance);
if (StringUtils.isNotEmpty(trackedEntityInstance) && tei == null) {
throw new IllegalQueryException("Tracked entity instance is specified but does not exist: " + trackedEntityInstance);
}
if (events != null && filters != null) {
throw new IllegalQueryException("Event UIDs and filters can not be specified at the same time");
}
if (events == null) {
events = new HashSet<>();
}
if (filters != null) {
if (StringUtils.isNotEmpty(programStage) && ps == null) {
throw new IllegalQueryException("ProgramStage needs to be specified for event filtering to work");
}
for (String filter : filters) {
QueryItem item = getQueryItem(filter);
params.getFilters().add(item);
}
}
if (dataElements != null) {
for (String de : dataElements) {
QueryItem dataElement = getQueryItem(de);
params.getDataElements().add(dataElement);
}
}
params.setProgram(pr);
params.setProgramStage(ps);
params.setOrgUnit(ou);
params.setTrackedEntityInstance(tei);
params.setProgramStatus(programStatus);
params.setFollowUp(followUp);
params.setOrgUnitSelectionMode(orgUnitSelectionMode);
params.setStartDate(startDate);
params.setEndDate(endDate);
params.setDueDateStart(dueDateStart);
params.setDueDateEnd(dueDateEnd);
params.setLastUpdatedStartDate(lastUpdatedStartDate);
params.setLastUpdatedEndDate(lastUpdatedEndDate);
params.setEventStatus(status);
params.setCategoryOptionCombo(attributeOptionCombo);
params.setIdSchemes(idSchemes);
params.setPage(page);
params.setPageSize(pageSize);
params.setTotalPages(totalPages);
params.setSkipPaging(skipPaging);
params.setIncludeAttributes(includeAttributes);
params.setOrders(orders);
params.setGridOrders(gridOrders);
params.setEvents(events);
params.setIncludeDeleted(includeDeleted);
return params;
}
use of org.hisp.dhis.program.ProgramStage in project dhis2-core by dhis2.
the class ProgramStageObjectBundleHook method preUpdate.
@Override
public <T extends IdentifiableObject> void preUpdate(T object, T persistedObject, ObjectBundle bundle) {
if (!ProgramStage.class.isInstance(object))
return;
ProgramStage programStage = (ProgramStage) object;
if (programStage.getPeriodType() != null) {
PeriodType periodType = bundle.getPreheat().getPeriodTypeMap().get(programStage.getPeriodType().getName());
programStage.setPeriodType(periodType);
}
sessionFactory.getCurrentSession().flush();
}
use of org.hisp.dhis.program.ProgramStage in project dhis2-core by dhis2.
the class RemoveDataEntryFormAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
DataEntryForm dataEntryForm = dataEntryFormService.getDataEntryForm(id);
Program program = programStageService.getProgramStage(programStageId).getProgram();
programId = program.getId();
Set<ProgramStage> programStages = program.getProgramStages();
for (ProgramStage programStage : programStages) {
DataEntryForm programEntryForm = programStage.getDataEntryForm();
if (programEntryForm != null && programEntryForm.equals(dataEntryForm)) {
programStage.setDataEntryForm(null);
programStageService.updateProgramStage(programStage);
}
}
program.increaseVersion();
dataEntryFormService.deleteDataEntryForm(dataEntryForm);
return SUCCESS;
}
Aggregations