Search in sources :

Example 1 with SyncopeFiqlSearchCondition

use of org.apache.syncope.common.lib.search.SyncopeFiqlSearchCondition in project syncope by apache.

the class SearchUtils method getPrimitiveSearchClause.

private static SearchClause getPrimitiveSearchClause(final SearchCondition<SearchBean> sc) {
    SearchClause clause = new SearchClause();
    String property = sc.getCondition().getKeySet().iterator().next();
    clause.setProperty(property);
    String value = sc.getCondition().get(property).replace("%252C", ",").replace("%253B", ";");
    clause.setValue(value);
    LOG.debug("Condition: " + sc.getCondition());
    if (SpecialAttr.ROLES.toString().equals(property)) {
        clause.setType(SearchClause.Type.ROLE_MEMBERSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.PRIVILEGES.toString().equals(property)) {
        clause.setType(SearchClause.Type.PRIVILEGE);
        clause.setProperty(value);
    } else if (SpecialAttr.RELATIONSHIPS.toString().equals(property)) {
        clause.setType(SearchClause.Type.RELATIONSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.RELATIONSHIP_TYPES.toString().equals(property)) {
        clause.setType(SearchClause.Type.RELATIONSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.GROUPS.toString().equals(property)) {
        clause.setType(SearchClause.Type.GROUP_MEMBERSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.RESOURCES.toString().equals(property)) {
        clause.setType(SearchClause.Type.RESOURCE);
        clause.setProperty(value);
    } else if (SpecialAttr.MEMBER.toString().equals(property)) {
        clause.setType(SearchClause.Type.GROUP_MEMBER);
        clause.setProperty(value);
    } else {
        clause.setType(SearchClause.Type.ATTRIBUTE);
    }
    ConditionType ct = sc.getConditionType();
    if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
        SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
        if (SyncopeFiqlParser.IEQ.equals(sfsc.getOperator())) {
            ct = ConditionType.EQUALS;
        } else if (SyncopeFiqlParser.NIEQ.equals(sfsc.getOperator())) {
            ct = ConditionType.NOT_EQUALS;
        }
    }
    switch(ct) {
        case EQUALS:
            if (SpecialAttr.RELATIONSHIP_TYPES.toString().equals(property)) {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value) ? SearchClause.Comparator.EQUALS : SearchClause.Comparator.IS_NULL);
            } else {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value) ? SearchClause.Comparator.IS_NULL : SearchClause.Comparator.EQUALS);
            }
            break;
        case NOT_EQUALS:
            if (SpecialAttr.RELATIONSHIP_TYPES.toString().equals(property)) {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value) ? SearchClause.Comparator.NOT_EQUALS : SearchClause.Comparator.IS_NOT_NULL);
            } else {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value) ? SearchClause.Comparator.IS_NOT_NULL : SearchClause.Comparator.NOT_EQUALS);
            }
            break;
        case GREATER_OR_EQUALS:
            clause.setComparator(SearchClause.Comparator.GREATER_OR_EQUALS);
            break;
        case GREATER_THAN:
            clause.setComparator(SearchClause.Comparator.GREATER_THAN);
            break;
        case LESS_OR_EQUALS:
            clause.setComparator(SearchClause.Comparator.LESS_OR_EQUALS);
            break;
        case LESS_THAN:
            clause.setComparator(SearchClause.Comparator.LESS_THAN);
            break;
        default:
            break;
    }
    return clause;
}
Also used : SyncopeFiqlSearchCondition(org.apache.syncope.common.lib.search.SyncopeFiqlSearchCondition) SearchBean(org.apache.cxf.jaxrs.ext.search.SearchBean) ConditionType(org.apache.cxf.jaxrs.ext.search.ConditionType)

Example 2 with SyncopeFiqlSearchCondition

use of org.apache.syncope.common.lib.search.SyncopeFiqlSearchCondition in project syncope by apache.

the class SearchCondVisitor method visitPrimitive.

@SuppressWarnings("ConvertToStringSwitch")
private SearchCond visitPrimitive(final SearchCondition<SearchBean> sc) {
    String name = getRealPropertyName(sc.getStatement().getProperty());
    Optional<SpecialAttr> specialAttrName = SpecialAttr.fromString(name);
    String value = null;
    try {
        value = SearchUtils.toSqlWildcardString(URLDecoder.decode(sc.getStatement().getValue().toString(), StandardCharsets.UTF_8.name()), false).replaceAll("\\\\_", "_");
    } catch (UnsupportedEncodingException e) {
        throw new IllegalArgumentException("While decoding " + sc.getStatement().getValue(), e);
    }
    Optional<SpecialAttr> specialAttrValue = SpecialAttr.fromString(value);
    AttributeCond attributeCond = createAttributeCond(name);
    attributeCond.setExpression(value);
    ConditionType ct = sc.getConditionType();
    if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
        SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
        if (SyncopeFiqlParser.IEQ.equals(sfsc.getOperator())) {
            ct = ConditionType.EQUALS;
        } else if (SyncopeFiqlParser.NIEQ.equals(sfsc.getOperator())) {
            ct = ConditionType.NOT_EQUALS;
        } else {
            throw new IllegalArgumentException(String.format("Condition type %s is not supported", sfsc.getOperator()));
        }
    }
    SearchCond leaf;
    switch(ct) {
        case EQUALS:
        case NOT_EQUALS:
            if (!specialAttrName.isPresent()) {
                if (specialAttrValue.isPresent() && specialAttrValue.get() == SpecialAttr.NULL) {
                    attributeCond.setType(AttributeCond.Type.ISNULL);
                    attributeCond.setExpression(null);
                } else if (value.indexOf('%') == -1) {
                    attributeCond.setType(sc.getConditionType() == ConditionType.CUSTOM ? AttributeCond.Type.IEQ : AttributeCond.Type.EQ);
                } else {
                    attributeCond.setType(sc.getConditionType() == ConditionType.CUSTOM ? AttributeCond.Type.ILIKE : AttributeCond.Type.LIKE);
                }
                leaf = SearchCond.getLeafCond(attributeCond);
            } else {
                switch(specialAttrName.get()) {
                    case TYPE:
                        AnyTypeCond typeCond = new AnyTypeCond();
                        typeCond.setAnyTypeKey(value);
                        leaf = SearchCond.getLeafCond(typeCond);
                        break;
                    case RESOURCES:
                        ResourceCond resourceCond = new ResourceCond();
                        resourceCond.setResourceKey(value);
                        leaf = SearchCond.getLeafCond(resourceCond);
                        break;
                    case GROUPS:
                        MembershipCond groupCond = new MembershipCond();
                        groupCond.setGroup(value);
                        leaf = SearchCond.getLeafCond(groupCond);
                        break;
                    case RELATIONSHIPS:
                        RelationshipCond relationshipCond = new RelationshipCond();
                        relationshipCond.setAnyObject(value);
                        leaf = SearchCond.getLeafCond(relationshipCond);
                        break;
                    case RELATIONSHIP_TYPES:
                        RelationshipTypeCond relationshipTypeCond = new RelationshipTypeCond();
                        relationshipTypeCond.setRelationshipTypeKey(value);
                        leaf = SearchCond.getLeafCond(relationshipTypeCond);
                        break;
                    case ROLES:
                        RoleCond roleCond = new RoleCond();
                        roleCond.setRole(value);
                        leaf = SearchCond.getLeafCond(roleCond);
                        break;
                    case PRIVILEGES:
                        PrivilegeCond privilegeCond = new PrivilegeCond();
                        privilegeCond.setPrivilege(value);
                        leaf = SearchCond.getLeafCond(privilegeCond);
                        break;
                    case DYNREALMS:
                        DynRealmCond dynRealmCond = new DynRealmCond();
                        dynRealmCond.setDynRealm(value);
                        leaf = SearchCond.getLeafCond(dynRealmCond);
                        break;
                    case ASSIGNABLE:
                        AssignableCond assignableCond = new AssignableCond();
                        assignableCond.setRealmFullPath(realm);
                        leaf = SearchCond.getLeafCond(assignableCond);
                        break;
                    case MEMBER:
                        MemberCond memberCond = new MemberCond();
                        memberCond.setMember(value);
                        leaf = SearchCond.getLeafCond(memberCond);
                        break;
                    default:
                        throw new IllegalArgumentException(String.format("Special attr name %s is not supported", specialAttrName));
                }
            }
            if (ct == ConditionType.NOT_EQUALS) {
                if (leaf.getAttributeCond() != null && leaf.getAttributeCond().getType() == AttributeCond.Type.ISNULL) {
                    leaf.getAttributeCond().setType(AttributeCond.Type.ISNOTNULL);
                } else if (leaf.getAnyCond() != null && leaf.getAnyCond().getType() == AnyCond.Type.ISNULL) {
                    leaf.getAnyCond().setType(AttributeCond.Type.ISNOTNULL);
                } else {
                    leaf = SearchCond.getNotLeafCond(leaf);
                }
            }
            break;
        case GREATER_OR_EQUALS:
            attributeCond.setType(AttributeCond.Type.GE);
            leaf = SearchCond.getLeafCond(attributeCond);
            break;
        case GREATER_THAN:
            attributeCond.setType(AttributeCond.Type.GT);
            leaf = SearchCond.getLeafCond(attributeCond);
            break;
        case LESS_OR_EQUALS:
            attributeCond.setType(AttributeCond.Type.LE);
            leaf = SearchCond.getLeafCond(attributeCond);
            break;
        case LESS_THAN:
            attributeCond.setType(AttributeCond.Type.LT);
            leaf = SearchCond.getLeafCond(attributeCond);
            break;
        default:
            throw new IllegalArgumentException(String.format("Condition type %s is not supported", ct.name()));
    }
    return leaf;
}
Also used : AssignableCond(org.apache.syncope.core.persistence.api.dao.search.AssignableCond) MemberCond(org.apache.syncope.core.persistence.api.dao.search.MemberCond) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) SearchBean(org.apache.cxf.jaxrs.ext.search.SearchBean) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) UnsupportedEncodingException(java.io.UnsupportedEncodingException) RelationshipCond(org.apache.syncope.core.persistence.api.dao.search.RelationshipCond) ResourceCond(org.apache.syncope.core.persistence.api.dao.search.ResourceCond) SpecialAttr(org.apache.syncope.common.lib.search.SpecialAttr) SyncopeFiqlSearchCondition(org.apache.syncope.common.lib.search.SyncopeFiqlSearchCondition) AnyTypeCond(org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond) RoleCond(org.apache.syncope.core.persistence.api.dao.search.RoleCond) DynRealmCond(org.apache.syncope.core.persistence.api.dao.search.DynRealmCond) RelationshipTypeCond(org.apache.syncope.core.persistence.api.dao.search.RelationshipTypeCond) ConditionType(org.apache.cxf.jaxrs.ext.search.ConditionType) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) PrivilegeCond(org.apache.syncope.core.persistence.api.dao.search.PrivilegeCond)

Aggregations

ConditionType (org.apache.cxf.jaxrs.ext.search.ConditionType)2 SearchBean (org.apache.cxf.jaxrs.ext.search.SearchBean)2 SyncopeFiqlSearchCondition (org.apache.syncope.common.lib.search.SyncopeFiqlSearchCondition)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SpecialAttr (org.apache.syncope.common.lib.search.SpecialAttr)1 AnyTypeCond (org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond)1 AssignableCond (org.apache.syncope.core.persistence.api.dao.search.AssignableCond)1 AttributeCond (org.apache.syncope.core.persistence.api.dao.search.AttributeCond)1 DynRealmCond (org.apache.syncope.core.persistence.api.dao.search.DynRealmCond)1 MemberCond (org.apache.syncope.core.persistence.api.dao.search.MemberCond)1 MembershipCond (org.apache.syncope.core.persistence.api.dao.search.MembershipCond)1 PrivilegeCond (org.apache.syncope.core.persistence.api.dao.search.PrivilegeCond)1 RelationshipCond (org.apache.syncope.core.persistence.api.dao.search.RelationshipCond)1 RelationshipTypeCond (org.apache.syncope.core.persistence.api.dao.search.RelationshipTypeCond)1 ResourceCond (org.apache.syncope.core.persistence.api.dao.search.ResourceCond)1 RoleCond (org.apache.syncope.core.persistence.api.dao.search.RoleCond)1 SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)1