Search in sources :

Example 1 with TrackerIdScheme

use of org.hisp.dhis.tracker.TrackerIdScheme in project dhis2-core by dhis2.

the class TrackerPreheat method getRelationship.

public Relationship getRelationship(TrackerIdScheme identifier, org.hisp.dhis.tracker.domain.Relationship relationship) {
    if (!relationships.containsKey(identifier)) {
        return null;
    }
    RelationshipType relationshipType = get(RelationshipType.class, relationship.getRelationshipType());
    if (Objects.nonNull(relationshipType)) {
        RelationshipKey relationshipKey = getRelationshipKey(relationship);
        RelationshipKey inverseKey = null;
        if (relationshipType.isBidirectional()) {
            inverseKey = relationshipKey.inverseKey();
        }
        return Stream.of(relationshipKey, inverseKey).filter(Objects::nonNull).map(key -> relationships.get(identifier).get(key.asString())).filter(Objects::nonNull).findFirst().orElse(null);
    }
    return null;
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Setter(lombok.Setter) Getter(lombok.Getter) CategoryOption(org.hisp.dhis.category.CategoryOption) TrackedEntityProgramOwnerOrgUnit(org.hisp.dhis.trackedentity.TrackedEntityProgramOwnerOrgUnit) HashMap(java.util.HashMap) ProgramStageInstance(org.hisp.dhis.program.ProgramStageInstance) TrackedEntityAttributeValue(org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) TrackerIdentifier(org.hisp.dhis.tracker.TrackerIdentifier) ArrayList(java.util.ArrayList) Enrollment(org.hisp.dhis.tracker.domain.Enrollment) TrackerIdentifierParams(org.hisp.dhis.tracker.TrackerIdentifierParams) Lists(com.google.common.collect.Lists) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) RelationshipType(org.hisp.dhis.relationship.RelationshipType) ProgramInstance(org.hisp.dhis.program.ProgramInstance) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) User(org.hisp.dhis.user.User) Period(org.hisp.dhis.period.Period) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) Event(org.hisp.dhis.tracker.domain.Event) HibernateProxyUtils(org.hisp.dhis.hibernate.HibernateProxyUtils) EnumMap(java.util.EnumMap) BaseIdentifiableObject(org.hisp.dhis.common.BaseIdentifiableObject) TreeNode(com.scalified.tree.TreeNode) Collection(java.util.Collection) RelationshipKey(org.hisp.dhis.relationship.RelationshipKey) TrackedEntityInstance(org.hisp.dhis.trackedentity.TrackedEntityInstance) TrackerType(org.hisp.dhis.tracker.TrackerType) RelationshipPreheatKeySupport.getRelationshipKey(org.hisp.dhis.tracker.preheat.RelationshipPreheatKeySupport.getRelationshipKey) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) Objects(java.util.Objects) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) Stream(java.util.stream.Stream) CategoryOptionCombo(org.hisp.dhis.category.CategoryOptionCombo) TrackedEntityComment(org.hisp.dhis.trackedentitycomment.TrackedEntityComment) StringJoiner(java.util.StringJoiner) Optional(java.util.Optional) PeriodType(org.hisp.dhis.period.PeriodType) Relationship(org.hisp.dhis.relationship.Relationship) ArrayMultiTreeNode(com.scalified.tree.multinode.ArrayMultiTreeNode) RelationshipType(org.hisp.dhis.relationship.RelationshipType) Objects(java.util.Objects) RelationshipKey(org.hisp.dhis.relationship.RelationshipKey) RelationshipPreheatKeySupport.getRelationshipKey(org.hisp.dhis.tracker.preheat.RelationshipPreheatKeySupport.getRelationshipKey)

Example 2 with TrackerIdScheme

use of org.hisp.dhis.tracker.TrackerIdScheme in project dhis2-core by dhis2.

the class ProgramInstancesWithAtLeastOneEventSupplier method preheatAdd.

@Override
public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) {
    final Map<TrackerIdScheme, Map<String, ProgramInstance>> enrollmentsMap = preheat.getEnrollments();
    final Map<String, ProgramInstance> enrollments = enrollmentsMap.getOrDefault(TrackerIdScheme.UID, new HashMap<>());
    List<Long> programStageIds = enrollments.values().stream().map(BaseIdentifiableObject::getId).collect(Collectors.toList());
    if (!programStageIds.isEmpty()) {
        List<String> uids = new ArrayList<>();
        MapSqlParameterSource parameters = new MapSqlParameterSource();
        parameters.addValue("ids", programStageIds);
        jdbcTemplate.query(SQL, parameters, rs -> {
            uids.add(rs.getString(COLUMN));
        });
        preheat.setProgramInstanceWithOneOrMoreNonDeletedEvent(uids);
    }
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) ProgramInstance(org.hisp.dhis.program.ProgramInstance) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TrackerIdScheme

use of org.hisp.dhis.tracker.TrackerIdScheme in project dhis2-core by dhis2.

the class AbstractSchemaStrategy method cacheAwareFetch.

@SuppressWarnings({ "unchecked", "rawtypes" })
private List<IdentifiableObject> cacheAwareFetch(User user, Schema schema, TrackerIdentifier identifier, List<String> ids, Class<? extends PreheatMapper> mapper) {
    TrackerIdScheme idScheme = identifier.getIdScheme();
    List<IdentifiableObject> objects;
    final String cacheKey = buildCacheKey(schema);
    if (// check if this strategy requires caching
    isCacheable()) {
        if (isLoadAllEntities(ids)) {
            return map(cacheAndReturnLookupData(schema), mapper);
        } else {
            Map<String, IdentifiableObject> foundInCache = new HashMap<>();
            for (String id : ids) {
                // is the object reference by the given id in cache?
                cache.get(cacheKey, id).ifPresent(identifiableObject -> foundInCache.put(id, identifiableObject));
            }
            // is there any object which was not found in cache?
            if (ids.size() > foundInCache.size()) {
                // remove from the list of ids the ids found in cache
                ids.removeAll(foundInCache.keySet());
                // execute the query, fetching only the ids which are not in
                // cache
                objects = map((List<IdentifiableObject>) queryService.query(buildQuery(schema, user, idScheme, ids)), mapper);
                // put objects in query based on given scheme. If the key
                // can't get resolved, send null to the
                // cacheService, which will ignore the entry
                objects.forEach(o -> cache.put(cacheKey, identifier.getIdentifier(o), o, getCacheTTL(), getCapacity()));
                // add back the cached objects to the final list
                objects.addAll(foundInCache.values());
            } else {
                objects = new ArrayList<>(foundInCache.values());
            }
        }
    } else {
        objects = isLoadAllEntities(ids) ? manager.getAll((Class<IdentifiableObject>) schema.getKlass()) : map((List<IdentifiableObject>) queryService.query(buildQuery(schema, user, idScheme, ids)), mapper);
    }
    return objects;
}
Also used : HashMap(java.util.HashMap) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) ArrayList(java.util.ArrayList) List(java.util.List) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Example 4 with TrackerIdScheme

use of org.hisp.dhis.tracker.TrackerIdScheme in project dhis2-core by dhis2.

the class AbstractSchemaStrategy method queryForIdentifiableObjects.

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void queryForIdentifiableObjects(TrackerPreheat preheat, Schema schema, TrackerIdentifier identifier, List<List<String>> splitList, Class<? extends PreheatMapper> mapper) {
    TrackerIdScheme idScheme = identifier.getIdScheme();
    for (List<String> ids : splitList) {
        List<? extends IdentifiableObject> objects;
        if (TrackerIdScheme.ATTRIBUTE.equals(idScheme)) {
            Attribute attribute = new Attribute();
            attribute.setUid(identifier.getValue());
            objects = manager.getAllByAttributeAndValues((Class<? extends IdentifiableObject>) schema.getKlass(), attribute, ids);
        } else {
            objects = cacheAwareFetch(preheat.getUser(), schema, identifier, ids, mapper);
        }
        preheat.put(identifier, objects);
    }
}
Also used : Attribute(org.hisp.dhis.attribute.Attribute) TrackerIdScheme(org.hisp.dhis.tracker.TrackerIdScheme) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject)

Aggregations

TrackerIdScheme (org.hisp.dhis.tracker.TrackerIdScheme)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)3 List (java.util.List)2 Map (java.util.Map)2 ProgramInstance (org.hisp.dhis.program.ProgramInstance)2 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 TreeNode (com.scalified.tree.TreeNode)1 ArrayMultiTreeNode (com.scalified.tree.multinode.ArrayMultiTreeNode)1 Collection (java.util.Collection)1 EnumMap (java.util.EnumMap)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 StringJoiner (java.util.StringJoiner)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1