コード例 #1
0
  @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);
    }
  }