use of org.hisp.dhis.trackedentity.TrackedEntity in project dhis2-core by dhis2.
the class AddProgramAction method execute.
// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
displayIncidentDate = (displayIncidentDate == null) ? false : displayIncidentDate;
ignoreOverdueEvents = (ignoreOverdueEvents == null) ? false : ignoreOverdueEvents;
onlyEnrollOnce = (onlyEnrollOnce == null) ? false : onlyEnrollOnce;
selectEnrollmentDatesInFuture = (selectEnrollmentDatesInFuture == null) ? false : selectEnrollmentDatesInFuture;
selectIncidentDatesInFuture = (selectIncidentDatesInFuture == null) ? false : selectIncidentDatesInFuture;
Program program = new Program();
program.setName(StringUtils.trimToNull(name));
program.setShortName(StringUtils.trimToNull(shortName));
program.setDescription(StringUtils.trimToNull(description));
program.setVersion(1);
program.setEnrollmentDateLabel(StringUtils.trimToNull(enrollmentDateLabel));
program.setIncidentDateLabel(StringUtils.trimToNull(incidentDateLabel));
program.setProgramType(programType);
program.setDisplayIncidentDate(displayIncidentDate);
program.setOnlyEnrollOnce(onlyEnrollOnce);
program.setSelectEnrollmentDatesInFuture(selectEnrollmentDatesInFuture);
program.setSelectIncidentDatesInFuture(selectIncidentDatesInFuture);
program.setSkipOffline(skipOffline);
program.setDisplayFrontPageList(displayFrontPageList);
program.setUseFirstStageDuringRegistration(useFirstStageDuringRegistration);
program.setCaptureCoordinates(captureCoordinates);
program.setExpiryDays(expiryDays);
program.setCompleteEventsExpiryDays(completeEventsExpiryDays);
if (programType == ProgramType.WITH_REGISTRATION) {
program.setIgnoreOverdueEvents(ignoreOverdueEvents);
} else {
program.setIgnoreOverdueEvents(false);
}
periodTypeName = StringUtils.trimToNull(periodTypeName);
if (periodTypeName != null) {
PeriodType periodType = PeriodType.getPeriodTypeByName(periodTypeName);
program.setExpiryPeriodType(periodService.getPeriodTypeByClass(periodType.getClass()));
} else {
program.setExpiryPeriodType(null);
}
if (relationshipTypeId != null) {
RelationshipType relationshipType = relationshipTypeService.getRelationshipType(relationshipTypeId);
program.setRelationshipType(relationshipType);
program.setRelationshipFromA(relationshipFromA);
program.setRelationshipText(relationshipText);
Program relatedProgram = programService.getProgram(relatedProgramId);
program.setRelatedProgram(relatedProgram);
} else {
program.setRelationshipType(null);
program.setRelationshipFromA(null);
program.setRelationshipText(null);
program.setRelatedProgram(null);
}
if (trackedEntityId != null) {
TrackedEntity trackedEntity = trackedEntityService.getTrackedEntity(trackedEntityId);
program.setTrackedEntity(trackedEntity);
}
if (categoryComboId != null) {
program.setCategoryCombo(categoryService.getDataElementCategoryCombo(categoryComboId));
}
if (workflowId != null && workflowId > 0) {
program.setWorkflow(dataApprovalService.getWorkflow(workflowId));
}
programService.addProgram(program);
int index = 0;
for (String selectedPropertyId : selectedPropertyIds) {
String[] ids = selectedPropertyId.split("_");
if (ids[0].equals(TrackedEntityInstance.PREFIX_TRACKED_ENTITY_ATTRIBUTE)) {
TrackedEntityAttribute attribute = trackedEntityAttributeService.getTrackedEntityAttribute(Integer.parseInt(ids[1]));
ProgramTrackedEntityAttribute programAttribute = new ProgramTrackedEntityAttribute(program, attribute, personDisplayNames.get(index), mandatory.get(index), allowFutureDate.get(index));
programAttribute.setRenderOptionsAsRadio(renderOptionsAsRadios.get(index));
programAttribute.setAutoFields();
program.getProgramAttributes().add(programAttribute);
}
index++;
}
if (jsonAttributeValues != null) {
attributeService.updateAttributeValues(program, jsonAttributeValues);
}
programService.updateProgram(program);
if (program.isWithoutRegistration()) {
ProgramStage programStage = new ProgramStage();
programStage.setName(name);
programStage.setDescription(description);
programStage.setProgram(program);
programStage.setMinDaysFromStart(0);
programStage.setAutoGenerateEvent(true);
programStage.setExecutionDateLabel(REPORT_DATE_DESCRIPTION);
programStageService.saveProgramStage(programStage);
}
if (program.isWithoutRegistration()) {
// Add a new program-instance
ProgramInstance programInstance = new ProgramInstance();
programInstance.setEnrollmentDate(new Date());
programInstance.setIncidentDate(new Date());
programInstance.setProgram(program);
programInstance.setStatus(ProgramStatus.ACTIVE);
programInstanceService.addProgramInstance(programInstance);
}
return SUCCESS;
}
use of org.hisp.dhis.trackedentity.TrackedEntity in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method updatePatient.
@Override
public Patient updatePatient(org.hisp.dhis.api.mobile.model.LWUITmodel.Patient patient, int orgUnitId, String programIdText) throws NotAllowedException {
TrackedEntityInstance entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
TrackedEntityInstance tempTEI = entityInstance;
TrackedEntity trackedEntity = null;
Program program = programService.getProgram(Integer.parseInt(programIdText));
trackedEntity = program.getTrackedEntity();
entityInstance.setTrackedEntity(trackedEntity);
// get attributes to be saved/updated/deleted
Collection<TrackedEntityAttribute> attributes = attributeService.getAllTrackedEntityAttributes();
List<TrackedEntityAttributeValue> valuesForSave = new ArrayList<>();
List<TrackedEntityAttributeValue> valuesForUpdate = new ArrayList<>();
Collection<TrackedEntityAttributeValue> valuesForDelete = null;
TrackedEntityAttributeValue attributeValue = null;
Collection<org.hisp.dhis.api.mobile.model.PatientAttribute> attributesMobile = patient.getAttributes();
if (attributes != null && attributes.size() > 0) {
valuesForDelete = attValueService.getTrackedEntityAttributeValues(entityInstance);
tempTEI.getTrackedEntityAttributeValues().clear();
for (TrackedEntityAttribute attribute : attributes) {
String value = getAttributeValue(attributesMobile, attribute.getName());
if (value != null) {
attributeValue = attValueService.getTrackedEntityAttributeValue(entityInstance, attribute);
if (attributeValue == null) {
attributeValue = new TrackedEntityAttributeValue();
attributeValue.setEntityInstance(entityInstance);
attributeValue.setAttribute(attribute);
attributeValue.setValue(value.trim());
valuesForSave.add(attributeValue);
} else {
attributeValue.setValue(value.trim());
valuesForUpdate.add(attributeValue);
valuesForDelete.remove(attributeValue);
}
tempTEI.getTrackedEntityAttributeValues().add(attributeValue);
}
}
}
// validate
String[] errorCode = entityInstanceService.validateTrackedEntityInstance(tempTEI, program).split("_");
int code = Integer.parseInt(errorCode[0]);
if (code >= 1) {
if (code == TrackedEntityInstanceService.ERROR_DUPLICATE_IDENTIFIER) {
throw new NotAllowedException("Duplicate value of " + attributeService.getTrackedEntityAttribute(Integer.parseInt(errorCode[1])).getDisplayName());
} else {
throw new NotAllowedException("Validation error");
}
}
entityInstanceService.updateTrackedEntityInstance(entityInstance, null, null, valuesForSave, valuesForUpdate, valuesForDelete);
enrollProgram(patient.getId() + "-" + programIdText, null, new Date());
entityInstance = entityInstanceService.getTrackedEntityInstance(patient.getId());
entityInstance.setTrackedEntity(trackedEntity);
return getPatientModel(entityInstance);
}
use of org.hisp.dhis.trackedentity.TrackedEntity in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method findPatientInAdvanced.
/**
* keyword is on format of
* {attribute-id1}:{operator1}:{filter-value1};{attribute
* -id2}:{operator2}:{filter-value2}
*/
@Override
public String findPatientInAdvanced(String keyword, int orgUnitId, int programId) throws NotAllowedException {
TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
List<TrackedEntityAttribute> displayAttributes = new ArrayList<>(attributeService.getTrackedEntityAttributesDisplayInList());
for (TrackedEntityAttribute trackedEntityAttribute : displayAttributes) {
QueryItem queryItem = new QueryItem(trackedEntityAttribute);
param.addAttribute(queryItem);
}
if (programId != 0) {
param.setProgram(programService.getProgram(programId));
}
if (orgUnitId != 0) {
param.addOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
param.setOrganisationUnitMode(OrganisationUnitSelectionMode.SELECTED);
} else {
param.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
}
String[] items = keyword.split(";");
if (items == null) {
items = new String[1];
items[0] = keyword;
}
for (int i = 0; i < items.length; i++) {
String[] split = keyword.split(":");
if (split == null || (split.length != 3 && split.length != 2)) {
throw NotAllowedException.INVALID_FILTER;
}
if (split.length == 2) {
QueryOperator operator = QueryOperator.fromString(split[0]);
param.setQuery(new QueryFilter(operator, split[1]));
} else {
TrackedEntityAttribute at = attributeService.getTrackedEntityAttributeByName(split[0]);
QueryItem queryItem = new QueryItem(at, at.getLegendSets().get(0), at.getValueType(), at.getAggregationType(), at.getOptionSet());
QueryOperator operator = QueryOperator.fromString(split[1]);
queryItem.getFilters().add(new QueryFilter(operator, split[2]));
param.getFilters().add(queryItem);
}
}
Grid trackedEntityInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
List<List<Object>> listOfTrackedEntityInstance = trackedEntityInstanceGrid.getRows();
if (listOfTrackedEntityInstance.size() == 0) {
throw NotAllowedException.NO_BENEFICIARY_FOUND;
}
/**
* Grid columns: 0 = instance 1 = created 2 = lastupdated 3 = ou 4 = te
* 5 onwards = attributes
*/
int instanceIndex = 0;
int teIndex = 4;
List<Integer> attributesIndex = new ArrayList<>();
List<GridHeader> headers = trackedEntityInstanceGrid.getHeaders();
int index = 0;
for (GridHeader header : headers) {
if (header.getName().equals("instance")) {
instanceIndex = index;
} else if (header.getName().equals("te")) {
teIndex = index;
} else if (!header.getName().equals("created") && !header.getName().equals("lastupdated") && !header.getName().equals("ou")) {
attributesIndex.add(new Integer(index));
}
index++;
}
String instanceInfo = "";
String trackedEntityName = "";
for (List<Object> row : listOfTrackedEntityInstance) {
TrackedEntity te = trackedEntityService.getTrackedEntity((String) row.get(teIndex));
if (!trackedEntityName.equals(te.getDisplayName())) {
trackedEntityName = te.getDisplayName();
instanceInfo += te.getDisplayName() + "$";
}
// NOTE: this line should be here but because the mobile client uses
// the int TEI id, we will temprarily get the int id for now.
// instanceInfo += (String) row.get( instanceIndex ) + "/";
TrackedEntityInstance tei = entityInstanceService.getTrackedEntityInstance((String) row.get(instanceIndex));
instanceInfo += tei.getId() + "/";
// end of temproary fix
String attText = "";
for (Integer attIndex : attributesIndex) {
if (row.get(attIndex.intValue()) != null) {
attText += (String) row.get(attIndex.intValue()) + " ";
}
}
instanceInfo += attText.trim() + "$";
}
return instanceInfo;
}
Aggregations