Search in sources :

Example 6 with UPDATED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED 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 7 with UPDATED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.

the class EventController method putJsonEventForEventDate.

@RequestMapping(value = "/{uid}/eventDate", method = RequestMethod.PUT, consumes = "application/json")
@PreAuthorize("hasRole('ALL') or hasRole('F_TRACKED_ENTITY_DATAVALUE_ADD')")
public void putJsonEventForEventDate(HttpServletResponse response, HttpServletRequest request, @PathVariable("uid") String uid, ImportOptions importOptions) throws IOException, WebMessageException {
    if (!programStageInstanceService.programStageInstanceExists(uid)) {
        throw new WebMessageException(WebMessageUtils.notFound("Event not found for ID " + uid));
    }
    InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream());
    Event updatedEvent = renderService.fromJson(inputStream, Event.class);
    updatedEvent.setEvent(uid);
    eventService.updateEventForEventDate(updatedEvent);
    webMessageService.send(WebMessageUtils.ok("Event updated " + uid), response, request);
}
Also used : WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) InputStream(java.io.InputStream) Event(org.hisp.dhis.dxf2.events.event.Event) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with UPDATED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.

the class UserKeyJsonValueController method updateUserKeyJsonValue.

/**
     * Update a key.
     */
@RequestMapping(value = "/{namespace}/{key}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
public void updateUserKeyJsonValue(@PathVariable String namespace, @PathVariable String key, @RequestBody String body, HttpServletResponse response) throws WebMessageException, IOException {
    UserKeyJsonValue userKeyJsonValue = userKeyJsonValueService.getUserKeyJsonValue(currentUserService.getCurrentUser(), namespace, key);
    if (userKeyJsonValue == null) {
        throw new WebMessageException(WebMessageUtils.notFound("The key '" + key + "' was not found in the namespace '" + namespace + "'."));
    }
    if (!renderService.isValidJson(body)) {
        throw new WebMessageException(WebMessageUtils.badRequest("The data is not valid JSON."));
    }
    userKeyJsonValue.setValue(body);
    userKeyJsonValueService.updateUserKeyJsonValue(userKeyJsonValue);
    response.setStatus(HttpServletResponse.SC_OK);
    messageService.sendJson(WebMessageUtils.created("Key '" + key + "' in namespace '" + namespace + "' updated."), response);
}
Also used : UserKeyJsonValue(org.hisp.dhis.userkeyjsonvalue.UserKeyJsonValue) WebMessageException(org.hisp.dhis.dxf2.webmessage.WebMessageException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with UPDATED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.

the class AnalyticsUtils method getDataValueSetFromGrid.

/**
 * Generates a data value set based on the given grid with aggregated data.
 * Sets the created and last updated fields to the current date.
 *
 * @param params the data query parameters.
 * @param grid the grid.
 * @return a data value set.
 */
public static DataValueSet getDataValueSetFromGrid(DataQueryParams params, Grid grid) {
    int dxInx = grid.getIndexOfHeader(DATA_X_DIM_ID);
    int peInx = grid.getIndexOfHeader(PERIOD_DIM_ID);
    int ouInx = grid.getIndexOfHeader(ORGUNIT_DIM_ID);
    int coInx = grid.getIndexOfHeader(CATEGORYOPTIONCOMBO_DIM_ID);
    int aoInx = grid.getIndexOfHeader(ATTRIBUTEOPTIONCOMBO_DIM_ID);
    int vlInx = grid.getHeaderWidth() - 1;
    Assert.isTrue(dxInx >= 0, "Data dimension index must be greater than or equal to zero");
    Assert.isTrue(peInx >= 0, "Period dimension index must be greater than or equal to zero");
    Assert.isTrue(ouInx >= 0, "Org unit dimension index must be greater than or equal to zero");
    Assert.isTrue(coInx >= 0, "Category option combo dimension index must be greater than or equal to zero");
    Assert.isTrue(aoInx >= 0, "Attribute option combo dimension index must be greater than or equal to zero");
    Assert.isTrue(vlInx >= 0, "Value index must be greater than or equal to zero");
    String created = DateUtils.getMediumDateString();
    DataValueSet dvs = new DataValueSet();
    Set<String> primaryKeys = Sets.newHashSet();
    for (List<Object> row : grid.getRows()) {
        DataValue dv = new DataValue();
        Object coc = row.get(coInx);
        Object aoc = row.get(aoInx);
        dv.setDataElement(String.valueOf(row.get(dxInx)));
        dv.setPeriod(String.valueOf(row.get(peInx)));
        dv.setOrgUnit(String.valueOf(row.get(ouInx)));
        dv.setCategoryOptionCombo(coc != null ? String.valueOf(coc) : null);
        dv.setAttributeOptionCombo(aoc != null ? String.valueOf(aoc) : null);
        dv.setValue(String.valueOf(row.get(vlInx)));
        dv.setComment(KEY_AGG_VALUE);
        dv.setStoredBy(KEY_AGG_VALUE);
        dv.setCreated(created);
        dv.setLastUpdated(created);
        if (!params.isDuplicatesOnly() || !primaryKeys.add(dv.getPrimaryKey())) {
            dvs.getDataValues().add(dv);
        }
    }
    return dvs;
}
Also used : DataValueSet(org.hisp.dhis.dxf2.datavalueset.DataValueSet) DataValue(org.hisp.dhis.dxf2.datavalue.DataValue) DimensionalObject(org.hisp.dhis.common.DimensionalObject) DataDimensionalItemObject(org.hisp.dhis.common.DataDimensionalItemObject) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString)

Example 10 with UPDATED

use of org.hisp.dhis.dxf2.events.trackedentity.store.query.EventQuery.COLUMNS.UPDATED in project dhis2-core by dhis2.

the class MetadataSyncPostProcessor method sendSuccessMailToAdmin.

public void sendSuccessMailToAdmin(MetadataSyncSummary metadataSyncSummary) {
    ImportReport importReport = metadataSyncSummary.getImportReport();
    StringBuilder text = new StringBuilder("Successful Import Report for the scheduler run for Metadata synchronization \n\n").append("Imported Version Details \n ").append("Version Name: " + metadataSyncSummary.getMetadataVersion().getName() + "\n").append("Version Type: " + metadataSyncSummary.getMetadataVersion().getType() + "\n");
    if (importReport.getTypeReportCount() == 0) {
        text.append("New Version created. It does not have any metadata changes. \n");
    } else {
        text.append("Imported Object Details: \n");
        importReport.forEachTypeReport(typeReport -> {
            Stats stats = typeReport.getStats();
            text.append("Metadata Object Type: ").append(typeReport.getKlass()).append("\n").append("Stats: \n").append("total: " + stats.getTotal() + "\n");
            if (stats.getCreated() > 0) {
                text.append(" created: " + stats.getCreated() + "\n");
            }
            if (stats.getUpdated() > 0) {
                text.append(" updated: " + stats.getUpdated() + "\n");
            }
            if (stats.getIgnored() > 0) {
                text.append(" ignored: " + stats.getIgnored() + "\n");
            }
        });
        text.append("\n\n");
    }
    if (text.length() > 0) {
        log.info("Success mail will be sent with the following message: " + text);
        emailService.sendSystemEmail(new Email("Success Notification: Metadata Synchronization", text.toString()));
    }
}
Also used : Email(org.hisp.dhis.email.Email) ImportReport(org.hisp.dhis.dxf2.metadata.feedback.ImportReport) Stats(org.hisp.dhis.feedback.Stats)

Aggregations

ImportReport (org.hisp.dhis.dxf2.metadata.feedback.ImportReport)9 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)8 User (org.hisp.dhis.user.User)8 Test (org.junit.jupiter.api.Test)8 Date (java.util.Date)7 List (java.util.List)7 TransactionalIntegrationTest (org.hisp.dhis.TransactionalIntegrationTest)6 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)6 DataSet (org.hisp.dhis.dataset.DataSet)6 ClassPathResource (org.springframework.core.io.ClassPathResource)6 Event (org.hisp.dhis.dxf2.events.event.Event)5 ImportCount (org.hisp.dhis.dxf2.importsummary.ImportCount)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)4 DataElement (org.hisp.dhis.dataelement.DataElement)3 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)3 Events (org.hisp.dhis.dxf2.events.event.Events)3 TrackedEntityInstance (org.hisp.dhis.dxf2.events.trackedentity.TrackedEntityInstance)3 ImportSummaries (org.hisp.dhis.dxf2.importsummary.ImportSummaries)3