Ejemplo n.º 1
0
  /**
   * Relink the content data from a new node to an existing node to preserve the version history.
   *
   * @param tempNodeRef temp nodeRef
   * @param nodeToMoveRef NodeRef
   * @param newParentNodeRef NodeRef
   * @param newName new name
   */
  public void relinkNode(
      NodeRef tempNodeRef, NodeRef nodeToMoveRef, NodeRef newParentNodeRef, String newName)
      throws FileNotFoundException, FileExistsException {
    // Get the properties for the old and new nodes
    org.alfresco.service.cmr.model.FileInfo tempFileInfo =
        fileFolderService.getFileInfo(tempNodeRef);
    org.alfresco.service.cmr.model.FileInfo fileToMoveInfo =
        fileFolderService.getFileInfo(nodeToMoveRef);

    // Save the current name of the old node
    String tempName = tempFileInfo.getName();

    try {
      // Rename operation will add or remove the sys:temporary aspect appropriately

      // rename temp file to the new name
      fileFolderService.rename(tempNodeRef, newName);

      // rename new file to old name
      fileFolderService.rename(nodeToMoveRef, tempName);
    } catch (org.alfresco.service.cmr.model.FileNotFoundException e) {
      throw new FileNotFoundException(e.getMessage());
    } catch (org.alfresco.service.cmr.model.FileExistsException e) {
      throw new FileExistsException(e.getMessage());
    }

    if (!tempFileInfo.isFolder() && !fileToMoveInfo.isFolder()) {
      // swap the content between the two
      ContentData oldContentData = tempFileInfo.getContentData();
      if (oldContentData == null) {
        String mimetype = mimetypeService.guessMimetype(tempName);
        oldContentData = ContentData.setMimetype(null, mimetype);
      }

      ContentData newContentData = fileToMoveInfo.getContentData();

      // Reset the mime type
      // TODO Pass the content along when guessing the mime type, so we're more accurate
      String mimetype = mimetypeService.guessMimetype(newName);
      newContentData = ContentData.setMimetype(newContentData, mimetype);

      nodeService.setProperty(tempNodeRef, ContentModel.PROP_CONTENT, newContentData);
      nodeService.setProperty(nodeToMoveRef, ContentModel.PROP_CONTENT, oldContentData);
    }
  }
  public void onCopyComplete(
      QName classRef,
      NodeRef sourceNodeRef,
      NodeRef targetNodeRef,
      boolean copyToNewNode,
      Map<NodeRef, NodeRef> copyMap) {
    Set<NodeRef> workingCopyNodeRefs = TransactionalResourceHelper.getSet(KEY_WORKING_COPIES);
    if (!workingCopyNodeRefs.contains(sourceNodeRef)) {
      // This is not one of the nodes that needs to have discussions copied over
      return;
    }

    // First check that the source node has forums
    NodeRef sourceForumNodeRef = getForum(sourceNodeRef);
    if (sourceForumNodeRef == null) {
      // Missing!  Clean the source node up!
      nodeService.removeAspect(sourceNodeRef, ForumModel.ASPECT_DISCUSSABLE);
      return;
    }

    // The aspect may or may not exist on the target node
    if (!nodeService.hasAspect(targetNodeRef, ForumModel.ASPECT_DISCUSSABLE)) {
      // Add the aspect
      nodeService.addAspect(targetNodeRef, ForumModel.ASPECT_DISCUSSABLE, null);
    }
    // Get the forum node
    NodeRef targetForumNodeRef = getForum(targetNodeRef);
    // Merge the forum topics
    List<ChildAssociationRef> topicAssocRefs =
        nodeService.getChildAssocs(
            sourceForumNodeRef, Collections.singleton(ForumModel.TYPE_TOPIC));
    int copied = 0;
    for (ChildAssociationRef topicAssocRef : topicAssocRefs) {
      NodeRef topicNodeRef = topicAssocRef.getChildRef();
      try {
        // work out the name for the copied topic
        String topicName;
        String topicNodeName =
            nodeService.getProperty(topicNodeRef, ContentModel.PROP_NAME).toString();
        Serializable labelProp =
            nodeService.getProperty(targetNodeRef, ContentModel.PROP_VERSION_LABEL);
        if (labelProp == null) {
          SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");
          topicName = topicNodeName + " - " + dateFormat.format(new Date());
        } else {
          topicName = topicNodeName + " (" + labelProp.toString() + ")";
        }

        if (fileFolderService.searchSimple(targetForumNodeRef, topicName) != null) {
          // A topic with that name already exists
          continue;
        }
        fileFolderService.copy(topicNodeRef, targetForumNodeRef, topicName);
        copied++;
      } catch (FileExistsException e) {
        // We checked for this, so this is a concurrency condition
        throw new ConcurrencyFailureException("Target topic exists: " + e.getMessage(), e);
      } catch (FileNotFoundException e) {
        // The node was there, but now it's gone
        throw new ConcurrencyFailureException("Forum was deleted: " + e.getMessage(), e);
      }
    }
  }