Search in sources :

Example 1 with EntityField

use of org.keycloak.models.map.common.EntityField in project keycloak by keycloak.

the class PerFieldDelegateProvider method get.

@Override
@SuppressWarnings("unchecked")
public <EF extends Enum<? extends EntityField<V>> & EntityField<V>> Object get(EF field) {
    switch(node.isCacheFor(field, null).max(() -> node.isPrimarySourceFor(field, null))) {
        case FULLY:
            return field.get(entity);
        case NOT_CONTAINED:
            return field.get(getEntityFromDescendantNode());
    }
    // It has to be partial field. The only supported partial field is a Map
    if (field.getMapKeyClass() == Void.class) {
        throw new IllegalStateException("Field " + field + " expected to be a map but is " + field.getFieldClass());
    }
    Map<Object, Object> m1 = (Map<Object, Object>) field.get(entity);
    Map m2 = (Map) field.get(getEntityFromDescendantNode());
    if (m1 == null) {
        return m2 == null ? null : new HashMap<>(m2);
    }
    Predicate<Map.Entry<Object, Object>> isInNode = me -> node.isCacheFor(field, me.getKey()).max(() -> node.isPrimarySourceFor(field, me.getKey())) == FieldContainedStatus.FULLY;
    Stream<Map.Entry<Object, Object>> s = m1.entrySet().stream().filter(isInNode);
    if (m2 == null) {
        return s.collect(ENTRY_TO_HASH_MAP_OVERRIDING_KEYS_COLLECTOR);
    }
    return Stream.concat(s, m2.entrySet().stream().filter(isInNode.negate())).collect(ENTRY_TO_HASH_MAP_OVERRIDING_KEYS_COLLECTOR);
}
Also used : Predicate(java.util.function.Predicate) UpdatableEntity(org.keycloak.models.map.common.UpdatableEntity) TreeStorageNodeInstance(org.keycloak.models.map.storage.tree.TreeStorageNodeInstance) HashMap(java.util.HashMap) Supplier(java.util.function.Supplier) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) AbstractEntity(org.keycloak.models.map.common.AbstractEntity) Stream(java.util.stream.Stream) Map(java.util.Map) FieldContainedStatus(org.keycloak.models.map.storage.tree.TreeStorageNodePrescription.FieldContainedStatus) Entry(java.util.Map.Entry) EntityField(org.keycloak.models.map.common.EntityField) Collector(java.util.stream.Collector) Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1 Predicate (java.util.function.Predicate)1 Supplier (java.util.function.Supplier)1 Collector (java.util.stream.Collector)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 AbstractEntity (org.keycloak.models.map.common.AbstractEntity)1 EntityField (org.keycloak.models.map.common.EntityField)1 UpdatableEntity (org.keycloak.models.map.common.UpdatableEntity)1 TreeStorageNodeInstance (org.keycloak.models.map.storage.tree.TreeStorageNodeInstance)1 FieldContainedStatus (org.keycloak.models.map.storage.tree.TreeStorageNodePrescription.FieldContainedStatus)1