package org.alfresco.repo.jscript; import java.io.Serializable; import java.util.Date; import java.util.HashMap; import java.util.Locale; import java.util.Map; import org.alfresco.repo.nodelocator.CompanyHomeNodeLocator; import org.alfresco.repo.nodelocator.NodeLocatorService; import org.alfresco.repo.nodelocator.SharedHomeNodeLocator; import org.alfresco.repo.nodelocator.SitesHomeNodeLocator; import org.alfresco.repo.nodelocator.UserHomeNodeLocator; import org.alfresco.repo.nodelocator.XPathNodeLocator; import org.alfresco.repo.security.permissions.noop.PermissionServiceNOOPImpl; import org.alfresco.service.ServiceRegistry; import org.alfresco.service.cmr.module.ModuleDetails; import org.alfresco.service.cmr.module.ModuleService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.security.AccessStatus; import org.alfresco.service.cmr.security.PermissionService; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.alfresco.util.ScriptPagingDetails; import org.springframework.extensions.surf.util.I18NUtil; import org.springframework.extensions.surf.util.ISO8601DateFormat; /** * Place for general and miscellaneous utility functions not already found in generic JavaScript. * * @author Kevin Roast */ public class ScriptUtils extends BaseScopableProcessorExtension { private final static String NAMESPACE_BEGIN = "" + QName.NAMESPACE_BEGIN; /** Services */ protected ServiceRegistry services; private NodeService unprotNodeService; /** * Sets the service registry * * @param services the service registry */ public void setServiceRegistry(ServiceRegistry services) { this.services = services; } /** * @param nodeService the NodeService to set */ public void setNodeService(NodeService nodeService) { this.unprotNodeService = nodeService; } /** * Function to return the cm:name display path for a node with minimum performance overhead. * * @param node ScriptNode * @return cm:name based human readable display path for the give node. */ public String displayPath(ScriptNode node) { return this.unprotNodeService.getPath(node.nodeRef).toDisplayPath( this.unprotNodeService, new PermissionServiceNOOPImpl()); } /** * Function to pad a string with zero '0' characters to the required length * * @param s String to pad with leading zero '0' characters * @param len Length to pad to * * @return padded string or the original if already at >=len characters */ public String pad(String s, int len) { String result = s; for (int i=0; i<(len - s.length()); i++) { result = "0" + result; } return result; } /** * Gets a JS node object from a string noderef * * @param nodeRefString string reference to a node * @return a JS node object */ public ScriptNode getNodeFromString(String nodeRefString) { NodeRef nodeRef = new NodeRef(nodeRefString); return (ScriptNode)new ValueConverter().convertValueForScript(this.services, getScope(), null, nodeRef); } /** * Use the Node Locator Service to find the a node reference from a number of possible locator types. * This method is responsible for determining the locator type and then calling the Service as the * Service does not know how to guess which locator to use. *
* This service supports 'virtual' nodes including the following: *
* alfresco://company/home The Company Home root node
* Possible arguments include:
* maxItems - max count of items to return, default -1 (all)
* alfresco://user/home The User Home node under Company Home
* alfresco://company/shared The Shared node under Company Home
* alfresco://sites/home The Sites home node under Company Home
* workspace://.../... Any standard NodeRef
* /app:company_home/cm:... XPath QName style node reference
*
* @param reference The node reference - See above for list of possible node references supported.
*
* @return ScriptNode representing the node or null if not found
*/
public ScriptNode resolveNodeReference(final String reference)
{
if (reference == null)
{
throw new IllegalArgumentException("Node 'reference' argument is mandatory.");
}
final NodeLocatorService locatorService = this.services.getNodeLocatorService();
NodeRef nodeRef = null;
switch (reference)
{
case "alfresco://company/home":
nodeRef = locatorService.getNode(CompanyHomeNodeLocator.NAME, null, null);
break;
case "alfresco://user/home":
nodeRef = locatorService.getNode(UserHomeNodeLocator.NAME, null, null);
break;
case "alfresco://company/shared":
nodeRef = locatorService.getNode(SharedHomeNodeLocator.NAME, null, null);
break;
case "alfresco://sites/home":
nodeRef = locatorService.getNode(SitesHomeNodeLocator.NAME, null, null);
break;
default:
if (reference.indexOf("://") > 0)
{
NodeRef ref = new NodeRef(reference);
if (this.services.getNodeService().exists(ref) &&
this.services.getPermissionService().hasPermission(ref, PermissionService.READ) == AccessStatus.ALLOWED)
{
nodeRef = ref;
}
}
else if (reference.startsWith("/"))
{
final Map
* skipCount - number of items to skip, default -1 (none)
* queryId
* queryExecutionId
*/
public ScriptPagingDetails createPaging(Map