Search in sources :

Example 26 with UID

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

the class ProgramInstanceSupplier method get.

public Map<String, ProgramInstance> get(ImportOptions importOptions, Map<String, Pair<TrackedEntityInstance, Boolean>> teiMap, List<Event> events) {
    if (events == null) {
        return new HashMap<>();
    }
    // Collect all the program instance UIDs to pass as SQL query argument
    Set<String> programInstanceUids = events.stream().filter(e -> StringUtils.isNotEmpty(e.getEnrollment())).map(Event::getEnrollment).collect(Collectors.toSet());
    Map<String, ProgramInstance> programInstances = new HashMap<>();
    if (!programInstanceUids.isEmpty()) {
        // Create a bi-directional map enrollment uid -> event uid
        Multimap<String, String> programInstanceToEvent = HashMultimap.create();
        for (Event event : events) {
            programInstanceToEvent.put(event.getEnrollment(), event.getUid());
        }
        // Collect all the Program Stage Instances specified in the Events
        // (enrollment
        // property)
        programInstances = getProgramInstancesByUid(importOptions, events, programInstanceToEvent, programInstanceUids);
    }
    mapExistingEventsToProgramInstances(importOptions, events, programInstances);
    mapEventsToProgramInstanceByTei(importOptions, events, programInstances, teiMap);
    return programInstances;
}
Also used : HashMap(java.util.HashMap) ProgramInstance(org.hisp.dhis.program.ProgramInstance) Event(org.hisp.dhis.dxf2.events.event.Event)

Example 27 with UID

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

the class TrackedEntityInstanceSupplier method get.

@Override
public Map<String, Pair<TrackedEntityInstance, Boolean>> get(ImportOptions importOptions, List<Event> events) {
    if (events == null) {
        return new HashMap<>();
    }
    // @formatter:off
    // Collect all the org unit uids to pass as SQL query argument
    Set<String> teiUids = events.stream().filter(e -> e.getTrackedEntityInstance() != null).map(Event::getTrackedEntityInstance).collect(Collectors.toSet());
    if (isEmpty(teiUids)) {
        return new HashMap<>();
    }
    // Create a map: tei uid -> List [event uid]
    Multimap<String, String> teiToEvent = HashMultimap.create();
    for (Event event : events) {
        teiToEvent.put(event.getTrackedEntityInstance(), event.getUid());
    }
    // 
    // Get all TEI associated to the events
    // 
    Map<String, TrackedEntityInstance> teiMap = getTrackedEntityInstances(teiUids, teiToEvent);
    Map<String, Pair<TrackedEntityInstance, Boolean>> result = new HashMap<>();
    // 
    for (String event : teiMap.keySet()) {
        TrackedEntityInstance tei = teiMap.get(event);
        result.put(event, Pair.of(tei, !importOptions.isSkipLastUpdated() ? aclService.canUpdate(importOptions.getUser(), tei) : null));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) Event(org.hisp.dhis.dxf2.events.event.Event) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) Pair(org.apache.commons.lang3.tuple.Pair)

Example 28 with UID

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

the class AssignedUserSupplier method get.

@Override
public Map<String, User> get(ImportOptions importOptions, List<Event> events) {
    // @formatter:off
    // Collect all the "assigned user" uids to pass as SQL query argument
    Set<String> userUids = events.stream().filter(e -> StringUtils.isNotEmpty(e.getAssignedUser())).map(Event::getAssignedUser).collect(Collectors.toSet());
    // Create a map user -> event
    Multimap<String, String> userToEvent = HashMultimap.create();
    for (Event event : events) {
        userToEvent.put(event.getAssignedUser(), event.getUid());
    }
    if (!userUids.isEmpty()) {
        final String sql = "select userinfoid, uid, code from userinfo " + "where uid in (:ids)";
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("ids", userUids);
        return jdbcTemplate.query(sql, parameters, (ResultSet rs) -> {
            Map<String, User> results = new HashMap<>();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getLong("userinfoid"));
                user.setUid(rs.getString("uid"));
                user.setCode(rs.getString("code"));
                for (String event : userToEvent.get(user.getUid())) {
                    results.put(event, user);
                }
            }
            return results;
        });
    }
    return new HashMap<>();
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) User(org.hisp.dhis.user.User) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) Event(org.hisp.dhis.dxf2.events.event.Event)

Example 29 with UID

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

the class NoteRowCallbackHandler method getNote.

private Note getNote(ResultSet rs) throws SQLException {
    Note note = new Note();
    note.setNote(rs.getString("uid"));
    note.setValue(rs.getString("commenttext"));
    note.setStoredBy(rs.getString("creator"));
    note.setStoredDate(DateUtils.getIso8601NoTz(rs.getDate("created")));
    return note;
}
Also used : Note(org.hisp.dhis.dxf2.events.event.Note)

Example 30 with UID

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

the class AbstractTrackedEntityInstanceService method checkAttributes.

private void checkAttributes(TrackedEntityInstance dtoEntityInstance, ImportOptions importOptions, ImportConflicts importConflicts, boolean teiExistsInDatabase) {
    if (dtoEntityInstance.getAttributes().isEmpty()) {
        return;
    }
    List<String> fileValues = new ArrayList<>();
    org.hisp.dhis.trackedentity.TrackedEntityInstance daoEntityInstance = null;
    if (teiExistsInDatabase) {
        daoEntityInstance = teiService.getTrackedEntityInstance(dtoEntityInstance.getTrackedEntityInstance(), importOptions.getUser());
        if (daoEntityInstance == null) {
            return;
        }
        daoEntityInstance.getTrackedEntityAttributeValues().stream().filter(attrVal -> attrVal.getAttribute().getValueType().isFile()).forEach(attrVal -> fileValues.add(attrVal.getValue()));
    }
    for (Attribute attribute : dtoEntityInstance.getAttributes()) {
        if (StringUtils.isNotEmpty(attribute.getValue())) {
            // Cache was populated in prepareCaches, so I should hit the
            // cache
            TrackedEntityAttribute daoEntityAttribute = getTrackedEntityAttribute(importOptions.getIdSchemes(), attribute.getAttribute());
            if (daoEntityAttribute == null) {
                importConflicts.addConflict("Attribute.attribute", "Invalid attribute " + attribute.getAttribute());
                continue;
            }
            if (attribute.getValue() != null && attribute.getValue().length() > TEA_VALUE_MAX_LENGTH) {
                // We shorten the value to first 25 characters, since we
                // dont want to post a 1200+ string back.
                importConflicts.addConflict("Attribute.value", String.format("Value exceeds the character limit of %s characters: '%s...'", TEA_VALUE_MAX_LENGTH, attribute.getValue().substring(0, 25)));
            }
            if (daoEntityAttribute.isUnique()) {
                // Cache was populated in prepareCaches, so I should hit the
                // cache
                OrganisationUnit organisationUnit = getOrganisationUnit(importOptions.getIdSchemes(), dtoEntityInstance.getOrgUnit());
                checkAttributeUniquenessWithinScope(daoEntityInstance, daoEntityAttribute, attribute.getValue(), organisationUnit, importConflicts);
            }
            validateAttributeType(attribute, importOptions, importConflicts);
            if (daoEntityAttribute.getValueType().isFile() && checkAssigned(attribute, fileValues)) {
                importConflicts.addConflict("Attribute.value", String.format("File resource with uid '%s' has already been assigned to a different object", attribute.getValue()));
            }
        }
    }
}
Also used : ImportStrategy(org.hisp.dhis.importexport.ImportStrategy) Authorities(org.hisp.dhis.security.Authorities) Date(java.util.Date) Restrictions(org.hisp.dhis.query.Restrictions) TrackedEntityAttributeValueService(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService) TrackedEntityInstanceAuditService(org.hisp.dhis.trackedentity.TrackedEntityInstanceAuditService) ReservedValueService(org.hisp.dhis.reservedvalue.ReservedValueService) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) StringUtils(org.apache.commons.lang3.StringUtils) TrackedEntityAttributeService(org.hisp.dhis.trackedentity.TrackedEntityAttributeService) ImportSummary(org.hisp.dhis.dxf2.importsummary.ImportSummary) RelationshipService(org.hisp.dhis.relationship.RelationshipService) FileResourceService(org.hisp.dhis.fileresource.FileResourceService) Map(java.util.Map) ProgramInstance(org.hisp.dhis.program.ProgramInstance) JobConfiguration(org.hisp.dhis.scheduling.JobConfiguration) TrackedEntityAttributeStore(org.hisp.dhis.trackedentity.TrackedEntityAttributeStore) EnrollmentService(org.hisp.dhis.dxf2.events.enrollment.EnrollmentService) Query(org.hisp.dhis.query.Query) UserService(org.hisp.dhis.user.UserService) TrackedEntityInstanceQueryParams(org.hisp.dhis.trackedentity.TrackedEntityInstanceQueryParams) Collection(java.util.Collection) Set(java.util.Set) SchemaService(org.hisp.dhis.schema.SchemaService) Collectors(java.util.stream.Collectors) QueryService(org.hisp.dhis.query.QueryService) TrackedEntityProgramOwner(org.hisp.dhis.trackedentity.TrackedEntityProgramOwner) ImportReportMode(org.hisp.dhis.dxf2.metadata.feedback.ImportReportMode) Objects(java.util.Objects) TrackerOwnershipManager(org.hisp.dhis.trackedentity.TrackerOwnershipManager) TEA_VALUE_MAX_LENGTH(org.hisp.dhis.trackedentity.TrackedEntityAttributeService.TEA_VALUE_MAX_LENGTH) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) TrackerAccessManager(org.hisp.dhis.trackedentity.TrackerAccessManager) ProgramInstanceService(org.hisp.dhis.program.ProgramInstanceService) Optional(java.util.Optional) TrackedEntityType(org.hisp.dhis.trackedentity.TrackedEntityType) Geometry(org.locationtech.jts.geom.Geometry) Enrollment(org.hisp.dhis.dxf2.events.enrollment.Enrollment) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityInstanceAggregate(org.hisp.dhis.dxf2.events.aggregates.TrackedEntityInstanceAggregate) TrackedEntityTypeService(org.hisp.dhis.trackedentity.TrackedEntityTypeService) Constants(org.hisp.dhis.dxf2.Constants) AuditType(org.hisp.dhis.common.AuditType) GeoUtils(org.hisp.dhis.system.util.GeoUtils) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) Program(org.hisp.dhis.program.Program) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Notifier(org.hisp.dhis.system.notification.Notifier) RelationshipParams(org.hisp.dhis.dxf2.events.RelationshipParams) Lists(com.google.common.collect.Lists) IdentifiableObjectManager(org.hisp.dhis.common.IdentifiableObjectManager) DbmsManager(org.hisp.dhis.dbms.DbmsManager) ImportConflicts(org.hisp.dhis.dxf2.importsummary.ImportConflicts) User(org.hisp.dhis.user.User) ImportStatus(org.hisp.dhis.dxf2.importsummary.ImportStatus) TrackedEntityInstanceAudit(org.hisp.dhis.audit.payloads.TrackedEntityInstanceAudit) IdSchemes(org.hisp.dhis.common.IdSchemes) FileResource(org.hisp.dhis.fileresource.FileResource) TrackedEntityInstanceParams(org.hisp.dhis.dxf2.events.TrackedEntityInstanceParams) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) ImportOptions(org.hisp.dhis.dxf2.common.ImportOptions) ImportSummaries(org.hisp.dhis.dxf2.importsummary.ImportSummaries) DebugUtils(org.hisp.dhis.commons.util.DebugUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) FeatureType(org.hisp.dhis.organisationunit.FeatureType) NotificationLevel(org.hisp.dhis.system.notification.NotificationLevel) RelationshipItem(org.hisp.dhis.relationship.RelationshipItem) CurrentUserService(org.hisp.dhis.user.CurrentUserService) CachingMap(org.hisp.dhis.commons.collection.CachingMap) ERROR(org.hisp.dhis.system.notification.NotificationLevel.ERROR) CodeGenerator(org.hisp.dhis.common.CodeGenerator) Collections(java.util.Collections) DateUtils(org.hisp.dhis.util.DateUtils) Transactional(org.springframework.transaction.annotation.Transactional) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) TrackedEntityAttribute(org.hisp.dhis.trackedentity.TrackedEntityAttribute) ArrayList(java.util.ArrayList)

Aggregations

WebMessageException (org.hisp.dhis.dxf2.webmessage.WebMessageException)92 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)52 Event (org.hisp.dhis.dxf2.events.event.Event)39 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)37 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)34 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)29 GetMapping (org.springframework.web.bind.annotation.GetMapping)28 User (org.hisp.dhis.user.User)23 Test (org.junit.jupiter.api.Test)21 HashMap (java.util.HashMap)19 ImportSummary (org.hisp.dhis.dxf2.importsummary.ImportSummary)19 UpdateAccessDeniedException (org.hisp.dhis.hibernate.exception.UpdateAccessDeniedException)19 InputStream (java.io.InputStream)18 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)18 ArrayList (java.util.ArrayList)17 MetadataImportParams (org.hisp.dhis.dxf2.metadata.MetadataImportParams)17 List (java.util.List)16 BaseIdentifiableObject (org.hisp.dhis.common.BaseIdentifiableObject)16 DataElement (org.hisp.dhis.dataelement.DataElement)15 ImportOptions (org.hisp.dhis.dxf2.common.ImportOptions)15