Search in sources :

Example 1 with SearchCond

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

the class JPAGroupDAO method save.

@Override
public Group save(final Group group) {
    Group merged = super.save(group);
    publisher.publishEvent(new AnyCreatedUpdatedEvent<>(this, merged, AuthContextUtils.getDomain()));
    // refresh dynamic memberships
    clearUDynMembers(merged);
    if (merged.getUDynMembership() != null) {
        SearchCond cond = buildDynMembershipCond(merged.getUDynMembership().getFIQLCond(), merged.getRealm());
        int count = searchDAO().count(Collections.<String>singleton(merged.getRealm().getFullPath()), cond, AnyTypeKind.USER);
        for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
            List<User> matching = searchDAO().search(Collections.<String>singleton(merged.getRealm().getFullPath()), cond, page, AnyDAO.DEFAULT_PAGE_SIZE, Collections.<OrderByClause>emptyList(), AnyTypeKind.USER);
            matching.forEach(user -> {
                Query insert = entityManager().createNativeQuery("INSERT INTO " + UDYNMEMB_TABLE + " VALUES(?, ?)");
                insert.setParameter(1, user.getKey());
                insert.setParameter(2, merged.getKey());
                insert.executeUpdate();
                publisher.publishEvent(new AnyCreatedUpdatedEvent<>(this, user, AuthContextUtils.getDomain()));
            });
        }
    }
    clearADynMembers(merged);
    merged.getADynMemberships().stream().forEach(memb -> {
        SearchCond cond = buildDynMembershipCond(memb.getFIQLCond(), merged.getRealm());
        int count = searchDAO().count(Collections.<String>singleton(merged.getRealm().getFullPath()), cond, AnyTypeKind.ANY_OBJECT);
        for (int page = 1; page <= (count / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
            List<AnyObject> matching = searchDAO().search(Collections.<String>singleton(merged.getRealm().getFullPath()), cond, page, AnyDAO.DEFAULT_PAGE_SIZE, Collections.<OrderByClause>emptyList(), AnyTypeKind.ANY_OBJECT);
            matching.forEach(anyObject -> {
                Query insert = entityManager().createNativeQuery("INSERT INTO " + ADYNMEMB_TABLE + " VALUES(?, ?, ?)");
                insert.setParameter(1, anyObject.getType().getKey());
                insert.setParameter(2, anyObject.getKey());
                insert.setParameter(3, merged.getKey());
                insert.executeUpdate();
                publisher.publishEvent(new AnyCreatedUpdatedEvent<>(this, anyObject, AuthContextUtils.getDomain()));
            });
        }
    });
    dynRealmDAO().refreshDynMemberships(merged);
    return merged;
}
Also used : JPAGroup(org.apache.syncope.core.persistence.jpa.entity.group.JPAGroup) Group(org.apache.syncope.core.persistence.api.entity.group.Group) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) User(org.apache.syncope.core.persistence.api.entity.user.User) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond)

Example 2 with SearchCond

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

the class AbstractAnyService method search.

@Override
public PagedResult<TO> search(final AnyQuery anyQuery) {
    String realm = StringUtils.prependIfMissing(anyQuery.getRealm(), SyncopeConstants.ROOT_REALM);
    // if an assignable query is provided in the FIQL string, start anyway from root realm
    boolean isAssignableCond = StringUtils.isBlank(anyQuery.getFiql()) ? false : -1 != anyQuery.getFiql().indexOf(SpecialAttr.ASSIGNABLE.toString());
    SearchCond searchCond = StringUtils.isBlank(anyQuery.getFiql()) ? null : getSearchCond(anyQuery.getFiql(), realm);
    Pair<Integer, List<TO>> result = getAnyLogic().search(searchCond, anyQuery.getPage(), anyQuery.getSize(), getOrderByClauses(anyQuery.getOrderBy()), isAssignableCond ? SyncopeConstants.ROOT_REALM : realm, anyQuery.getDetails());
    return buildPagedResult(result.getRight(), anyQuery.getPage(), anyQuery.getSize(), result.getLeft());
}
Also used : List(java.util.List) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond)

Example 3 with SearchCond

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

the class GroupDataBinderImpl method setDynMembership.

private void setDynMembership(final Group group, final AnyType anyType, final String dynMembershipFIQL) {
    SearchCond dynMembershipCond = SearchCondConverter.convert(dynMembershipFIQL);
    if (!dynMembershipCond.isValid()) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
        sce.getElements().add(dynMembershipFIQL);
        throw sce;
    }
    if (anyType.getKind() == AnyTypeKind.GROUP) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidAnyType);
        sce.getElements().add(anyType.getKind().name());
        throw sce;
    }
    DynGroupMembership<?> dynMembership;
    if (anyType.getKind() == AnyTypeKind.ANY_OBJECT && !group.getADynMembership(anyType).isPresent()) {
        dynMembership = entityFactory.newEntity(ADynGroupMembership.class);
        dynMembership.setGroup(group);
        ((ADynGroupMembership) dynMembership).setAnyType(anyType);
        group.add((ADynGroupMembership) dynMembership);
    } else if (anyType.getKind() == AnyTypeKind.USER && group.getUDynMembership() == null) {
        dynMembership = entityFactory.newEntity(UDynGroupMembership.class);
        dynMembership.setGroup(group);
        group.setUDynMembership((UDynGroupMembership) dynMembership);
    } else {
        dynMembership = anyType.getKind() == AnyTypeKind.ANY_OBJECT ? group.getADynMembership(anyType).get() : group.getUDynMembership();
    }
    dynMembership.setFIQLCond(dynMembershipFIQL);
}
Also used : UDynGroupMembership(org.apache.syncope.core.persistence.api.entity.user.UDynGroupMembership) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) ADynGroupMembership(org.apache.syncope.core.persistence.api.entity.anyobject.ADynGroupMembership)

Example 4 with SearchCond

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

the class DynRealmDataBinderImpl method setDynMembership.

private void setDynMembership(final DynRealm dynRealm, final AnyType anyType, final String dynMembershipFIQL) {
    SearchCond dynMembershipCond = SearchCondConverter.convert(dynMembershipFIQL);
    if (!dynMembershipCond.isValid()) {
        SyncopeClientException sce = SyncopeClientException.build(ClientExceptionType.InvalidSearchExpression);
        sce.getElements().add(dynMembershipFIQL);
        throw sce;
    }
    DynRealmMembership dynMembership;
    if (dynRealm.getDynMembership(anyType).isPresent()) {
        dynMembership = dynRealm.getDynMembership(anyType).get();
    } else {
        dynMembership = entityFactory.newEntity(DynRealmMembership.class);
        dynMembership.setDynRealm(dynRealm);
        dynMembership.setAnyType(anyType);
        dynRealm.add(dynMembership);
    }
    dynMembership.setFIQLCond(dynMembershipFIQL);
}
Also used : SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) DynRealmMembership(org.apache.syncope.core.persistence.api.entity.DynRealmMembership) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond)

Example 5 with SearchCond

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

the class SyncopeLogic method searchAssignableGroups.

@PreAuthorize("isAuthenticated()")
public Pair<Integer, List<GroupTO>> searchAssignableGroups(final String realm, final String term, final int page, final int size) {
    AssignableCond assignableCond = new AssignableCond();
    assignableCond.setRealmFullPath(realm);
    SearchCond searchCond;
    if (StringUtils.isNotBlank(term)) {
        AnyCond termCond = new AnyCond(AttributeCond.Type.ILIKE);
        termCond.setSchema("name");
        String termSearchableValue = (term.startsWith("*") && !term.endsWith("*")) ? term + "%" : (!term.startsWith("*") && term.endsWith("*")) ? "%" + term : (term.startsWith("*") && term.endsWith("*") ? term : "%" + term + "%");
        termCond.setExpression(termSearchableValue);
        searchCond = SearchCond.getAndCond(SearchCond.getLeafCond(assignableCond), SearchCond.getLeafCond(termCond));
    } else {
        searchCond = SearchCond.getLeafCond(assignableCond);
    }
    int count = searchDAO.count(SyncopeConstants.FULL_ADMIN_REALMS, searchCond, AnyTypeKind.GROUP);
    OrderByClause orderByClause = new OrderByClause();
    orderByClause.setField("name");
    orderByClause.setDirection(OrderByClause.Direction.ASC);
    List<Group> matching = searchDAO.search(SyncopeConstants.FULL_ADMIN_REALMS, searchCond, page, size, Collections.singletonList(orderByClause), AnyTypeKind.GROUP);
    List<GroupTO> result = matching.stream().map(group -> groupDataBinder.getGroupTO(group, false)).collect(Collectors.toList());
    return Pair.of(count, result);
}
Also used : Arrays(java.util.Arrays) SecurityQuestionDAO(org.apache.syncope.core.persistence.api.dao.SecurityQuestionDAO) AopUtils(org.springframework.aop.support.AopUtils) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) AnyType(org.apache.syncope.core.persistence.api.entity.AnyType) Autowired(org.springframework.beans.factory.annotation.Autowired) Entity(org.apache.syncope.core.persistence.api.entity.Entity) GroupProvisioningManager(org.apache.syncope.core.provisioning.api.GroupProvisioningManager) PlatformInfo(org.apache.syncope.common.lib.info.PlatformInfo) StringUtils(org.apache.commons.lang3.StringUtils) RoleDAO(org.apache.syncope.core.persistence.api.dao.RoleDAO) AssignableCond(org.apache.syncope.core.persistence.api.dao.search.AssignableCond) InetAddress(java.net.InetAddress) AnyTypeKind(org.apache.syncope.common.lib.types.AnyTypeKind) 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) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) Map(java.util.Map) URI(java.net.URI) AuthContextUtils(org.apache.syncope.core.spring.security.AuthContextUtils) Method(java.lang.reflect.Method) PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) RuntimeMXBean(java.lang.management.RuntimeMXBean) TypeExtension(org.apache.syncope.core.persistence.api.entity.group.TypeExtension) GroupWorkflowAdapter(org.apache.syncope.core.workflow.api.GroupWorkflowAdapter) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) NotificationDAO(org.apache.syncope.core.persistence.api.dao.NotificationDAO) EntitlementsHolder(org.apache.syncope.core.provisioning.api.EntitlementsHolder) Resource(javax.annotation.Resource) EventListener(org.springframework.context.event.EventListener) GroupTO(org.apache.syncope.common.lib.to.GroupTO) Collectors(java.util.stream.Collectors) AnyTypeDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeDAO) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) SystemInfo(org.apache.syncope.common.lib.info.SystemInfo) List(java.util.List) AccountPolicy(org.apache.syncope.core.persistence.api.entity.policy.AccountPolicy) TypeExtensionTO(org.apache.syncope.common.lib.to.TypeExtensionTO) PolicyDAO(org.apache.syncope.core.persistence.api.dao.PolicyDAO) ConnIdBundleManager(org.apache.syncope.core.provisioning.api.ConnIdBundleManager) PropagationTaskExecutor(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor) Group(org.apache.syncope.core.persistence.api.entity.group.Group) Optional(java.util.Optional) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) ConfDAO(org.apache.syncope.core.persistence.api.dao.ConfDAO) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) VirAttrCache(org.apache.syncope.core.provisioning.api.cache.VirAttrCache) AbstractBaseBean(org.apache.syncope.common.lib.AbstractBaseBean) NumbersInfo(org.apache.syncope.common.lib.info.NumbersInfo) AnySearchDAO(org.apache.syncope.core.persistence.api.dao.AnySearchDAO) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) PasswordGenerator(org.apache.syncope.core.spring.security.PasswordGenerator) GroupDataBinder(org.apache.syncope.core.provisioning.api.data.GroupDataBinder) UserWorkflowAdapter(org.apache.syncope.core.workflow.api.UserWorkflowAdapter) TaskDAO(org.apache.syncope.core.persistence.api.dao.TaskDAO) PasswordPolicy(org.apache.syncope.core.persistence.api.entity.policy.PasswordPolicy) ImplementationType(org.apache.syncope.common.lib.types.ImplementationType) ManagementFactory(java.lang.management.ManagementFactory) OperatingSystemMXBean(java.lang.management.OperatingSystemMXBean) AnyObjectProvisioningManager(org.apache.syncope.core.provisioning.api.AnyObjectProvisioningManager) SyncopeConstants(org.apache.syncope.common.lib.SyncopeConstants) Iterator(java.util.Iterator) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) UnknownHostException(java.net.UnknownHostException) ImplementationLookup(org.apache.syncope.core.persistence.api.ImplementationLookup) Component(org.springframework.stereotype.Component) UserProvisioningManager(org.apache.syncope.core.provisioning.api.UserProvisioningManager) JavaImplInfo(org.apache.syncope.common.lib.info.JavaImplInfo) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) Collections(java.util.Collections) AnyTypeClassDAO(org.apache.syncope.core.persistence.api.dao.AnyTypeClassDAO) AnyObjectWorkflowAdapter(org.apache.syncope.core.workflow.api.AnyObjectWorkflowAdapter) TaskType(org.apache.syncope.common.lib.types.TaskType) Transactional(org.springframework.transaction.annotation.Transactional) AssignableCond(org.apache.syncope.core.persistence.api.dao.search.AssignableCond) Group(org.apache.syncope.core.persistence.api.entity.group.Group) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) SearchCond(org.apache.syncope.core.persistence.api.dao.search.SearchCond) AnyCond(org.apache.syncope.core.persistence.api.dao.search.AnyCond) GroupTO(org.apache.syncope.common.lib.to.GroupTO) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

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