Search in sources :

Example 1 with Note

use of org.hisp.dhis.dxf2.events.event.Note in project dhis2-core by dhis2.

the class AbstractEnrollmentService method getEnrollment.

@Override
public Enrollment getEnrollment(ProgramInstance programInstance, TrackedEntityInstanceParams params) {
    Enrollment enrollment = new Enrollment();
    enrollment.setEnrollment(programInstance.getUid());
    if (programInstance.getEntityInstance() != null) {
        enrollment.setTrackedEntity(programInstance.getEntityInstance().getTrackedEntity().getUid());
        enrollment.setTrackedEntityInstance(programInstance.getEntityInstance().getUid());
    }
    if (programInstance.getOrganisationUnit() != null) {
        enrollment.setOrgUnit(programInstance.getOrganisationUnit().getUid());
        enrollment.setOrgUnitName(programInstance.getOrganisationUnit().getName());
    }
    if (programInstance.getProgram().getCaptureCoordinates()) {
        Coordinate coordinate = null;
        if (programInstance.getLongitude() != null && programInstance.getLatitude() != null) {
            coordinate = new Coordinate(programInstance.getLongitude(), programInstance.getLatitude());
            try {
                List<Double> list = OBJECT_MAPPER.readValue(coordinate.getCoordinateString(), new TypeReference<List<Double>>() {
                });
                coordinate.setLongitude(list.get(0));
                coordinate.setLatitude(list.get(1));
            } catch (IOException ignored) {
            }
        }
        if (coordinate != null && coordinate.isValid()) {
            enrollment.setCoordinate(coordinate);
        }
    }
    enrollment.setCreated(DateUtils.getIso8601NoTz(programInstance.getCreated()));
    enrollment.setCreatedAtClient(DateUtils.getIso8601NoTz(programInstance.getCreatedAtClient()));
    enrollment.setLastUpdated(DateUtils.getIso8601NoTz(programInstance.getLastUpdated()));
    enrollment.setLastUpdatedAtClient(DateUtils.getIso8601NoTz(programInstance.getLastUpdatedAtClient()));
    enrollment.setProgram(programInstance.getProgram().getUid());
    enrollment.setStatus(EnrollmentStatus.fromProgramStatus(programInstance.getStatus()));
    enrollment.setEnrollmentDate(programInstance.getEnrollmentDate());
    enrollment.setIncidentDate(programInstance.getIncidentDate());
    enrollment.setFollowup(programInstance.getFollowup());
    enrollment.setCompletedDate(programInstance.getEndDate());
    enrollment.setCompletedBy(programInstance.getCompletedBy());
    List<TrackedEntityComment> comments = programInstance.getComments();
    for (TrackedEntityComment comment : comments) {
        Note note = new Note();
        note.setValue(comment.getCommentText());
        note.setStoredBy(comment.getCreator());
        if (comment.getCreatedDate() != null) {
            note.setStoredDate(comment.getCreatedDate().toString());
        }
        enrollment.getNotes().add(note);
    }
    if (params.isIncludeEvents()) {
        for (ProgramStageInstance programStageInstance : programInstance.getProgramStageInstances()) {
            enrollment.getEvents().add(eventService.getEvent(programStageInstance));
        }
    }
    return enrollment;
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Coordinate(org.hisp.dhis.dxf2.events.event.Coordinate) Note(org.hisp.dhis.dxf2.events.event.Note) List(java.util.List) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance)

Example 2 with Note

use of org.hisp.dhis.dxf2.events.event.Note in project dhis2-core by dhis2.

the class AbstractEnrollmentService method saveTrackedEntityComment.

private void saveTrackedEntityComment(ProgramInstance programInstance, Enrollment enrollment) {
    String storedBy = currentUserService.getCurrentUsername();
    for (Note note : enrollment.getNotes()) {
        TrackedEntityComment comment = new TrackedEntityComment();
        comment.setCreator(storedBy);
        comment.setCreatedDate(new Date());
        comment.setCommentText(note.getValue());
        commentService.addTrackedEntityComment(comment);
        programInstance.getComments().add(comment);
        programInstanceService.updateProgramInstance(programInstance);
    }
}
Also used : TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) Note(org.hisp.dhis.dxf2.events.event.Note) Date(java.util.Date)

Example 3 with Note

use of org.hisp.dhis.dxf2.events.event.Note 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 4 with Note

use of org.hisp.dhis.dxf2.events.event.Note in project dhis2-core by dhis2.

the class JdbcEventStore method getEventRows.

@Override
public List<EventRow> getEventRows(EventSearchParams params, List<OrganisationUnit> organisationUnits) {
    User user = currentUserService.getCurrentUser();
    setAccessiblePrograms(user, params);
    List<EventRow> eventRows = new ArrayList<>();
    String sql = buildSql(params, organisationUnits, user);
    SqlRowSet rowSet = jdbcTemplate.queryForRowSet(sql);
    log.debug("Event query SQL: " + sql);
    EventRow eventRow = new EventRow();
    eventRow.setEvent("not_valid");
    Set<String> notes = new HashSet<>();
    Map<String, List<DataValue>> processedDataValues = new HashMap<>();
    while (rowSet.next()) {
        if (rowSet.getString("psi_uid") == null || (params.getCategoryOptionCombo() == null && !isSuper(user) && !userHasAccess(rowSet))) {
            continue;
        }
        if (eventRow.getUid() == null || !eventRow.getUid().equals(rowSet.getString("psi_uid"))) {
            validateIdentifiersPresence(rowSet, params.getIdSchemes(), false);
            eventRow = new EventRow();
            eventRow.setUid(rowSet.getString("psi_uid"));
            eventRow.setEvent(rowSet.getString("psi_uid"));
            eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
            eventRow.setTrackedEntityInstanceOrgUnit(rowSet.getString("tei_ou"));
            eventRow.setTrackedEntityInstanceOrgUnitName(rowSet.getString("tei_ou_name"));
            eventRow.setTrackedEntityInstanceCreated(rowSet.getString("tei_created"));
            eventRow.setTrackedEntityInstanceInactive(rowSet.getBoolean("tei_inactive"));
            eventRow.setDeleted(rowSet.getBoolean("psi_deleted"));
            eventRow.setProgram(rowSet.getString("p_identifier"));
            eventRow.setProgramStage(rowSet.getString("ps_identifier"));
            eventRow.setOrgUnit(rowSet.getString("ou_identifier"));
            ProgramType programType = ProgramType.fromValue(rowSet.getString("p_type"));
            if (programType == ProgramType.WITHOUT_REGISTRATION) {
                eventRow.setEnrollment(rowSet.getString("pi_uid"));
                eventRow.setFollowup(rowSet.getBoolean("pi_followup"));
            }
            eventRow.setTrackedEntityInstance(rowSet.getString("tei_uid"));
            eventRow.setOrgUnitName(rowSet.getString("ou_name"));
            eventRow.setDueDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_duedate")));
            eventRow.setEventDate(DateUtils.getIso8601NoTz(rowSet.getDate("psi_executiondate")));
            eventRows.add(eventRow);
        }
        if (rowSet.getString("pav_value") != null && rowSet.getString("ta_uid") != null) {
            String valueType = rowSet.getString("ta_valuetype");
            Attribute attribute = new Attribute();
            attribute.setCreated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_created")));
            attribute.setLastUpdated(DateUtils.getIso8601NoTz(rowSet.getDate("pav_lastupdated")));
            attribute.setValue(rowSet.getString("pav_value"));
            attribute.setDisplayName(rowSet.getString("ta_name"));
            attribute.setValueType(valueType != null ? ValueType.valueOf(valueType.toUpperCase()) : null);
            attribute.setAttribute(rowSet.getString("ta_uid"));
            eventRow.getAttributes().add(attribute);
        }
        if (!StringUtils.isEmpty(rowSet.getString("psi_eventdatavalues")) && !processedDataValues.containsKey(rowSet.getString("psi_uid"))) {
            List<DataValue> dataValues = new ArrayList<>();
            Set<EventDataValue> eventDataValues = convertEventDataValueJsonIntoSet(rowSet.getString("psi_eventdatavalues"));
            for (EventDataValue dv : eventDataValues) {
                dataValues.add(convertEventDataValueIntoDtoDataValue(dv));
            }
            processedDataValues.put(rowSet.getString("psi_uid"), dataValues);
        }
        if (rowSet.getString("psinote_value") != null && !notes.contains(rowSet.getString("psinote_id"))) {
            Note note = new Note();
            note.setNote(rowSet.getString("psinote_uid"));
            note.setValue(rowSet.getString("psinote_value"));
            note.setStoredDate(DateUtils.getIso8601NoTz(rowSet.getDate("psinote_storeddate")));
            note.setStoredBy(rowSet.getString("psinote_storedby"));
            eventRow.getNotes().add(note);
            notes.add(rowSet.getString("psinote_id"));
        }
    }
    eventRows.forEach(e -> e.setDataValues(processedDataValues.get(e.getUid())));
    IdSchemes idSchemes = ObjectUtils.firstNonNull(params.getIdSchemes(), new IdSchemes());
    IdScheme dataElementIdScheme = idSchemes.getDataElementIdScheme();
    if (dataElementIdScheme != IdScheme.ID && dataElementIdScheme != IdScheme.UID) {
        CachingMap<String, String> dataElementUidToIdentifierCache = new CachingMap<>();
        List<Collection<DataValue>> dataValuesList = eventRows.stream().map(EventRow::getDataValues).collect(Collectors.toList());
        populateCache(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
        convertDataValuesIdentifiers(dataElementIdScheme, dataValuesList, dataElementUidToIdentifierCache);
    }
    return eventRows;
}
Also used : SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) User(org.hisp.dhis.user.User) IdSchemes(org.hisp.dhis.common.IdSchemes) HashMap(java.util.HashMap) Attribute(org.hisp.dhis.dxf2.events.trackedentity.Attribute) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) ArrayList(java.util.ArrayList) DateUtils.getMediumDateString(org.hisp.dhis.util.DateUtils.getMediumDateString) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DateUtils.getLongGmtDateString(org.hisp.dhis.util.DateUtils.getLongGmtDateString) IdScheme(org.hisp.dhis.common.IdScheme) EventRow(org.hisp.dhis.dxf2.events.report.EventRow) CachingMap(org.hisp.dhis.commons.collection.CachingMap) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) ProgramType(org.hisp.dhis.program.ProgramType) EventDataValue(org.hisp.dhis.eventdatavalue.EventDataValue) HashSet(java.util.HashSet)

Example 5 with Note

use of org.hisp.dhis.dxf2.events.event.Note in project dhis2-core by dhis2.

the class NoteHelper method toNote.

private Note toNote(TrackedEntityComment trackedEntityComment) {
    Note note = new Note();
    note.setNote(trackedEntityComment.getUid());
    note.setValue(trackedEntityComment.getCommentText());
    note.setStoredBy(trackedEntityComment.getCreator());
    note.setStoredDate(DateUtils.getIso8601NoTz(trackedEntityComment.getCreated()));
    note.setLastUpdatedBy(UserInfoSnapshot.from(trackedEntityComment.getLastUpdatedBy()));
    note.setLastUpdated(trackedEntityComment.getLastUpdated());
    return note;
}
Also used : Note(org.hisp.dhis.dxf2.events.event.Note)

Aggregations

Note (org.hisp.dhis.dxf2.events.event.Note)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 DataElement (org.hisp.dhis.dataelement.DataElement)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 Collection (java.util.Collection)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)4 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)4 User (org.hisp.dhis.user.User)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 IdScheme (org.hisp.dhis.common.IdScheme)3 CachingMap (org.hisp.dhis.commons.collection.CachingMap)3 ProgramStageInstance (org.hisp.dhis.program.ProgramStageInstance)3 ImmutableList (com.google.common.collect.ImmutableList)2 PipedOutputStream (java.io.PipedOutputStream)2