protected SearchCond visitPrimitive()

in core/persistence-api/src/main/java/org/apache/syncope/core/persistence/api/search/SearchCondVisitor.java [106:246]


    protected SearchCond visitPrimitive(final SearchCondition<SearchBean> sc) {
        String name = getRealPropertyName(sc.getStatement().getProperty());
        Optional<SpecialAttr> specialAttrName = SpecialAttr.fromString(name);

        String value = getValue(sc);
        Optional<SpecialAttr> specialAttrValue = SpecialAttr.fromString(value);

        AttrCond attrCond = createAttrCond(name);
        attrCond.setExpression(value);

        ConditionType ct = getConditionType(sc);

        SearchCond leaf;
        switch (ct) {
            case EQUALS:
            case NOT_EQUALS:
                if (specialAttrName.isEmpty()) {
                    if (specialAttrValue.isPresent() && specialAttrValue.get() == SpecialAttr.NULL) {
                        attrCond.setType(AttrCond.Type.ISNULL);
                        attrCond.setExpression(null);
                    } else if (value.indexOf('%') == -1) {
                        attrCond.setType(sc.getConditionType() == ConditionType.CUSTOM
                                ? AttrCond.Type.IEQ
                                : AttrCond.Type.EQ);
                    } else {
                        attrCond.setType(sc.getConditionType() == ConditionType.CUSTOM
                                ? AttrCond.Type.ILIKE
                                : AttrCond.Type.LIKE);
                    }

                    leaf = SearchCond.of(attrCond);
                } else {
                    switch (specialAttrName.get()) {
                        case TYPE:
                            AnyTypeCond typeCond = new AnyTypeCond();
                            typeCond.setAnyTypeKey(value);
                            leaf = SearchCond.of(typeCond);
                            break;

                        case AUX_CLASSES:
                            AuxClassCond auxClassCond = new AuxClassCond();
                            auxClassCond.setAuxClass(value);
                            leaf = SearchCond.of(auxClassCond);
                            break;

                        case RESOURCES:
                            ResourceCond resourceCond = new ResourceCond();
                            resourceCond.setResource(value);
                            leaf = SearchCond.of(resourceCond);
                            break;

                        case GROUPS:
                            MembershipCond groupCond = new MembershipCond();
                            groupCond.setGroup(value);
                            leaf = SearchCond.of(groupCond);
                            break;

                        case RELATIONSHIPS:
                            RelationshipCond relationshipCond = new RelationshipCond();
                            relationshipCond.setAnyObject(value);
                            leaf = SearchCond.of(relationshipCond);
                            break;

                        case RELATIONSHIP_TYPES:
                            RelationshipTypeCond relationshipTypeCond = new RelationshipTypeCond();
                            relationshipTypeCond.setRelationshipType(value);
                            leaf = SearchCond.of(relationshipTypeCond);
                            break;

                        case ROLES:
                            RoleCond roleCond = new RoleCond();
                            roleCond.setRole(value);
                            leaf = SearchCond.of(roleCond);
                            break;

                        case DYNREALMS:
                            DynRealmCond dynRealmCond = new DynRealmCond();
                            dynRealmCond.setDynRealm(value);
                            leaf = SearchCond.of(dynRealmCond);
                            break;

                        case MEMBER:
                            MemberCond memberCond = new MemberCond();
                            memberCond.setMember(value);
                            leaf = SearchCond.of(memberCond);
                            break;

                        default:
                            throw new IllegalArgumentException(
                                    String.format("Special attr name %s is not supported", specialAttrName));
                    }
                }
                if (ct == ConditionType.NOT_EQUALS) {
                    Optional<AttrCond> notEquals = leaf.asLeaf(AttrCond.class);
                    if (notEquals.isPresent() && notEquals.get().getType() == AttrCond.Type.ISNULL) {
                        notEquals.get().setType(AttrCond.Type.ISNOTNULL);
                    } else {
                        leaf = SearchCond.negate(leaf);
                    }
                }
                break;

            case GREATER_OR_EQUALS:
                attrCond.setType(AttrCond.Type.GE);
                leaf = SearchCond.of(attrCond);
                break;

            case GREATER_THAN:
                attrCond.setType(AttrCond.Type.GT);
                leaf = SearchCond.of(attrCond);
                break;

            case LESS_OR_EQUALS:
                attrCond.setType(AttrCond.Type.LE);
                leaf = SearchCond.of(attrCond);
                break;

            case LESS_THAN:
                attrCond.setType(AttrCond.Type.LT);
                leaf = SearchCond.of(attrCond);
                break;

            default:
                throw new IllegalArgumentException(String.format("Condition type %s is not supported", ct.name()));
        }

        // SYNCOPE-1293: explicitly re-process to allow 'token==$null' or 'token!=$null'
        Optional<AttrCond> reprocess = leaf.asLeaf(AttrCond.class).
                filter(cond -> "token".equals(cond.getSchema())
                && (cond.getType() == AttrCond.Type.ISNULL || cond.getType() == AttrCond.Type.ISNOTNULL)
                && cond.getExpression() == null);
        if (reprocess.isPresent()) {
            AnyCond tokenCond = new AnyCond();
            tokenCond.setSchema(reprocess.get().getSchema());
            tokenCond.setType(reprocess.get().getType());
            tokenCond.setExpression(null);
            leaf = SearchCond.of(tokenCond);
        }

        return leaf;
    }