use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class TrackedEntityInstanceCheck method check.
@Override
public ImportSummary check(ImmutableEvent event, WorkContext ctx) {
Program program = ctx.getProgramsMap().get(event.getProgram());
final Optional<TrackedEntityInstance> trackedEntityInstance = ctx.getTrackedEntityInstance(event.getUid());
if (program.isRegistration() && !trackedEntityInstance.isPresent()) {
return new ImportSummary(ImportStatus.ERROR, "Event.trackedEntityInstance does not point to a valid tracked entity instance: " + event.getTrackedEntityInstance()).setReference(event.getEvent()).incrementIgnored();
}
return success();
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method prepareCaches.
private void prepareCaches(List<TrackedEntityInstance> trackedEntityInstances, User user) {
Collection<String> orgUnits = trackedEntityInstances.stream().map(TrackedEntityInstance::getOrgUnit).collect(Collectors.toSet());
if (!orgUnits.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(OrganisationUnit.class));
query.setUser(user);
query.add(Restrictions.in("id", orgUnits));
queryService.query(query).forEach(ou -> organisationUnitCache.put(ou.getUid(), (OrganisationUnit) ou));
}
Collection<String> trackedEntityAttributes = new HashSet<>();
trackedEntityInstances.forEach(e -> e.getAttributes().forEach(at -> trackedEntityAttributes.add(at.getAttribute())));
if (!trackedEntityAttributes.isEmpty()) {
Query query = Query.from(schemaService.getDynamicSchema(TrackedEntityAttribute.class));
query.setUser(user);
query.add(Restrictions.in("id", trackedEntityAttributes));
queryService.query(query).forEach(tea -> trackedEntityAttributeCache.put(tea.getUid(), (TrackedEntityAttribute) tea));
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method mergeOrDeleteTrackedEntityInstances.
// -------------------------------------------------------------------------
// CREATE, UPDATE or DELETE
// -------------------------------------------------------------------------
@Override
@Transactional
public ImportSummaries mergeOrDeleteTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions, JobConfiguration jobId) {
notifier.clear(jobId).notify(jobId, "Importing tracked entities");
try {
ImportSummaries importSummaries = new ImportSummaries();
importOptions = updateImportOptions(importOptions);
List<TrackedEntityInstance> create = new ArrayList<>();
List<TrackedEntityInstance> update = new ArrayList<>();
List<TrackedEntityInstance> delete = new ArrayList<>();
// TODO: Check whether relationships are modified during
// create/update/delete TEI logic. Decide whether logic below can be
// removed
List<Relationship> relationships = getRelationships(trackedEntityInstances);
setTrackedEntityListByStrategy(trackedEntityInstances, importOptions, create, update, delete);
importSummaries.addImportSummaries(addTrackedEntityInstances(create, importOptions));
importSummaries.addImportSummaries(updateTrackedEntityInstances(update, importOptions));
importSummaries.addImportSummaries(deleteTrackedEntityInstances(delete, importOptions));
// TODO: Created importSummaries don't contain correct href (TEI
// endpoint instead of relationships is used)
importSummaries.addImportSummaries(relationshipService.processRelationshipList(relationships, importOptions));
if (ImportReportMode.ERRORS == importOptions.getReportMode()) {
importSummaries.getImportSummaries().removeIf(is -> !is.hasConflicts());
}
notifier.notify(jobId, NotificationLevel.INFO, "Import done", true).addJobSummary(jobId, importSummaries, ImportSummaries.class);
return importSummaries;
} catch (RuntimeException ex) {
log.error(DebugUtils.getStackTrace(ex));
notifier.notify(jobId, ERROR, "Process failed: " + ex.getMessage(), true);
return new ImportSummaries().addImportSummary(new ImportSummary(ImportStatus.ERROR, "The import process failed: " + ex.getMessage()));
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method addAttributeValues.
private void addAttributeValues(TrackedEntityInstance dtoEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance, User user) {
for (Attribute dtoAttribute : dtoEntityInstance.getAttributes()) {
TrackedEntityAttribute daoEntityAttribute = trackedEntityAttributeService.getTrackedEntityAttribute(dtoAttribute.getAttribute());
if (daoEntityAttribute != null) {
TrackedEntityAttributeValue daoAttributeValue = new TrackedEntityAttributeValue();
daoAttributeValue.setEntityInstance(daoEntityInstance);
daoAttributeValue.setValue(dtoAttribute.getValue());
daoAttributeValue.setAttribute(daoEntityAttribute);
daoEntityInstance.addAttributeValue(daoAttributeValue);
String storedBy = getStoredBy(dtoAttribute, new ImportSummary(), User.username(user, Constants.UNKNOWN));
daoAttributeValue.setStoredBy(storedBy);
trackedEntityAttributeValueService.addTrackedEntityAttributeValue(daoAttributeValue);
}
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method handleRelationships.
private ImportSummaries handleRelationships(TrackedEntityInstance dtoEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance, ImportOptions importOptions) {
ImportSummaries importSummaries = new ImportSummaries();
List<Relationship> create = new ArrayList<>();
List<Relationship> update = new ArrayList<>();
List<String> relationshipUids = dtoEntityInstance.getRelationships().stream().map(Relationship::getRelationship).collect(Collectors.toList());
List<Relationship> delete = daoEntityInstance.getRelationshipItems().stream().map(RelationshipItem::getRelationship).filter(relationship -> trackerAccessManager.canWrite(importOptions.getUser(), relationship).isEmpty()).filter(relationship -> isTeiPartOfRelationship(relationship, daoEntityInstance)).map(org.hisp.dhis.relationship.Relationship::getUid).filter((uid) -> !relationshipUids.contains(uid)).map(uid -> {
Relationship relationship = new Relationship();
relationship.setRelationship(uid);
return relationship;
}).collect(Collectors.toList());
for (Relationship relationship : dtoEntityInstance.getRelationships()) {
if (importOptions.getImportStrategy() == ImportStrategy.SYNC && dtoEntityInstance.isDeleted()) {
delete.add(relationship);
} else if (relationship.getRelationship() == null) {
org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem relationshipItem = new org.hisp.dhis.dxf2.events.trackedentity.RelationshipItem();
if (!isTeiPartOfRelationship(relationship, daoEntityInstance)) {
relationshipItem.setTrackedEntityInstance(dtoEntityInstance);
relationship.setFrom(relationshipItem);
}
create.add(relationship);
} else {
if (isTeiPartOfRelationship(relationship, daoEntityInstance)) {
if (_relationshipService.relationshipExists(relationship.getRelationship())) {
update.add(relationship);
} else {
create.add(relationship);
}
} else {
String message = String.format("Can't update relationship '%s': TrackedEntityInstance '%s' is not the owner of the relationship", relationship.getRelationship(), daoEntityInstance.getUid());
importSummaries.addImportSummary(new ImportSummary(ImportStatus.ERROR, message).setReference(relationship.getRelationship()).incrementIgnored());
}
}
}
importSummaries.addImportSummaries(relationshipService.addRelationships(create, importOptions));
importSummaries.addImportSummaries(relationshipService.updateRelationships(update, importOptions));
importSummaries.addImportSummaries(relationshipService.deleteRelationships(delete, importOptions));
return importSummaries;
}
Aggregations