private Group getPeerGroupOf(
     Group group, AuthorizableManager authorizableManager, Map<String, Object> toSave)
     throws AccessDeniedException, StorageClientException {
   Group peerGroup = null;
   if (group.hasProperty(UserConstants.PROP_MANAGERS_GROUP)) {
     String managersGroupId = (String) group.getProperty(UserConstants.PROP_MANAGERS_GROUP);
     if (group.getId().equals(managersGroupId)) {
       return group;
     }
     peerGroup = (Group) toSave.get(managersGroupId);
     if (peerGroup == null) {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} Not in toSave List loading Managers Group from store {} ",
             group.getId(),
             managersGroupId);
       }
       peerGroup = (Group) authorizableManager.findAuthorizable(managersGroupId);
     } else {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} got Managers Group from save list {} ", group.getId(), managersGroupId);
       }
     }
   } else if (group.hasProperty(UserConstants.PROP_MANAGED_GROUP)) {
     String managedGroupId = (String) group.getProperty(UserConstants.PROP_MANAGED_GROUP);
     if (group.getId().equals(managedGroupId)) {
       return group;
     }
     peerGroup = (Group) toSave.get(managedGroupId);
     if (peerGroup == null) {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} Not in toSave List loading Managed Group from store {} ",
             group.getId(),
             managedGroupId);
       }
       peerGroup = (Group) authorizableManager.findAuthorizable(managedGroupId);
     } else {
       if (LOGGER.isDebugEnabled()) {
         LOGGER.debug(
             "For {} got Managed Group from save list {} ", group.getId(), managedGroupId);
       }
     }
   }
   return peerGroup;
 }
  /**
   * @param request The request that contains the authorizables.
   * @param group The group that should be modified.
   * @param propertyName The name of the property on the group where the authorizable IDs should be
   *     added/deleted.
   * @param paramName The name of the parameter that contains the authorizable IDs. ie: :manager or
   *     :viewer. If :manager is specified, :manager@Delete will be used for deletes.
   * @param extraPropertyValues An array of authorizable IDs that should be added as well.
   * @param toSave
   * @throws RepositoryException
   */
  protected void handleAuthorizablesOnProperty(
      SlingHttpServletRequest request,
      Group group,
      String propertyName,
      String paramName,
      String[] extraPropertyValues,
      Map<String, Object> toSave) {
    Set<String> propertyValueSet = new HashSet<String>();

    if (group.hasProperty(propertyName)) {
      String[] existingProperties = (String[]) group.getProperty(propertyName);
      for (String property : existingProperties) {
        propertyValueSet.add(property);
      }
    }

    boolean changed = false;

    // Remove all the managers that are in the :manager@Delete request parameter.
    String[] propertiesToDelete =
        request.getParameterValues(paramName + SlingPostConstants.SUFFIX_DELETE);
    if (propertiesToDelete != null) {
      for (String propertyToDelete : propertiesToDelete) {
        propertyValueSet.remove(propertyToDelete);
        changed = true;
      }
    }

    // Add the new ones (if any)
    String[] proeprtiesToAdd = request.getParameterValues(paramName);
    if (proeprtiesToAdd != null) {
      for (String propertyToAdd : proeprtiesToAdd) {
        propertyValueSet.add(propertyToAdd);
        changed = true;
      }
    }

    // Add the extra ones (if any.)
    if (extraPropertyValues != null) {
      for (String propertyValue : extraPropertyValues) {
        propertyValueSet.add(propertyValue);
        changed = true;
      }
    }

    // Write the property.
    if (changed) {
      group.setProperty(
          propertyName, propertyValueSet.toArray(new String[propertyValueSet.size()]));
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Adding to save Queue {} {}", group.getId(), group.getSafeProperties());
      }
      toSave.put(group.getId(), group);
    }
  }