From 0c5d02d5433f4117b25788a539cfbc9e6785f46d Mon Sep 17 00:00:00 2001 From: Mark Rogers Date: Thu, 11 Mar 2010 14:14:37 +0000 Subject: [PATCH] Transfer Service : implementing ScriptTransferService - functionality to find transfertargets and do a synchronous transfer of a collection of nodes git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@19225 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../messages/transfer-service.properties | 1 + config/alfresco/transfer-service-context.xml | 3 + .../org/alfresco/repo/jscript/ScriptNode.java | 4 +- .../org/alfresco/repo/site/package-info.java | 8 ++ .../repo/transfer/TransferServiceImpl.java | 3 +- .../alfresco/repo/transfer/package-info.java | 3 + .../script/ScriptTransferService.java | 135 +++++++++++++++++- .../script/ScriptTransferServiceTest.java | 105 ++++++++++++++ .../transfer/script/ScriptTransferTarget.java | 56 ++++++++ .../transfer/script/test_transferService.js | 71 ++++++++- 10 files changed, 378 insertions(+), 11 deletions(-) create mode 100644 source/java/org/alfresco/repo/site/package-info.java create mode 100644 source/java/org/alfresco/repo/transfer/script/ScriptTransferServiceTest.java create mode 100644 source/java/org/alfresco/repo/transfer/script/ScriptTransferTarget.java diff --git a/config/alfresco/messages/transfer-service.properties b/config/alfresco/messages/transfer-service.properties index 2daac874e9..fbe3eba660 100644 --- a/config/alfresco/messages/transfer-service.properties +++ b/config/alfresco/messages/transfer-service.properties @@ -3,6 +3,7 @@ transfer_service.unable_to_find_transfer_home=Unable to find transfer home: {0} transfer_service.unable_to_find_transfer_group=Unable to find transfer group with name, {0} transfer_service.unable_to_find_transfer_target=Unable to find transfer target with name, {0} +transfer_service.unable_to_transfer_async=Unable to transfer async transfer_service.target_exists=Unable to create a new transfer target with the name, {0}, since there is already a target with that name. transfer_service.comms.unsupported_protocol=Unsupported protocol: {0} transfer_service.comms.unsuccessful_response=Received unsuccessful response code from target server: {0}, {1} diff --git a/config/alfresco/transfer-service-context.xml b/config/alfresco/transfer-service-context.xml index a5578abbf7..f7420e745e 100644 --- a/config/alfresco/transfer-service-context.xml +++ b/config/alfresco/transfer-service-context.xml @@ -197,6 +197,9 @@ transfer + + + diff --git a/source/java/org/alfresco/repo/jscript/ScriptNode.java b/source/java/org/alfresco/repo/jscript/ScriptNode.java index 51dc83d3d8..4d2057eb28 100644 --- a/source/java/org/alfresco/repo/jscript/ScriptNode.java +++ b/source/java/org/alfresco/repo/jscript/ScriptNode.java @@ -99,7 +99,7 @@ import org.mozilla.javascript.UniqueTag; import org.mozilla.javascript.Wrapper; /** - * Node class implementation, specific for use by ScriptService as part of the object model. + * Script Node class implementation, specific for use by ScriptService as part of the object model. *

* The class exposes Node properties, children and assocs as dynamically populated maps and lists. The various collection classes are mirrored as JavaScript properties. So can be * accessed using standard JavaScript property syntax, such as node.children[0].properties.name. @@ -180,7 +180,6 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol * * @param nodeRef The NodeRef this Node wrapper represents * @param services The ServiceRegistry the Node can use to access services - * @param resolver Image resolver to use to retrieve icons */ public ScriptNode(NodeRef nodeRef, ServiceRegistry services) { @@ -192,7 +191,6 @@ public class ScriptNode implements Serializable, Scopeable, NamespacePrefixResol * * @param nodeRef The NodeRef this Node wrapper represents * @param services The ServiceRegistry the Node can use to access services - * @param resolver Image resolver to use to retrieve icons * @param scope Root scope for this Node */ public ScriptNode(NodeRef nodeRef, ServiceRegistry services, Scriptable scope) diff --git a/source/java/org/alfresco/repo/site/package-info.java b/source/java/org/alfresco/repo/site/package-info.java new file mode 100644 index 0000000000..f9ac6aa643 --- /dev/null +++ b/source/java/org/alfresco/repo/site/package-info.java @@ -0,0 +1,8 @@ +/** + * Provides the implementation of the site service which is used to provide services used by the sites of the + * Share interface. + * + * @See org.alfresco.service.cmr.site.SiteService + * @since 3.0 + */ +package org.alfresco.repo.site; diff --git a/source/java/org/alfresco/repo/transfer/TransferServiceImpl.java b/source/java/org/alfresco/repo/transfer/TransferServiceImpl.java index 43b1879686..e20a4e6d16 100644 --- a/source/java/org/alfresco/repo/transfer/TransferServiceImpl.java +++ b/source/java/org/alfresco/repo/transfer/TransferServiceImpl.java @@ -83,6 +83,7 @@ public class TransferServiceImpl implements TransferService private static final String MSG_NO_HOME = "transfer_service.unable_to_find_transfer_home"; private static final String MSG_NO_GROUP = "transfer_service.unable_to_find_transfer_group"; private static final String MSG_NO_TARGET = "transfer_service.unable_to_find_transfer_target"; + private static final String MSG_ERR_TRANSFER_ASYNC = "transfer_service.unable_to_transfer_async"; private static final String MSG_TARGET_EXISTS = "transfer_service.target_exists"; private static final String MSG_CANCELLED = "transfer_service.cancelled"; private static final String MSG_NO_NODES = "transfer_service.no_nodes"; @@ -438,7 +439,7 @@ public class TransferServiceImpl implements TransferService catch (Exception error) { logger.error("unexpected exception", error); - throw new AlfrescoRuntimeException("unable to transfer async", error); + throw new AlfrescoRuntimeException(MSG_ERR_TRANSFER_ASYNC, error); } finally { diff --git a/source/java/org/alfresco/repo/transfer/package-info.java b/source/java/org/alfresco/repo/transfer/package-info.java index 56da0eb236..490d661812 100644 --- a/source/java/org/alfresco/repo/transfer/package-info.java +++ b/source/java/org/alfresco/repo/transfer/package-info.java @@ -1,6 +1,9 @@ /** * Provides the implementation of the transfer service which can be used to * transfer nodes from one repository to another. + * + * @See org.alfresco.service.cmr.transfer.TransferService + * * @since 3.3 */ package org.alfresco.repo.transfer; diff --git a/source/java/org/alfresco/repo/transfer/script/ScriptTransferService.java b/source/java/org/alfresco/repo/transfer/script/ScriptTransferService.java index 26c89656bc..5d263756aa 100644 --- a/source/java/org/alfresco/repo/transfer/script/ScriptTransferService.java +++ b/source/java/org/alfresco/repo/transfer/script/ScriptTransferService.java @@ -1,8 +1,35 @@ +/* + * Copyright (C) 2005-2010 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ package org.alfresco.repo.transfer.script; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Set; + import org.alfresco.repo.jscript.BaseScopableProcessorExtension; +import org.alfresco.repo.jscript.ScriptNode; +import org.alfresco.repo.jscript.ValueConverter; +import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.transfer.TransferDefinition; import org.alfresco.service.cmr.transfer.TransferService; +import org.alfresco.service.cmr.transfer.TransferTarget; import org.mozilla.javascript.NativeArray; import org.mozilla.javascript.Scriptable; @@ -15,6 +42,10 @@ import org.mozilla.javascript.Scriptable; public class ScriptTransferService extends BaseScopableProcessorExtension { private TransferService transferService; + + private ServiceRegistry serviceRegistry; + + ValueConverter valueConverter = new ValueConverter(); /** * @param transferService @@ -38,15 +69,107 @@ public class ScriptTransferService extends BaseScopableProcessorExtension */ /** - * Transfer a set of nodes, with no callback - * @param targetName - * @param nodes - * - * @return node ref of transfer report. + * Get the transfer targets for the specified group */ - public NodeRef transfer(String targetName, Scriptable nodes) + public ScriptTransferTarget[] getTransferTargetsByGroup(String groupName) { + + Set values = transferService.getTransferTargets(groupName); + + ScriptTransferTarget[] retVal = new ScriptTransferTarget[values.size()]; + + int i = 0; + for(TransferTarget value : values) + { + retVal[i++] = new ScriptTransferTarget(value); + } + return retVal; + } + + public ScriptTransferTarget[] getAllTransferTargets() + { + Set values = transferService.getTransferTargets(); + + ScriptTransferTarget[] retVal = new ScriptTransferTarget[values.size()]; + + int i = 0; + for(TransferTarget value : values) + { + retVal[i++] = new ScriptTransferTarget(value); + } + + return retVal; + } + + public ScriptTransferTarget getTransferTarget(String name) + { + TransferTarget value = transferService.getTransferTarget(name); + + if(value != null) + { + return new ScriptTransferTarget(value); + } return null; } + /** + * Transfer a set of nodes, with no callback + * @param targetName the name of the target to transfer to + * @param nodes the nodes to transfer - Java Script Array of either ScriptNodes, NodeRef or String + * @return node ref of transfer report. + */ + public NodeRef transfer(String targetName, Object nodesToTransfer) + { + Object nodesObject = valueConverter.convertValueForJava(nodesToTransfer); + + TransferDefinition toTransfer = new TransferDefinition(); + Collection nodeCollection = new ArrayList(); + + 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.setNodes(nodeCollection); + NodeRef retVal = transferService.transfer(targetName, toTransfer); + + return retVal; + } + + public void setServiceRegistry(ServiceRegistry serviceRegistry) + { + this.serviceRegistry = serviceRegistry; + } + + public ServiceRegistry getServiceRegistry() + { + return serviceRegistry; + } } diff --git a/source/java/org/alfresco/repo/transfer/script/ScriptTransferServiceTest.java b/source/java/org/alfresco/repo/transfer/script/ScriptTransferServiceTest.java new file mode 100644 index 0000000000..d18cc87a4a --- /dev/null +++ b/source/java/org/alfresco/repo/transfer/script/ScriptTransferServiceTest.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2005-2010 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.transfer.script; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.alfresco.repo.jscript.ClasspathScriptLocation; +import org.alfresco.repo.transfer.TransferServiceImpl; +import org.alfresco.repo.transfer.TransferTargetImpl; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.ScriptLocation; +import org.alfresco.service.cmr.repository.ScriptService; +import org.alfresco.service.cmr.repository.StoreRef; +import org.alfresco.service.cmr.transfer.TransferDefinition; +import org.alfresco.service.cmr.transfer.TransferException; +import org.alfresco.service.cmr.transfer.TransferService; +import org.alfresco.service.cmr.transfer.TransferTarget; +import org.alfresco.util.BaseAlfrescoSpringTest; +import org.mockito.Matchers; +import org.mockito.Mockito; + +/** + * Script Transfer unit test + * + * @author Mark Rogers + */ +public class ScriptTransferServiceTest extends BaseAlfrescoSpringTest +{ + +// String COMPANY_HOME_XPATH_QUERY = "/{http://www.alfresco.org/model/application/1.0}company_home"; +// String GUEST_HOME_XPATH_QUERY = "/{http://www.alfresco.org/model/application/1.0}company_home/{http://www.alfresco.org/model/application/1.0}guest_home"; + + private ScriptService scriptService; + private TransferService transferService; + private TransferService transferServiceImpl; + private ScriptTransferService scriptTransferService; + + + protected void onSetUpInTransaction() throws Exception + { + super.onSetUpInTransaction(); + + // Get the required services + this.scriptService = (ScriptService)this.applicationContext.getBean("ScriptService"); + this.transferService = (TransferService)this.applicationContext.getBean("TransferService"); + this.transferServiceImpl = (TransferServiceImpl)this.applicationContext.getBean("transferService"); + this.scriptTransferService = (ScriptTransferService)this.applicationContext.getBean("transferServiceScript"); + + } + + // == Test the JavaScript API == + + public void testJSAPI() throws Exception + { + String name = "testTransferJSAPI"; + + /** + * Prepare some dummy data for tests + */ + TransferTargetImpl dummyTarget = new TransferTargetImpl(); + dummyTarget.setName("dummyTarget"); + dummyTarget.setNodeRef(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "4")); + + SetdummyTargets = new HashSet(); + dummyTargets.add(dummyTarget); + + TransferService mockedTransferService = Mockito.mock(TransferService.class); + scriptTransferService.setTransferService(mockedTransferService); + + // When the transfer method is called return a node ref - mocks a good call. + // When the transfer method is called with a transfer name of exception - throw a transferException + Mockito.when(mockedTransferService.transfer(Matchers.anyString(), Matchers.isA(TransferDefinition.class))).thenReturn(new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, "123")); + Mockito.when(mockedTransferService.transfer(Matchers.eq("exception"), Matchers.isA(TransferDefinition.class))).thenThrow(new TransferException("mocked transfer exception")); + + // When getTransferTarget called return a TransferTarget + Mockito.when(mockedTransferService.getTransferTarget(Matchers.anyString())).thenReturn(dummyTarget); + Mockito.when(mockedTransferService.getTransferTargets(Matchers.anyString())).thenReturn(dummyTargets); + Mockito.when(mockedTransferService.getTransferTargets()).thenReturn(dummyTargets); + + // Execute the unit test script + Map model = new HashMap(1); + ScriptLocation location = new ClasspathScriptLocation("org/alfresco/repo/transfer/script/test_transferService.js"); + this.scriptService.executeScript(location, model); + + } +} diff --git a/source/java/org/alfresco/repo/transfer/script/ScriptTransferTarget.java b/source/java/org/alfresco/repo/transfer/script/ScriptTransferTarget.java new file mode 100644 index 0000000000..1d28b8b003 --- /dev/null +++ b/source/java/org/alfresco/repo/transfer/script/ScriptTransferTarget.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2005-2010 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + */ +package org.alfresco.repo.transfer.script; + +import org.alfresco.service.cmr.transfer.TransferTarget; + +/** + * Java Script wrapper for TransferTarget + * + * @author Mark Rogers + */ +public class ScriptTransferTarget +{ + TransferTarget internal; + + public ScriptTransferTarget(TransferTarget target) + { + internal = target; + } + public String getName() + { + return internal.getName(); + } + public String getDescription() + { + return internal.getDescription(); + } + public String getEndpointHost() + { + return internal.getEndpointHost(); + } + public String getEndpointProtocol() + { + return internal.getEndpointProtocol(); + } + public int getEndpointPort() + { + return internal.getEndpointPort(); + } +} diff --git a/source/java/org/alfresco/repo/transfer/script/test_transferService.js b/source/java/org/alfresco/repo/transfer/script/test_transferService.js index a5e35a57a9..b6e34d69dd 100644 --- a/source/java/org/alfresco/repo/transfer/script/test_transferService.js +++ b/source/java/org/alfresco/repo/transfer/script/test_transferService.js @@ -2,8 +2,77 @@ function testTransferService() { test.assertNotNull(transfer, "RootScoped object not found :transfer: "); + + test.assertNotNull(testNode, "RootScoped object not found :testNode: "); - //test.assertNotNull(transfer, "Form should have been found for: " + testDoc); + var testNode = "foo://123/123"; + + // First transfer - with a rubbish target + try + { + var ret = transfer.transfer("exception", testNode); + test.fail("should have thrown exception.") + } + catch (e) + { + // expect to go here. + } + + // second transfer - with a good target name + { + var ret = transfer.transfer("good", testNode); + } + + // third transfer - with an array of strings + { + var testNodes=new Array(); // regular array (add an optional integer + testNodes[0]="foo://123/1"; + testNodes[1]="foo://123/2"; + testNodes[2]="foo://123/3"; + var ret = transfer.transfer("good", testNodes); + } + + // Test getTransferTarget + { + var ret = transfer.getTransferTarget("good"); + test.assertNotNull(ret, "return from getTransferTarget is null"); + test.assertNotNull(ret.name, "target name is null"); + test.assertNotNull(ret.noderef, "node ref is null"); + } + + // Test getTransferTargets + { + var ret = transfer.getAllTransferTargets(); + test.assertNotNull(ret, "return from getTransferTargets is null"); + //test.assertNotNull(ret.name, "target name is null"); + //test.assertNotNull(ret.noderef, "node ref is null"); + for(x in ret) + { + test.assertNotNull(x.name, "target name is null"); + test.assertNotNull(x.noderef, "node ref is null"); + } + } + + // Test getTransferTargets + { + var ret = transfer.getAllTransferTargets(); + test.assertNotNull(ret, "return from getAllTransferTargets is null"); + + for(x in ret) + { + test.assertNotNull(x.name, "target name is null"); + test.assertNotNull(x.noderef, "node ref is null"); + } + + } + + // Test getTransferTargets + { + var ret = transfer.getTransferTargetsByGroup("good"); + test.assertNotNull(ret, "return from getTransferTargetsByGroup is null"); + //test.assertNotNull(ret.name, "target name is null"); + //test.assertNotNull(ret.noderef, "node ref is null"); + } } // Execute test's