/* * 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 java.io.Serializable; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.MessageFormat; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.model.ContentModel; import org.alfresco.repo.action.executer.TransformActionExecuter; import org.alfresco.repo.content.transform.magick.ImageMagickContentTransformer; import org.alfresco.repo.security.permissions.AccessDeniedException; import org.alfresco.repo.template.FreeMarkerProcessor; import org.alfresco.repo.version.VersionModel; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.dictionary.DictionaryService; import org.alfresco.service.cmr.dictionary.InvalidAspectException; import org.alfresco.service.cmr.lock.LockStatus; import org.alfresco.service.cmr.model.FileExistsException; import org.alfresco.service.cmr.model.FileInfo; import org.alfresco.service.cmr.repository.AssociationRef; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ContentData; import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentService; import org.alfresco.service.cmr.repository.ContentWriter; 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.TemplateImageResolver; import org.alfresco.service.cmr.repository.TemplateNode; import org.alfresco.service.cmr.security.AccessStatus; import org.alfresco.service.cmr.security.PermissionService; import org.alfresco.service.cmr.version.Version; 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.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; import org.mozilla.javascript.Wrapper; import org.springframework.util.StringUtils; /** * 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
.
*
* Various helper methods are provided to access common and useful node variables such
* as the content url and type information.
*
* @author Kevin Roast
*/
public class Node implements Serializable, Scopeable
{
/**
* Comment for
* The default permissions are found in
* For a container node, this method return the URL to browse to the folder in the web-client
*/
public String getUrl()
{
if (getIsDocument() == true)
{
try
{
return MessageFormat.format(CONTENT_DEFAULT_URL, new Object[] {
nodeRef.getStoreRef().getProtocol(),
nodeRef.getStoreRef().getIdentifier(),
nodeRef.getId(),
StringUtils.replace(URLEncoder.encode(getName(), "UTF-8"), "+", "%20") } );
}
catch (UnsupportedEncodingException err)
{
throw new AlfrescoRuntimeException("Failed to encode content URL for node: " + nodeRef, err);
}
}
else
{
return MessageFormat.format(FOLDER_BROWSE_URL, new Object[] {
nodeRef.getStoreRef().getProtocol(),
nodeRef.getStoreRef().getIdentifier(),
nodeRef.getId() } );
}
}
public String jsGet_url()
{
return getUrl();
}
/**
* @return The mimetype encoding for content attached to the node from the default content property
* (@see ContentModel.PROP_CONTENT)
*/
public String getMimetype()
{
String mimetype = null;
ScriptContentData content = (ScriptContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
if (content != null)
{
mimetype = content.getMimetype();
}
return mimetype;
}
public String jsGet_mimetype()
{
return getMimetype();
}
/**
* Set the mimetype encoding for the content attached to the node from the default content property
* (@see ContentModel.PROP_CONTENT)
*
* @param mimetype Mimetype to set
*/
public void setMimetype(String mimetype)
{
ScriptContentData content = (ScriptContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
if (content != null)
{
content.setMimetype(mimetype);
}
}
public void jsSet_mimetype(String mimetype)
{
setMimetype(mimetype);
}
/**
* @return The size in bytes of the content attached to the node from the default content property
* (@see ContentModel.PROP_CONTENT)
*/
public long getSize()
{
long size = 0;
ScriptContentData content = (ScriptContentData)this.getProperties().get(ContentModel.PROP_CONTENT);
if (content != null)
{
size = content.getSize();
}
return size;
}
public long jsGet_size()
{
return getSize();
}
/**
* @return the image resolver instance used by this node
*/
public TemplateImageResolver getImageResolver()
{
return this.imageResolver;
}
// ------------------------------------------------------------------------------
// Security API
/**
* @return true if the node inherits permissions from the parent node, false otherwise
*/
public boolean inheritsPermissions()
{
return this.services.getPermissionService().getInheritParentPermissions(this.nodeRef);
}
/**
* Set whether this node should inherit permissions from the parent node.
*
* @param inherit True to inherit parent permissions, false otherwise.
*/
public void setInheritsPermissions(boolean inherit)
{
this.services.getPermissionService().setInheritParentPermissions(this.nodeRef, inherit);
}
/**
* Apply a permission for ALL users to the node.
*
* @param permission Permission to apply @see org.alfresco.service.cmr.security.PermissionService
*/
public void setPermission(String permission)
{
this.services.getPermissionService().setPermission(this.nodeRef, PermissionService.ALL_AUTHORITIES, permission, true);
}
/**
* Apply a permission for the specified authority (e.g. username or group) to the node.
*
* @param permission Permission to apply @see org.alfresco.service.cmr.security.PermissionService
* @param authority Authority (generally a username or group name) to apply the permission for
*/
public void setPermission(String permission, String authority)
{
this.services.getPermissionService().setPermission(this.nodeRef, authority, permission, true);
}
/**
* Remove a permission for ALL user from the node.
*
* @param permission Permission to remove @see org.alfresco.service.cmr.security.PermissionService
*/
public void removePermission(String permission)
{
this.services.getPermissionService().deletePermission(this.nodeRef, PermissionService.ALL_AUTHORITIES, permission);
}
/**
* Remove a permission for the specified authority (e.g. username or group) from the node.
*
* @param permission Permission to remove @see org.alfresco.service.cmr.security.PermissionService
* @param authority Authority (generally a username or group name) to apply the permission for
*/
public void removePermission(String permission, String authority)
{
this.services.getPermissionService().deletePermission(this.nodeRef, authority, permission);
}
// ------------------------------------------------------------------------------
// Ownership API
/**
* Set the owner of the node
*/
public void setOwner(String userId)
{
this.services.getOwnableService().setOwner(this.nodeRef, userId);
}
/**
* Take ownership of the node.
*/
public void takeOwnership()
{
this.services.getOwnableService().takeOwnership(this.nodeRef);
}
/**
* Get the owner of the node.
* @return
*/
public String getOwner()
{
return this.services.getOwnableService().getOwner(this.nodeRef);
}
/**
* Make owner available as a property.
*
* @return
*/
public String jsGet_owner()
{
return getOwner();
}
// ------------------------------------------------------------------------------
// Create and Modify API
/**
* Persist the properties of this Node.
*/
public void save()
{
// persist properties back to the node in the DB
Map
* Once created the file should have content set using the serialVersionUID
*/
private static final long serialVersionUID = -3378946227712939600L;
private static Log logger = LogFactory.getLog(Node.class);
private final static String NAMESPACE_BEGIN = "" + QName.NAMESPACE_BEGIN;
private final static String CONTENT_DEFAULT_URL = "/download/direct/{0}/{1}/{2}/{3}";
private final static String CONTENT_PROP_URL = "/download/direct/{0}/{1}/{2}/{3}?property={4}";
private final static String FOLDER_BROWSE_URL = "/navigate/browse/{0}/{1}/{2}";
/** Root scope for this object */
protected Scriptable scope;
/** Node Value Converter */
protected NodeValueConverter converter = null;
/** Cached values */
protected NodeRef nodeRef;
private String name;
private QName type;
protected String id;
/** The aspects applied to this node */
private Setmynode.childrenByXPath("*[@cm:name='Testing']/*");
*/
public Node[] childrenByXPath(String xpath)
{
return getChildrenByXPath(xpath, false);
}
// TODO: find out why this doesn't work - the function defs do not seem to get found
//public Node[] jsFunction_childrenByXPath(String xpath)
//{
// return childrenByXPath(xpath);
//}
/**
* Return the associations for this Node. As a Map of assoc name to an Array of Nodes.
*
* The Map returned implements the Scriptable interface to allow access to the assoc arrays via
* JavaScript associative array access. This means associations of this node can be access thus:
* node.assocs["translations"][0]
*
* @return associations as a Map of assoc name to an Array of Nodes.
*/
public Mapnode.properties["name"]
*
* @return Map of properties for this Node.
*/
public Maporg.alfresco.service.cmr.security.PermissionService
.
* Most commonly used are "Write", "Delete" and "AddChildren".
*
* @param permission as found in org.alfresco.service.cmr.security.PermissionService
*
* @return true if the user has the specified permission on the node.
*/
public boolean hasPermission(String permission)
{
boolean allowed = false;
if (permission != null && permission.length() != 0)
{
AccessStatus status = this.services.getPermissionService().hasPermission(this.nodeRef, permission);
allowed = (AccessStatus.ALLOWED == status);
}
return allowed;
}
/**
* @return Display path to this node
*/
public String getDisplayPath()
{
if (displayPath == null)
{
try
{
displayPath = this.nodeService.getPath(this.nodeRef).toDisplayPath(this.nodeService);
}
catch (AccessDeniedException err)
{
displayPath = "";
}
}
return displayPath;
}
public String jsGet_displayPath()
{
return getDisplayPath();
}
/**
* @return the small icon image for this node
*/
public String getIcon16()
{
if (this.imageResolver != null)
{
if (getIsDocument())
{
return this.imageResolver.resolveImagePathForName(getName(), true);
}
else
{
return "/images/icons/space_small.gif";
}
}
else
{
return "/images/filetypes/_default.gif";
}
}
public String jsGet_icon16()
{
return getIcon16();
}
/**
* @return the large icon image for this node
*/
public String getIcon32()
{
if (this.imageResolver != null)
{
if (getIsDocument())
{
return this.imageResolver.resolveImagePathForName(getName(), false);
}
else
{
String icon = (String)getProperties().get("app:icon");
if (icon != null)
{
return "/images/icons/" + icon + ".gif";
}
else
{
return "/images/icons/space-icon-default.gif";
}
}
}
else
{
return "/images/filetypes32/_default.gif";
}
}
public String jsGet_icon32()
{
return getIcon32();
}
/**
* @return true if the node is currently locked
*/
public boolean isLocked()
{
boolean locked = false;
if (getAspects().contains(ContentModel.ASPECT_LOCKABLE))
{
LockStatus lockStatus = this.services.getLockService().getLockStatus(this.nodeRef);
if (lockStatus == LockStatus.LOCKED || lockStatus == LockStatus.LOCK_OWNER)
{
locked = true;
}
}
return locked;
}
public boolean jsGet_isLocked()
{
return isLocked();
}
/**
* @return the parent node
*/
public Node getParent()
{
if (parent == null)
{
NodeRef parentRef = getPrimaryParentAssoc().getParentRef();
// handle root node (no parent!)
if (parentRef != null)
{
parent = newInstance(parentRef, this.services, this.imageResolver, this.scope);
}
}
return parent;
}
public Node jsGet_parent()
{
return getParent();
}
/**
*
* @return the primary parent association so we can get at the association QName and the association type QName.
*/
public ChildAssociationRef getPrimaryParentAssoc()
{
if (primaryParentAssoc == null)
{
primaryParentAssoc = this.nodeService.getPrimaryParent(nodeRef);
}
return primaryParentAssoc;
}
public ChildAssociationRef jsGet_primaryParentAssoc()
{
return getPrimaryParentAssoc();
}
// ------------------------------------------------------------------------------
// Content API
/**
* @return the content String for this node from the default content property
* (@see ContentModel.PROP_CONTENT)
*/
public String getContent()
{
String content = "";
ScriptContentData contentData = (ScriptContentData)getProperties().get(ContentModel.PROP_CONTENT);
if (contentData != null)
{
content = contentData.getContent();
}
return content;
}
public String jsGet_content()
{
return getContent();
}
/**
* Set the content for this node
*
* @param content Content string to set
*/
public void setContent(String content)
{
ScriptContentData contentData = (ScriptContentData)getProperties().get(ContentModel.PROP_CONTENT);
if (contentData == null)
{
// guess a mimetype based on the filename
String mimetype = this.services.getMimetypeService().guessMimetype(getName());
ContentData cdata = new ContentData(null, mimetype, 0L, "UTF-8");
contentData = new ScriptContentData(cdata, ContentModel.PROP_CONTENT);
getProperties().put(ContentModel.PROP_CONTENT.toString(), contentData);
}
contentData.setContent(content);
}
public void jsSet_content(String content)
{
setContent(content);
}
/**
* @return For a content document, this method returns the URL to the content stream for
* the default content property (@see ContentModel.PROP_CONTENT)
* content
property.
*
* @param name Name of the file to create
*
* @return Newly created Node or null if failed to create.
*/
public Node createFile(String name)
{
Node node = null;
try
{
if (name != null && name.length() != 0)
{
FileInfo fileInfo = this.services.getFileFolderService().create(
this.nodeRef, name, ContentModel.TYPE_CONTENT);
node = newInstance(fileInfo.getNodeRef(), this.services, this.imageResolver, this.scope);
}
}
catch (FileExistsException fileErr)
{
// default of null will be returned
// TODO: how to report this kind of exception to the script writer?
}
catch (AccessDeniedException accessErr)
{
// default of null will be returned
}
return node;
}
/**
* Create a new folder (cm:folder) node as a child of this node.
*
* @param name Name of the folder to create
*
* @return Newly created Node or null if failed to create.
*/
public Node createFolder(String name)
{
Node node = null;
try
{
if (name != null && name.length() != 0)
{
FileInfo fileInfo = this.services.getFileFolderService().create(
this.nodeRef, name, ContentModel.TYPE_FOLDER);
node = newInstance(fileInfo.getNodeRef(), this.services, this.imageResolver, this.scope);
}
}
catch (FileExistsException fileErr)
{
// default of null will be returned
// TODO: how to report this kind of exception to the script writer?
}
catch (AccessDeniedException accessErr)
{
// default of null will be returned
}
return node;
}
/**
* Create a new Node of the specified type as a child of this node.
*
* @param name Name of the node to create
* @param type QName type (can either be fully qualified or short form such as 'cm:content')
*
* @return Newly created Node or null if failed to create.
*/
public Node createNode(String name, String type)
{
Node node = null;
try
{
if (name != null && name.length() != 0 &&
type != null && type.length() != 0)
{
Map