use of org.hisp.dhis.trackedentity.TrackedEntityAttribute 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.TrackedEntityAttribute 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;
}
use of org.hisp.dhis.trackedentity.TrackedEntityAttribute in project dhis2-core by dhis2.
the class DefaultProgramService method getPatientAttributeForMobile.
private org.hisp.dhis.api.mobile.model.PatientAttribute getPatientAttributeForMobile(ProgramTrackedEntityAttribute ppa) {
TrackedEntityAttribute pa = ppa.getAttribute();
org.hisp.dhis.api.mobile.model.PatientAttribute mobileAttribute = new org.hisp.dhis.api.mobile.model.PatientAttribute();
mobileAttribute.setName(pa.getName());
mobileAttribute.setType(pa.getValueType());
mobileAttribute.setValue("");
if (ppa.isDisplayInList()) {
mobileAttribute.setDisplayedInList(true);
} else {
mobileAttribute.setDisplayedInList(false);
}
mobileAttribute.setMandatory(ppa.isMandatory());
if (pa.hasOptionSet()) {
OptionSet optionSet = new OptionSet();
if (pa.getOptionSet() != null) {
optionSet.setId(pa.getOptionSet().getId());
optionSet.setName(pa.getOptionSet().getName());
optionSet.setOptions(pa.getOptionSet().getOptionValues());
mobileAttribute.setOptionSet(optionSet);
}
}
return mobileAttribute;
}
use of org.hisp.dhis.trackedentity.TrackedEntityAttribute in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method findLostToFollowUp.
@Override
public String findLostToFollowUp(int orgUnitId, String searchEventInfos) throws NotAllowedException {
String[] searchEventInfosArray = searchEventInfos.split("-");
EventStatus eventStatus = EventStatus.ACTIVE;
if (searchEventInfosArray[1].equalsIgnoreCase("Scheduled in future")) {
eventStatus = EventStatus.SCHEDULE;
} else if (searchEventInfosArray[1].equalsIgnoreCase("Overdue")) {
eventStatus = EventStatus.OVERDUE;
}
String eventsInfo = "";
Calendar toCalendar = new GregorianCalendar();
toCalendar.add(Calendar.DATE, -1);
toCalendar.add(Calendar.YEAR, 100);
Date toDate = toCalendar.getTime();
Calendar fromCalendar = new GregorianCalendar();
fromCalendar.add(Calendar.DATE, -1);
fromCalendar.add(Calendar.YEAR, -100);
Date fromDate = fromCalendar.getTime();
TrackedEntityInstanceQueryParams param = new TrackedEntityInstanceQueryParams();
List<TrackedEntityAttribute> trackedEntityAttributeList = new ArrayList<>(attributeService.getTrackedEntityAttributesByDisplayOnVisitSchedule(true));
for (TrackedEntityAttribute trackedEntityAttribute : trackedEntityAttributeList) {
QueryItem queryItem = new QueryItem(trackedEntityAttribute);
param.addAttribute(queryItem);
}
param.addOrganisationUnit(organisationUnitService.getOrganisationUnit(orgUnitId));
param.setEventStatus(eventStatus);
param.setEventStartDate(fromDate);
param.setEventEndDate(toDate);
Grid programStageInstanceGrid = entityInstanceService.getTrackedEntityInstancesGrid(param);
List<List<Object>> rows = programStageInstanceGrid.getRows();
if (rows.size() == 0) {
throw NotAllowedException.NO_EVENT_FOUND;
} else if (rows.size() > 0) {
for (List<Object> row : rows) {
for (int i = 5; i < row.size(); i++) {
eventsInfo += row.get(i) + "/";
if (i == row.size() - 1) {
eventsInfo += "$";
}
}
}
throw new NotAllowedException(eventsInfo);
} else {
return "";
}
}
use of org.hisp.dhis.trackedentity.TrackedEntityAttribute in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method getPatientAtts.
@Override
public List<TrackedEntityAttribute> getPatientAtts(String programId) {
List<TrackedEntityAttribute> patientAttributes = null;
if (programId != null && !programId.trim().equals("")) {
Program program = programService.getProgram(Integer.parseInt(programId));
patientAttributes = program.getTrackedEntityAttributes();
} else {
patientAttributes = attributeService.getAllTrackedEntityAttributes();
}
return patientAttributes;
}
Aggregations