use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateAttributeValues.
private void updateAttributeValues(TrackedEntityInstance dtoEntityInstance, org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance, Program program, User user) {
Set<String> incomingAttributes = new HashSet<>();
Map<String, TrackedEntityAttributeValue> teiAttributeToValueMap = getTeiAttributeValueMap(trackedEntityAttributeValueService.getTrackedEntityAttributeValues(daoEntityInstance));
for (Attribute dtoAttribute : dtoEntityInstance.getAttributes()) {
String storedBy = getStoredBy(dtoAttribute, new ImportSummary(), User.username(user, Constants.UNKNOWN));
TrackedEntityAttributeValue existingAttributeValue = teiAttributeToValueMap.get(dtoAttribute.getAttribute());
incomingAttributes.add(dtoAttribute.getAttribute());
if (// value exists
existingAttributeValue != null) {
if (// value
!existingAttributeValue.getValue().equals(dtoAttribute.getValue())) // is
// changed,
// do
// update
{
existingAttributeValue.setStoredBy(storedBy);
existingAttributeValue.setValue(dtoAttribute.getValue());
trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(existingAttributeValue, user);
}
} else // value is new, do add
{
TrackedEntityAttribute daoEntityAttribute = trackedEntityAttributeService.getTrackedEntityAttribute(dtoAttribute.getAttribute());
TrackedEntityAttributeValue newAttributeValue = new TrackedEntityAttributeValue();
newAttributeValue.setStoredBy(storedBy);
newAttributeValue.setEntityInstance(daoEntityInstance);
newAttributeValue.setValue(dtoAttribute.getValue());
newAttributeValue.setAttribute(daoEntityAttribute);
daoEntityInstance.getTrackedEntityAttributeValues().add(newAttributeValue);
trackedEntityAttributeValueService.addTrackedEntityAttributeValue(newAttributeValue);
}
}
if (program != null) {
for (TrackedEntityAttribute att : program.getTrackedEntityAttributes()) {
TrackedEntityAttributeValue attVal = teiAttributeToValueMap.get(att.getUid());
if (attVal != null && !incomingAttributes.contains(att.getUid())) {
trackedEntityAttributeValueService.deleteTrackedEntityAttributeValue(attVal);
}
}
}
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstances.
// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
private ImportSummaries updateTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
List<List<TrackedEntityInstance>> partitions = Lists.partition(trackedEntityInstances, FLUSH_FREQUENCY);
importOptions = updateImportOptions(importOptions);
ImportSummaries importSummaries = new ImportSummaries();
List<Enrollment> enrollments = new ArrayList<>();
for (List<TrackedEntityInstance> _trackedEntityInstances : partitions) {
reloadUser(importOptions);
prepareCaches(_trackedEntityInstances, importOptions.getUser());
for (TrackedEntityInstance trackedEntityInstance : _trackedEntityInstances) {
ImportSummary importSummary = updateTrackedEntityInstance(trackedEntityInstance, null, importOptions, false, false);
importSummaries.addImportSummary(importSummary);
if (importSummary.isStatus(ImportStatus.SUCCESS)) {
enrollments.addAll(trackedEntityInstance.getEnrollments());
}
}
clearSession();
}
ImportSummaries enrollmentImportSummaries = enrollmentService.addEnrollmentList(enrollments, importOptions);
linkEnrollmentSummaries(importSummaries, enrollmentImportSummaries, enrollments);
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method deleteTrackedEntityInstances.
@Override
@Transactional
public ImportSummaries deleteTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
ImportSummaries importSummaries = new ImportSummaries();
importOptions = updateImportOptions(importOptions);
int counter = 0;
for (TrackedEntityInstance tei : trackedEntityInstances) {
importSummaries.addImportSummary(deleteTrackedEntityInstance(tei.getTrackedEntityInstance(), tei, importOptions));
if (counter % FLUSH_FREQUENCY == 0) {
clearSession();
}
counter++;
}
clearSession();
return importSummaries;
}
use of org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method addTrackedEntityInstance.
private ImportSummary addTrackedEntityInstance(TrackedEntityInstance dtoEntityInstance, ImportOptions importOptions, boolean handleEnrollments, boolean bulkImport) {
if (!bulkImport && teiService.trackedEntityInstanceExistsIncludingDeleted(dtoEntityInstance.getTrackedEntityInstance())) {
return new ImportSummary(ImportStatus.ERROR, "Tracked entity instance " + dtoEntityInstance.getTrackedEntityInstance() + " already exists or was deleted earlier").setReference(dtoEntityInstance.getTrackedEntityInstance()).incrementIgnored();
}
importOptions = updateImportOptions(importOptions);
dtoEntityInstance.trimValuesToNull();
ImportSummary importSummary = new ImportSummary(dtoEntityInstance.getTrackedEntityInstance());
checkTrackedEntityType(dtoEntityInstance, importOptions, importSummary);
checkAttributes(dtoEntityInstance, importOptions, importSummary, false);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = createDAOTrackedEntityInstance(dtoEntityInstance, importOptions, importSummary);
if (importSummary.hasConflicts()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
if (daoEntityInstance == null) {
return importSummary;
}
List<String> errors = trackerAccessManager.canWrite(importOptions.getUser(), daoEntityInstance);
if (!errors.isEmpty()) {
return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
}
teiService.addTrackedEntityInstance(daoEntityInstance);
addAttributeValues(dtoEntityInstance, daoEntityInstance, importOptions.getUser());
importSummary.setReference(daoEntityInstance.getUid());
importSummary.getImportCount().incrementImported();
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.TrackedEntityInstance in project dhis2-core by dhis2.
the class RelationshipRowCallbackHandler method createItem.
/**
* The SQL query that generates the ResultSet used by this
* {@see RowCallbackHandler} fetches both sides of a relationship: since
* each side can be a Tracked Entity Instance, a Program Instance or a
* Program Stage Instance, the query adds an "hint" to the final result to
* help this Handler to correctly associate the type to the left or right
* side of the relationship. The "typeWithUid" variable contains the UID of
* the object and a string representing the type. E.g.
*
* tei|dj3382832 psi|332983893
*
* This function parses the string and extract the type and the uid, in
* order to instantiate the appropriate object and assign it to the
* {@see RelationshipItem}
*
* @param typeWithUid a String containing the object type and the UID of the
* object, separated by | (pipe)
* @return a {@see RelationshipItem}
*/
private RelationshipItem createItem(String typeWithUid) {
if (StringUtils.isEmpty(typeWithUid)) {
return new RelationshipItem();
}
RelationshipItem ri = new RelationshipItem();
final String type = typeWithUid.split("\\|")[0];
final String uid = typeWithUid.split("\\|")[1];
switch(type) {
case "tei":
TrackedEntityInstance tei = new TrackedEntityInstance();
tei.clear();
tei.setTrackedEntityInstance(uid);
ri.setTrackedEntityInstance(tei);
break;
case "pi":
Enrollment pi = new Enrollment();
pi.setEnrollment(uid);
ri.setEnrollment(pi);
break;
case "psi":
Event psi = new Event();
psi.setEvent(uid);
ri.setEvent(psi);
break;
default:
log.warn("Expecting tei|psi|pi as type when fetching a relationship, got: " + type);
}
return ri;
}
Aggregations