Search in sources :

Example 1 with QNameUtil

use of com.evolveum.midpoint.util.QNameUtil in project midpoint by Evolveum.

the class IndexedRelationDefinitions method initializeRelationDefinitionsByRelationName.

/**
 * Removes duplicate definitions as well.
 */
@NotNull
private Map<QName, RelationDefinitionType> initializeRelationDefinitionsByRelationName(List<RelationDefinitionType> definitions) {
    Map<QName, RelationDefinitionType> map = new HashMap<>();
    ListValuedMap<String, QName> expansions = new ArrayListValuedHashMap<>();
    for (Iterator<RelationDefinitionType> iterator = definitions.iterator(); iterator.hasNext(); ) {
        RelationDefinitionType definition = iterator.next();
        if (map.containsKey(definition.getRef())) {
            LOGGER.error("Duplicate relation definition for '{}'; ignoring: {}", definition.getRef(), definition);
            iterator.remove();
        } else {
            map.put(definition.getRef(), definition);
            expansions.put(definition.getRef().getLocalPart(), definition.getRef());
        }
    }
    // add entries for unqualified versions of the relation names
    for (String unqualified : expansions.keySet()) {
        List<QName> names = expansions.get(unqualified);
        if (names.contains(new QName(unqualified))) {
            // cannot expand unqualified if the expanded value is also unqualified
            continue;
        }
        assert !names.isEmpty();
        assert names.stream().allMatch(QNameUtil::isQualified);
        @NotNull QName chosenExpansion;
        if (names.size() == 1) {
            chosenExpansion = names.get(0);
        } else {
            QName nameInOrgNamespace = names.stream().filter(n -> SchemaConstants.NS_ORG.equals(n.getNamespaceURI())).findFirst().orElse(null);
            if (nameInOrgNamespace != null) {
                // org:xxx expansion will be the default one
                chosenExpansion = nameInOrgNamespace;
            } else {
                chosenExpansion = names.get(0);
                LOGGER.warn("Multiple resolutions of unqualified relation name '{}' ({}); " + "using the first one as default: '{}'. Please reconsider this as it could lead to " + "unpredictable behavior.", unqualified, names, chosenExpansion);
            }
        }
        assert QNameUtil.isQualified(chosenExpansion);
        map.put(new QName(unqualified), map.get(chosenExpansion));
    }
    return map;
}
Also used : RelationDefinitionType(com.evolveum.midpoint.xml.ns._public.common.common_3.RelationDefinitionType) HashSetValuedHashMap(org.apache.commons.collections4.multimap.HashSetValuedHashMap) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) QName(javax.xml.namespace.QName) QNameUtil(com.evolveum.midpoint.util.QNameUtil) ArrayListValuedHashMap(org.apache.commons.collections4.multimap.ArrayListValuedHashMap) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

QNameUtil (com.evolveum.midpoint.util.QNameUtil)1 RelationDefinitionType (com.evolveum.midpoint.xml.ns._public.common.common_3.RelationDefinitionType)1 QName (javax.xml.namespace.QName)1 ArrayListValuedHashMap (org.apache.commons.collections4.multimap.ArrayListValuedHashMap)1 HashSetValuedHashMap (org.apache.commons.collections4.multimap.HashSetValuedHashMap)1 NotNull (org.jetbrains.annotations.NotNull)1