Esempio n. 1
0
  public void updateFile(long companyId, long repositoryId, String fileName, String newFileName)
      throws PortalException, SystemException {

    Session session = null;

    try {
      session = JCRFactoryUtil.createSession();

      Workspace workspace = session.getWorkspace();

      VersionManager versionManager = workspace.getVersionManager();

      Node rootNode = getRootNode(session, companyId);

      Node repositoryNode = getFolderNode(rootNode, repositoryId);

      if (fileName.contains(StringPool.SLASH)) {
        String path = fileName.substring(0, fileName.lastIndexOf(StringPool.SLASH));

        fileName = fileName.substring(path.length() + 1);

        repositoryNode = getFolderNode(repositoryNode, path);
      }

      Node fileNode = repositoryNode.getNode(fileName);

      Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT);

      Node newFileNode = repositoryNode.addNode(newFileName, JCRConstants.NT_FILE);

      Node newContentNode = newFileNode.addNode(JCRConstants.JCR_CONTENT, JCRConstants.NT_RESOURCE);

      VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath());

      String[] versionLabels = versionHistory.getVersionLabels();

      for (int i = (versionLabels.length - 1); i >= 0; i--) {
        Version version = versionHistory.getVersionByLabel(versionLabels[i]);

        Node frozenContentNode = version.getNode(JCRConstants.JCR_FROZEN_NODE);

        if (i == (versionLabels.length - 1)) {
          newContentNode.addMixin(JCRConstants.MIX_VERSIONABLE);
        } else {
          versionManager.checkout(newContentNode.getPath());
        }

        newContentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain");

        copyBinaryProperty(frozenContentNode, newContentNode, JCRConstants.JCR_DATA);

        newContentNode.setProperty(JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance());

        session.save();

        Version newVersion = versionManager.checkin(newContentNode.getPath());

        VersionHistory newVersionHistory =
            versionManager.getVersionHistory(newContentNode.getPath());

        newVersionHistory.addVersionLabel(
            newVersion.getName(), versionLabels[i], PropsValues.DL_STORE_JCR_MOVE_VERSION_LABELS);
      }

      fileNode.remove();

      session.save();
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(fileName);
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }
  }