use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.GEOMETRY in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.
private ImportSummary updateTrackedEntityInstance(TrackedEntityInstance dtoEntityInstance, String programId, ImportOptions importOptions, boolean singleUpdate, boolean handleEnrollments) {
ImportSummary importSummary = new ImportSummary(dtoEntityInstance.getTrackedEntityInstance());
importOptions = updateImportOptions(importOptions);
dtoEntityInstance.trimValuesToNull();
checkAttributes(dtoEntityInstance, importOptions, importSummary, true);
org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = teiService.getTrackedEntityInstance(dtoEntityInstance.getTrackedEntityInstance(), importOptions.getUser());
List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoEntityInstance);
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), dtoEntityInstance.getOrgUnit());
Program program = getProgram(importOptions.getIdSchemes(), programId);
if (daoEntityInstance == null || !errors.isEmpty() || organisationUnit == null || importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
if (daoEntityInstance == null) {
String message = "You are trying to add or update tracked entity instance " + dtoEntityInstance.getTrackedEntityInstance() + " that has already been deleted";
importSummary.addConflict("TrackedEntityInstance", message);
} else if (!errors.isEmpty()) {
importSummary.setDescription(errors.toString());
} else if (organisationUnit == null) {
String message = "Org unit " + dtoEntityInstance.getOrgUnit() + " does not exist";
importSummary.addConflict("OrganisationUnit", message);
}
return importSummary;
}
daoEntityInstance.setOrganisationUnit(organisationUnit);
daoEntityInstance.setInactive(dtoEntityInstance.isInactive());
daoEntityInstance.setPotentialDuplicate(dtoEntityInstance.isPotentialDuplicate());
if (dtoEntityInstance.getGeometry() != null) {
FeatureType featureType = daoEntityInstance.getTrackedEntityType().getFeatureType();
if (featureType.equals(FeatureType.NONE) || !featureType.equals(FeatureType.getTypeFromName(dtoEntityInstance.getGeometry().getGeometryType()))) {
importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Geometry does not conform to feature type '" + featureType + "'");
importSummary.getImportCount().incrementIgnored();
return importSummary;
} else {
daoEntityInstance.setGeometry(dtoEntityInstance.getGeometry());
}
} else if (!FeatureType.NONE.equals(dtoEntityInstance.getFeatureType()) && dtoEntityInstance.getCoordinates() != null) {
try {
daoEntityInstance.setGeometry(GeoUtils.getGeometryFromCoordinatesAndType(dtoEntityInstance.getFeatureType(), dtoEntityInstance.getCoordinates()));
} catch (IOException e) {
importSummary.addConflict(dtoEntityInstance.getTrackedEntityInstance(), "Could not parse coordinates");
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
} else {
daoEntityInstance.setGeometry(null);
}
if (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getAttributes().isEmpty()) {
updateAttributeValues(dtoEntityInstance, daoEntityInstance, program, importOptions.getUser());
}
updateDateFields(dtoEntityInstance, daoEntityInstance);
teiService.updateTrackedEntityInstance(daoEntityInstance);
importSummary.setReference(daoEntityInstance.getUid());
importSummary.getImportCount().incrementUpdated();
if (singleUpdate && (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getRelationships().isEmpty())) {
importSummary.setRelationships(handleRelationships(dtoEntityInstance, daoEntityInstance, importOptions));
}
if (handleEnrollments) {
importSummary.setEnrollments(handleEnrollments(dtoEntityInstance, daoEntityInstance, importOptions));
} else {
for (Enrollment enrollment : dtoEntityInstance.getEnrollments()) {
enrollment.setTrackedEntityType(dtoEntityInstance.getTrackedEntityType());
enrollment.setTrackedEntityInstance(daoEntityInstance.getUid());
}
}
return importSummary;
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.GEOMETRY in project dhis2-core by dhis2.
the class ProgramStageInstanceMapper method mapForInsert.
public ProgramStageInstance mapForInsert(Event event) {
ImportOptions importOptions = workContext.getImportOptions();
ProgramStageInstance psi = new ProgramStageInstance();
if (importOptions.getIdSchemes().getProgramStageInstanceIdScheme().equals(CODE)) {
psi.setCode(event.getEvent());
} else if (importOptions.getIdSchemes().getProgramStageIdScheme().equals(UID)) {
psi.setUid(event.getUid());
}
// Program Instance
psi.setProgramInstance(this.workContext.getProgramInstanceMap().get(event.getUid()));
// Program Stage
psi.setProgramStage(this.workContext.getProgramStage(importOptions.getIdSchemes().getProgramStageIdScheme(), event.getProgramStage()));
// Org Unit
psi.setOrganisationUnit(this.workContext.getOrganisationUnitMap().get(event.getUid()));
// Status
psi.setStatus(fromInt(event.getStatus().getValue()));
// Attribute Option Combo
psi.setAttributeOptionCombo(this.workContext.getCategoryOptionComboMap().get(event.getUid()));
// Geometry
psi.setGeometry(event.getGeometry());
// Notes
psi.setComments(convertNotes(event, this.workContext));
// Data Values
psi.setEventDataValues(workContext.getEventDataValueMap().get(event.getUid()));
Date dueDate = new Date();
if (event.getDueDate() != null) {
dueDate = parseDate(event.getDueDate());
}
psi.setDueDate(dueDate);
setCompletedDate(event, psi);
// Note that execution date can be null
setExecutionDate(event, psi);
if (psi.getProgramStage() != null && psi.getProgramStage().isEnableUserAssignment()) {
psi.setAssignedUser(this.workContext.getAssignedUserMap().get(event.getUid()));
}
// CREATED AT CLIENT + UPDATED AT CLIENT
psi.setCreatedAtClient(parseDate(event.getCreatedAtClient()));
psi.setLastUpdatedAtClient(parseDate(event.getLastUpdatedAtClient()));
psi.setStoredBy(event.getStoredBy());
psi.setCompletedBy(event.getCompletedBy());
psi.setCreatedByUserInfo(event.getCreatedByUserInfo());
psi.setLastUpdatedByUserInfo(event.getLastUpdatedByUserInfo());
return psi;
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.GEOMETRY in project dhis2-core by dhis2.
the class EventGeometryCheckTest method failOnEventWithGeometryAndProgramStageWithNoGeometry.
@Test
void failOnEventWithGeometryAndProgramStageWithNoGeometry() {
event.setGeometry(createRandomPoint());
ProgramStage programStage = createProgramStage();
programStage.setFeatureType(FeatureType.NONE);
when(workContext.getProgramStage(programStageIdScheme, event.getProgramStage())).thenReturn(programStage);
ImportSummary importSummary = rule.check(new ImmutableEvent(event), workContext);
assertHasError(importSummary, event, "Geometry (Point) does not conform to the feature type (None) specified for the program stage: " + programStage.getUid());
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.GEOMETRY in project dhis2-core by dhis2.
the class DefaultCsvImportService method fromCsv.
// -------------------------------------------------------------------------
// CsvImportService implementation
// -------------------------------------------------------------------------
// TODO Add unit tests
@Override
public Metadata fromCsv(InputStream input, CsvImportOptions options) throws IOException {
CsvReader reader = CsvUtils.getReader(input);
// Disabled due to large geometry
reader.setSafetySwitch(false);
if (options.isFirstRowIsHeader()) {
// Ignore first row
reader.readRecord();
}
Metadata metadata = new Metadata();
switch(options.getImportClass()) {
case DATA_ELEMENT:
metadata.setDataElements(dataElementsFromCsv(reader));
break;
case DATA_ELEMENT_GROUP:
metadata.setDataElementGroups(dataElementGroupsFromCsv(reader));
break;
case DATA_ELEMENT_GROUP_MEMBERSHIP:
metadata.setDataElementGroups(dataElementGroupMembersFromCsv(reader));
break;
case INDICATOR_GROUP_MEMBERSHIP:
metadata.setIndicatorGroups(indicatorGroupMembersFromCsv(reader));
break;
case CATEGORY_OPTION:
metadata.setCategoryOptions(categoryOptionsFromCsv(reader));
break;
case CATEGORY:
metadata.setCategories(categoriesFromCsv(reader));
break;
case CATEGORY_COMBO:
metadata.setCategoryCombos(categoryCombosFromCsv(reader));
break;
case CATEGORY_OPTION_GROUP:
metadata.setCategoryOptionGroups(categoryOptionGroupsFromCsv(reader));
break;
case ORGANISATION_UNIT:
metadata.setOrganisationUnits(orgUnitsFromCsv(reader));
break;
case ORGANISATION_UNIT_GROUP:
metadata.setOrganisationUnitGroups(orgUnitGroupsFromCsv(reader));
break;
case ORGANISATION_UNIT_GROUP_MEMBERSHIP:
metadata.setOrganisationUnitGroups(orgUnitGroupMembersFromCsv(reader));
break;
case VALIDATION_RULE:
metadata.setValidationRules(validationRulesFromCsv(reader));
break;
case OPTION_SET:
setOptionSetsFromCsv(reader, metadata);
break;
case OPTION_GROUP:
setOptionGroupsFromCsv(reader, metadata);
break;
case OPTION_GROUP_SET:
metadata.setOptionGroupSets(setOptionGroupSetFromCsv(reader));
break;
case OPTION_GROUP_SET_MEMBERSHIP:
metadata.setOptionGroupSets(optionGroupSetMembersFromCsv(reader));
break;
default:
break;
}
return metadata;
}
use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.GEOMETRY 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