Search in sources :

Example 1 with IdScheme

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

the class DefaultAdxDataService method saveDataValueSetInternal.

private ImportSummary saveDataValueSetInternal(InputStream in, ImportOptions importOptions, TaskId id) {
    notifier.clear(id).notify(id, "ADX parsing process started");
    ImportOptions adxImportOptions = ObjectUtils.firstNonNull(importOptions, ImportOptions.getDefaultImportOptions()).instance().setNotificationLevel(NotificationLevel.OFF);
    // Get import options
    IdScheme dataSetIdScheme = importOptions.getIdSchemes().getDataSetIdScheme();
    IdScheme dataElementIdScheme = importOptions.getIdSchemes().getDataElementIdScheme();
    // Create meta-data maps
    CachingMap<String, DataSet> dataSetMap = new CachingMap<>();
    CachingMap<String, DataElement> dataElementMap = new CachingMap<>();
    // Get meta-data maps
    IdentifiableObjectCallable<DataSet> dataSetCallable = new IdentifiableObjectCallable<>(identifiableObjectManager, DataSet.class, dataSetIdScheme, null);
    IdentifiableObjectCallable<DataElement> dataElementCallable = new IdentifiableObjectCallable<>(identifiableObjectManager, DataElement.class, dataElementIdScheme, null);
    // Heat cache
    if (importOptions.isPreheatCacheDefaultFalse()) {
        dataSetMap.load(identifiableObjectManager.getAll(DataSet.class), o -> o.getPropertyValue(dataSetIdScheme));
        dataElementMap.load(identifiableObjectManager.getAll(DataElement.class), o -> o.getPropertyValue(dataElementIdScheme));
    }
    XMLReader adxReader = XMLFactory.getXMLReader(in);
    ImportSummary importSummary;
    adxReader.moveToStartElement(AdxDataService.ROOT, AdxDataService.NAMESPACE);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    // Give the DXF import a different notification task ID so it doesn't conflict with notifications from this level.
    TaskId dxfTaskId = new TaskId(TaskCategory.DATAVALUE_IMPORT_INTERNAL, id.getUser());
    int groupCount = 0;
    try (PipedOutputStream pipeOut = new PipedOutputStream()) {
        Future<ImportSummary> futureImportSummary = executor.submit(new AdxPipedImporter(dataValueSetService, adxImportOptions, dxfTaskId, pipeOut, sessionFactory));
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter dxfWriter = factory.createXMLStreamWriter(pipeOut);
        List<ImportConflict> adxConflicts = new LinkedList<>();
        dxfWriter.writeStartDocument("1.0");
        dxfWriter.writeStartElement("dataValueSet");
        dxfWriter.writeDefaultNamespace("http://dhis2.org/schema/dxf/2.0");
        notifier.notify(id, "Starting to import ADX data groups.");
        while (adxReader.moveToStartElement(AdxDataService.GROUP, AdxDataService.NAMESPACE)) {
            notifier.update(id, "Importing ADX data group: " + groupCount);
            // note this returns conflicts which are detected at ADX level
            adxConflicts.addAll(parseAdxGroupToDxf(adxReader, dxfWriter, adxImportOptions, dataSetMap, dataSetCallable, dataElementMap, dataElementCallable));
            groupCount++;
        }
        // end dataValueSet
        dxfWriter.writeEndElement();
        dxfWriter.writeEndDocument();
        pipeOut.flush();
        importSummary = futureImportSummary.get(TOTAL_MINUTES_TO_WAIT, TimeUnit.MINUTES);
        importSummary.getConflicts().addAll(adxConflicts);
        importSummary.getImportCount().incrementIgnored(adxConflicts.size());
    } catch (AdxException ex) {
        importSummary = new ImportSummary();
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("Data set import failed within group number: " + groupCount);
        importSummary.getConflicts().add(ex.getImportConflict());
        notifier.update(id, NotificationLevel.ERROR, "ADX data import done", true);
        log.warn("Import failed: " + DebugUtils.getStackTrace(ex));
    } catch (IOException | XMLStreamException | InterruptedException | ExecutionException | TimeoutException ex) {
        importSummary = new ImportSummary();
        importSummary.setStatus(ImportStatus.ERROR);
        importSummary.setDescription("Data set import failed within group number: " + groupCount);
        notifier.update(id, NotificationLevel.ERROR, "ADX data import done", true);
        log.warn("Import failed: " + DebugUtils.getStackTrace(ex));
    }
    executor.shutdown();
    notifier.update(id, INFO, "ADX data import done", true).addTaskSummary(id, importSummary);
    ImportCount c = importSummary.getImportCount();
    log.info("ADX data import done, imported: " + c.getImported() + ", updated: " + c.getUpdated() + ", deleted: " + c.getDeleted() + ", ignored: " + c.getIgnored());
    return importSummary;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) TaskId(org.hisp.dhis.scheduling.TaskId) DataSet(org.hisp.dhis.dataset.DataSet) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) PipedOutputStream(java.io.PipedOutputStream) DataElement(org.hisp.dhis.dataelement.DataElement) CachingMap(org.hisp.dhis.commons.collection.CachingMap) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) ExecutionException(java.util.concurrent.ExecutionException) XMLReader(org.hisp.staxwax.reader.XMLReader) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) TimeoutException(java.util.concurrent.TimeoutException) ImportCount(org.hisp.dhis.dxf2.importsummary.ImportCount) IdScheme(org.hisp.dhis.common.IdScheme) IOException(java.io.IOException) IdentifiableObjectCallable(org.hisp.dhis.system.callable.IdentifiableObjectCallable) LinkedList(java.util.LinkedList) XMLStreamException(javax.xml.stream.XMLStreamException) ExecutorService(java.util.concurrent.ExecutorService) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions)

Example 2 with IdScheme

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

the class SpringDataValueSetStore method writeDataValueSet.

private void writeDataValueSet(String sql, DataExportParams params, Date completeDate, final DataValueSet dataValueSet) {
    if (params.isSingleDataValueSet()) {
        IdSchemes idScheme = params.getOutputIdSchemes() != null ? params.getOutputIdSchemes() : new IdSchemes();
        IdScheme ouScheme = idScheme.getOrgUnitIdScheme();
        IdScheme dataSetScheme = idScheme.getDataSetIdScheme();
        dataValueSet.setDataSet(params.getFirstDataSet().getPropertyValue(dataSetScheme));
        dataValueSet.setCompleteDate(getLongGmtDateString(completeDate));
        dataValueSet.setPeriod(params.getFirstPeriod().getIsoDate());
        dataValueSet.setOrgUnit(params.getFirstOrganisationUnit().getPropertyValue(ouScheme));
    }
    final Calendar calendar = PeriodType.getCalendar();
    jdbcTemplate.query(sql, new RowCallbackHandler() {

        @Override
        public void processRow(ResultSet rs) throws SQLException {
            DataValue dataValue = dataValueSet.getDataValueInstance();
            PeriodType pt = PeriodType.getPeriodTypeByName(rs.getString("ptname"));
            boolean deleted = rs.getBoolean("deleted");
            dataValue.setDataElement(rs.getString("deid"));
            dataValue.setPeriod(pt.createPeriod(rs.getDate("pestart"), calendar).getIsoDate());
            dataValue.setOrgUnit(rs.getString("ouid"));
            dataValue.setCategoryOptionCombo(rs.getString("cocid"));
            dataValue.setAttributeOptionCombo(rs.getString("aocid"));
            dataValue.setValue(rs.getString("value"));
            dataValue.setStoredBy(rs.getString("storedby"));
            dataValue.setCreated(getLongGmtDateString(rs.getTimestamp("created")));
            dataValue.setLastUpdated(getLongGmtDateString(rs.getTimestamp("lastupdated")));
            dataValue.setComment(rs.getString("comment"));
            dataValue.setFollowup(rs.getBoolean("followup"));
            if (deleted) {
                dataValue.setDeleted(deleted);
            }
            dataValue.close();
        }
    });
    dataValueSet.close();
}
Also used : PeriodType(org.hisp.dhis.period.PeriodType) IdSchemes(org.hisp.dhis.common.IdSchemes) SQLException(java.sql.SQLException) DataValue(org.hisp.dhis.dxf2.datavalue.DataValue) Calendar(org.hisp.dhis.calendar.Calendar) ResultSet(java.sql.ResultSet) IdScheme(org.hisp.dhis.common.IdScheme) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Example 3 with IdScheme

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

the class AbstractEventService method updateEvent.

private ImportSummary updateEvent(Event event, User user, boolean singleValue, ImportOptions importOptions) {
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    ImportSummary importSummary = new ImportSummary(event.getEvent());
    ProgramStageInstance programStageInstance = programStageInstanceService.getProgramStageInstance(event.getEvent());
    if (programStageInstance == null) {
        importSummary.getConflicts().add(new ImportConflict("Invalid Event ID.", event.getEvent()));
        return importSummary.incrementIgnored();
    }
    OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), event.getOrgUnit());
    if (organisationUnit == null) {
        organisationUnit = programStageInstance.getOrganisationUnit();
    }
    Date executionDate = new Date();
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
        programStageInstance.setExecutionDate(executionDate);
    }
    Date dueDate = new Date();
    if (event.getDueDate() != null) {
        dueDate = DateUtils.parseDate(event.getDueDate());
    }
    String storedBy = getStoredBy(event, null, user);
    programStageInstance.setStoredBy(storedBy);
    String completedBy = getCompletedBy(event, null, user);
    if (event.getStatus() == EventStatus.ACTIVE) {
        programStageInstance.setStatus(EventStatus.ACTIVE);
        programStageInstance.setCompletedBy(null);
        programStageInstance.setCompletedDate(null);
    } else if (programStageInstance.getStatus() != event.getStatus() && event.getStatus() == EventStatus.COMPLETED) {
        programStageInstance.setStatus(EventStatus.COMPLETED);
        programStageInstance.setCompletedBy(completedBy);
        programStageInstance.setCompletedDate(executionDate);
        if (programStageInstance.isCompleted()) {
            programStageInstanceService.completeProgramStageInstance(programStageInstance, importOptions.isSkipNotifications(), i18nManager.getI18nFormat());
        }
    } else if (event.getStatus() == EventStatus.SKIPPED) {
        programStageInstance.setStatus(EventStatus.SKIPPED);
    } else if (event.getStatus() == EventStatus.SCHEDULE) {
        programStageInstance.setStatus(EventStatus.SCHEDULE);
    }
    programStageInstance.setDueDate(dueDate);
    programStageInstance.setOrganisationUnit(organisationUnit);
    if (!singleValue) {
        if (programStageInstance.getProgramStage().getCaptureCoordinates()) {
            if (event.getCoordinate() != null && event.getCoordinate().isValid()) {
                programStageInstance.setLatitude(event.getCoordinate().getLatitude());
                programStageInstance.setLongitude(event.getCoordinate().getLongitude());
            } else {
                programStageInstance.setLatitude(null);
                programStageInstance.setLongitude(null);
            }
        }
    }
    Program program = getProgram(importOptions.getIdSchemes().getProgramIdScheme(), event.getProgram());
    validateExpiryDays(event, program, programStageInstance);
    if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
        IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
        DataElementCategoryOptionCombo attributeOptionCombo = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
        if (attributeOptionCombo == null) {
            importSummary.getConflicts().add(new ImportConflict("Invalid attribute option combo identifier:", event.getAttributeCategoryOptions()));
            return importSummary.incrementIgnored();
        }
        programStageInstance.setAttributeOptionCombo(attributeOptionCombo);
    }
    programStageInstance.setDeleted(event.isDeleted());
    programStageInstanceService.updateProgramStageInstance(programStageInstance);
    updateTrackedEntityInstance(programStageInstance);
    saveTrackedEntityComment(programStageInstance, event, storedBy);
    Set<TrackedEntityDataValue> dataValues = new HashSet<>(dataValueService.getTrackedEntityDataValues(programStageInstance));
    Map<String, TrackedEntityDataValue> existingDataValues = getDataElementDataValueMap(dataValues);
    for (DataValue value : event.getDataValues()) {
        DataElement dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), value.getDataElement());
        TrackedEntityDataValue dataValue = dataValueService.getTrackedEntityDataValue(programStageInstance, dataElement);
        if (!validateDataValue(dataElement, value.getValue(), importSummary)) {
            continue;
        }
        if (dataValue != null) {
            if (StringUtils.isEmpty(value.getValue()) && dataElement.isFileType() && !StringUtils.isEmpty(dataValue.getValue())) {
                fileResourceService.deleteFileResource(dataValue.getValue());
            }
            dataValue.setValue(value.getValue());
            dataValue.setProvidedElsewhere(value.getProvidedElsewhere());
            dataValueService.updateTrackedEntityDataValue(dataValue);
            dataValues.remove(dataValue);
        } else {
            TrackedEntityDataValue existingDataValue = existingDataValues.get(value.getDataElement());
            saveDataValue(programStageInstance, event.getStoredBy(), dataElement, value.getValue(), value.getProvidedElsewhere(), existingDataValue, null);
        }
    }
    if (!singleValue) {
        dataValues.forEach(dataValueService::deleteTrackedEntityDataValue);
    }
    importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
    return importSummary;
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) Program(org.hisp.dhis.program.Program) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IdScheme(org.hisp.dhis.common.IdScheme) Date(java.util.Date) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict) HashSet(java.util.HashSet)

Example 4 with IdScheme

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

the class AbstractEventService method saveEvent.

private ImportSummary saveEvent(Program program, ProgramInstance programInstance, ProgramStage programStage, ProgramStageInstance programStageInstance, OrganisationUnit organisationUnit, Event event, User user, ImportOptions importOptions) {
    Assert.notNull(program, "Program cannot be null");
    Assert.notNull(programInstance, "Program instance cannot be null");
    Assert.notNull(programStage, "Program stage cannot be null");
    ImportSummary importSummary = new ImportSummary(event.getEvent());
    if (importOptions == null) {
        importOptions = new ImportOptions();
    }
    boolean existingEvent = programStageInstance != null;
    boolean dryRun = importOptions.isDryRun();
    // = new Date();
    Date executionDate = null;
    if (event.getEventDate() != null) {
        executionDate = DateUtils.parseDate(event.getEventDate());
    }
    Date dueDate = new Date();
    if (event.getDueDate() != null) {
        dueDate = DateUtils.parseDate(event.getDueDate());
    }
    String storedBy = getStoredBy(event, importSummary, user);
    String completedBy = getCompletedBy(event, importSummary, user);
    DataElementCategoryOptionCombo aoc = null;
    if ((event.getAttributeCategoryOptions() != null && program.getCategoryCombo() != null) || event.getAttributeOptionCombo() != null) {
        IdScheme idScheme = importOptions.getIdSchemes().getCategoryOptionIdScheme();
        try {
            aoc = inputUtils.getAttributeOptionCombo(program.getCategoryCombo(), event.getAttributeCategoryOptions(), event.getAttributeOptionCombo(), idScheme);
        } catch (IllegalQueryException ex) {
            importSummary.getConflicts().add(new ImportConflict(ex.getMessage(), event.getAttributeCategoryOptions()));
        }
    } else {
        aoc = categoryService.getDefaultDataElementCategoryOptionCombo();
    }
    if (!dryRun) {
        if (programStageInstance == null) {
            programStageInstance = createProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, event.getEvent(), aoc, importOptions);
        } else {
            updateProgramStageInstance(event, programStage, programInstance, organisationUnit, dueDate, executionDate, event.getStatus().getValue(), event.getCoordinate(), completedBy, programStageInstance, aoc, importOptions);
        }
        updateTrackedEntityInstance(programStageInstance);
        saveTrackedEntityComment(programStageInstance, event, storedBy);
        importSummary.setReference(programStageInstance.getUid());
    }
    Map<String, TrackedEntityDataValue> dataElementValueMap = Maps.newHashMap();
    if (existingEvent) {
        dataElementValueMap = getDataElementDataValueMap(dataValueService.getTrackedEntityDataValues(programStageInstance));
    }
    for (DataValue dataValue : event.getDataValues()) {
        DataElement dataElement;
        if (dataElementValueMap.containsKey(dataValue.getDataElement())) {
            dataElement = dataElementValueMap.get(dataValue.getDataElement()).getDataElement();
        } else {
            dataElement = getDataElement(importOptions.getIdSchemes().getDataElementIdScheme(), dataValue.getDataElement());
        }
        if (dataElement != null) {
            if (validateDataValue(dataElement, dataValue.getValue(), importSummary)) {
                String dataValueStoredBy = dataValue.getStoredBy() != null ? dataValue.getStoredBy() : storedBy;
                if (!dryRun) {
                    TrackedEntityDataValue existingDataValue = dataElementValueMap.get(dataValue.getDataElement());
                    saveDataValue(programStageInstance, dataValueStoredBy, dataElement, dataValue.getValue(), dataValue.getProvidedElsewhere(), existingDataValue, importSummary);
                }
            }
        } else {
            importSummary.getConflicts().add(new ImportConflict("dataElement", dataValue.getDataElement() + " is not a valid data element"));
            importSummary.getImportCount().incrementIgnored();
        }
    }
    importSummary.setStatus(importSummary.getConflicts().isEmpty() ? ImportStatus.SUCCESS : ImportStatus.WARNING);
    return importSummary;
}
Also used : TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) TrackedEntityDataValue(org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) IdScheme(org.hisp.dhis.common.IdScheme) Date(java.util.Date) ProgramStageDataElement(org.hisp.dhis.program.ProgramStageDataElement) DataElement(org.hisp.dhis.dataelement.DataElement) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo) ImportConflict(org.hisp.dhis.dxf2.importsummary.ImportConflict)

Aggregations

IdScheme (org.hisp.dhis.common.IdScheme)4 DataElement (org.hisp.dhis.dataelement.DataElement)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 ImportConflict (org.hisp.dhis.dxf2.importsummary.ImportConflict)3 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)3 Date (java.util.Date)2 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)2 ProgramStageDataElement (org.hisp.dhis.program.ProgramStageDataElement)2 TrackedEntityDataValue (org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValue)2 IOException (java.io.IOException)1 PipedOutputStream (java.io.PipedOutputStream)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 TimeoutException (java.util.concurrent.TimeoutException)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1