public void updateCollaborators( Resource resource, List<User> userList, User apiCaller, String predicate, boolean addToShelf) { Set<ContentPermission> contentPermissions = resource.getContentPermissions(); List<User> newUsers = new ArrayList<User>(); if (contentPermissions == null) { contentPermissions = new HashSet<ContentPermission>(); } if (userList != null && userList.size() > 0) { Date date = new Date(); for (User user : userList) { if (!user.getGooruUId().equals(resource.getUser().getGooruUId())) { boolean newFlag = true; for (ContentPermission contentPermission : contentPermissions) { if (contentPermission.getParty().getPartyUid().equals(user.getPartyUid())) { newFlag = false; break; } } if (newFlag) { ContentPermission contentPerm = new ContentPermission(); contentPerm.setParty(user); contentPerm.setContent(resource); contentPerm.setPermission(EDIT); contentPerm.setValidFrom(date); contentPermissions.add(contentPerm); if (!newUsers.contains(user)) { newUsers.add(user); } if (addToShelf) { this.getShelfService().addCollaboratorShelf(contentPerm); } } } } if (addToShelf) { if (newUsers.size() > 0) { sendMailToCollabrators(newUsers, resource, apiCaller); } } } Set<ContentPermission> removePermissions = new HashSet<ContentPermission>(); for (ContentPermission contentPermission : contentPermissions) { boolean remove = true; if (userList != null) { for (User user : userList) { if (user.getPartyUid().equals(contentPermission.getParty().getPartyUid())) { remove = false; break; } } } if (remove) { removePermissions.add(contentPermission); } } if (removePermissions.size() > 0) { this.getShelfService().removeCollaboratorShelf(removePermissions); contentPermissions.removeAll(removePermissions); this.getBaseRepository().removeAll(removePermissions); } this.getBaseRepository().save(resource); }