Search in sources :

Example 1 with ImportConflicts

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;
}
Also used : ArrayList(java.util.ArrayList) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 2 with 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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 3 with ImportConflicts

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;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ArrayList(java.util.ArrayList) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 4 with 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;
}
Also used : ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 5 with ImportConflicts

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;
}
Also used : ArrayList(java.util.ArrayList) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Aggregations

ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)13 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)9 ArrayList (java.util.ArrayList)8 HashSet (java.util.HashSet)7 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)7 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)6 TrackedEntityAttribute (org.hisp.dhis.trackedentity.TrackedEntityAttribute)6 ImportConflicts (org.hisp.dhis.dxf2.importsummary.ImportConflicts)5 ImportStatus (org.hisp.dhis.dxf2.importsummary.ImportStatus)5 Program (org.hisp.dhis.program.Program)5 ProgramInstance (org.hisp.dhis.program.ProgramInstance)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Date (java.util.Date)4 Map (java.util.Map)4 Set (java.util.Set)4 IdSchemes (org.hisp.dhis.common.IdSchemes)4 Lists (com.google.common.collect.Lists)3 Collections (java.util.Collections)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3