. JavaScript API inter-store copy support

- Transparent support for inter-store copy added to JavaScript Node and AVMNode implementations
   The existing API mechanisms for copy() now support passing of Node or AVMNode and visa-versa as the destination target
   No changes to existing copy() semantics and no new services/APIs to learn
. Improvements to handling of missing input parameters in JavaScript Node APIs
. Added Workspace->Workspace support to CrossRepositoryCopyService

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5047 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Kevin Roast 2007-02-06 10:36:46 +00:00
parent 92c73dd54a
commit d781cc74c1
5 changed files with 2097 additions and 1931 deletions

View File

@ -99,4 +99,13 @@
</property>
</bean>
<bean id="crossCopyScript" parent="baseScriptImplementation" class="org.alfresco.repo.jscript.CrossRepositoryCopy">
<property name="scriptName">
<value>crossRepoCopy</value>
</property>
<property name="serviceRegistry">
<ref bean="ServiceRegistry"/>
</property>
</bean>
</beans>

View File

@ -20,6 +20,7 @@ import org.alfresco.service.cmr.avm.AVMService;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.model.FileInfo;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.CopyService;
import org.alfresco.service.cmr.repository.CrossRepositoryCopyService;
@ -117,24 +118,24 @@ public class CrossRepositoryCopyServiceImpl implements
{
StoreRef srcStoreRef = src.getStoreRef();
StoreRef dstStoreRef = dst.getStoreRef();
if (srcStoreRef.getProtocol().equals("avm"))
if (srcStoreRef.getProtocol().equals(StoreRef.PROTOCOL_AVM))
{
if (dstStoreRef.getProtocol().equals("avm"))
if (dstStoreRef.getProtocol().equals(StoreRef.PROTOCOL_AVM))
{
copyAVMToAVM(src, dst, name);
}
else
else if (dstStoreRef.getProtocol().equals(StoreRef.PROTOCOL_WORKSPACE))
{
copyAVMToRepo(src, dst, name);
}
}
else
else if (srcStoreRef.getProtocol().equals(StoreRef.PROTOCOL_WORKSPACE))
{
if (dstStoreRef.getProtocol().equals("avm"))
if (dstStoreRef.getProtocol().equals(StoreRef.PROTOCOL_AVM))
{
copyRepoToAVM(src, dst, name);
}
else
else if (dstStoreRef.getProtocol().equals(StoreRef.PROTOCOL_WORKSPACE))
{
copyRepoToRepo(src, dst, name);
}
@ -277,6 +278,8 @@ public class CrossRepositoryCopyServiceImpl implements
*/
private void copyRepoToRepo(NodeRef src, NodeRef dst, String name)
{
ChildAssociationRef assocRef = fNodeService.getPrimaryParent(src);
fCopyService.copyAndRename(src, dst, ContentModel.ASSOC_CONTAINS, assocRef.getQName(), true);
}
private void copyData(InputStream in, OutputStream out)

View File

@ -19,7 +19,8 @@ package org.alfresco.repo.jscript;
import org.alfresco.repo.avm.AVMNodeConverter;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
import org.alfresco.util.Pair;
import org.alfresco.util.ParameterCheck;
import org.mozilla.javascript.Scriptable;
/**
@ -31,6 +32,7 @@ import org.mozilla.javascript.Scriptable;
public class AVMNode extends Node
{
private String path;
private int version;
/**
* Constructor
@ -41,8 +43,7 @@ public class AVMNode extends Node
*/
public AVMNode(NodeRef nodeRef, ServiceRegistry services)
{
super(nodeRef, services);
this.path = AVMNodeConverter.ToAVMVersionPath(nodeRef).getSecond();
this(nodeRef, services, null);
}
/**
@ -56,11 +57,13 @@ public class AVMNode extends Node
public AVMNode(NodeRef nodeRef, ServiceRegistry services, Scriptable scope)
{
super(nodeRef, services, scope);
this.path = AVMNodeConverter.ToAVMVersionPath(nodeRef).getSecond();
Pair<Integer, String> versionPath = AVMNodeConverter.ToAVMVersionPath(nodeRef);
this.path = versionPath.getSecond();
this.version = versionPath.getFirst();
}
/**
* Factory method
* Factory methods
*/
@Override
public Node newInstance(NodeRef nodeRef, ServiceRegistry services, Scriptable scope)
@ -68,6 +71,11 @@ public class AVMNode extends Node
return new AVMNode(nodeRef, services, scope);
}
public Node newInstance(String path, int version, ServiceRegistry services, Scriptable scope)
{
return new AVMNode(AVMNodeConverter.ToNodeRef(version, path), services, scope);
}
// TODO: changing the 'name' property (either directly using .name or with .properties.name)
// invalidates the path and the base noderef instance!
// AVMService has a specific rename method - use this and block name property changes?
@ -85,6 +93,16 @@ public class AVMNode extends Node
return getPath();
}
public int getVersion()
{
return this.version;
}
public int jsGet_version()
{
return getVersion();
}
/**
* Copy this Node into a new parent destination.
*
@ -95,14 +113,9 @@ public class AVMNode extends Node
@Override
public Node copy(Node destination)
{
Node copy = null;
ParameterCheck.mandatory("Destination Node", destination);
if (destination instanceof AVMNode)
{
copy = copy(((AVMNode)destination).getPath());
}
return copy;
return getCrossRepositoryCopyHelper().copy(this, destination, getName());
}
/**
@ -114,19 +127,14 @@ public class AVMNode extends Node
*/
public Node copy(String destination)
{
Node copy = null;
ParameterCheck.mandatoryString("Destination Path", destination);
if (destination != null && destination.length() != 0)
{
this.services.getAVMService().copy(-1, getPath(), destination, getName());
copy = newInstance(
AVMNodeConverter.ToNodeRef(-1, destination + '/' + getName()),
this.services.getAVMService().copy(this.version, this.path, destination, getName());
return newInstance(
AVMNodeConverter.ToNodeRef(-1, AVMNodeConverter.ExtendAVMPath(destination, getName())),
this.services, this.scope);
}
return copy;
}
/**
* Move this Node to a new parent destination node.
*
@ -137,6 +145,8 @@ public class AVMNode extends Node
@Override
public boolean move(Node destination)
{
ParameterCheck.mandatory("Destination Node", destination);
boolean success = false;
if (destination instanceof AVMNode)
@ -156,6 +166,8 @@ public class AVMNode extends Node
*/
public boolean move(String destination)
{
ParameterCheck.mandatoryString("Destination Path", destination);
boolean success = false;
if (destination != null && destination.length() != 0)
@ -164,7 +176,7 @@ public class AVMNode extends Node
this.services.getAVMService().rename(
parent.getPath(), getName(), destination, getName());
reset(destination + '/' + getName());
reset(AVMNodeConverter.ExtendAVMPath(destination, getName()));
success = true;
}
@ -181,6 +193,8 @@ public class AVMNode extends Node
*/
public boolean rename(String name)
{
ParameterCheck.mandatoryString("Destination name", name);
boolean success = false;
if (name != null && name.length() != 0)
@ -189,7 +203,7 @@ public class AVMNode extends Node
this.services.getAVMService().rename(
parentPath, getName(), parentPath, name);
reset(parentPath + '/' + name);
reset(AVMNodeConverter.ExtendAVMPath(parentPath, name));
success = true;
}
@ -204,14 +218,14 @@ public class AVMNode extends Node
{
super.reset();
this.path = path;
this.nodeRef = AVMNodeConverter.ToNodeRef(-1, path);
this.nodeRef = AVMNodeConverter.ToNodeRef(version, path);
this.id = nodeRef.getId();
}
@Override
public String toString()
{
if (this.services.getAVMService().lookup(-1, this.path) != null)
if (this.services.getAVMService().lookup(version, this.path) != null)
{
return "AVM Path: " + getPath() +
"\nNode Type: " + getType() +

View File

@ -0,0 +1,103 @@
/*
* Copyright (C) 2005 Alfresco, Inc.
*
* Licensed under the Mozilla Public License version 1.1
* with a permitted attribution clause. You may obtain a
* copy of the License at
*
* http://www.alfresco.org/legal/license.txt
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the
* License.
*/
package org.alfresco.repo.jscript;
import org.alfresco.repo.avm.AVMNodeConverter;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.avm.AVMNodeDescriptor;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.util.Pair;
import org.alfresco.util.ParameterCheck;
import org.mozilla.javascript.Scriptable;
/**
* Helper bean to access Cross Repository copy services from a script context.
*
* @author Kevin Roast
*/
public final class CrossRepositoryCopy extends BaseScriptImplementation implements Scopeable
{
public final static String BEAN_NAME = "crossCopyScript";
/** Service registry */
private ServiceRegistry services;
/** Root scope for this object */
private Scriptable scope;
/**
* @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable)
*/
public void setScope(Scriptable scope)
{
this.scope = scope;
}
/**
* Set the service registry
*
* @param services the service registry
*/
public void setServiceRegistry(ServiceRegistry services)
{
this.services = services;
}
/**
* Perform a copy of a source node to the specified parent destination node. The name will
* be applied to the destination node copy.
* <p>
* Inter-store copy operations between Workspace and AVM and visa-versa are supported.
*
* @param src Source node instance
* @param dest Destination parent node instance
* @param name Name of the node copy
*
* @return node representing the copy if successful, null on unsupported store type.
*
* @throws org.alfresco.error.AlfrescoRuntimeException on copy error
*/
public Node copy(Node src, Node dest, String name)
{
ParameterCheck.mandatory("Node source", src);
ParameterCheck.mandatory("Node destination", dest);
ParameterCheck.mandatory("Node destination name", name);
Node result = null;
// perform the copy operation using the repository service
this.services.getCrossRepositoryCopyService().copy(src.getNodeRef(), dest.getNodeRef(), name);
// if we get here then copy succeeded - retrieve the new node reference
if (dest.getNodeRef().getStoreRef().getProtocol().equals(StoreRef.PROTOCOL_AVM))
{
Pair<Integer, String> versionPath = AVMNodeConverter.ToAVMVersionPath(dest.getNodeRef());
String destPath = AVMNodeConverter.ExtendAVMPath(versionPath.getSecond(), name);
AVMNodeDescriptor node = this.services.getAVMService().lookup(-1, destPath);
if (node != null)
{
result = ((AVMNode)dest).newInstance(destPath, -1, this.services, this.scope);
}
}
else if (dest.getNodeRef().getStoreRef().getProtocol().equals(StoreRef.PROTOCOL_WORKSPACE))
{
result = dest.childByNamePath(name);
}
return result;
}
}

View File

@ -49,6 +49,7 @@ import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.NoTransformerException;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.TemplateImageResolver;
import org.alfresco.service.cmr.repository.TemplateNode;
import org.alfresco.service.cmr.security.AccessStatus;
@ -58,6 +59,8 @@ import org.alfresco.service.cmr.version.VersionType;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.ApplicationContextHelper;
import org.alfresco.util.ParameterCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.Scriptable;
@ -544,6 +547,8 @@ public class Node implements Serializable, Scopeable
*/
public boolean hasPermission(String permission)
{
ParameterCheck.mandatory("Permission Name", permission);
boolean allowed = false;
if (permission != null && permission.length() != 0)
@ -670,6 +675,7 @@ public class Node implements Serializable, Scopeable
return getPrimaryParentAssoc();
}
// ------------------------------------------------------------------------------
// Content API
@ -843,6 +849,7 @@ public class Node implements Serializable, Scopeable
*/
public void setPermission(String permission)
{
ParameterCheck.mandatoryString("Permission Name", permission);
this.services.getPermissionService().setPermission(this.nodeRef, PermissionService.ALL_AUTHORITIES, permission,
true);
}
@ -855,6 +862,8 @@ public class Node implements Serializable, Scopeable
*/
public void setPermission(String permission, String authority)
{
ParameterCheck.mandatoryString("Permission Name", permission);
ParameterCheck.mandatoryString("Authority", authority);
this.services.getPermissionService().setPermission(this.nodeRef, authority, permission, true);
}
@ -865,6 +874,7 @@ public class Node implements Serializable, Scopeable
*/
public void removePermission(String permission)
{
ParameterCheck.mandatoryString("Permission Name", permission);
this.services.getPermissionService()
.deletePermission(this.nodeRef, PermissionService.ALL_AUTHORITIES, permission);
}
@ -877,6 +887,8 @@ public class Node implements Serializable, Scopeable
*/
public void removePermission(String permission, String authority)
{
ParameterCheck.mandatoryString("Permission Name", permission);
ParameterCheck.mandatoryString("Authority", authority);
this.services.getPermissionService().deletePermission(this.nodeRef, authority, permission);
}
@ -955,6 +967,8 @@ public class Node implements Serializable, Scopeable
*/
public boolean specializeType(String type)
{
ParameterCheck.mandatoryString("Type", type);
QName qnameType = createQName(type);
// Ensure that we are performing a specialise
@ -1126,16 +1140,23 @@ public class Node implements Serializable, Scopeable
*/
public Node copy(Node destination, boolean deepCopy)
{
ParameterCheck.mandatory("Destination Node", destination);
Node copy = null;
try
{
if (destination != null)
if (destination.getNodeRef().getStoreRef().getProtocol().equals(StoreRef.PROTOCOL_WORKSPACE))
{
NodeRef copyRef = this.services.getCopyService().copyAndRename(this.nodeRef, destination.getNodeRef(),
ContentModel.ASSOC_CONTAINS, getPrimaryParentAssoc().getQName(), deepCopy);
copy = newInstance(copyRef, this.services, this.scope);
}
else
{
// NOTE: the deepCopy flag is not respected for this copy mechanism
copy = getCrossRepositoryCopyHelper().copy(this, destination, getName());
}
}
catch (AccessDeniedException accessErr)
{
@ -1158,11 +1179,11 @@ public class Node implements Serializable, Scopeable
*/
public boolean move(Node destination)
{
ParameterCheck.mandatory("Destination Node", destination);
boolean success = false;
try
{
if (destination != null)
{
this.primaryParentAssoc = this.nodeService.moveNode(this.nodeRef, destination.getNodeRef(),
ContentModel.ASSOC_CONTAINS, getPrimaryParentAssoc().getQName());
@ -1172,7 +1193,6 @@ public class Node implements Serializable, Scopeable
success = true;
}
}
catch (AccessDeniedException accessErr)
{
// default of false will be returned
@ -1208,10 +1228,10 @@ public class Node implements Serializable, Scopeable
*/
public boolean addAspect(String type, Object props)
{
ParameterCheck.mandatoryString("Aspect Type", type);
boolean success = false;
if (type != null && type.length() != 0)
{
try
{
Map<QName, Serializable> aspectProps = null;
@ -1250,7 +1270,6 @@ public class Node implements Serializable, Scopeable
{
// default of failed will be returned
}
}
return success;
}
@ -1264,20 +1283,15 @@ public class Node implements Serializable, Scopeable
*/
public boolean removeAspect(String type)
{
boolean success = false;
ParameterCheck.mandatoryString("Aspect Type", type);
if (type != null && type.length() != 0)
{
QName aspectQName = createQName(type);
this.nodeService.removeAspect(this.nodeRef, aspectQName);
// reset the relevant cached node members
reset();
success = true;
}
return success;
return true;
}
@ -1310,6 +1324,8 @@ public class Node implements Serializable, Scopeable
*/
public Node checkout(Node destination)
{
ParameterCheck.mandatory("Destination Node", destination);
ChildAssociationRef childAssocRef = this.nodeService.getPrimaryParent(destination.getNodeRef());
NodeRef workingCopyRef = this.services.getCheckOutCheckInService().checkout(this.nodeRef,
destination.getNodeRef(), ContentModel.ASSOC_CONTAINS, childAssocRef.getQName());
@ -1380,6 +1396,7 @@ public class Node implements Serializable, Scopeable
return newInstance(original, this.services, this.scope);
}
// ------------------------------------------------------------------------------
// Transformation and Rendering API
@ -1412,6 +1429,9 @@ public class Node implements Serializable, Scopeable
private Node transformDocument(String mimetype, NodeRef destination)
{
ParameterCheck.mandatoryString("Mimetype", mimetype);
ParameterCheck.mandatory("Destination Node", destination);
// the delegate definition for transforming a document
Transformer transformer = new Transformer()
{
@ -1519,6 +1539,7 @@ public class Node implements Serializable, Scopeable
*/
public Node transformImage(String mimetype, Node destination)
{
ParameterCheck.mandatory("Destination Node", destination);
return transformImage(mimetype, null, destination.getNodeRef());
}
@ -1535,11 +1556,14 @@ public class Node implements Serializable, Scopeable
*/
public Node transformImage(String mimetype, String options, Node destination)
{
ParameterCheck.mandatory("Destination Node", destination);
return transformImage(mimetype, options, destination.getNodeRef());
}
private Node transformImage(String mimetype, final String options, NodeRef destination)
{
ParameterCheck.mandatoryString("Mimetype", mimetype);
// the delegate definition for transforming an image
Transformer transformer = new Transformer()
{
@ -1574,6 +1598,7 @@ public class Node implements Serializable, Scopeable
*/
public String processTemplate(Node template)
{
ParameterCheck.mandatory("Template Node", template);
return processTemplate(template.getContent(), null, null);
}
@ -1588,6 +1613,7 @@ public class Node implements Serializable, Scopeable
*/
public String processTemplate(Node template, Object args)
{
ParameterCheck.mandatory("Template Node", template);
return processTemplate(template.getContent(), null, (ScriptableObject) args);
}
@ -1600,6 +1626,7 @@ public class Node implements Serializable, Scopeable
*/
public String processTemplate(String template)
{
ParameterCheck.mandatoryString("Template", template);
return processTemplate(template, null, null);
}
@ -1614,6 +1641,7 @@ public class Node implements Serializable, Scopeable
*/
public String processTemplate(String template, Object args)
{
ParameterCheck.mandatoryString("Template", template);
return processTemplate(template, null, (ScriptableObject) args);
}
@ -1730,6 +1758,15 @@ public class Node implements Serializable, Scopeable
this.primaryParentAssoc = null;
}
/**
* @return helper object to perform cross repository copy of JavaScript Node objects
*/
protected CrossRepositoryCopy getCrossRepositoryCopyHelper()
{
return (CrossRepositoryCopy)this.services.getService(
QName.createQName("", CrossRepositoryCopy.BEAN_NAME));
}
/**
* Return a list or a single Node from executing an xpath against the parent Node.
*