@Override @RequiredPermission(Permission.MANAGE_SECURITY) public void setAssignedBundleGroups(Subject subject, int roleId, int[] bundleGroupIds) { Role role = getRole(subject, roleId); List<Integer> currentBundleGroups = new ArrayList<Integer>(); for (BundleGroup group : role.getBundleGroups()) { currentBundleGroups.add(group.getId()); } List<Integer> newBundleGroups = ArrayUtils.wrapInList(bundleGroupIds); // members needing addition newBundleGroups.removeAll(currentBundleGroups); int[] newBundleGroupIds = ArrayUtils.unwrapCollection(newBundleGroups); roleManager.addBundleGroupsToRole(subject, roleId, newBundleGroupIds); List<Integer> removedBundleGroups = new ArrayList<Integer>(currentBundleGroups); // members needing removal removedBundleGroups.removeAll(ArrayUtils.wrapInList(bundleGroupIds)); int[] removedGroupIds = ArrayUtils.unwrapCollection(removedBundleGroups); roleManager.removeBundleGroupsFromRole(subject, roleId, removedGroupIds); }
@Override public BundleGroup updateBundleGroup(Subject subject, BundleGroup bundleGroup) throws Exception { BundleGroup attachedBundleGroup = entityManager.find(BundleGroup.class, bundleGroup.getId()); if (attachedBundleGroup == null) { throw new IllegalArgumentException("Cannot update " + bundleGroup + ", because no bundle group exists with id [" + bundleGroup.getId() + "]."); } // First update the simple fields and the permissions. attachedBundleGroup.setName(bundleGroup.getName()); attachedBundleGroup.setDescription(bundleGroup.getDescription()); Set<Bundle> newBundles = bundleGroup.getBundles(); if (newBundles != null) { // wrap in new HashSet to avoid ConcurrentModificationExceptions. Set<Bundle> currentBundles = attachedBundleGroup.getBundles(); Set<Bundle> BundlesToRemove = new HashSet<Bundle>(currentBundles); for (Bundle bg : newBundles) { BundlesToRemove.remove(bg); } for (Bundle bg : BundlesToRemove) { attachedBundleGroup.removeBundle(bg); } for (Bundle bg : newBundles) { Bundle attachedBundle = entityManager.find(Bundle.class, bg.getId()); attachedBundleGroup.addBundle(attachedBundle); } } // Fetch the lazy Set for the return attachedBundleGroup.getBundles().size(); return attachedBundleGroup; }
@Override @RequiredPermission(Permission.MANAGE_BUNDLE_GROUPS) public BundleGroup createBundleGroup(Subject subject, BundleGroup bundleGroup) throws Exception { String name = bundleGroup.getName(); if (null == name || "".equals(name.trim())) { throw new IllegalArgumentException("Invalid bundleGroupName: " + name); } BundleGroupCriteria c = new BundleGroupCriteria(); c.addFilterName(name); c.setStrict(true); if (!bundleManager.findBundleGroupsByCriteria(subject, c).isEmpty()) { throw new IllegalArgumentException("Invalid bundleGroupName, bundle group already exists with name: " + name); } entityManager.persist(bundleGroup); Set<Bundle> bundles = bundleGroup.getBundles(); if (null != bundles) { int[] bundleIds = new int[bundles.size()]; int i = 0; for (Bundle b : bundles) { bundleIds[i++] = b.getId(); } assignBundlesToBundleGroups(subject, new int[] { bundleGroup.getId() }, bundleIds); } return bundleGroup; }
BundleGroup attachedBundleGroup = entityManager.find(BundleGroup.class, bg.getId()); attachedRole.addBundleGroup(attachedBundleGroup);