use of org.apache.syncope.core.persistence.api.entity.group.Group in project syncope by apache.
the class DefaultGroupWorkflowAdapter method doCreate.
@Override
protected WorkflowResult<String> doCreate(final GroupTO groupTO) {
Group group = entityFactory.newEntity(Group.class);
dataBinder.create(group, groupTO);
group = groupDAO.save(group);
PropagationByResource propByRes = new PropagationByResource();
propByRes.set(ResourceOperation.CREATE, groupDAO.findAllResourceKeys(group.getKey()));
return new WorkflowResult<>(group.getKey(), propByRes, "create");
}
use of org.apache.syncope.core.persistence.api.entity.group.Group 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.entity.group.Group in project syncope by apache.
the class JPAGroupDAO method removeDynMemberships.
@Override
public Set<String> removeDynMemberships(final User user) {
List<Group> dynGroups = userDAO().findDynGroups(user.getKey());
Query delete = entityManager().createNativeQuery("DELETE FROM " + UDYNMEMB_TABLE + " WHERE any_id=?");
delete.setParameter(1, user.getKey());
delete.executeUpdate();
Set<String> before = new HashSet<>();
dynGroups.forEach(group -> {
before.add(group.getKey());
publisher.publishEvent(new AnyCreatedUpdatedEvent<>(this, group, AuthContextUtils.getDomain()));
});
return before;
}
use of org.apache.syncope.core.persistence.api.entity.group.Group in project syncope by apache.
the class JPAGroupDAO method delete.
@Override
public void delete(final Group group) {
dynRealmDAO().removeDynMemberships(group.getKey());
findAMemberships(group).forEach(membership -> {
AnyObject leftEnd = membership.getLeftEnd();
leftEnd.getMemberships().remove(membership);
membership.setRightEnd(null);
leftEnd.getPlainAttrs(membership).stream().map(attr -> {
leftEnd.remove(attr);
attr.setOwner(null);
attr.setMembership(null);
return attr;
}).forEachOrdered(attr -> plainAttrDAO.delete(attr));
anyObjectDAO().save(leftEnd);
publisher.publishEvent(new AnyCreatedUpdatedEvent<>(this, leftEnd, AuthContextUtils.getDomain()));
});
findUMemberships(group).forEach(membership -> {
User leftEnd = membership.getLeftEnd();
leftEnd.getMemberships().remove(membership);
membership.setRightEnd(null);
leftEnd.getPlainAttrs(membership).stream().map(attr -> {
leftEnd.remove(attr);
attr.setOwner(null);
attr.setMembership(null);
return attr;
}).forEachOrdered(attr -> plainAttrDAO.delete(attr));
userDAO().save(leftEnd);
publisher.publishEvent(new AnyCreatedUpdatedEvent<>(this, leftEnd, AuthContextUtils.getDomain()));
});
clearUDynMembers(group);
clearADynMembers(group);
entityManager().remove(group);
publisher.publishEvent(new AnyDeletedEvent(this, AnyTypeKind.GROUP, group.getKey(), AuthContextUtils.getDomain()));
}
use of org.apache.syncope.core.persistence.api.entity.group.Group in project syncope by apache.
the class JPAGroupDAO method findUDynMembers.
@Override
@SuppressWarnings("unchecked")
public List<String> findUDynMembers(final Group group) {
if (group.getUDynMembership() == null) {
return Collections.emptyList();
}
Query query = entityManager().createNativeQuery("SELECT any_id FROM " + UDYNMEMB_TABLE + " WHERE group_id=?");
query.setParameter(1, group.getKey());
List<String> result = new ArrayList<>();
query.getResultList().stream().map(key -> key instanceof Object[] ? (String) ((Object[]) key)[0] : ((String) key)).forEachOrdered(actualKey -> result.add(actualKey.toString()));
return result;
}
Aggregations