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;
}
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());
}
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);
}
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);
}
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);
}
Aggregations