Search in sources :

Example 51 with SearchCond

use of org.apache.syncope.core.persistence.api.dao.search.SearchCond in project syncope by apache.

the class ReconciliationReportlet method doExtract.

@Override
protected void doExtract(final ReportletConf conf, final ContentHandler handler, final AtomicReference<String> status) throws SAXException {
    if (conf instanceof ReconciliationReportletConf) {
        this.conf = ReconciliationReportletConf.class.cast(conf);
    } else {
        throw new ReportException(new IllegalArgumentException("Invalid configuration provided"));
    }
    AttributesImpl atts = new AttributesImpl();
    if (StringUtils.isBlank(this.conf.getUserMatchingCond())) {
        int total = userDAO.count();
        int pages = (total / AnyDAO.DEFAULT_PAGE_SIZE) + 1;
        status.set("Processing " + total + " users in " + pages + " pages");
        atts.addAttribute("", "", "total", ReportXMLConst.XSD_INT, String.valueOf(total));
        handler.startElement("", "", getAnyElementName(AnyTypeKind.USER) + "s", atts);
        for (int page = 1; page <= pages; page++) {
            status.set("Processing " + total + " users: page " + page + " of " + pages);
            doExtract(handler, userDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE));
        }
    } else {
        SearchCond cond = SearchCondConverter.convert(this.conf.getUserMatchingCond());
        int total = searchDAO.count(SyncopeConstants.FULL_ADMIN_REALMS, cond, AnyTypeKind.USER);
        int pages = (total / AnyDAO.DEFAULT_PAGE_SIZE) + 1;
        status.set("Processing " + total + " users in " + pages + " pages");
        atts.addAttribute("", "", "total", ReportXMLConst.XSD_INT, String.valueOf(total));
        handler.startElement("", "", getAnyElementName(AnyTypeKind.USER) + "s", atts);
        for (int page = 1; page <= pages; page++) {
            status.set("Processing " + total + " users: page " + page + " of " + pages);
            doExtract(handler, searchDAO.search(SyncopeConstants.FULL_ADMIN_REALMS, cond, page, PAGE_SIZE, Collections.<OrderByClause>emptyList(), AnyTypeKind.USER));
        }
    }
    handler.endElement("", "", getAnyElementName(AnyTypeKind.USER) + "s");
    atts.clear();
    if (StringUtils.isBlank(this.conf.getGroupMatchingCond())) {
        int total = groupDAO.count();
        int pages = (total / AnyDAO.DEFAULT_PAGE_SIZE) + 1;
        status.set("Processing " + total + " groups in " + pages + " pages");
        atts.addAttribute("", "", "total", ReportXMLConst.XSD_INT, String.valueOf(total));
        handler.startElement("", "", getAnyElementName(AnyTypeKind.GROUP) + "s", atts);
        for (int page = 1; page <= pages; page++) {
            status.set("Processing " + total + " groups: page " + page + " of " + pages);
            doExtract(handler, groupDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE));
        }
    } else {
        SearchCond cond = SearchCondConverter.convert(this.conf.getUserMatchingCond());
        int total = searchDAO.count(SyncopeConstants.FULL_ADMIN_REALMS, cond, AnyTypeKind.GROUP);
        int pages = (total / AnyDAO.DEFAULT_PAGE_SIZE) + 1;
        status.set("Processing " + total + " groups in " + pages + " pages");
        atts.addAttribute("", "", "total", ReportXMLConst.XSD_INT, String.valueOf(total));
        handler.startElement("", "", getAnyElementName(AnyTypeKind.GROUP) + "s", atts);
        for (int page = 1; page <= pages; page++) {
            status.set("Processing " + total + " groups: page " + page + " of " + pages);
            doExtract(handler, searchDAO.search(SyncopeConstants.FULL_ADMIN_REALMS, cond, page, PAGE_SIZE, Collections.<OrderByClause>emptyList(), AnyTypeKind.GROUP));
        }
    }
    handler.endElement("", "", getAnyElementName(AnyTypeKind.GROUP) + "s");
    for (AnyType anyType : anyTypeDAO.findAll()) {
        if (!anyType.equals(anyTypeDAO.findUser()) && !anyType.equals(anyTypeDAO.findGroup())) {
            AnyTypeCond anyTypeCond = new AnyTypeCond();
            anyTypeCond.setAnyTypeKey(anyType.getKey());
            SearchCond cond = StringUtils.isBlank(this.conf.getAnyObjectMatchingCond()) ? SearchCond.getLeafCond(anyTypeCond) : SearchCond.getAndCond(SearchCond.getLeafCond(anyTypeCond), SearchCondConverter.convert(this.conf.getAnyObjectMatchingCond()));
            int total = searchDAO.count(SyncopeConstants.FULL_ADMIN_REALMS, cond, AnyTypeKind.ANY_OBJECT);
            int pages = (total / AnyDAO.DEFAULT_PAGE_SIZE) + 1;
            status.set("Processing " + total + " any objects " + anyType.getKey() + " in " + pages + " pages");
            atts.clear();
            atts.addAttribute("", "", "type", ReportXMLConst.XSD_STRING, anyType.getKey());
            atts.addAttribute("", "", "total", ReportXMLConst.XSD_INT, String.valueOf(total));
            handler.startElement("", "", getAnyElementName(AnyTypeKind.ANY_OBJECT) + "s", atts);
            for (int page = 1; page <= pages; page++) {
                status.set("Processing " + total + " any objects " + anyType.getKey() + ": page " + page + " of " + pages);
                doExtract(handler, searchDAO.search(SyncopeConstants.FULL_ADMIN_REALMS, cond, page, PAGE_SIZE, Collections.<OrderByClause>emptyList(), AnyTypeKind.ANY_OBJECT));
            }
            handler.endElement("", "", getAnyElementName(AnyTypeKind.ANY_OBJECT) + "s");
        }
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) AnyTypeCond(org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond) ReconciliationReportletConf(org.apache.syncope.common.lib.report.ReconciliationReportletConf) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType)

Example 52 with SearchCond

use of org.apache.syncope.core.persistence.api.dao.search.SearchCond in project syncope by apache.

the class RealmLogic method delete.

@PreAuthorize("hasRole('" + StandardEntitlement.REALM_DELETE + "')")
public ProvisioningResult<RealmTO> delete(final String fullPath) {
    Realm realm = realmDAO.findByFullPath(fullPath);
    if (realm == null) {
        LOG.error("Could not find realm '" + fullPath + "'");
        throw new NotFoundException(fullPath);
    }
    if (!realmDAO.findChildren(realm).isEmpty()) {
        throw SyncopeClientException.build(ClientExceptionType.HasChildren);
    }
    Set<String> adminRealms = Collections.singleton(realm.getFullPath());
    AnyCond keyCond = new AnyCond(AttributeCond.Type.ISNOTNULL);
    keyCond.setSchema("key");
    SearchCond allMatchingCond = SearchCond.getLeafCond(keyCond);
    int users = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.USER);
    int groups = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.GROUP);
    int anyObjects = searchDAO.count(adminRealms, allMatchingCond, AnyTypeKind.ANY_OBJECT);
    if (users + groups + anyObjects > 0) {
        SyncopeClientException containedAnys = SyncopeClientException.build(ClientExceptionType.AssociatedAnys);
        containedAnys.getElements().add(users + " user(s)");
        containedAnys.getElements().add(groups + " group(s)");
        containedAnys.getElements().add(anyObjects + " anyObject(s)");
        throw containedAnys;
    }
    PropagationByResource propByRes = new PropagationByResource();
    realm.getResourceKeys().forEach(resource -> {
        propByRes.add(ResourceOperation.DELETE, resource);
    });
    List<PropagationTaskTO> tasks = propagationManager.createTasks(realm, propByRes, null);
    PropagationReporter propagationReporter = taskExecutor.execute(tasks, false);
    ProvisioningResult<RealmTO> result = new ProvisioningResult<>();
    result.setEntity(binder.getRealmTO(realm, true));
    result.getPropagationStatuses().addAll(propagationReporter.getStatuses());
    realmDAO.delete(realm);
    return result;
}
Also used : PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) ProvisioningResult(org.apache.syncope.common.lib.to.ProvisioningResult) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) RealmTO(org.apache.syncope.common.lib.to.RealmTO) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) PropagationByResource(org.apache.syncope.core.provisioning.api.PropagationByResource) PropagationReporter(org.apache.syncope.core.provisioning.api.propagation.PropagationReporter) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) Realm(org.apache.syncope.core.persistence.api.entity.Realm) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 53 with SearchCond

use of org.apache.syncope.core.persistence.api.dao.search.SearchCond in project syncope by apache.

the class AbstractAnySearchDAO method buildEffectiveCond.

protected SearchCond buildEffectiveCond(final SearchCond cond, final Set<String> dynRealmKeys) {
    List<SearchCond> effectiveConds = dynRealmKeys.stream().map(dynRealmKey -> {
        DynRealmCond dynRealmCond = new DynRealmCond();
        dynRealmCond.setDynRealm(dynRealmKey);
        return SearchCond.getLeafCond(dynRealmCond);
    }).collect(Collectors.toList());
    effectiveConds.add(cond);
    return SearchCond.getAndCond(effectiveConds);
}
Also used : AnySearchDAO(org.apache.syncope.core.persistence.api.dao.AnySearchDAO) AnyUtilsFactory(org.apache.syncope.core.persistence.api.entity.AnyUtilsFactory) Realm(org.apache.syncope.core.persistence.api.entity.Realm) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) Autowired(org.springframework.beans.factory.annotation.Autowired) ArrayUtils(org.apache.commons.lang3.ArrayUtils) SerializationUtils(org.apache.commons.lang3.SerializationUtils) AssignableCond(org.apache.syncope.core.persistence.api.dao.search.AssignableCond) ArrayList(java.util.ArrayList) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) ClassUtils(org.apache.commons.lang3.ClassUtils) AttributeCond(org.apache.syncope.core.persistence.api.dao.search.AttributeCond) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) Pair(org.apache.commons.lang3.tuple.Pair) AttrSchemaType(org.apache.syncope.common.lib.types.AttrSchemaType) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) JPAPlainSchema(org.apache.syncope.core.persistence.jpa.entity.JPAPlainSchema) Max(javax.validation.constraints.Max) RealmDAO(org.apache.syncope.core.persistence.api.dao.RealmDAO) Method(java.lang.reflect.Method) Triple(org.apache.commons.lang3.tuple.Triple) Entity(javax.persistence.Entity) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) DynRealmDAO(org.apache.syncope.core.persistence.api.dao.DynRealmDAO) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) PlainSchemaDAO(org.apache.syncope.core.persistence.api.dao.PlainSchemaDAO) Set(java.util.Set) Min(javax.validation.constraints.Min) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) Field(java.lang.reflect.Field) Collectors(java.util.stream.Collectors) MemberCond(org.apache.syncope.core.persistence.api.dao.search.MemberCond) DynRealmCond(org.apache.syncope.core.persistence.api.dao.search.DynRealmCond) List(java.util.List) ValidationException(javax.validation.ValidationException) RelationshipCond(org.apache.syncope.core.persistence.api.dao.search.RelationshipCond) ReflectionUtils(org.springframework.util.ReflectionUtils) Annotation(java.lang.annotation.Annotation) PlainSchema(org.apache.syncope.core.persistence.api.entity.PlainSchema) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) MembershipCond(org.apache.syncope.core.persistence.api.dao.search.MembershipCond) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils) Collections(java.util.Collections) Any(org.apache.syncope.core.persistence.api.entity.Any) DynRealmCond(org.apache.syncope.core.persistence.api.dao.search.DynRealmCond) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond)

Example 54 with SearchCond

use of org.apache.syncope.core.persistence.api.dao.search.SearchCond 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)

Example 55 with SearchCond

use of org.apache.syncope.core.persistence.api.dao.search.SearchCond in project syncope by apache.

the class SearchCondVisitor method visitCompount.

private SearchCond visitCompount(final SearchCondition<SearchBean> sc) {
    List<SearchCond> searchConds = new ArrayList<>();
    sc.getSearchConditions().forEach(searchCondition -> {
        searchConds.add(searchCondition.getStatement() == null ? visitCompount(searchCondition) : visitPrimitive(searchCondition));
    });
    SearchCond compound;
    switch(sc.getConditionType()) {
        case AND:
            compound = SearchCond.getAndCond(searchConds);
            break;
        case OR:
            compound = SearchCond.getOrCond(searchConds);
            break;
        default:
            throw new IllegalArgumentException(String.format("Condition type %s is not supported", sc.getConditionType().name()));
    }
    return compound;
}
Also used : ArrayList(java.util.ArrayList) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond)

Aggregations

SearchCond (org.apache.syncope.core.persistence.api.dao.search.SearchCond)74 Test (org.junit.jupiter.api.Test)55 AttributeCond (org.apache.syncope.core.persistence.api.dao.search.AttributeCond)30 AnyCond (org.apache.syncope.core.persistence.api.dao.search.AnyCond)26 AbstractTest (org.apache.syncope.core.persistence.jpa.AbstractTest)25 User (org.apache.syncope.core.persistence.api.entity.user.User)20 UserFiqlSearchConditionBuilder (org.apache.syncope.common.lib.search.UserFiqlSearchConditionBuilder)17 MembershipCond (org.apache.syncope.core.persistence.api.dao.search.MembershipCond)12 OrderByClause (org.apache.syncope.core.persistence.api.dao.search.OrderByClause)11 ArrayList (java.util.ArrayList)10 Group (org.apache.syncope.core.persistence.api.entity.group.Group)10 List (java.util.List)8 AnyTypeCond (org.apache.syncope.core.persistence.api.dao.search.AnyTypeCond)8 AssignableCond (org.apache.syncope.core.persistence.api.dao.search.AssignableCond)7 MemberCond (org.apache.syncope.core.persistence.api.dao.search.MemberCond)7 Collections (java.util.Collections)6 Collectors (java.util.stream.Collectors)6 SyncopeConstants (org.apache.syncope.common.lib.SyncopeConstants)6 RelationshipCond (org.apache.syncope.core.persistence.api.dao.search.RelationshipCond)6 ResourceCond (org.apache.syncope.core.persistence.api.dao.search.ResourceCond)6