protected void remove() {
   getSlash();
   for (User user : super.getUsersSet()) {
     this.removeUsers(user.getUsername());
     user.remove();
   }
   deleteDomainObject();
 }
 @Override
 public void removeUsers(User user) {
   if (user.isRoot() || user.isGuest()) {
     throw new IllegalRemovalException("You can't remove " + user.getName());
   } else {
     super.removeUsers(user);
   }
 }
 protected void removeUsers(String username) throws IllegalRemovalException, UserUnknownException {
   hasUser(username);
   if (username.equals(ROOT_USER)) throw new IllegalRemovalException(username);
   else {
     User toRemove = getUserByUsername(username);
     toRemove.remove();
     super.removeUsers(toRemove);
   }
 }
 @Override
 public void addUsers(User user) throws UserAlreadyExistsException, PasswordIsTooWeakException {
   try {
     hasUser(user.getUsername());
   } catch (UserUnknownException e) {
     super.addUsers(user);
     return;
   }
   throw new UserAlreadyExistsException(user.getUsername());
 }
 public void addUsers(String username)
     throws UserAlreadyExistsException, InvalidUsernameException, PasswordIsTooWeakException {
   try {
     hasUser(username);
   } catch (UserUnknownException e) {
     User toCreate = new User(username, this);
     Directory homeDirectory =
         new Directory(
             this.generateUniqueId(), username, toCreate.getUmask(), toCreate, getHomeDirectory());
     this.addDirectoryToHome(homeDirectory);
     toCreate.setHomeDirectory(homeDirectory);
     super.addUsers(toCreate);
     return;
   }
   throw new UserAlreadyExistsException(username);
 }
  Element xmlExport(Element el) {

    for (User usr : getUsersSet()) {
      if (!usr.isRoot()) el.addContent(usr.xmlExport());
    }
    ArrayList<File> allfiles = getSlash().getAllFiles();

    /* Remove files that were created by users */
    for (File f : getHomeDirectory().getFilesSet()) {
      for (User usr : getUsersSet()) {
        if (f.getOwner().equals(usr)) allfiles.remove(f);
      }
    }
    Collections.reverse(allfiles);
    for (File file : allfiles) {
      if (file.getId() >= 3) el.addContent(file.xmlExport());
    }
    return el;
  }
  void createDirectory(String path, Directory currentDirectory, User currentUser)
      throws InvalidFileNameException, FileAlreadyExistsException, InvalidMaskException,
          FileUnknownException {

    Directory beforeLast = absolutePath(path, currentUser, currentDirectory);
    beforeLast.addFile(
        new Directory(
            this.generateUniqueId(),
            getLastPathToken(path),
            currentUser.getUmask(),
            currentUser,
            beforeLast));
  }
  void createPlainFile(String path, Directory currentDirectory, User currentUser)
      throws InvalidFileNameException, InvalidMaskException, FileAlreadyExistsException {

    Directory directory = absolutePath(path, currentUser, currentDirectory);
    String filename = getLastPathToken(path);
    directory.checkAccessWrite(currentUser);

    for (File f : directory.getFilesSet()) {
      if (f.getFilename().equals(filename)) throw new FileAlreadyExistsException(filename);
    }
    directory.addFile(
        new PlainFile(
            this.generateUniqueId(), filename, currentUser.getUmask(), currentUser, directory));
  }
  private void xmlImportUser(List<Element> user)
      throws ImportDocumentException, PasswordIsTooWeakException {
    for (Element node : user) {
      String username = node.getAttributeValue("username");
      User toInsert;

      try {
        this.getUserByUsername(username);
      } catch (UserAlreadyExistsException e) {
        e.getMessage();
        throw new ImportDocumentException();
      } catch (UserUnknownException e) {
        toInsert = new User(username, this);

        if (node.getChild("password") != null)
          toInsert.setPassword(node.getChild("password").getValue());

        if (node.getChild("name") != null) toInsert.setName(node.getChild("name").getValue());

        if (node.getChild("mask") != null) toInsert.setUmask(node.getChild("mask").getValue());

        if (node.getChild("home") != null) {
          String[] tokens = node.getChild("home").getValue().split(PATH_DELIM);
          toInsert.setHomeDirectory(
              createDir(
                  createPath(node.getChild("home").getValue()),
                  tokens[tokens.length - 1],
                  toInsert));
        } else {
          Directory homeDirectory =
              new Directory(
                  this.generateUniqueId(),
                  username,
                  toInsert.getUmask(),
                  toInsert,
                  getHomeDirectory());
          toInsert.setHomeDirectory(homeDirectory);
          this.addDirectoryToHome(homeDirectory);
        }
        super.addUsers(toInsert);
      }
    }
  }
  private Directory createDir(Directory current, String name, User user)
      throws ImportDocumentException {
    Directory next = null;

    try {
      File file = current.getFileByName(name);
      file.isCdAble();
      next = (Directory) current.getFileByName(name);
    } catch (FileUnknownException e) {
      /* This exception should not occur it only exists to protect the method against
       * bad programming
       */
      log.trace(e.getMessage());
      e.printStackTrace();
    } catch (IsNotDirectoryException e) {
      throw new ImportDocumentException();
    } finally {
      if (next == null) {
        next = new Directory(this.generateUniqueId(), name, user.getUmask(), user, current);
        current.addFile(next);
      }
    }
    return next;
  }
 private void checkUserPass(User user, String password) throws WrongPasswordException {
   if (!user.getPassword().equals(password)) throw new WrongPasswordException();
 }
 public User getUserByUsername(String username) throws UserUnknownException {
   for (User user : super.getUsersSet()) {
     if (user.getUsername().equals(username)) return user;
   }
   throw new UserUnknownException(username);
 }