use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class DefaultDimensionService method getProgramDataElementDimensionItem.
/**
* Returns a {@link ProgramDataElementDimensionItem}.
*
* @param idScheme the identifier scheme.
* @param programId the program identifier.
* @param dataElementId the data element identifier.
*/
private ProgramDataElementDimensionItem getProgramDataElementDimensionItem(IdScheme idScheme, String programId, String dataElementId) {
Program program = idObjectManager.getObject(Program.class, idScheme, programId);
DataElement dataElement = idObjectManager.getObject(DataElement.class, idScheme, dataElementId);
if (program == null || dataElement == null) {
return null;
}
return new ProgramDataElementDimensionItem(program, dataElement);
}
use of org.hisp.dhis.program.Program in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method savePatient.
@Override
public Patient savePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
TrackedEntityInstance patientWeb = new TrackedEntityInstance();
patientWeb.setOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
Set<TrackedEntityAttribute> patientAttributeSet = new HashSet<>();
Set<TrackedEntityAttributeValue> patientAttributeValues = new HashSet<>();
Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
if (attributesMobile != null) {
for (org.hisp.dhis.api.mobile.model.PatientAttribute paAtt : attributesMobile) {
TrackedEntityAttribute patientAttribute = attributeService.getTrackedEntityAttributeByName(paAtt.getName());
patientAttributeSet.add(patientAttribute);
TrackedEntityAttributeValue patientAttributeValue = new TrackedEntityAttributeValue();
patientAttributeValue.setEntityInstance(patientWeb);
patientAttributeValue.setAttribute(patientAttribute);
patientAttributeValue.setValue(paAtt.getValue());
patientAttributeValues.add(patientAttributeValue);
}
}
patientWeb.setTrackedEntity(trackedEntityService.getTrackedEntityByName(patient.getTrackedEntityName()));
patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, null, null, patientAttributeValues);
TrackedEntityInstance newTrackedEntityInstance = entityInstanceService.getTrackedEntityInstance(this.patientId);
String errorMsg = null;
try {
for (org.hisp.dhis.api.mobile.model.LWUITmodel.ProgramInstance mobileProgramInstance : patient.getEnrollmentPrograms()) {
Date incidentDate = DateUtils.getMediumDate(mobileProgramInstance.getDateOfIncident());
enrollProgram(patientId + "-" + mobileProgramInstance.getProgramId(), mobileProgramInstance.getProgramStageInstances(), incidentDate);
}
Program program = programService.getProgram(Integer.parseInt(programIdText));
String[] errorCode = entityInstanceService.validateTrackedEntityInstance(newTrackedEntityInstance, program).split("_");
int code = Integer.parseInt(errorCode[0]);
if (code >= 1) {
entityInstanceService.deleteTrackedEntityInstance(newTrackedEntityInstance);
if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
errorMsg = "Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName();
} else {
errorMsg = "Validation error";
}
}
} catch (Exception e) {
e.printStackTrace();
}
if (errorMsg != null) {
throw new NotAllowedException(errorMsg);
}
return getPatientModel(newTrackedEntityInstance);
}
use of org.hisp.dhis.program.Program 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.program.Program 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.Program 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);
}
Aggregations