删除时删除副本

时间:2018-02-28 19:27:10

标签: java alfresco

我正在尝试设置自定义规则操作,以便在删除或移动节点时删除它们的副本。

基本上,我们希望有一个规则,当项目进入文件夹或更新时,副本将被复制到另一个文件夹。这样可以正常工作,但是当项目移出文件夹或删除时,我们还需要它反向执行。

由于不存在规则操作,我正在尝试(很差)自己完成任务,并且当项目移出文件夹时它似乎正常运行,但如果项目已删除。

以下是我的执行者:

package ca.work.action.executer;

import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

import org.alfresco.repo.action.executer.ActionExecuterAbstractBase;
import org.alfresco.model.ContentModel;
import org.alfresco.query.PagingRequest;
import org.alfresco.query.PagingResults;
import org.alfresco.repo.action.ParameterDefinitionImpl;
import org.alfresco.repo.action.executer.MoveActionExecuter;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ParameterDefinition;
import org.alfresco.service.cmr.coci.CheckOutCheckInService;
import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.CopyService.CopyInfo;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.rule.RuleServiceException;
import org.alfresco.service.namespace.QName;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Delete Copy action executor.
 * <p>
 * Deletes any copies the actioned upon node has at a specified location.
 */
public class DeleteCopyActionExecuter extends ActionExecuterAbstractBase
{
    public static final String NAME = "delete copy";

    public static final String ERR_MULTIPLE = "Unable to delete copy because more than one have been found.";
    public static final String PARAM_DESTINATION_FOLDER = "destination-folder";

    private CopyService copyService;

    /**
     * The node service
     */
    private NodeService nodeService;
    private CheckOutCheckInService checkOutCheckInService;

    /**
     * Sets the node service
     */
    public void setNodeService(NodeService nodeService)
    {
        this.nodeService = nodeService;
    }

    /**
     * Sets the copy service
     */
    public void setCopyService(CopyService copyService)
    {
        this.copyService = copyService;
    }

    /**
     * Service to determine check-in or check-out status
     */
    public void setCheckOutCheckInService(CheckOutCheckInService checkOutCheckInService)
    {
        this.checkOutCheckInService = checkOutCheckInService;
    }

    @Override
    protected void addParameterDefinitions(List<ParameterDefinition> paramList)
    {
        paramList.add(new ParameterDefinitionImpl(
                PARAM_DESTINATION_FOLDER,
                DataTypeDefinition.NODE_REF,
                true,
                getParamDisplayLabel(PARAM_DESTINATION_FOLDER)
        ));
    }

    @Override
    public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef)
    {
        NodeRef destinationParent = (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);

        // Since we are deleting we need to figure out whether the destination node exists
        NodeRef copyNodeRef = null;

        // Try and find copies of the actioned upon node reference.
        // Include the parent folder because that's where the copy will be if this action
        // had done the first copy.
        PagingResults<CopyInfo> copies = this.copyService.getCopies(
                actionedUponNodeRef,
                destinationParent,
                new PagingRequest(1000));

        for (CopyInfo copyInfo : copies.getPage()) {
            NodeRef copy = copyInfo.getNodeRef();
            // We know that it is in the destination parent, but avoid working copies
            if (this.checkOutCheckInService.isWorkingCopy(copy)) {
                continue;
            }

            if (copyNodeRef == null) {
                copyNodeRef = copy;
            } else {
                throw new RuleServiceException(ERR_MULTIPLE);
            }
        }

        if (copyNodeRef != null) {
            this.nodeService.deleteNode(copyNodeRef);
        }
    }
}

0 个答案:

没有答案