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));
}
}
Aggregations