/** * Create a GroupMembership linking this profile to the given group, within the given * organisation. Is immediately saved * * <p>If a duplicate membership exists an exception is thrown * * <p>Check for existing membership by calling getGroupMembership(group, hasGruopInOrg, session) * * @param g * @param hasGroupInOrg * @param session * @return */ public GroupMembership createGroupMembership( Group g, Organisation hasGroupInOrg, Session session) { GroupMembership gm = getGroupMembership(g, hasGroupInOrg, session); if (gm != null) { return gm; } gm = new GroupMembership(); gm.setCreatedDate(new Date()); gm.setGroupEntity(g); gm.setMember(this); gm.setWithinOrg(hasGroupInOrg); gm.setModifiedDate(new Date()); session.save(gm); if (g.getGroupMemberships() == null) { g.setGroupMemberships(new ArrayList<GroupMembership>()); } g.getGroupMemberships().add(gm); // Need to create a subordinate record for each parent organisation Organisation subordinateTo = hasGroupInOrg; while (subordinateTo != null) { createSubordinate(subordinateTo, gm, session); subordinateTo = subordinateTo.getOrganisation(); } if (getMemberships() == null) { setMemberships(new ArrayList<GroupMembership>()); } getMemberships().add(gm); return gm; }
@Override public void delete(Session session) { if (getMemberships() != null) { for (GroupMembership m : getMemberships()) { session.delete(m); } setMemberships(null); } super.delete(session); }