@Transactional
  public void createDocument(Document info, String folder, InputStream docData, User user)
      throws IOException {
    boolean created = false;
    File file = null;
    try {
      info.setId(null);
      documentDao.makePersistent(info);

      String uuid = UUID.randomUUID().toString();

      file = new File(docsFolderPath + uuid);
      while (file.exists()) {
        uuid = UUID.randomUUID().toString();
        file = new File(docsFolderPath + uuid);
      }

      created = file.createNewFile();

      info.setUuid(uuid);

      Folder f = folderDao.getFolderByPath(folder, user);

      if (f == null) {
        if (folder != null && !folder.equals("")) {
          f = new Folder();
          f.setPath(folder);
          folderDao.makePersistent(f);
        }
      }

      if (f == null) {
        throw new IOException("Internal Error");
      }

      if (!f.canWrite(user)) throw new InsufficientPrivilagesException();

      f.getDocs().add(info);

      Integer id = info.getId();
      OutputStream out = new FileOutputStream(file);
      ByteUtils.write(docData, out);
      out.flush();
      out.close();
      File fi = new File(docsFolderPath + id);
      info.setSize((int) fi.length());
      documentDao.makePersistent(info);
    } catch (Throwable t) {
      if (created && file != null) {
        file.delete();
      }
      throw new RuntimeException(t);
    }
  }
  @Transactional
  public void deleteDocument(String path, User user) throws InsufficientPrivilagesException {
    String folderPath = path.substring(0, path.lastIndexOf('/'));
    Folder f = getFolderByPath(folderPath, user);
    if (!f.getOwner().getUsername().equals(user.getUsername()) && !f.canWrite(user))
      throw new InsufficientPrivilagesException(
          "User " + user + " does not have permission to delete documents from this folder");

    Document doc = f.getDocumentByName(path.substring(path.lastIndexOf('/') + 1));
    File docFile = new File(docsFolderPath + doc.getUuid());
    f.removeDocument(doc);
    documentDao.makeTransient(doc);
    docFile.delete();
  }
  @Transactional
  public Folder createFolder(String path, User user)
      throws IllegalPathException, FolderAlreadyExistsException, InsufficientPrivilagesException {
    if (path.equals("/")) throw new FolderAlreadyExistsException("Folder: '" + path + "'");

    int lastIndex = path.lastIndexOf('/');

    while (lastIndex == path.length() - 1 && lastIndex > 0) {
      path = path.substring(0, path.length() - 1);
      lastIndex = path.lastIndexOf('/');
    }

    if (lastIndex < 0) {
      throw new IllegalPathException("Illegal Path");
    }

    String parentPath = path.substring(0, lastIndex);
    String lastPart = path.substring(lastIndex + 1);

    Folder parent = getFolderByPath(parentPath, user);

    if (!parent.canWrite(user)) throw new InsufficientPrivilagesException();
    return parent.addSubFolder(lastPart);
  }