use of org.hisp.dhis.trackedentity.TrackedEntityInstance 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.trackedentity.TrackedEntityInstance 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.trackedentity.TrackedEntityInstance 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.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractEventService method updateTrackedEntityInstance.
private void updateTrackedEntityInstance(List<ProgramStageInstance> programStageInstances) {
Set<ProgramInstance> programInstances = new HashSet<>();
Set<TrackedEntityInstance> trackedEntityInstances = new HashSet<>();
for (ProgramStageInstance programStageInstance : programStageInstances) {
if (programStageInstance.getProgramInstance() != null) {
programInstances.add(programStageInstance.getProgramInstance());
if (programStageInstance.getProgramInstance().getEntityInstance() != null) {
trackedEntityInstances.add(programStageInstance.getProgramInstance().getEntityInstance());
}
}
}
programInstances.forEach(manager::update);
trackedEntityInstances.forEach(manager::update);
}
use of org.hisp.dhis.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method registerRelative.
@Override
public Patient registerRelative(Patient patient, int orgUnitId, String programId) 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()));
if (patient.getIdToAddRelative() != 0) {
TrackedEntityInstance relative = entityInstanceService.getTrackedEntityInstance(patient.getIdToAddRelative());
if (relative == null) {
throw new NotAllowedException("relative does not exist");
}
patientId = entityInstanceService.createTrackedEntityInstance(patientWeb, relative.getUid(), patient.getRelTypeIdToAdd(), patientAttributeValues);
} else {
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(programId));
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);
}
if (patient.getEnrollmentRelationship() != null) {
org.hisp.dhis.api.mobile.model.LWUITmodel.Relationship enrollmentRelationship = patient.getEnrollmentRelationship();
enrollmentRelationship.setPersonBId(newTrackedEntityInstance.getId());
addRelationship(enrollmentRelationship, orgUnitId);
}
return getPatientModel(newTrackedEntityInstance);
}
Aggregations