use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.
the class AbstractRelationshipService method getRelationship.
@Override
@Transactional(readOnly = true)
public Relationship getRelationship(org.hisp.dhis.relationship.Relationship dao, RelationshipParams params, User user) {
List<String> errors = trackerAccessManager.canRead(user, dao);
if (!errors.isEmpty()) {
// Dont include relationship
return null;
}
Relationship relationship = new Relationship();
relationship.setRelationship(dao.getUid());
relationship.setRelationshipType(dao.getRelationshipType().getUid());
relationship.setRelationshipName(dao.getRelationshipType().getName());
relationship.setFrom(includeRelationshipItem(dao.getFrom(), !params.isIncludeFrom()));
relationship.setTo(includeRelationshipItem(dao.getTo(), !params.isIncludeTo()));
relationship.setBidirectional(dao.getRelationshipType().isBidirectional());
relationship.setCreated(DateUtils.getIso8601NoTz(dao.getCreated()));
relationship.setLastUpdated(DateUtils.getIso8601NoTz(dao.getLastUpdated()));
return relationship;
}
use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.
the class AbstractRelationshipService method addRelationship.
@Override
@Transactional
public ImportSummary addRelationship(Relationship relationship, ImportOptions importOptions) {
importOptions = updateImportOptions(importOptions);
// Set up cache if not set already
if (!cacheExists()) {
prepareCaches(Lists.newArrayList(relationship), importOptions.getUser());
}
ImportSummary importSummary = new ImportSummary(relationship.getRelationship());
checkRelationship(relationship, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
org.hisp.dhis.relationship.Relationship daoRelationship = createDAORelationship(relationship);
Optional<org.hisp.dhis.relationship.Relationship> existing = relationshipService.getRelationshipByRelationship(daoRelationship);
if (existing.isPresent()) {
String message = "Relationship " + existing.get().getUid() + " already exists";
return new ImportSummary(ImportStatus.ERROR, message).setReference(existing.get().getUid()).incrementIgnored();
}
// Check access for both sides
List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoRelationship);
if (!errors.isEmpty()) {
return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
}
relationshipService.addRelationship(daoRelationship);
importSummary.setReference(daoRelationship.getUid());
importSummary.getImportCount().incrementImported();
return importSummary;
}
use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.
the class AbstractRelationshipService method deleteRelationship.
private ImportSummary deleteRelationship(String uid, ImportOptions importOptions) {
ImportSummary importSummary = new ImportSummary();
importOptions = updateImportOptions(importOptions);
if (uid.isEmpty()) {
importSummary.setStatus(ImportStatus.WARNING);
importSummary.setDescription("Missing required property 'relationship'");
return importSummary.incrementIgnored();
}
org.hisp.dhis.relationship.Relationship daoRelationship = relationshipService.getRelationship(uid);
if (daoRelationship != null) {
importSummary.setReference(uid);
List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoRelationship);
if (!errors.isEmpty()) {
importSummary.setDescription(errors.toString());
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
relationshipService.deleteRelationship(daoRelationship);
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setDescription("Deletion of relationship " + uid + " was successful");
return importSummary.incrementDeleted();
} else {
importSummary.setStatus(ImportStatus.WARNING);
importSummary.setDescription("Relationship " + uid + " cannot be deleted as it is not present in the system");
return importSummary.incrementIgnored();
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.
the class AbstractRelationshipService method updateRelationships.
@Override
@Transactional
public ImportSummaries updateRelationships(List<Relationship> relationships, ImportOptions importOptions) {
List<List<Relationship>> partitions = Lists.partition(relationships, FLUSH_FREQUENCY);
importOptions = updateImportOptions(importOptions);
ImportSummaries importSummaries = new ImportSummaries();
for (List<Relationship> _relationships : partitions) {
reloadUser(importOptions);
prepareCaches(_relationships, importOptions.getUser());
for (Relationship relationship : _relationships) {
importSummaries.addImportSummary(updateRelationship(relationship, importOptions));
}
clearSession();
}
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.trackedentity.Relationship in project dhis2-core by dhis2.
the class TrackedEntityInstanceAggregateTest method testTrackedEntityInstanceRelationshipsTei2Event.
@Test
void testTrackedEntityInstanceRelationshipsTei2Event() {
final String[] relationshipItemsUid = new String[2];
doInTransaction(() -> {
org.hisp.dhis.trackedentity.TrackedEntityInstance t1 = this.persistTrackedEntityInstance();
org.hisp.dhis.trackedentity.TrackedEntityInstance t2 = this.persistTrackedEntityInstanceWithEnrollmentAndEvents();
sessionFactory.getCurrentSession().flush();
sessionFactory.getCurrentSession().clear();
t2 = manager.getByUid(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, Collections.singletonList(t2.getUid())).get(0);
ProgramInstance pi = t2.getProgramInstances().iterator().next();
final ProgramStageInstance psi = pi.getProgramStageInstances().iterator().next();
this.persistRelationship(t1, psi);
relationshipItemsUid[0] = t1.getUid();
relationshipItemsUid[1] = psi.getUid();
});
TrackedEntityInstanceQueryParams queryParams = new TrackedEntityInstanceQueryParams();
queryParams.setOrganisationUnits(Sets.newHashSet(organisationUnitA));
queryParams.setTrackedEntityType(trackedEntityTypeA);
queryParams.setIncludeAllAttributes(true);
TrackedEntityInstanceParams params = new TrackedEntityInstanceParams();
params.setIncludeRelationships(true);
params.setIncludeEnrollments(true);
params.setIncludeEvents(true);
final List<TrackedEntityInstance> trackedEntityInstances = trackedEntityInstanceService.getTrackedEntityInstances(queryParams, params, false, true);
// Fetch the TEI which is the vertex of the relationship TEI <-->
// ENROLLMENT
Optional<TrackedEntityInstance> trackedEntityInstance = trackedEntityInstances.stream().filter(t -> t.getTrackedEntityInstance().equals(relationshipItemsUid[0])).findFirst();
if (trackedEntityInstance.isPresent()) {
assertThat(trackedEntityInstance.get().getRelationships(), hasSize(1));
final Relationship relationship = trackedEntityInstance.get().getRelationships().get(0);
assertThat(relationship.getFrom().getTrackedEntityInstance().getTrackedEntityInstance(), is(relationshipItemsUid[0]));
assertThat(relationship.getTo().getEvent().getEvent(), is(relationshipItemsUid[1]));
} else {
fail();
}
}
Aggregations