public void delete() {
   for (Principal memberOfPrincipal : getMemberships()) {
     memberOfPrincipal.removeMember(this);
   }
   for (Relationship accessRel :
       getUnderlyingNode().getRelationships(SecurityRelationshipTypes.ACCESS)) {
     accessRel.delete();
   }
   deleteRootRelIfAny();
   getUnderlyingNode().delete();
 }
 public void addMember(Principal principal) {
   // Add the principal as member in this principal if it isn't
   // already a member.
   if (findMemberRelationshipTo(principal) != null) {
     return;
   }
   principal
       .getUnderlyingNode()
       .createRelationshipTo(getUnderlyingNode(), SecurityRelationshipTypes.MEMBER);
   principal.deleteRootRelIfAny();
 }
 public void removeMember(Principal principal) {
   Relationship rel = findMemberRelationshipTo(principal);
   if (rel == null) {
     throw new IllegalArgumentException(principal + " not a member of " + this);
   }
   rel.delete();
   if (!principal.getMemberships().iterator().hasNext()) {
     getGraphDb()
         .getReferenceNode()
         .createRelationshipTo(principal.getUnderlyingNode(), SecurityRelationshipTypes.PRINCIPAL);
   }
 }
 private Relationship findMemberRelationshipTo(Principal principal) {
   for (Relationship rel :
       principal
           .getUnderlyingNode()
           .getRelationships(SecurityRelationshipTypes.MEMBER, Direction.OUTGOING)) {
     if (rel.getEndNode().equals(getUnderlyingNode())) {
       return rel;
     }
   }
   return null;
 }