use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method checkRelationships.
private List<ImportConflict> checkRelationships(TrackedEntityInstance trackedEntityInstance) {
List<ImportConflict> importConflicts = new ArrayList<>();
for (org.hisp.dhis.dxf2.events.trackedentity.Relationship relationship : trackedEntityInstance.getRelationships()) {
RelationshipType relationshipType = manager.get(RelationshipType.class, relationship.getRelationship());
if (relationshipType == null) {
importConflicts.add(new ImportConflict("Relationship.type", "Invalid type " + relationship.getRelationship()));
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstanceA = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, relationship.getTrackedEntityInstanceA());
if (entityInstanceA == null) {
importConflicts.add(new ImportConflict("Relationship.trackedEntityInstance", "Invalid trackedEntityInstance " + relationship.getTrackedEntityInstanceA()));
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstanceB = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, relationship.getTrackedEntityInstanceB());
if (entityInstanceB == null) {
importConflicts.add(new ImportConflict("Relationship.trackedEntityInstance", "Invalid trackedEntityInstance " + relationship.getTrackedEntityInstanceB()));
}
}
return importConflicts;
}
use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstance.
@Override
public ImportSummary updateTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(trackedEntityInstance.getTrackedEntityInstance());
trackedEntityInstance.trimValuesToNull();
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkRelationships(trackedEntityInstance));
importConflicts.addAll(checkAttributes(trackedEntityInstance, importOptions));
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = manager.get(org.hisp.dhis.trackedentity.TrackedEntityInstance.class, trackedEntityInstance.getTrackedEntityInstance());
if (entityInstance == null) {
importConflicts.add(new ImportConflict("TrackedEntityInstance", "trackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " does not point to valid trackedEntityInstance"));
}
OrganisationUnit organisationUnit = manager.get(OrganisationUnit.class, trackedEntityInstance.getOrgUnit());
if (organisationUnit == null) {
importConflicts.add(new ImportConflict("OrganisationUnit", "orgUnit " + trackedEntityInstance.getOrgUnit() + " does not point to valid organisation unit"));
}
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
entityInstance.setInactive(trackedEntityInstance.isInactive());
removeRelationships(entityInstance);
removeAttributeValues(entityInstance);
teiService.updateTrackedEntityInstance(entityInstance);
updateRelationships(trackedEntityInstance, entityInstance);
updateAttributeValues(trackedEntityInstance, entityInstance);
updateDateFields(trackedEntityInstance, entityInstance);
teiService.updateTrackedEntityInstance(entityInstance);
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setReference(entityInstance.getUid());
importSummary.getImportCount().incrementUpdated();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEnrollments(handleEnrollments(trackedEntityInstance, entityInstance, importOptions));
return importSummary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method checkAttributes.
private List<ImportConflict> checkAttributes(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
List<ImportConflict> importConflicts = new ArrayList<>();
for (Attribute attribute : trackedEntityInstance.getAttributes()) {
TrackedEntityAttribute entityAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
if (entityAttribute == null) {
importConflicts.add(new ImportConflict("Attribute.attribute", "Invalid attribute " + attribute.getAttribute()));
continue;
}
if (entityAttribute.isUnique()) {
OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), trackedEntityInstance.getOrgUnit());
org.hisp.dhis.trackedentity.TrackedEntityInstance tei = teiService.getTrackedEntityInstance(trackedEntityInstance.getTrackedEntityInstance());
importConflicts.addAll(checkScope(tei, entityAttribute, attribute.getValue(), organisationUnit));
}
importConflicts.addAll(validateAttributeType(attribute, importOptions));
}
return importConflicts;
}
use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class AbstractTrackedEntityInstanceService method addTrackedEntityInstance.
@Override
public ImportSummary addTrackedEntityInstance(TrackedEntityInstance trackedEntityInstance, ImportOptions importOptions) {
if (importOptions == null) {
importOptions = new ImportOptions();
}
ImportSummary importSummary = new ImportSummary(trackedEntityInstance.getTrackedEntityInstance());
trackedEntityInstance.trimValuesToNull();
Set<ImportConflict> importConflicts = new HashSet<>();
importConflicts.addAll(checkTrackedEntity(trackedEntityInstance, importOptions));
importConflicts.addAll(checkAttributes(trackedEntityInstance, importOptions));
importSummary.setConflicts(importConflicts);
if (!importConflicts.isEmpty()) {
importSummary.setStatus(ImportStatus.ERROR);
importSummary.getImportCount().incrementIgnored();
return importSummary;
}
org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(trackedEntityInstance, importOptions, importSummary);
if (entityInstance == null) {
return importSummary;
}
teiService.addTrackedEntityInstance(entityInstance);
updateRelationships(trackedEntityInstance, entityInstance);
updateAttributeValues(trackedEntityInstance, entityInstance);
updateDateFields(trackedEntityInstance, entityInstance);
teiService.updateTrackedEntityInstance(entityInstance);
importSummary.setReference(entityInstance.getUid());
importSummary.getImportCount().incrementImported();
importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
importSummary.setEnrollments(handleEnrollments(trackedEntityInstance, entityInstance, importOptions));
return importSummary;
}
use of org.hisp.dhis.dxf2.importsummary.ImportConflicts in project dhis2-core by dhis2.
the class AbstractEnrollmentService method checkScope.
private List<ImportConflict> checkScope(org.hisp.dhis.trackedentity.TrackedEntityInstance trackedEntityInstance, TrackedEntityAttribute trackedEntityAttribute, String value, OrganisationUnit organisationUnit, Program program) {
List<ImportConflict> importConflicts = new ArrayList<>();
if (trackedEntityAttribute == null || value == null) {
return importConflicts;
}
String errorMessage = trackedEntityAttributeService.validateScope(trackedEntityAttribute, value, trackedEntityInstance, organisationUnit, program);
if (errorMessage != null) {
importConflicts.add(new ImportConflict("Attribute.value", errorMessage));
}
return importConflicts;
}
Aggregations