Search in sources :

Example 1 with RPolyString

use of com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString in project midpoint by Evolveum.

the class NameResolutionHelper method resolveNamesIfRequested.

public void resolveNamesIfRequested(Session session, List<? extends PrismContainerValue> containerValues, Collection<SelectorOptions<GetOperationOptions>> options) {
    List<ItemPath> pathsToResolve = getPathsToResolve(options);
    if (pathsToResolve.isEmpty()) {
        return;
    }
    final Set<String> oidsToResolve = new HashSet<>();
    Visitor oidExtractor = visitable -> {
        if (visitable instanceof PrismReferenceValue) {
            PrismReferenceValue value = (PrismReferenceValue) visitable;
            if (!ItemPath.containsSubpathOrEquivalent(pathsToResolve, value.getPath())) {
                return;
            }
            if (value.getTargetName() != null) {
                return;
            }
            if (value.getObject() != null) {
                value.setTargetName(value.getObject().getName());
                return;
            }
            if (value.getOid() == null) {
                return;
            }
            oidsToResolve.add(value.getOid());
        }
    };
    Set<PrismContainerValue> roots = containerValues.stream().map(pcv -> pcv.getRootValue()).collect(Collectors.toSet());
    roots.forEach(root -> root.accept(oidExtractor));
    Map<String, PolyString> oidNameMap = new HashMap<>();
    List<String> batch = new ArrayList<>();
    for (Iterator<String> iterator = oidsToResolve.iterator(); iterator.hasNext(); ) {
        batch.add(iterator.next());
        if (batch.size() >= MAX_OIDS_TO_RESOLVE_AT_ONCE || !iterator.hasNext()) {
            Query query = session.getNamedQuery("resolveReferences");
            query.setParameterList("oid", batch);
            query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
            @SuppressWarnings({ "unchecked", "raw" }) List<Map<String, Object>> // returns oid + name
            results = query.list();
            for (Map<String, Object> result : results) {
                String oid = (String) result.get("0");
                RPolyString name = (RPolyString) result.get("1");
                oidNameMap.put(oid, new PolyString(name.getOrig(), name.getNorm()));
            }
            batch.clear();
        }
    }
    if (!oidNameMap.isEmpty()) {
        Visitor nameSetter = visitable -> {
            if (visitable instanceof PrismReferenceValue) {
                PrismReferenceValue value = (PrismReferenceValue) visitable;
                if (value.getTargetName() == null && value.getOid() != null) {
                    value.setTargetName(oidNameMap.get(value.getOid()));
                }
            }
        };
        roots.forEach(root -> root.accept(nameSetter));
    }
}
Also used : SelectorOptions(com.evolveum.midpoint.schema.SelectorOptions) java.util(java.util) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString) Transformers(org.hibernate.transform.Transformers) Session(org.hibernate.Session) Collectors(java.util.stream.Collectors) ItemPath(com.evolveum.midpoint.prism.path.ItemPath) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Component(org.springframework.stereotype.Component) GetOperationOptions(com.evolveum.midpoint.schema.GetOperationOptions) Query(org.hibernate.Query) NotNull(org.jetbrains.annotations.NotNull) com.evolveum.midpoint.prism(com.evolveum.midpoint.prism) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) Query(org.hibernate.Query) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) RPolyString(com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString) PolyString(com.evolveum.midpoint.prism.polystring.PolyString) ItemPath(com.evolveum.midpoint.prism.path.ItemPath)

Aggregations

com.evolveum.midpoint.prism (com.evolveum.midpoint.prism)1 ItemPath (com.evolveum.midpoint.prism.path.ItemPath)1 PolyString (com.evolveum.midpoint.prism.polystring.PolyString)1 RPolyString (com.evolveum.midpoint.repo.sql.data.common.embedded.RPolyString)1 GetOperationOptions (com.evolveum.midpoint.schema.GetOperationOptions)1 SelectorOptions (com.evolveum.midpoint.schema.SelectorOptions)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 Query (org.hibernate.Query)1 Session (org.hibernate.Session)1 Transformers (org.hibernate.transform.Transformers)1 NotNull (org.jetbrains.annotations.NotNull)1 Component (org.springframework.stereotype.Component)1