diff --git a/source/java/org/alfresco/repo/jscript/AVM.java b/source/java/org/alfresco/repo/jscript/AVM.java new file mode 100644 index 0000000000..22a83427a9 --- /dev/null +++ b/source/java/org/alfresco/repo/jscript/AVM.java @@ -0,0 +1,109 @@ +/* + * 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.avm.AVMService; +import org.mozilla.javascript.Scriptable; + +/** + * @author Kevin Roast + */ +public final class AVM implements Scopeable +{ + /** Root scope for this object */ + private Scriptable scope; + + private AVMService avmService; + private ServiceRegistry services; + + /** + * Constructor + */ + public AVM(ServiceRegistry services) + { + this.services = services; + this.avmService = services.getAVMService(); + } + + /** + * @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable) + */ + public void setScope(Scriptable scope) + { + this.scope = scope; + } + + /** + * Return an AVM Node representing the public store root folder. + * + * @param store Store name to lookup root folder for + * + * @return AVM Node representing the public store root folder, or null if not found. + */ + public AVMNode lookupStoreRoot(String store) + { + AVMNode rootNode = null; + if (store != null && store.length() != 0) + { + String rootPath = store + ':' + getWebappsFolderPath(); + AVMNodeDescriptor nodeDesc = this.avmService.lookup(-1, rootPath); + if (nodeDesc != null) + { + rootNode = new AVMNode(AVMNodeConverter.ToNodeRef(-1, rootPath), this.services, null, this.scope); + } + } + return rootNode; + } + + /** + * Return an AVM Node for the fully qualified path. + * + * @param path Fully qualified path to node to lookup + * + * @return AVM Node for the fully qualified path, or null if not found. + */ + public AVMNode lookupNode(String path) + { + AVMNode node = null; + if (path != null && path.length() != 0) + { + AVMNodeDescriptor nodeDesc = this.avmService.lookup(-1, path); + if (nodeDesc != null) + { + node = new AVMNode(AVMNodeConverter.ToNodeRef(-1, path), this.services, null, this.scope); + } + } + return node; + } + + public static String getWebappsFolderPath() + { + return '/' + DIR_APPBASE + '/' + DIR_WEBAPPS; + } + + public static String jsGet_webappsFolderPath() + { + return getWebappsFolderPath(); + } + + // system directories at the top level of an AVM website + private final static String DIR_APPBASE = "appBase"; + private final static String DIR_WEBAPPS = "avm_webapps"; +} diff --git a/source/java/org/alfresco/repo/jscript/AVMNode.java b/source/java/org/alfresco/repo/jscript/AVMNode.java new file mode 100644 index 0000000000..e979791db2 --- /dev/null +++ b/source/java/org/alfresco/repo/jscript/AVMNode.java @@ -0,0 +1,192 @@ +/* + * 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.error.AlfrescoRuntimeException; +import org.alfresco.model.ContentModel; +import org.alfresco.repo.avm.AVMNodeConverter; +import org.alfresco.repo.security.permissions.AccessDeniedException; +import org.alfresco.service.ServiceRegistry; +import org.alfresco.service.cmr.repository.InvalidNodeRefException; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.service.cmr.repository.TemplateImageResolver; +import org.mozilla.javascript.Scriptable; + +/** + * @author Kevin Roast + */ +public class AVMNode extends Node +{ + private String path; + + /** + * Constructor + * + * @param nodeRef + * @param services + * @param resolver + */ + public AVMNode(NodeRef nodeRef, ServiceRegistry services, TemplateImageResolver resolver) + { + super(nodeRef, services, resolver); + this.path = AVMNodeConverter.ToAVMVersionPath(nodeRef).getSecond(); + } + + /** + * Constructor + * + * @param nodeRef + * @param services + * @param resolver + * @param scope + */ + public AVMNode(NodeRef nodeRef, ServiceRegistry services, TemplateImageResolver resolver, Scriptable scope) + { + super(nodeRef, services, resolver, scope); + this.path = AVMNodeConverter.ToAVMVersionPath(nodeRef).getSecond(); + } + + /** + * Factory method + */ + @Override + public Node newInstance(NodeRef nodeRef, ServiceRegistry services, TemplateImageResolver resolver, Scriptable scope) + { + return new AVMNode(nodeRef, services, resolver, 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? + + /** + * @return the full AVM Path to this node + */ + public String getPath() + { + return this.path; + } + + public String jsGet_path() + { + return getPath(); + } + + /*@Override + public Node copy(Node destination) + { + AVM + this.services.getAVMService(). + }*/ + + /** + * Move this Node to a new parent destination node. + * + * @param destination Node + * + * @return true on successful move, false on failure to move. + */ + @Override + public boolean move(Node destination) + { + boolean success = false; + + if (destination instanceof AVMNode) + { + success = move(((AVMNode)destination).getPath()); + } + + return success; + } + + /** + * Move this Node to a new parent destination path. + * + * @param destination Path + * + * @return true on successful move, false on failure to move. + */ + public boolean move(String destination) + { + boolean success = false; + + if (destination != null && destination.length() != 0) + { + AVMNode parent = (AVMNode)this.getParent(); + this.services.getAVMService().rename( + parent.getPath(), getName(), destination, getName()); + + reset(destination + '/' + getName()); + + success = true; + } + + return success; + } + + /** + * Rename this node to the specified name + * + * @param name New name for the node + * + * @return true on success, false otherwise + */ + public boolean rename(String name) + { + boolean success = false; + + if (name != null && name.length() != 0) + { + String parentPath = ((AVMNode)this.getParent()).getPath(); + this.services.getAVMService().rename( + parentPath, getName(), parentPath, name); + + reset(parentPath + '/' + name); + + success = true; + } + + return success; + } + + /** + * Reset the Node cached state + */ + private void reset(String path) + { + super.reset(); + this.path = path; + this.nodeRef = AVMNodeConverter.ToNodeRef(-1, path); + this.id = nodeRef.getId(); + } + + @Override + public String toString() + { + if (this.services.getAVMService().lookup(-1, this.path) != null) + { + return "AVM Path: " + getPath() + + "\nNode Type: " + getType() + + "\nNode Properties: " + this.getProperties().size() + + "\nNode Aspects: " + this.getAspects().toString(); + } + else + { + return "Node no longer exists: " + nodeRef; + } + } +} diff --git a/source/java/org/alfresco/repo/jscript/Node.java b/source/java/org/alfresco/repo/jscript/Node.java index 5d5195833c..17acb5cbd7 100644 --- a/source/java/org/alfresco/repo/jscript/Node.java +++ b/source/java/org/alfresco/repo/jscript/Node.java @@ -95,13 +95,13 @@ public class Node implements Serializable, Scopeable protected Scriptable scope; /** Node Value Converter */ - private NodeValueConverter converter = null; + protected NodeValueConverter converter = null; /** Cached values */ - private NodeRef nodeRef; + protected NodeRef nodeRef; private String name; private QName type; - private String id; + protected String id; /** The aspects applied to this node */ private Set aspects = null; /** The associations from this node */ @@ -116,7 +116,7 @@ public class Node implements Serializable, Scopeable private Boolean isContainer = null; private String displayPath = null; protected TemplateImageResolver imageResolver = null; - private Node parent = null; + protected Node parent = null; private ChildAssociationRef primaryParentAssoc = null; // NOTE: see the reset() method when adding new cached members! @@ -164,6 +164,14 @@ public class Node implements Serializable, Scopeable this.scope = scope; } + /** + * Factory method + */ + public Node newInstance(NodeRef nodeRef, ServiceRegistry services, TemplateImageResolver resolver, Scriptable scope) + { + return new Node(nodeRef, services, resolver, scope); + } + /** * @see org.alfresco.repo.jscript.Scopeable#setScope(org.mozilla.javascript.Scriptable) */ @@ -283,7 +291,7 @@ public class Node implements Serializable, Scopeable for (int i=0; i opts = new HashMap(1); opts.put(ImageMagickContentTransformer.KEY_OPTIONS, options != null ? options : ""); contentService.getImageTransformer().transform(reader, writer, opts); - transformedNode = new Node(nodeRef, services, imageResolver, scope); + transformedNode = newInstance(nodeRef, services, imageResolver, scope); } catch (NoTransformerException err) { @@ -1783,7 +1796,7 @@ public class Node implements Serializable, Scopeable if (nodes.size() != 0) { result = new Node[1]; - result[0] = new Node(nodes.get(0), this.services, this.imageResolver, this.scope); + result[0] = newInstance(nodes.get(0), this.services, this.imageResolver, this.scope); } } // or all the results @@ -1793,7 +1806,7 @@ public class Node implements Serializable, Scopeable for (int i=0; i