Search in sources :

Example 51 with ImportOptions

use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.

the class AbstractEnrollmentService method addEnrollment.

@Override
public ImportSummary addEnrollment(Enrollment enrollment, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(enrollment.getEnrollment());
    org.hisp.dhis.trackedentity.TrackedEntityInstance entityInstance = getTrackedEntityInstance(enrollment.getTrackedEntityInstance());
    TrackedEntityInstance trackedEntityInstance = trackedEntityInstanceService.getTrackedEntityInstance(entityInstance);
    Program program = getProgram(importOptions.getIdSchemes(), enrollment.getProgram());
    ProgramInstanceQueryParams params = new ProgramInstanceQueryParams();
    params.setOrganisationUnitMode(OrganisationUnitSelectionMode.ALL);
    params.setSkipPaging(true);
    params.setProgram(program);
    params.setTrackedEntityInstance(entityInstance);
    params.setProgramStatus(ProgramStatus.ACTIVE);
    List<Enrollment> enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
    if (!enrollments.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have an active enrollment in program " + program.getUid());
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getOnlyEnrollOnce()) {
        params.setProgramStatus(ProgramStatus.COMPLETED);
        enrollments = getEnrollments(programInstanceService.getProgramInstances(params));
        if (!enrollments.isEmpty()) {
            importSummary.setStatus(ImportStatus.ERROR);
            importSummary.setDescription("TrackedEntityInstance " + trackedEntityInstance.getTrackedEntityInstance() + " already have a completed enrollment in program " + program.getUid() + ", and this program is" + " configured to only allow enrolling one time.");
            importSummary.incrementIgnored();
            return importSummary;
        }
    }
    Set<ImportConflict> importConflicts = new HashSet<>();
    importConflicts.addAll(checkAttributes(enrollment, importOptions));
    importSummary.setConflicts(importConflicts);
    if (!importConflicts.isEmpty()) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.incrementIgnored();
        return importSummary;
    }
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), enrollment.getOrgUnit());
    ProgramInstance programInstance = programInstanceService.enrollTrackedEntityInstance(entityInstance, program, enrollment.getEnrollmentDate(), enrollment.getIncidentDate(), organisationUnit, enrollment.getEnrollment());
    if (programInstance == null) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("Could not enroll TrackedEntityInstance " + enrollment.getTrackedEntityInstance() + " into program " + enrollment.getProgram());
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getDisplayIncidentDate() && programInstance.getIncidentDate() == null) {
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("DisplayIncidentDate is true but IncidentDate is null ");
        importSummary.incrementIgnored();
        return importSummary;
    }
    if (program.getCaptureCoordinates()) {
        if (enrollment.getCoordinate() != null && enrollment.getCoordinate().isValid()) {
            programInstance.setLatitude(enrollment.getCoordinate().getLatitude());
            programInstance.setLongitude(enrollment.getCoordinate().getLongitude());
        } else {
            programInstance.setLatitude(null);
            programInstance.setLongitude(null);
        }
    }
    updateAttributeValues(enrollment, importOptions);
    updateDateFields(enrollment, programInstance);
    programInstance.setFollowup(enrollment.getFollowup());
    programInstanceService.updateProgramInstance(programInstance);
    manager.update(programInstance.getEntityInstance());
    saveTrackedEntityComment(programInstance, enrollment);
    importSummary.setReference(programInstance.getUid());
    importSummary.getImportCount().incrementImported();
    importOptions.setStrategy(ImportStrategy.CREATE_AND_UPDATE);
    importSummary.setEvents(handleEvents(enrollment, programInstance, importOptions));
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackedEntityInstance(org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramInstanceQueryParams(org.hisp.dhis.program.ProgramInstanceQueryParams) HashSet(java.util.HashSet) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 52 with ImportOptions

use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method validateAttributeType.

//--------------------------------------------------------------------------
// VALIDATION
//--------------------------------------------------------------------------
private List<ImportConflict> validateAttributeType(Attribute attribute, ImportOptions importOptions) {
    List<ImportConflict> importConflicts = Lists.newArrayList();
    if (attribute == null || attribute.getValue() == null) {
        return importConflicts;
    }
    TrackedEntityAttribute trackedEntityAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
    if (trackedEntityAttribute == null) {
        importConflicts.add(new ImportConflict("Attribute.attribute", "Does not point to a valid attribute."));
        return importConflicts;
    }
    String errorMessage = trackedEntityAttributeService.validateValueType(trackedEntityAttribute, attribute.getValue());
    if (errorMessage != null) {
        importConflicts.add(new ImportConflict("Attribute.value", errorMessage));
    }
    return importConflicts;
}
Also used : TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Example 53 with ImportOptions

use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.

the class AbstractTrackedEntityInstanceService method updateTrackedEntityInstances.

// -------------------------------------------------------------------------
// UPDATE
// -------------------------------------------------------------------------
@Override
public ImportSummaries updateTrackedEntityInstances(List<TrackedEntityInstance> trackedEntityInstances, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummaries importSummaries = new ImportSummaries();
    int counter = 0;
    for (TrackedEntityInstance trackedEntityInstance : trackedEntityInstances) {
        importSummaries.addImportSummary(updateTrackedEntityInstance(trackedEntityInstance, importOptions));
        if (counter % FLUSH_FREQUENCY == 0) {
            clearSession();
        }
        counter++;
    }
    return importSummaries;
}
Also used : ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 54 with ImportOptions

use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.

the class GmlImportServiceTest method setUpTest.

@Override
public void setUpTest() throws IOException {
    inputStream = new ClassPathResource("gml/testGmlPayload.gml").getInputStream();
    /**
         * Create orgunits present in testGmlPayload.gml and set ID properties.
         *      Name                    - FeatureType   - ID property
         *      Bo                      - Poly          - Name
         *      Bonthe                  - Multi         - Code
         *      Ole Johan Dahls Hus     - Point         - Uid
         *      Blindern                - Point (pos)   - Name
         *      Forskningsparken        - Poly (list)   - Name
         *
         * Note: some of these are included to cover different coordinate element schemes
         *       such as <posList>, <coordinates> and <pos>.
         */
    userService = _userService;
    boOrgUnit = createOrganisationUnit('A');
    boOrgUnit.setName("Bo");
    organisationUnitService.addOrganisationUnit(boOrgUnit);
    bontheOrgUnit = createOrganisationUnit('B');
    // Match on Code, therefore wrong name
    bontheOrgUnit.setName("AA Bonthe");
    bontheOrgUnit.setCode("CODE_BONTHE");
    organisationUnitService.addOrganisationUnit(bontheOrgUnit);
    ojdOrgUnit = createOrganisationUnit('C');
    ojdOrgUnit.setUid("ImspTQPwCqd");
    // Match on UID, therefore wrong name
    ojdOrgUnit.setName("AA Ole Johan Dahls Hus");
    organisationUnitService.addOrganisationUnit(ojdOrgUnit);
    bliOrgUnit = createOrganisationUnit('D');
    bliOrgUnit.setName("Blindern");
    organisationUnitService.addOrganisationUnit(bliOrgUnit);
    forskOrgUnit = createOrganisationUnit('E');
    forskOrgUnit.setName("Forskningsparken");
    organisationUnitService.addOrganisationUnit(forskOrgUnit);
    user = createAndInjectAdminUser();
    taskId = new TaskId(TaskCategory.METADATA_IMPORT, user);
    importOptions = new ImportOptions().setImportStrategy(ImportStrategy.UPDATE);
    importOptions.setDryRun(false);
    importOptions.setPreheatCache(true);
}
Also used : TaskId(org.hisp.dhis.scheduling.TaskId) ClassPathResource(org.springframework.core.io.ClassPathResource) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 55 with ImportOptions

use of org.hisp.dhis.dxf2.common.ImportOptions in project dhis2-core by dhis2.

the class ImportDataValueAction method execute.

// -------------------------------------------------------------------------
// Action implementation
// -------------------------------------------------------------------------
@Override
public String execute() throws Exception {
    strategy = strategy != null ? strategy : ImportStrategy.NEW_AND_UPDATES;
    TaskId taskId = new TaskId(TaskCategory.DATAVALUE_IMPORT, currentUserService.getCurrentUser());
    notifier.clear(taskId);
    InputStream in = new FileInputStream(upload);
    in = StreamUtils.wrapAndCheckCompressionFormat(in);
    ImportOptions options = new ImportOptions().setDryRun(dryRun).setStrategy(strategy).setPreheatCache(preheatCache).setSkipExistingCheck(skipExistingCheck).setIdScheme(StringUtils.trimToNull(idScheme)).setDataElementIdScheme(StringUtils.trimToNull(dataElementIdScheme)).setOrgUnitIdScheme(StringUtils.trimToNull(orgUnitIdScheme)).setFilename(uploadFileName);
    log.info(options);
    scheduler.executeTask(new ImportDataValueTask(dataValueSetService, adxDataService, sessionFactory, in, options, taskId, importFormat));
    return SUCCESS;
}
Also used : TaskId(org.hisp.dhis.scheduling.TaskId) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportDataValueTask(org.hisp.dhis.dxf2.datavalueset.tasks.ImportDataValueTask)

Aggregations

ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)51 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)39 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)22 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)20 InputStream (java.io.InputStream)19 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)19 ClassPathResource (org.springframework.core.io.ClassPathResource)17 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)16 Test (org.junit.Test)16 DhisSpringTest (org.hisp.dhis.DhisSpringTest)15 ArrayList (java.util.ArrayList)11 Date (java.util.Date)11 IOException (java.io.IOException)10 Event (org.hisp.dhis.dxf2.events.event.Event)10 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)10 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)9 TaskId (org.hisp.dhis.scheduling.TaskId)9 Program (org.hisp.dhis.program.Program)8 Lists (com.google.common.collect.Lists)7 HashSet (java.util.HashSet)7