Ejemplo n.º 1
0
  @Override
  public void updateGroup(PDPGroup group) throws PAPException {
    if (group == null || group.getId() == null) {
      throw new PAPException("Group or id is null");
    }
    if (group.getName() == null || group.getName().trim().length() == 0) {
      throw new PAPException("New name for group cannot be null or blank");
    }
    StdPDPGroup existingGroup = (StdPDPGroup) getGroup(group.getId());
    if (existingGroup == null) {
      throw new PAPException("Update found no existing group with id '" + group.getId() + "'");
    }

    // We do dramatically different things when the Name changes
    // because the Name is essentially the identity of the group (as the User knows it) so when the
    // Identity changes we have to change the group ID.
    if (group.getName().equals(existingGroup.getName())) {

      // update the disk
      try {
        ((StdPDPGroup) group).saveGroupConfiguration();
      } catch (IOException e) {
        throw new PAPException(
            "Unable to save new configuration for '" + group.getName() + "': " + e.getMessage());
      }
      // update the group in the set by simply replacing the old instance with the new one
      this.groups.remove(existingGroup);
      this.groups.add((StdPDPGroup) group);

    } else {
      // the name/identity of the group has changed
      // generate the new id
      String newId = createNewPDPGroupId(group.getName());

      // make sure no other group uses the new id
      for (PDPGroup g : groups) {
        if (g.getId().equals(newId)) {
          throw new PAPException(
              "Replacement name maps to ID '" + newId + "' which is already in use");
        }
      }
      ((StdPDPGroup) group).setId(newId);

      // rename the existing directory to the new id
      Path oldPath = existingGroup.getDirectory();
      Path newPath = Paths.get(oldPath.getParent().toString(), newId);
      ((StdPDPGroup) group).setDirectory(newPath);

      try {
        boolean success = oldPath.toFile().renameTo(newPath.toFile());
        if (!success) {
          throw new PAPException("Unable to rename directory; reason unknown");
        }
      } catch (Exception e) {
        logger.error("Move '" + oldPath + "' to '" + newPath + "': " + e.getMessage(), e);
        throw new PAPException(
            "Unable to move directory from '"
                + oldPath
                + "' to '"
                + newPath
                + "': "
                + e.getMessage());
      }
      // update the disk
      try {
        ((StdPDPGroup) group).saveGroupConfiguration();
      } catch (IOException e) {
        throw new PAPException(
            "Unable to save new configuration for '" + group.getName() + "': " + e.getMessage());
      }

      // save the new group into the Set
      groups.remove(existingGroup);
      groups.add((StdPDPGroup) group);
    }

    // perhaps only the group changed, but if the name/id changed it may look to a listener like
    // more than
    // one group
    changed();
  }