/** * Deletes the authorization after removing it from any groups that might be referring to it * * @param em the entity manager reference * @param id the ID for authorizatino entity * @throws PersistenceException if there were any errors deleting the authorization */ static void deleteAuthorization(EntityManager em, long id) throws PersistenceException { Authorization auth = em.find(Authorization.class, id); // may return null, if this entity is not saved, in which // case don't do anything. if (auth == null) { return; } // Iterate through all groups and remove the authorization from them Set<SummaryGroup> groups = auth.getGroups(); if (groups != null) { for (SummaryGroup g : groups) { Group group = new SingleGroupQuery(g.getId()).fetch(); Set<Authorization> auths = group.getAuthorizations(); HashSet<Authorization> updated = new HashSet<Authorization>(); for (Authorization a : auths) { if (a.getId() != auth.getId()) { updated.add(a); } } group.setAuthorizations(updated); group.save(); } } em.remove(em.getReference(Authorization.class, auth.getId())); }