Search in sources :

Example 1 with CopyInfo

use of org.alfresco.service.cmr.repository.CopyService.CopyInfo in project alfresco-repository by Alfresco.

the class TransformActionExecuter method executeImpl.

/**
 * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(Action, org.alfresco.service.cmr.repository.NodeRef)
 */
@Override
protected void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
    if (this.nodeService.exists(actionedUponNodeRef) == false) {
        // node doesn't exist - can't do anything
        return;
    }
    // First check that the node is a sub-type of content
    QName typeQName = this.nodeService.getType(actionedUponNodeRef);
    if (this.dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false) {
        // it is not content, so can't transform
        return;
    }
    // Get the mime type
    String mimeType = (String) ruleAction.getParameterValue(PARAM_MIME_TYPE);
    // Get the content reader
    ContentReader contentReader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
    if (null == contentReader || !contentReader.exists()) {
        throw new RuleServiceException(CONTENT_READER_NOT_FOUND_MESSAGE);
    }
    TransformationOptions transformationOptions = newTransformationOptions(ruleAction, actionedUponNodeRef);
    // getExecuteAsynchronously() is not true for async convert content rules, so using Thread name
    // options.setUse(ruleAction.getExecuteAsynchronously() ? "asyncRule" :"syncRule");
    transformationOptions.setUse(Thread.currentThread().getName().contains("Async") ? "asyncRule" : "syncRule");
    String sourceMimetype = contentReader.getMimetype();
    long sourceSizeInBytes = contentReader.getSize();
    String contentUrl = contentReader.getContentUrl();
    Map<String, String> options = converter.getOptions(transformationOptions, sourceMimetype, mimeType);
    if (!synchronousTransformClient.isSupported(sourceMimetype, sourceSizeInBytes, contentUrl, mimeType, options, null, actionedUponNodeRef)) {
        String optionsString = TransformerDebug.toString(options);
        throw new RuleServiceException(String.format(TRANSFORMER_NOT_EXISTS_MESSAGE_PATTERN, sourceMimetype, mimeType, optionsString));
    }
    // Get the details of the copy destination
    NodeRef destinationParent = (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
    QName destinationAssocTypeQName = (QName) ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME);
    QName destinationAssocQName = (QName) ruleAction.getParameterValue(PARAM_ASSOC_QNAME);
    // default the assoc params if they're not present
    if (destinationAssocTypeQName == null) {
        destinationAssocTypeQName = ContentModel.ASSOC_CONTAINS;
    }
    if (destinationAssocQName == null) {
        destinationAssocQName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "copy");
    }
    // Get the overwirte value
    boolean overwrite = true;
    Boolean overwriteValue = (Boolean) ruleAction.getParameterValue(PARAM_OVERWRITE_COPY);
    if (overwriteValue != null) {
        overwrite = overwriteValue.booleanValue();
    }
    // Calculate the destination name
    String originalName = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
    String newName = transformName(this.mimetypeService, originalName, mimeType, true);
    // Since we are overwriting we need to figure out whether the destination node exists
    NodeRef copyNodeRef = null;
    if (overwrite == true) {
        // 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 = copyService.getCopies(actionedUponNodeRef, destinationParent, new PagingRequest(1000));
        for (CopyInfo copyInfo : copies.getPage()) {
            NodeRef copy = copyInfo.getNodeRef();
            String copyName = copyInfo.getName();
            // We know that it is in the destination parent, but avoid working copies
            if (checkOutCheckInService.isWorkingCopy(copy)) {
                // It is a working copy
                continue;
            } else if (!newName.equals(copyName)) {
                // The copy's name is not what this action would have set it to
                continue;
            }
            if (copyNodeRef == null) {
                copyNodeRef = copy;
            } else {
                throw new RuleServiceException(ERR_OVERWRITE);
            }
        }
    }
    if (copyNodeRef == null) {
        // Copy the content node
        copyNodeRef = this.copyService.copy(actionedUponNodeRef, destinationParent, destinationAssocTypeQName, QName.createQName(destinationAssocQName.getNamespaceURI(), newName));
        // Adjust the name of the copy
        nodeService.setProperty(copyNodeRef, ContentModel.PROP_NAME, newName);
        String originalTitle = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_TITLE);
        if (originalTitle != null) {
            nodeService.setProperty(copyNodeRef, ContentModel.PROP_TITLE, originalTitle);
        }
    }
    // Only do the transformation if some content is available
    if (contentReader != null) {
        // get the writer and set it up
        ContentWriter contentWriter = this.contentService.getWriter(copyNodeRef, ContentModel.PROP_CONTENT, true);
        // new mimetype
        contentWriter.setMimetype(mimeType);
        // original encoding
        contentWriter.setEncoding(contentReader.getEncoding());
        // TODO: Check failure patterns for actions.
        try {
            doTransform(ruleAction, actionedUponNodeRef, contentReader, copyNodeRef, contentWriter);
            ruleAction.setParameterValue(PARAM_RESULT, copyNodeRef);
        } catch (NoTransformerException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("No transformer found to execute rule: \n" + "   reader: " + contentReader + "\n" + "   writer: " + contentWriter + "\n" + "   action: " + this);
            }
            throw new RuleServiceException(TRANSFORMING_ERROR_MESSAGE + e.getMessage());
        }
    }
}
Also used : RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) QName(org.alfresco.service.namespace.QName) ContentReader(org.alfresco.service.cmr.repository.ContentReader) CopyInfo(org.alfresco.service.cmr.repository.CopyService.CopyInfo) PagingRequest(org.alfresco.query.PagingRequest) NoTransformerException(org.alfresco.service.cmr.repository.NoTransformerException) TransformationOptions(org.alfresco.service.cmr.repository.TransformationOptions) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter)

Example 2 with CopyInfo

use of org.alfresco.service.cmr.repository.CopyService.CopyInfo in project alfresco-repository by Alfresco.

the class CopyServiceImplTest method testCopiedFromAspect.

public void testCopiedFromAspect() {
    IntegrityChecker integrityChecker = (IntegrityChecker) ctx.getBean("integrityChecker");
    // Create the node used for copying
    ChildAssociationRef childAssocRef = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}test"), TEST_TYPE_QNAME, createTypePropertyBag());
    NodeRef nodeRef = childAssocRef.getChildRef();
    PagingRequest pageRequest = new PagingRequest(10);
    pageRequest.setRequestTotalCountMax(200);
    PagingResults<CopyInfo> copies = null;
    NodeRef firstCopy = null;
    NodeRef secondCopy = null;
    for (int i = 1; i <= 100; i++) {
        NodeRef copyNodeRef = copyService.copy(nodeRef, rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}copyAssoc"));
        if (firstCopy == null) {
            firstCopy = copyNodeRef;
        } else if (secondCopy == null) {
            secondCopy = copyNodeRef;
        }
        copies = copyService.getCopies(nodeRef, pageRequest);
        assertEquals("Total count not correct", new Pair<Integer, Integer>(i, i), copies.getTotalResultCount());
        assertEquals("Incorrect number of copies", (i > 10 ? 10 : i), copies.getPage().size());
        // Since the results are paged, make sure that we have the correct results while we only have a page
        boolean found = (i > 10) ? true : false;
        for (CopyInfo copy : copies.getPage()) {
            if (// Might not be checking if we are over a page
            found) {
                break;
            }
            if (copy.getNodeRef().equals(copyNodeRef)) {
                found = true;
            }
        }
        assertTrue("Did not find the copy in the list of copies.", found);
        // Run integrity checks to ensure that commit has a chance
        integrityChecker.checkIntegrity();
        // Now query for copies in current parent location
        copies = copyService.getCopies(nodeRef, rootNodeRef, pageRequest);
        assertEquals("Total count not correct", new Pair<Integer, Integer>(i, i), copies.getTotalResultCount());
        assertEquals("Incorrect number of copies", (i > 10 ? 10 : i), copies.getPage().size());
        // Check that the original node can be retrieved
        NodeRef originalCheck = copyService.getOriginal(copyNodeRef);
        assertEquals("Original is not as expected. ", nodeRef, originalCheck);
        // Check that the parent node can be included
        copies = copyService.getCopies(nodeRef, rootNodeRef, pageRequest);
        assertEquals("Total count not correct", new Pair<Integer, Integer>(i, i), copies.getTotalResultCount());
        assertEquals("Incorrect number of copies", (i > 10 ? 10 : i), copies.getPage().size());
        // And query against some other parent node
        // Some arbitrary parent
        copies = copyService.getCopies(nodeRef, sourceNodeRef, pageRequest);
        assertEquals("Expected to find no copies", 0, copies.getPage().size());
    }
    // Should be able to delete the original
    nodeService.deleteNode(nodeRef);
    // Run integrity checks to ensure that commit has a chance
    integrityChecker.checkIntegrity();
    // Should be no original
    NodeRef originalCheck = copyService.getOriginal(firstCopy);
    assertNull("Original should not be present. ", originalCheck);
    assertFalse("Copy should not have cm:copiedfrom aspect. ", nodeService.hasAspect(firstCopy, ContentModel.ASPECT_COPIEDFROM));
}
Also used : IntegrityChecker(org.alfresco.repo.node.integrity.IntegrityChecker) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) PagingRequest(org.alfresco.query.PagingRequest) CopyInfo(org.alfresco.service.cmr.repository.CopyService.CopyInfo)

Example 3 with CopyInfo

use of org.alfresco.service.cmr.repository.CopyService.CopyInfo in project alfresco-repository by Alfresco.

the class CopyServiceImplTest method testCopyOfCopyOfCopy.

/**
 * <a href="https://issues.alfresco.com/jira/browse/MNT-9580">
 *      MNT-9580: Daisy chained cm:original associations are cascade-deleted when the first original is deleted
 * </a>
 */
public void testCopyOfCopyOfCopy() {
    IntegrityChecker integrityChecker = (IntegrityChecker) ctx.getBean("integrityChecker");
    // Create the node used for copying
    ChildAssociationRef childAssocRef = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}test"), TEST_TYPE_QNAME, createTypePropertyBag());
    NodeRef nodeRef = childAssocRef.getChildRef();
    PagingRequest pageRequest = new PagingRequest(10);
    pageRequest.setRequestTotalCountMax(200);
    PagingResults<CopyInfo> copies = null;
    NodeRef currentOriginal = nodeRef;
    NodeRef copyNodeRef = null;
    for (int i = 1; i <= 5; i++) {
        copyNodeRef = copyService.copy(currentOriginal, rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}copyAssoc" + i));
        copies = copyService.getCopies(currentOriginal, pageRequest);
        assertEquals("Incorrect number of copies on iteration " + i, 1, copies.getPage().size());
        // Check that the original node can be retrieved
        NodeRef originalCheck = copyService.getOriginal(copyNodeRef);
        assertEquals("Original is not as expected. ", currentOriginal, originalCheck);
        // Run integrity checks to ensure that commit has a chance
        integrityChecker.checkIntegrity();
        currentOriginal = copyNodeRef;
    }
    // Now, delete the nodes starting with the first original
    currentOriginal = nodeRef;
    copyNodeRef = null;
    for (int i = 1; i < 5; i++) {
        // Each node must be an original
        copies = copyService.getCopies(currentOriginal, pageRequest);
        assertEquals("Incorrect number of copies on iteration " + i, 1, copies.getPage().size());
        copyNodeRef = copies.getPage().get(0).getNodeRef();
        // Delete current original
        nodeService.deleteNode(currentOriginal);
        // Run integrity checks to ensure that commit has a chance
        integrityChecker.checkIntegrity();
        currentOriginal = copyNodeRef;
    }
}
Also used : IntegrityChecker(org.alfresco.repo.node.integrity.IntegrityChecker) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) PagingRequest(org.alfresco.query.PagingRequest) CopyInfo(org.alfresco.service.cmr.repository.CopyService.CopyInfo)

Example 4 with CopyInfo

use of org.alfresco.service.cmr.repository.CopyService.CopyInfo in project alfresco-repository by Alfresco.

the class CopyServiceImplTest method testCopyToNewNode.

/**
 * Test copy new node within store
 */
public void testCopyToNewNode() {
    PagingRequest pageRequest = new PagingRequest(10);
    PagingResults<CopyInfo> copies = null;
    // Check that the node has no copies
    copies = copyService.getCopies(sourceNodeRef, pageRequest);
    assertEquals("Incorrect number of copies", 0, copies.getPage().size());
    // Copy to new node without copying children
    NodeRef copy = copyService.copy(sourceNodeRef, rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}copyAssoc"));
    checkCopiedNode(sourceNodeRef, copy, true, true, false);
    copies = copyService.getCopies(sourceNodeRef, pageRequest);
    assertEquals("Incorrect number of copies", 1, copies.getPage().size());
    // Copy to new node, copying children
    NodeRef copy2 = copyService.copy(sourceNodeRef, rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}copyAssoc2"), true);
    checkCopiedNode(sourceNodeRef, copy2, true, true, true);
    copies = copyService.getCopies(sourceNodeRef, pageRequest);
    assertEquals("Incorrect number of copies", 2, copies.getPage().size());
    // Check that a copy of a copy works correctly
    NodeRef copyOfCopy = copyService.copy(copy, rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}copyOfCopy"));
    checkCopiedNode(copy, copyOfCopy, true, true, false);
    // TODO check copying from a versioned copy
    // TODO check copying from a lockable copy
    // Check copying from a node with content
    ContentWriter contentWriter = contentService.getWriter(sourceNodeRef, ContentModel.PROP_CONTENT, true);
    contentWriter.putContent(SOME_CONTENT);
    NodeRef copyWithContent = copyService.copy(sourceNodeRef, rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}copyWithContent"));
    checkCopiedNode(sourceNodeRef, copyWithContent, true, true, false);
    ContentReader contentReader = contentService.getReader(copyWithContent, ContentModel.PROP_CONTENT);
    assertNotNull(contentReader);
    assertEquals(SOME_CONTENT, contentReader.getContentString());
// TODO check copying to a different store
// System.out.println(
// NodeStoreInspector.dumpNodeStore(nodeService, storeRef));
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) ContentReader(org.alfresco.service.cmr.repository.ContentReader) PagingRequest(org.alfresco.query.PagingRequest) CopyInfo(org.alfresco.service.cmr.repository.CopyService.CopyInfo)

Example 5 with CopyInfo

use of org.alfresco.service.cmr.repository.CopyService.CopyInfo in project alfresco-repository by Alfresco.

the class CopyActionExecuter method executeImpl.

@Override
public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) {
    if (!nodeService.exists(actionedUponNodeRef)) {
        return;
    }
    NodeRef destinationParent = (NodeRef) ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
    // Check the destination not to be in a pending delete list
    // MNT-11695
    Set<QName> destinationAspects = nodeService.getAspects(destinationParent);
    if (destinationAspects.contains(ContentModel.ASPECT_PENDING_DELETE)) {
        return;
    }
    // Get the deep copy value
    boolean deepCopy = false;
    Boolean deepCopyValue = (Boolean) ruleAction.getParameterValue(PARAM_DEEP_COPY);
    if (deepCopyValue != null) {
        deepCopy = deepCopyValue.booleanValue();
    }
    // Get the overwirte value
    boolean overwrite = true;
    Boolean overwriteValue = (Boolean) ruleAction.getParameterValue(PARAM_OVERWRITE_COPY);
    if (overwriteValue != null) {
        overwrite = overwriteValue.booleanValue();
    }
    // Since we are overwriting we need to figure out whether the destination node exists
    NodeRef copyNodeRef = null;
    if (overwrite == true) {
        // 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 = 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 (checkOutCheckInService.isWorkingCopy(copy)) {
                continue;
            }
            if (copyNodeRef == null) {
                copyNodeRef = copy;
            } else {
                throw new RuleServiceException(ERR_OVERWRITE);
            }
        }
    }
    if (copyNodeRef != null) {
        // Overwrite the state of the destination node ref with the actioned upon node state
        this.copyService.copy(actionedUponNodeRef, copyNodeRef);
    } else {
        ChildAssociationRef originalAssoc = nodeService.getPrimaryParent(actionedUponNodeRef);
        // Create a new copy of the node
        this.copyService.copyAndRename(actionedUponNodeRef, destinationParent, originalAssoc.getTypeQName(), originalAssoc.getQName(), deepCopy);
    }
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) RuleServiceException(org.alfresco.service.cmr.rule.RuleServiceException) QName(org.alfresco.service.namespace.QName) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) CopyInfo(org.alfresco.service.cmr.repository.CopyService.CopyInfo) PagingRequest(org.alfresco.query.PagingRequest)

Aggregations

PagingRequest (org.alfresco.query.PagingRequest)5 CopyInfo (org.alfresco.service.cmr.repository.CopyService.CopyInfo)5 NodeRef (org.alfresco.service.cmr.repository.NodeRef)5 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)3 IntegrityChecker (org.alfresco.repo.node.integrity.IntegrityChecker)2 ContentReader (org.alfresco.service.cmr.repository.ContentReader)2 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)2 RuleServiceException (org.alfresco.service.cmr.rule.RuleServiceException)2 QName (org.alfresco.service.namespace.QName)2 NoTransformerException (org.alfresco.service.cmr.repository.NoTransformerException)1 TransformationOptions (org.alfresco.service.cmr.repository.TransformationOptions)1