use of org.hisp.dhis.program.ProgramType in project dhis2-core by dhis2.
the class JdbcEventStore method getEvents.
// -------------------------------------------------------------------------
// EventStore implementation
// -------------------------------------------------------------------------
@Override
public List<Event> getEvents(EventSearchParams params, List<OrganisationUnit> organisationUnits) {
List<Event> events = new ArrayList<>();
String sql = buildSql(params, organisationUnits);
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
log.debug("Event query SQL: " + sql);
Event event = new Event();
event.setEvent("not_valid");
Set<String> notes = new HashSet<>();
IdSchemes idSchemes = ObjectUtils.firstNonNull(params.getIdSchemes(), new IdSchemes());
while (rowSet.next()) {
if (rowSet.getString("psi_uid") == null) {
continue;
}
if (event.getUid() == null || !event.getUid().equals(rowSet.getString("psi_uid"))) {
event = new Event();
event.setUid(rowSet.getString("psi_uid"));
event.setEvent(IdSchemes.getValue(rowSet.getString("psi_uid"), rowSet.getString("psi_code"), idSchemes.getProgramStageInstanceIdScheme()));
event.setTrackedEntityInstance(rowSet.getString("tei_uid"));
event.setStatus(EventStatus.valueOf(rowSet.getString("psi_status")));
event.setProgram(IdSchemes.getValue(rowSet.getString("p_uid"), rowSet.getString("p_code"), idSchemes.getProgramIdScheme()));
event.setProgramStage(IdSchemes.getValue(rowSet.getString("ps_uid"), rowSet.getString("ps_code"), idSchemes.getProgramStageIdScheme()));
event.setOrgUnit(IdSchemes.getValue(rowSet.getString("ou_uid"), rowSet.getString("ou_code"), idSchemes.getOrgUnitIdScheme()));
event.setDeleted(rowSet.getBoolean("psi_deleted"));
ProgramType programType = ProgramType.fromValue(rowSet.getString("p_type"));
if (programType != ProgramType.WITHOUT_REGISTRATION) {
event.setEnrollment(rowSet.getString("pi_uid"));
event.setEnrollmentStatus(EnrollmentStatus.fromProgramStatus(ProgramStatus.valueOf(rowSet.getString("pi_status"))));
event.setFollowup(rowSet.getBoolean("pi_followup"));
}
event.setAttributeOptionCombo(rowSet.getString("coc_categoryoptioncombouid"));
event.setAttributeCategoryOptions(rowSet.getString("deco_uid"));
event.setTrackedEntityInstance(rowSet.getString("tei_uid"));
event.setStoredBy(rowSet.getString("psi_storedby"));
event.setOrgUnitName(rowSet.getString("ou_name"));
event.setDueDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_duedate")));
event.setEventDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_executiondate")));
event.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("psi_created")));
event.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("psi_lastupdated")));
event.setCompletedBy(rowSet.getString("psi_completedby"));
event.setCompletedDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_completeddate")));
Double longitude = rowSet.getDouble("psi_longitude");
Double latitude = rowSet.getDouble("psi_latitude");
if (longitude != null && latitude != null) {
Coordinate coordinate = new Coordinate(longitude, latitude);
try {
List<Double> list = OBJECT_MAPPER.readValue(coordinate.getCoordinateString(), new TypeReference<List<Double>>() {
});
coordinate.setLongitude(list.get(0));
coordinate.setLatitude(list.get(1));
} catch (IOException ignored) {
}
if (coordinate.isValid()) {
event.setCoordinate(coordinate);
}
}
events.add(event);
} else {
String attributeCategoryCombination = event.getAttributeCategoryOptions();
String currentAttributeCategoryCombination = rowSet.getString("deco_uid");
if (!attributeCategoryCombination.contains(currentAttributeCategoryCombination)) {
event.setAttributeCategoryOptions(attributeCategoryCombination + ";" + currentAttributeCategoryCombination);
}
}
if (rowSet.getString("pdv_value") != null && rowSet.getString("de_uid") != null && isNewDataValue(rowSet, event.getDataValues())) {
DataValue dataValue = new DataValue();
dataValue.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("pdv_created")));
dataValue.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("pdv_lastupdated")));
dataValue.setValue(rowSet.getString("pdv_value"));
dataValue.setProvidedElsewhere(rowSet.getBoolean("pdv_providedelsewhere"));
dataValue.setDataElement(IdSchemes.getValue(rowSet.getString("de_uid"), rowSet.getString("de_code"), idSchemes.getDataElementIdScheme()));
dataValue.setStoredBy(rowSet.getString("pdv_storedby"));
event.getDataValues().add(dataValue);
}
if (rowSet.getString("psinote_value") != null && !notes.contains(rowSet.getString("psinote_id"))) {
Note note = new Note();
note.setValue(rowSet.getString("psinote_value"));
note.setStoredDate(rowSet.getString("psinote_storeddate"));
note.setStoredBy(rowSet.getString("psinote_storedby"));
event.getNotes().add(note);
notes.add(rowSet.getString("psinote_id"));
}
}
return events;
}
use of org.hisp.dhis.program.ProgramType in project dhis2-core by dhis2.
the class ActivityReportingServiceImpl method getAllProgramByOrgUnit.
@Override
public org.hisp.dhis.api.mobile.model.LWUITmodel.Program getAllProgramByOrgUnit(int orgUnitId, String type) throws NotAllowedException {
String programsInfo = "";
OrganisationUnit organisationUnit = organisationUnitService.getOrganisationUnit(orgUnitId);
Set<Program> tempPrograms = null;
ProgramType programType = ProgramType.fromValue(type);
if (programType == ProgramType.WITHOUT_REGISTRATION) {
tempPrograms = programService.getUserPrograms(ProgramType.WITHOUT_REGISTRATION);
} else // ProgramType.WITH_REGISTRATION
{
tempPrograms = programService.getUserPrograms(ProgramType.WITH_REGISTRATION);
}
List<Program> programs = new ArrayList<>();
for (Program program : tempPrograms) {
if (program.getOrganisationUnits().contains(organisationUnit)) {
programs.add(program);
}
}
if (programs.size() != 0) {
if (programs.size() == 1) {
Program program = programs.get(0);
return getMobileProgramWithoutData(program);
} else {
for (Program program : programs) {
if (program.getOrganisationUnits().contains(organisationUnit)) {
programsInfo += program.getId() + "/" + program.getName() + "$";
}
}
throw new NotAllowedException(programsInfo);
}
} else {
throw NotAllowedException.NO_PROGRAM_FOUND;
}
}
use of org.hisp.dhis.program.ProgramType in project dhis2-core by dhis2.
the class JdbcEventStore method getEventRows.
@Override
public List<EventRow> getEventRows(EventSearchParams params, List<OrganisationUnit> organisationUnits) {
User user = currentUserService.getCurrentUser();
setAccessiblePrograms(user, params);
List<EventRow> eventRows = new ArrayList<>();
String sql = buildSql(params, organisationUnits, user);
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
log.debug("Event query SQL: " + sql);
EventRow eventRow = new EventRow();
eventRow.setEvent("not_valid");
Set<String> notes = new HashSet<>();
Map<String, List<DataValue>> processedDataValues = new HashMap<>();
while (rowSet.next()) {
if (rowSet.getString("psi_uid") == null || (params.getCategoryOptionCombo() == null && !isSuper(user) && !userHasAccess(rowSet))) {
continue;
}
if (eventRow.getUid() == null || !eventRow.getUid().equals(rowSet.getString("psi_uid"))) {
validateIdentifiersPresence(rowSet, params.getIdSchemes(), false);
eventRow = new EventRow();
eventRow.setUid(rowSet.getString("psi_uid"));
eventRow.setEvent(rowSet.getString("psi_uid"));
eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
eventRow.setTrackedEntityInstanceOrgUnit(rowSet.getString("tei_ou"));
eventRow.setTrackedEntityInstanceOrgUnitName(rowSet.getString("tei_ou_name"));
eventRow.setTrackedEntityInstanceCreated(rowSet.getString("tei_created"));
eventRow.setTrackedEntityInstanceInactive(rowSet.getBoolean("tei_inactive"));
eventRow.setDeleted(rowSet.getBoolean("psi_deleted"));
eventRow.setProgram(rowSet.getString("p_identifier"));
eventRow.setProgramStage(rowSet.getString("ps_identifier"));
eventRow.setOrgUnit(rowSet.getString("ou_identifier"));
ProgramType programType = ProgramType.fromValue(rowSet.getString("p_type"));
if (programType == ProgramType.WITHOUT_REGISTRATION) {
eventRow.setEnrollment(rowSet.getString("pi_uid"));
eventRow.setFollowup(rowSet.getBoolean("pi_followup"));
}
eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
eventRow.setOrgUnitName(rowSet.getString("ou_name"));
eventRow.setDueDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_duedate")));
eventRow.setEventDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_executiondate")));
eventRows.add(eventRow);
}
if (rowSet.getString("pav_value") != null && rowSet.getString("ta_uid") != null) {
String valueType = rowSet.getString("ta_valuetype");
Attribute attribute = new Attribute();
attribute.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_created")));
attribute.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_lastupdated")));
attribute.setValue(rowSet.getString("pav_value"));
attribute.setDisplayName(rowSet.getString("ta_name"));
attribute.setValueType(valueType != null ? ValueType.valueOf(valueType.toUpperCase()) : null);
attribute.setAttribute(rowSet.getString("ta_uid"));
eventRow.getAttributes().add(attribute);
}
if (!StringUtils.isEmpty(rowSet.getString("psi_eventdatavalues")) && !processedDataValues.containsKey(rowSet.getString("psi_uid"))) {
List<DataValue> dataValues = new ArrayList<>();
Set<EventDataValue> eventDataValues = convertEventDataValueJsonIntoSet(rowSet.getString("psi_eventdatavalues"));
for (EventDataValue dv : eventDataValues) {
dataValues.add(convertEventDataValueIntoDtoDataValue(dv));
}
processedDataValues.put(rowSet.getString("psi_uid"), dataValues);
}
if (rowSet.getString("psinote_value") != null && !notes.contains(rowSet.getString("psinote_id"))) {
Note note = new Note();
note.setNote(rowSet.getString("psinote_uid"));
note.setValue(rowSet.getString("psinote_value"));
note.setStoredDate(DateUtils.getIso8601NoTz(rowSet.getDate("psinote_storeddate")));
note.setStoredBy(rowSet.getString("psinote_storedby"));
eventRow.getNotes().add(note);
notes.add(rowSet.getString("psinote_id"));
}
}
eventRows.forEach(e -> e.setDataValues(processedDataValues.get(e.getUid())));
IdSchemes idSchemes = ObjectUtils.firstNonNull(params.getIdSchemes(), new IdSchemes());
IdScheme dataElementIdScheme = idSchemes.getDataElementIdScheme();
if (dataElementIdScheme != IdScheme.ID && dataElementIdScheme != IdScheme.UID) {
CachingMap<String, String> dataElementUidToIdentifierCache = new CachingMap<>();
List<Collection<DataValue>> dataValuesList = eventRows.stream().map(EventRow::getDataValues).collect(Collectors.toList());
populateCache(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
convertDataValuesIdentifiers(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
}
return eventRows;
}
use of org.hisp.dhis.program.ProgramType in project dhis2-core by dhis2.
the class JdbcEventStore method getEvents.
// -------------------------------------------------------------------------
// EventStore implementation
// -------------------------------------------------------------------------
@Override
public List<Event> getEvents(EventSearchParams params, List<OrganisationUnit> organisationUnits, Map<String, Set<String>> psdesWithSkipSyncTrue) {
User user = currentUserService.getCurrentUser();
setAccessiblePrograms(user, params);
Map<String, Event> eventUidToEventMap = new HashMap<>(params.getPageSizeWithDefault());
List<Event> events = new ArrayList<>();
List<Long> relationshipIds = new ArrayList<>();
final Gson gson = new Gson();
String sql = buildSql(params, organisationUnits, user);
SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
log.debug("Event query SQL: " + sql);
Set<String> notes = new HashSet<>();
while (rowSet.next()) {
if (rowSet.getString("psi_uid") == null || (params.getCategoryOptionCombo() == null && !isSuper(user) && !userHasAccess(rowSet))) {
continue;
}
String psiUid = rowSet.getString("psi_uid");
Event event;
if (!eventUidToEventMap.containsKey(psiUid)) {
validateIdentifiersPresence(rowSet, params.getIdSchemes(), true);
event = new Event();
eventUidToEventMap.put(psiUid, event);
if (!params.isSkipEventId()) {
event.setUid(psiUid);
event.setEvent(psiUid);
}
event.setTrackedEntityInstance(rowSet.getString("tei_uid"));
event.setStatus(EventStatus.valueOf(rowSet.getString("psi_status")));
ProgramType programType = ProgramType.fromValue(rowSet.getString("p_type"));
event.setProgram(rowSet.getString("p_identifier"));
event.setProgramType(programType);
event.setProgramStage(rowSet.getString("ps_identifier"));
event.setOrgUnit(rowSet.getString("ou_identifier"));
event.setDeleted(rowSet.getBoolean("psi_deleted"));
if (programType != ProgramType.WITHOUT_REGISTRATION) {
event.setEnrollment(rowSet.getString("pi_uid"));
event.setEnrollmentStatus(EnrollmentStatus.fromProgramStatus(ProgramStatus.valueOf(rowSet.getString("pi_status"))));
event.setFollowup(rowSet.getBoolean("pi_followup"));
}
if (params.getCategoryOptionCombo() == null && !isSuper(user)) {
event.setOptionSize(rowSet.getInt("option_size"));
}
event.setAttributeOptionCombo(rowSet.getString("coc_identifier"));
event.setAttributeCategoryOptions(rowSet.getString("deco_uid"));
event.setTrackedEntityInstance(rowSet.getString("tei_uid"));
event.setStoredBy(rowSet.getString("psi_storedby"));
event.setOrgUnitName(rowSet.getString("ou_name"));
event.setDueDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_duedate")));
event.setEventDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_executiondate")));
event.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("psi_created")));
event.setCreatedByUserInfo(jsonToUserInfo(rowSet.getString("psi_createdbyuserinfo"), jsonMapper));
event.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("psi_lastupdated")));
event.setLastUpdatedByUserInfo(jsonToUserInfo(rowSet.getString("psi_lastupdatedbyuserinfo"), jsonMapper));
event.setCompletedBy(rowSet.getString("psi_completedby"));
event.setCompletedDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_completeddate")));
if (rowSet.getObject("psi_geometry") != null) {
try {
Geometry geom = new WKTReader().read(rowSet.getString("psi_geometry"));
event.setGeometry(geom);
} catch (ParseException e) {
log.error("Unable to read geometry for event '" + event.getUid() + "': ", e);
}
}
if (rowSet.getObject("user_assigned") != null) {
event.setAssignedUser(rowSet.getString("user_assigned"));
event.setAssignedUserUsername(rowSet.getString("user_assigned_username"));
event.setAssignedUserDisplayName(rowSet.getString("user_assigned_name"));
}
events.add(event);
} else {
event = eventUidToEventMap.get(psiUid);
String attributeCategoryCombination = event.getAttributeCategoryOptions();
String currentAttributeCategoryCombination = rowSet.getString("deco_uid");
if (!attributeCategoryCombination.contains(currentAttributeCategoryCombination)) {
event.setAttributeCategoryOptions(attributeCategoryCombination + ";" + currentAttributeCategoryCombination);
}
}
if (!StringUtils.isEmpty(rowSet.getString("psi_eventdatavalues"))) {
Set<EventDataValue> eventDataValues = convertEventDataValueJsonIntoSet(rowSet.getString("psi_eventdatavalues"));
for (EventDataValue dv : eventDataValues) {
DataValue dataValue = convertEventDataValueIntoDtoDataValue(dv);
if (params.isSynchronizationQuery()) {
if (psdesWithSkipSyncTrue.containsKey(rowSet.getString("ps_uid")) && psdesWithSkipSyncTrue.get(rowSet.getString("ps_uid")).contains(dv.getDataElement())) {
dataValue.setSkipSynchronization(true);
} else {
dataValue.setSkipSynchronization(false);
}
}
event.getDataValues().add(dataValue);
}
}
if (rowSet.getString("psinote_value") != null && !notes.contains(rowSet.getString("psinote_id"))) {
Note note = new Note();
note.setNote(rowSet.getString("psinote_uid"));
note.setValue(rowSet.getString("psinote_value"));
note.setStoredDate(DateUtils.getIso8601NoTz(rowSet.getDate("psinote_storeddate")));
note.setStoredBy(rowSet.getString("psinote_storedby"));
if (rowSet.getObject("usernote_id") != null) {
note.setLastUpdatedBy(UserInfoSnapshot.of(rowSet.getLong("usernote_id"), rowSet.getString("usernote_code"), rowSet.getString("usernote_uid"), rowSet.getString("usernote_username"), rowSet.getString("userinfo_firstname"), rowSet.getString("userinfo_surname")));
}
note.setLastUpdated(rowSet.getDate("psinote_lastupdated"));
event.getNotes().add(note);
notes.add(rowSet.getString("psinote_id"));
}
if (params.isIncludeRelationships() && rowSet.getObject("psi_rl") != null) {
PGobject pGobject = (PGobject) rowSet.getObject("psi_rl");
if (pGobject != null) {
String value = pGobject.getValue();
relationshipIds.addAll(Lists.newArrayList(gson.fromJson(value, Long[].class)));
}
}
}
final Multimap<String, Relationship> map = eventStore.getRelationshipsByIds(relationshipIds);
if (!map.isEmpty()) {
events.forEach(e -> e.getRelationships().addAll(map.get(e.getEvent())));
}
IdSchemes idSchemes = ObjectUtils.firstNonNull(params.getIdSchemes(), new IdSchemes());
IdScheme dataElementIdScheme = idSchemes.getDataElementIdScheme();
if (dataElementIdScheme != IdScheme.ID && dataElementIdScheme != IdScheme.UID) {
CachingMap<String, String> dataElementUidToIdentifierCache = new CachingMap<>();
List<Collection<DataValue>> dataValuesList = events.stream().map(Event::getDataValues).collect(Collectors.toList());
populateCache(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
convertDataValuesIdentifiers(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
}
if (params.getCategoryOptionCombo() == null && !isSuper(user)) {
return events.stream().filter(ev -> ev.getAttributeCategoryOptions() != null && splitToArray(ev.getAttributeCategoryOptions(), TextUtils.SEMICOLON).size() == ev.getOptionSize()).collect(Collectors.toList());
}
return events;
}
Aggregations