@Override public void assignBundlesToBundleGroups(Subject subject, int bundleGroupIds[], int[] bundleIds) { if (null == bundleGroupIds || null == bundleIds) { return; } for (int bundleGroupId : bundleGroupIds) { BundleGroup bundleGroup = entityManager.find(BundleGroup.class, bundleGroupId); if (null == bundleGroup) { throw new IllegalArgumentException("BundleGroup does not exist for bundleGroupId [" + bundleGroupId + "]"); } checkAssignBundleGroupAuthz(subject, bundleGroupId, bundleIds); for (int bundleId : bundleIds) { Bundle bundle = entityManager.find(Bundle.class, bundleId); if (null == bundle) { throw new IllegalArgumentException("Bundle does not exist for bundleId [" + bundleId + "]"); } bundleGroup.addBundle(bundle); } } }
/** * This also updates the inverse relations * @param bundles */ public void setBundles(Set<Bundle> bundles) { for (Bundle bundle : getBundles()) { bundle.removeBundleGroup(this); } this.bundles.clear(); if (null != bundles) { for (Bundle bundle : bundles) { addBundle(bundle); } } }
@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; }
bundleGroup.addBundle(bundle);