Search in sources :

Example 26 with TransferDefinition

use of org.alfresco.service.cmr.transfer.TransferDefinition in project alfresco-repository by Alfresco.

the class TransferTreeWithCancelActionExecuter method executeImpl.

/* (non-Javadoc)
     * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.action.Action, org.alfresco.service.cmr.repository.NodeRef)
     */
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
    TransferTarget target = serviceRegistry.getTransactionService().getRetryingTransactionHelper().doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<TransferTarget>() {

        public TransferTarget execute() throws Throwable {
            return TransferTestUtil.getTestTarget(transferService);
        }
    }, false, true);
    NodeCrawler crawler = new StandardNodeCrawlerImpl(serviceRegistry);
    crawler.setNodeFinders(new ChildAssociatedNodeFinder(ContentModel.ASSOC_CONTAINS));
    Set<NodeRef> nodes = crawler.crawl(actionedUponNodeRef);
    TransferDefinition td = new TransferDefinition();
    td.setNodes(nodes);
    transferService.transferAsync(target.getName(), td, new TransferCallback() {

        private String transferId;

        public void processEvent(TransferEvent event) {
            if (event instanceof TransferEventBegin) {
                transferId = ((TransferEventBegin) event).getTransferId();
            } else if (event instanceof TransferEventCommittingStatus) {
                transferService.cancelAsync(transferId);
            }
        }
    });
}
Also used : NodeCrawler(org.alfresco.service.cmr.transfer.NodeCrawler) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) TransferEvent(org.alfresco.service.cmr.transfer.TransferEvent) TransferEventBegin(org.alfresco.service.cmr.transfer.TransferEventBegin) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) TransferCallback(org.alfresco.service.cmr.transfer.TransferCallback) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) TransferEventCommittingStatus(org.alfresco.service.cmr.transfer.TransferEventCommittingStatus)

Example 27 with TransferDefinition

use of org.alfresco.service.cmr.transfer.TransferDefinition in project alfresco-repository by Alfresco.

the class ScriptTransferService method remove.

/**
 * Remove a set of nodes, with no callback
 * <p>
 * Nodes are not locked on the target.
 *
 * @param targetName the name of the target to transfer to
 * @param nodesToRemove the nodes to transfer - Java Script Array of either ScriptNodes, NodeRef or String
 * @return node ref of transfer report.
 */
@SuppressWarnings("unchecked")
public ScriptNode remove(String targetName, Object nodesToRemove) {
    Object nodesObject = valueConverter.convertValueForJava(nodesToRemove);
    TransferDefinition toTransfer = new TransferDefinition();
    toTransfer.setExcludedAspects(excludedAspects);
    Collection<NodeRef> nodeCollection = new ArrayList<NodeRef>();
    if (nodesObject instanceof Collection) {
        for (Object value : (Collection) nodesObject) {
            if (value instanceof NodeRef) {
                nodeCollection.add((NodeRef) value);
            } else if (value instanceof String) {
                nodeCollection.add(new NodeRef((String) value));
            } else {
                throw new IllegalArgumentException("transfer: unknown type in collection: " + value.getClass().getName());
            }
        }
    } else if (nodesObject instanceof NodeRef) {
        nodeCollection.add((NodeRef) nodesObject);
    } else if (nodesObject instanceof String) {
        nodeCollection.add(new NodeRef((String) nodesObject));
    } else {
        throw new IllegalArgumentException("transfer: unexpected type for nodes :" + nodesObject.getClass().getName());
    }
    toTransfer.setNodesToRemove(nodeCollection);
    NodeRef reportNode = transferService.transfer(targetName, toTransfer);
    return new ScriptNode(reportNode, serviceRegistry, getScope());
}
Also used : TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ScriptNode(org.alfresco.repo.jscript.ScriptNode)

Example 28 with TransferDefinition

use of org.alfresco.service.cmr.transfer.TransferDefinition in project alfresco-repository by Alfresco.

the class ReplicationServiceIntegrationTest method testTransferDefinitionBuilding.

/**
 * Test that we turn a replication definition correctly
 *  into a transfer definition
 */
public void testTransferDefinitionBuilding() throws Exception {
    ReplicationDefinition rd = replicationService.createReplicationDefinition(ACTION_NAME, "Test");
    Set<NodeRef> nodes = new HashSet<NodeRef>();
    nodes.add(folder1);
    nodes.add(content1_1);
    TransferDefinition td = replicationActionExecutor.buildTransferDefinition(rd, nodes);
    assertEquals(true, td.isSync());
    assertEquals(replicationParams.getTransferReadOnly(), td.isReadOnly());
    assertEquals(2, td.getNodes().size());
    assertEquals(true, td.getNodes().contains(folder1));
    assertEquals(true, td.getNodes().contains(content1_1));
}
Also used : TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ScriptReplicationDefinition(org.alfresco.repo.replication.script.ScriptReplicationDefinition) ReplicationDefinition(org.alfresco.service.cmr.replication.ReplicationDefinition) HashSet(java.util.HashSet)

Example 29 with TransferDefinition

use of org.alfresco.service.cmr.transfer.TransferDefinition in project alfresco-repository by Alfresco.

the class TransferServiceToBeRefactoredTest method testReadOnlyFlag.

/**
 * Transfer with read only flag
 *
 * Node tree for this test
 * <pre>
 *           A (Folder)
 *   |                 |
 *   B (Content)   C (Folder)
 *                     |
 *                     D (Content)
 * </pre>
 * Step 1
 * transfer Nodes ABCD with read only flag set - content should all be locked on destination
 * <p>
 * Step 2
 * lock B (Content node) as user fred
 * transfer (read only) - destination lock should change to Admin
 * <p>
 * Step 3
 * lock C (Folder) as user fred
 * transfer (read only) - destination lock should change to Admin
 * <p>
 * Step 4
 * transfer without read only flag - locks should revert from Admin to Fred.
 * <p>
 * Step 5
 * remove locks on A and B - transfer without read only flag - content should all be unlocked.
 */
@Test
public void testReadOnlyFlag() throws Exception {
    final String CONTENT_TITLE = "ContentTitle";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "The quick brown fox";
    final Set<NodeRef> nodes = new HashSet<NodeRef>();
    final String USER_ONE = "TransferServiceImplTest";
    final String PASSWORD = "Password";
    final String targetName = "testReadOnlyFlag";
    class TestData {

        NodeRef nodeA;

        NodeRef nodeB;

        NodeRef nodeC;

        NodeRef nodeD;

        ChildAssociationRef child;

        @SuppressWarnings("unused")
        TransferTarget transferMe;
    }
    final TestData testData = new TestData();
    /**
     *  For unit test
     *  - replace the HTTP transport with the in-process transport
     *  - replace the node factory with one that will map node refs, paths etc.
     */
    TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(this.receiver, this.contentService, transactionService);
    transferServiceImpl.setTransmitter(transmitter);
    final UnitTestTransferManifestNodeFactory testNodeFactory = new UnitTestTransferManifestNodeFactory(this.transferManifestNodeFactory);
    transferServiceImpl.setTransferManifestNodeFactory(testNodeFactory);
    List<Pair<Path, Path>> pathMap = testNodeFactory.getPathMap();
    // Map company_home/guest_home to company_home so tranferred nodes and moved "up" one level.
    pathMap.add(new Pair<Path, Path>(PathHelper.stringToPath(GUEST_HOME_XPATH_QUERY), PathHelper.stringToPath(COMPANY_HOME_XPATH_QUERY)));
    DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A);
    transferServiceImpl.setDescriptorService(mockedDescriptorService);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test node that we will read and write
             */
            String guid = GUID.generate();
            testData.child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(guid), ContentModel.TYPE_FOLDER);
            testData.nodeA = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeA, ContentModel.PROP_TITLE, guid);
            nodeService.setProperty(testData.nodeA, ContentModel.PROP_NAME, guid);
            nodes.add(testData.nodeA);
            testData.child = nodeService.createNode(testData.nodeA, ContentModel.ASSOC_CONTAINS, QName.createQName("testNodeB"), ContentModel.TYPE_CONTENT);
            testData.nodeB = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeB, ContentModel.PROP_TITLE, CONTENT_TITLE + "B");
            nodeService.setProperty(testData.nodeB, ContentModel.PROP_NAME, "DemoNodeB");
            {
                ContentWriter writer = contentService.getWriter(testData.nodeB, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
                nodes.add(testData.nodeB);
            }
            testData.child = nodeService.createNode(testData.nodeA, ContentModel.ASSOC_CONTAINS, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "testNodeC"), ContentModel.TYPE_FOLDER);
            testData.nodeC = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeC, ContentModel.PROP_TITLE, "TestNodeC");
            nodeService.setProperty(testData.nodeC, ContentModel.PROP_NAME, "TestNodeC");
            nodes.add(testData.nodeC);
            testData.child = nodeService.createNode(testData.nodeC, ContentModel.ASSOC_CONTAINS, QName.createQName("testNodeD"), ContentModel.TYPE_CONTENT);
            testData.nodeD = testData.child.getChildRef();
            nodeService.setProperty(testData.nodeD, ContentModel.PROP_TITLE, CONTENT_TITLE + "D");
            nodeService.setProperty(testData.nodeD, ContentModel.PROP_NAME, "DemoNodeD");
            {
                ContentWriter writer = contentService.getWriter(testData.nodeD, ContentModel.PROP_CONTENT, true);
                writer.setLocale(CONTENT_LOCALE);
                writer.putContent(CONTENT_STRING);
                nodes.add(testData.nodeD);
            }
            // Create users
            createUser(USER_ONE, USER_ONE, PASSWORD);
            /**
             * Now go ahead and create our first transfer target
             */
            if (!transferService.targetExists(targetName)) {
                testData.transferMe = createTransferTarget(targetName);
            } else {
                testData.transferMe = transferService.getTransferTarget(targetName);
            }
            return null;
        }
    });
    /**
     * Step 1.
     * transfer Nodes ABCD with read only flag set - content should all be locked on destination
     */
    logger.debug("transfer read only - step 1");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                definition.setReadOnly(true);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertTrue("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            return null;
        }
    });
    /**
     * Step 2
     * lock B (Content node) as user ONE
     * transfer (read only) - destination lock should change user to "Admin"
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            AuthenticationUtil.pushAuthentication();
            AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
            lockService.lock(testData.nodeB, LockType.READ_ONLY_LOCK);
            assertEquals("test error: dest node B lock ownership", nodeService.getProperty(testData.nodeB, ContentModel.PROP_LOCK_OWNER), USER_ONE);
            AuthenticationUtil.popAuthentication();
            return null;
        }
    });
    logger.debug("transfer read only - step 2");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                definition.setReadOnly(true);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertTrue("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            // check that the lock owner is no longer USER_ONE
            assertTrue("lock owner not changed", !USER_ONE.equalsIgnoreCase((String) nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.PROP_LOCK_OWNER)));
            return null;
        }
    });
    /**
     * Step 3
     * lock C (Folder node) as user ONE
     * transfer (read only) - destination lock should change to Admin
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            AuthenticationUtil.pushAuthentication();
            AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
            lockService.lock(testData.nodeC, LockType.READ_ONLY_LOCK);
            assertEquals("test error: dest node C lock ownership", nodeService.getProperty(testData.nodeC, ContentModel.PROP_LOCK_OWNER), USER_ONE);
            AuthenticationUtil.popAuthentication();
            return null;
        }
    });
    logger.debug("transfer read only - step 3");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                definition.setReadOnly(true);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertTrue("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            // check that the lock owner is no longer USER_ONE for content node B and folder node C
            assertTrue("lock owner not changed", !USER_ONE.equalsIgnoreCase((String) nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.PROP_LOCK_OWNER)));
            assertTrue("lock owner not changed", !USER_ONE.equalsIgnoreCase((String) nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.PROP_LOCK_OWNER)));
            return null;
        }
    });
    /**
     * Step 4
     * transfer without read only flag - locks should revert from Admin to USER_ONE.
     */
    logger.debug("transfer read only - step 4");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                // turn off read-only
                definition.setReadOnly(false);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are not locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertFalse("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertTrue("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            assertEquals("dest node B lock ownership", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.PROP_LOCK_OWNER), USER_ONE);
            assertEquals("dest node C lock ownership", nodeService.getProperty(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.PROP_LOCK_OWNER), USER_ONE);
            return null;
        }
    });
    /**
     * Step 5
     * remove locks on A and B - transfer without read only flag - content should all be unlocked.
     */
    logger.debug("transfer read only - step 5");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            lockService.unlock(testData.nodeB);
            lockService.unlock(testData.nodeC);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target nodes
             */
            {
                TransferDefinition definition = new TransferDefinition();
                definition.setNodes(nodes);
                // turn off read-only
                definition.setReadOnly(false);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Check destination nodes are not locked.
            assertTrue("dest node A does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeA)));
            assertTrue("dest node B does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeB)));
            assertTrue("dest node C does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeC)));
            assertTrue("dest node D does not exist", nodeService.exists(testNodeFactory.getMappedNodeRef(testData.nodeD)));
            assertFalse("test fail: dest node B is still locked", nodeService.hasAspect(testData.nodeB, ContentModel.ASPECT_LOCKABLE));
            assertFalse("test fail: dest node C is still locked", nodeService.hasAspect(testData.nodeC, ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node A not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeA), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node B not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeB), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node C not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeC), ContentModel.ASPECT_LOCKABLE));
            assertFalse("dest node D not locked", nodeService.hasAspect(testNodeFactory.getMappedNodeRef(testData.nodeD), ContentModel.ASPECT_LOCKABLE));
            return null;
        }
    });
}
Also used : Locale(java.util.Locale) Path(org.alfresco.service.cmr.repository.Path) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) DescriptorService(org.alfresco.service.descriptor.DescriptorService) HashSet(java.util.HashSet) Pair(org.alfresco.util.Pair) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Example 30 with TransferDefinition

use of org.alfresco.service.cmr.transfer.TransferDefinition in project alfresco-repository by Alfresco.

the class TransferServiceToBeRefactoredTest method testTransferWithPermissions.

/**
 * Test the transfer method with regard to permissions on a node.
 * <p>
 * Step 1:
 * Create a node with a single permission
 *     Inherit:false
 *     Read, Admin, Allow
 *     Transfer
 * <p>
 * Step 2:
 * Update it to have several permissions
 *     Inherit:false
 *     Read, Everyone, DENY
 *     Read, Admin, Allow
 * <p>
 * Step 3:
 * Remove a permission
 *     Inherit:false
 *     Read, Admin, Allow
 * <p>
 * Step 4:
 * Revert to inherit all permissions
 *     Inherit:true
 * <p>
 * This is a unit test so it does some shenanigans to send to the same instance of alfresco.
 */
@Test
public void testTransferWithPermissions() throws Exception {
    final String CONTENT_TITLE = "ContentTitle";
    final Locale CONTENT_LOCALE = Locale.GERMAN;
    final String CONTENT_STRING = "Hello";
    /**
     * For unit test - replace the HTTP transport with the in-process transport - replace the node factory with one
     * that will map node refs, paths etc.
     */
    TransferTransmitter transmitter = new UnitTestInProcessTransmitterImpl(receiver, contentService, transactionService);
    transferServiceImpl.setTransmitter(transmitter);
    final UnitTestTransferManifestNodeFactory testNodeFactory = new UnitTestTransferManifestNodeFactory(this.transferManifestNodeFactory);
    transferServiceImpl.setTransferManifestNodeFactory(testNodeFactory);
    List<Pair<Path, Path>> pathMap = testNodeFactory.getPathMap();
    // Map company_home/guest_home to company_home so tranferred nodes and moved "up" one level.
    pathMap.add(new Pair<Path, Path>(PathHelper.stringToPath(GUEST_HOME_XPATH_QUERY), PathHelper.stringToPath(COMPANY_HOME_XPATH_QUERY)));
    DescriptorService mockedDescriptorService = getMockDescriptorService(REPO_ID_A);
    transferServiceImpl.setDescriptorService(mockedDescriptorService);
    /**
     * Now go ahead and create our transfer target
     */
    final String targetName = "testTransferWithPermissions";
    class TestData {

        TransferTarget transferMe;

        NodeRef contentNodeRef;

        NodeRef destNodeRef;
    }
    final TestData testData = new TestData();
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            NodeRef guestHome = repositoryHelper.getGuestHome();
            /**
             * Create a test node that we will read and write
             */
            String name = GUID.generate();
            ChildAssociationRef child = nodeService.createNode(guestHome, ContentModel.ASSOC_CONTAINS, QName.createQName(name), ContentModel.TYPE_CONTENT);
            testData.contentNodeRef = child.getChildRef();
            nodeService.setProperty(testData.contentNodeRef, ContentModel.PROP_TITLE, CONTENT_TITLE);
            nodeService.setProperty(testData.contentNodeRef, ContentModel.PROP_NAME, name);
            ContentWriter writer = contentService.getWriter(testData.contentNodeRef, ContentModel.PROP_CONTENT, true);
            writer.setLocale(CONTENT_LOCALE);
            writer.putContent(CONTENT_STRING);
            permissionService.setInheritParentPermissions(testData.contentNodeRef, false);
            permissionService.setPermission(testData.contentNodeRef, "admin", PermissionService.READ, true);
            if (!transferService.targetExists(targetName)) {
                testData.transferMe = createTransferTarget(targetName);
            } else {
                testData.transferMe = transferService.getTransferTarget(targetName);
            }
            return null;
        }
    });
    /**
     * Step 1
     */
    logger.debug("First transfer - create new node with inheritParent permission off");
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            assertFalse("unit test stuffed up - comparing with self", testData.destNodeRef.equals(testData.transferMe.getNodeRef()));
            assertTrue("dest node ref does not exist", nodeService.exists(testData.destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(testData.destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE);
            assertEquals("type is wrong", nodeService.getType(testData.contentNodeRef), nodeService.getType(testData.destNodeRef));
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertFalse("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertFalse("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                logger.debug("checking permission :" + p);
                assertTrue("permission is missing", destPerm.contains(p));
            }
            return null;
        }
    });
    /**
     * Step 2
     * Update it to have several permissions
     *     Inherit:false
     *     Read, Everyone, DENY
     *     Read, Admin, Allow
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            permissionService.setPermission(testData.contentNodeRef, "EVERYONE", PermissionService.READ, false);
            permissionService.setPermission(testData.contentNodeRef, "admin", PermissionService.FULL_CONTROL, true);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertFalse("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertFalse("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                logger.debug("checking permission :" + p);
                assertTrue("Step2, permission is missing", destPerm.contains(p));
            }
            return null;
        }
    });
    /**
     * Step 3 Remove a permission
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            permissionService.deletePermission(testData.contentNodeRef, "admin", PermissionService.FULL_CONTROL);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertFalse("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertFalse("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                logger.debug("checking permission :" + p);
                assertTrue("permission is missing", destPerm.contains(p));
            }
            return null;
        }
    });
    /**
     * Step 4 Revert to inherit all permissions
     */
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            permissionService.setInheritParentPermissions(testData.contentNodeRef, true);
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            /**
             * Transfer our transfer target node
             */
            {
                TransferDefinition definition = new TransferDefinition();
                Set<NodeRef> nodes = new HashSet<NodeRef>();
                nodes.add(testData.contentNodeRef);
                definition.setNodes(nodes);
                transferService.transfer(targetName, definition);
            }
            return null;
        }
    });
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // Now validate that the target node exists with the correct permissions
            testData.destNodeRef = testNodeFactory.getMappedNodeRef(testData.contentNodeRef);
            assertFalse("unit test stuffed up - comparing with self", testData.destNodeRef.equals(testData.transferMe.getNodeRef()));
            assertTrue("dest node ref does not exist", nodeService.exists(testData.destNodeRef));
            assertEquals("title is wrong", (String) nodeService.getProperty(testData.destNodeRef, ContentModel.PROP_TITLE), CONTENT_TITLE);
            assertEquals("type is wrong", nodeService.getType(testData.contentNodeRef), nodeService.getType(testData.destNodeRef));
            // Check ACL of destination node
            boolean srcInherit = permissionService.getInheritParentPermissions(testData.contentNodeRef);
            Set<AccessPermission> srcPerm = permissionService.getAllSetPermissions(testData.contentNodeRef);
            boolean destInherit = permissionService.getInheritParentPermissions(testData.destNodeRef);
            Set<AccessPermission> destPerm = permissionService.getAllSetPermissions(testData.destNodeRef);
            assertTrue("inherit parent permissions (src) flag is incorrect", srcInherit);
            assertTrue("inherit parent permissions (dest) flag is incorrect", destInherit);
            // Check destination has the source's permissions
            for (AccessPermission p : srcPerm) {
                if (p.isSetDirectly()) {
                    logger.debug("checking permission :" + p);
                    assertTrue("permission is missing:" + p, destPerm.contains(p));
                }
            }
            return null;
        }
    });
}
Also used : Locale(java.util.Locale) Path(org.alfresco.service.cmr.repository.Path) ResultSet(org.alfresco.service.cmr.search.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) AccessPermission(org.alfresco.service.cmr.security.AccessPermission) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) DescriptorService(org.alfresco.service.descriptor.DescriptorService) Pair(org.alfresco.util.Pair) HashSet(java.util.HashSet) BaseAlfrescoSpringTest(org.alfresco.util.BaseAlfrescoSpringTest) Test(org.junit.Test)

Aggregations

TransferDefinition (org.alfresco.service.cmr.transfer.TransferDefinition)38 NodeRef (org.alfresco.service.cmr.repository.NodeRef)31 TransferTarget (org.alfresco.service.cmr.transfer.TransferTarget)25 ChildAssociationRef (org.alfresco.service.cmr.repository.ChildAssociationRef)24 BaseAlfrescoSpringTest (org.alfresco.util.BaseAlfrescoSpringTest)24 Test (org.junit.Test)24 HashSet (java.util.HashSet)21 Locale (java.util.Locale)19 DescriptorService (org.alfresco.service.descriptor.DescriptorService)18 ArrayList (java.util.ArrayList)17 Pair (org.alfresco.util.Pair)16 ContentWriter (org.alfresco.service.cmr.repository.ContentWriter)14 RetryingTransactionCallback (org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback)13 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)11 List (java.util.List)10 Path (org.alfresco.service.cmr.repository.Path)10 TransferEvent (org.alfresco.service.cmr.transfer.TransferEvent)8 Collection (java.util.Collection)7 QName (org.alfresco.service.namespace.QName)7 ContentReader (org.alfresco.service.cmr.repository.ContentReader)5