Java Code Examples for org.alfresco.service.cmr.repository.Path#Element

The following examples show how to use org.alfresco.service.cmr.repository.Path#Element . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ACPExportPackageHandler.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * @param path Path
 * @return  display path
 */
private String toDisplayPath(Path path)
{
    StringBuffer displayPath = new StringBuffer();
    if (path.size() == 1)
    {
        displayPath.append("/");
    }
    else
    {
        for (int i = 1; i < path.size(); i++)
        {
            Path.Element element = path.get(i);
            if (element instanceof ChildAssocElement)
            {
                ChildAssociationRef assocRef = ((ChildAssocElement)element).getRef();
                NodeRef node = assocRef.getChildRef();
                displayPath.append("/");
                displayPath.append(nodeService.getProperty(node, ContentModel.PROP_NAME));
            }
        }
    }
    return displayPath.toString();
}
 
Example 2
Source File: NodesMetaDataGet.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 6 votes vote down vote up
private ArrayList<NodeRef> getAncestors(Path path)
{
    ArrayList<NodeRef> ancestors = new ArrayList<NodeRef>(8);
    for (Iterator<Path.Element> elit = path.iterator(); elit.hasNext(); /**/)
    {
        Path.Element element = elit.next();
        if (!(element instanceof Path.ChildAssocElement))
        {
            throw new IndexerException("Confused path: " + path);
        }
        Path.ChildAssocElement cae = (Path.ChildAssocElement) element;
        NodeRef parentRef = cae.getRef().getParentRef();
        if(parentRef != null)
        {
            ancestors.add(0, parentRef);
        }

    }
    return ancestors;
}
 
Example 3
Source File: PathUtil.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Return the node ids from the specified node Path, so that
 * the first element is the immediate parent.
 *
 * @param path     the node's path object
 * @param showLeaf whether to process the final leaf element of the path
 * @return list of node ids
 */
public static List<String> getNodeIdsInReverse(Path path, boolean showLeaf)
{
    int count = path.size() - (showLeaf ? 1 : 2);
    if (count < 0)
    {
        return Collections.emptyList();
    }

    List<String> nodeIds = new ArrayList<>(count);
    // Add in reverse order (so the first element is the immediate parent)
    for (int i = count; i >= 0; i--)
    {
        Path.Element element = path.get(i);
        if (element instanceof ChildAssocElement)
        {
            ChildAssocElement childAssocElem = (ChildAssocElement) element;
            NodeRef childNodeRef = childAssocElem.getRef().getChildRef();
            nodeIds.add(childNodeRef.getId());
        }
    }

    return nodeIds;
}
 
Example 4
Source File: LuceneCategoryServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private String buildXPath(Path path)
{
    StringBuilder pathBuffer = new StringBuilder(64);
    for (Iterator<Path.Element> elit = path.iterator(); elit.hasNext(); /**/)
    {
        Path.Element element = elit.next();
        if (!(element instanceof Path.ChildAssocElement))
        {
            throw new IndexerException("Confused path: " + path);
        }
        Path.ChildAssocElement cae = (Path.ChildAssocElement) element;
        if (cae.getRef().getParentRef() != null)
        {
            pathBuffer.append("/");
            pathBuffer.append(getPrefix(cae.getRef().getQName().getNamespaceURI()));
            pathBuffer.append(ISO9075.encode(cae.getRef().getQName().getLocalName()));
        }
    }
    return pathBuffer.toString();
}
 
Example 5
Source File: AlfrescoScriptVirtualContext.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @return an array containing the plain qname path at index 0 and the
 *         ISO9075 element-encoded qname path at index 1
 */
private String[] createQNamePaths()
{
    final NamespaceService ns = serviceRegistry.getNamespaceService();
    final Map<String, String> cache = new HashMap<String, String>();
    final StringBuilder bufPlain = new StringBuilder(128);
    final StringBuilder bufISO9075 = new StringBuilder(128);

    final Path path = serviceRegistry.getNodeService().getPath(context.getActualNodeRef());
    for (final Path.Element e : path)
    {
        if (e instanceof Path.ChildAssocElement)
        {
            final QName qname = ((Path.ChildAssocElement) e).getRef().getQName();
            if (qname != null)
            {
                String prefix = cache.get(qname.getNamespaceURI());
                if (prefix == null)
                {
                    // first request for this namespace prefix, get and
                    // cache result
                    Collection<String> prefixes = ns.getPrefixes(qname.getNamespaceURI());
                    prefix = prefixes.size() != 0 ? prefixes.iterator().next() : "";
                    cache.put(qname.getNamespaceURI(),
                              prefix);
                }
                bufISO9075.append('/').append(prefix).append(':').append(ISO9075.encode(qname.getLocalName()));
                bufPlain.append('/').append(prefix).append(':').append(qname.getLocalName());
            }
        }
        else
        {
            bufISO9075.append('/').append(e.toString());
            bufPlain.append('/').append(e.toString());
        }
    }
    String[] qnamePaths = new String[] { bufPlain.toString(), bufISO9075.toString() };

    return qnamePaths;
}
 
Example 6
Source File: ScriptNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the short name of the site this node is located within. If the 
 * node is not located within a site null is returned.
 * 
 * @return The short name of the site this node is located within, null
 *         if the node is not located within a site.
 */
public String getSiteShortName()
{
    if (!this.siteNameResolved)
    {
        this.siteNameResolved = true;
        
        Path path = this.services.getNodeService().getPath(getNodeRef());
        
        if (logger.isDebugEnabled())
            logger.debug("Determing if node is within a site using path: " + path);
        
        for (int i = 0; i < path.size(); i++)
        {
            if ("st:sites".equals(path.get(i).getPrefixedString(this.services.getNamespaceService())))
            {
                // we now know the node is in a site, find the next element in the array (if there is one)
                if ((i+1) < path.size())
                {
                    // get the site name
                    Path.Element siteName = path.get(i+1);
                 
                    // remove the "cm:" prefix and add to result object
                    this.siteName = ISO9075.decode(siteName.getPrefixedString(
                                this.services.getNamespaceService()).substring(3));
                }
              
                break;
            }
        }
    }
    
    if (logger.isDebugEnabled())
    {
        logger.debug(this.siteName != null ? 
                    "Node is in the site named \"" + this.siteName + "\"" : "Node is not in a site");
    }
    
    return this.siteName;
}
 
Example 7
Source File: BaseContentNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns the short name of the site this node is located within. If the 
 * node is not located within a site null is returned.
 * 
 * @return The short name of the site this node is located within, null
 *         if the node is not located within a site.
 */
public String getSiteShortName()
{
    if (!this.siteNameResolved)
    {
        this.siteNameResolved = true;
        
        Path path = this.services.getNodeService().getPath(getNodeRef());
        
        for (int i = 0; i < path.size(); i++)
        {
            if ("st:sites".equals(path.get(i).getPrefixedString(this.services.getNamespaceService())))
            {
                // we now know the node is in a site, find the next element in the array (if there is one)
                if ((i+1) < path.size())
                {
                    // get the site name
                    Path.Element siteName = path.get(i+1);
                 
                    // remove the "cm:" prefix and add to result object
                    this.siteName = ISO9075.decode(siteName.getPrefixedString(
                                this.services.getNamespaceService()).substring(3));
                }
              
                break;
            }
        }
    }
    
    return this.siteName;
}
 
Example 8
Source File: AbstractEventsService.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * For each path, construct a list of ancestor node ids starting with the parent node id first.
 * 
 * @param nodePaths
 * @return
 */
protected List<List<String>> getNodeIdsFromParent(List<Path> nodePaths)
{
    // TODO use fileFolderService.getNamePath instead?
    List<List<String>> pathNodeIds = new ArrayList<List<String>>(nodePaths.size());
    for(Path path : nodePaths)
    {
        List<String> nodeIds = new ArrayList<String>(path.size());

        // don't include the leaf node id
        // add in reverse order (so the first element is the immediate parent)
        for(int i = path.size() - 2; i >= 0; i--)
        {
            Path.Element element = path.get(i);
            if(element instanceof ChildAssocElement)
            {
                ChildAssocElement childAssocElem = (ChildAssocElement)element;
                NodeRef childNodeRef = childAssocElem.getRef().getChildRef();
                nodeIds.add(childNodeRef.getId());
            }
        }

        pathNodeIds.add(nodeIds);
    }

    return pathNodeIds;
}
 
Example 9
Source File: AbstractEventsService.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * For each path, construct a list of ancestor node ids starting with the leaf path element node id first.
 * 
 * @param nodePaths
 * @return
 */
protected List<List<String>> getNodeIds(List<Path> nodePaths)
{
    // TODO use fileFolderService.getNamePath instead?
    List<List<String>> pathNodeIds = new ArrayList<List<String>>(nodePaths.size());
    for(Path path : nodePaths)
    {
        List<String> nodeIds = new ArrayList<String>(path.size());

        // don't include the leaf node id
        // add in reverse order (so the first element is the immediate parent)
        for(int i = path.size() - 1; i >= 0; i--)
        {
            Path.Element element = path.get(i);
            if(element instanceof ChildAssocElement)
            {
                ChildAssocElement childAssocElem = (ChildAssocElement)element;
                NodeRef childNodeRef = childAssocElem.getRef().getChildRef();
                nodeIds.add(childNodeRef.getId());
            }
        }

        pathNodeIds.add(nodeIds);
    }

    return pathNodeIds;
}
 
Example 10
Source File: PathUtil.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Return the human readable form of the specified node Path. Fast version
 * of the method that simply converts QName localname components to Strings.
 * 
 * @param path Path to extract readable form from
 * @param showLeaf Whether to process the final leaf element of the path
 * 
 * @return human readable form of the Path
 */
public static String getDisplayPath(Path path, boolean showLeaf)
{
    // This method was moved here from org.alfresco.web.bean.repository.Repository
    StringBuilder buf = new StringBuilder(64);

    int count = path.size() - (showLeaf ? 0 : 1);
    for (int i = 0; i < count; i++)
    {
        String elementString = null;
        Path.Element element = path.get(i);
        if (element instanceof Path.ChildAssocElement)
        {
            ChildAssociationRef elementRef = ((Path.ChildAssocElement) element).getRef();
            if (elementRef.getParentRef() != null)
            {
                elementString = elementRef.getQName().getLocalName();
            }
        } else
        {
            elementString = element.getElementString();
        }

        if (elementString != null)
        {
            buf.append("/");
            buf.append(elementString);
        }
    }

    return buf.toString();
}
 
Example 11
Source File: ViewXMLExporter.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Helper to convert a path into an indexed path which uniquely identifies a node
 * 
 * @param nodeRef NodeRef
 * @param path Path
 * @return Path
 */
private Path createIndexedPath(NodeRef nodeRef, Path path)
{
    // Add indexes for same name siblings
    // TODO: Look at more efficient approach
    for (int i = path.size() - 1; i >= 0; i--)
    {
        Path.Element pathElement = path.get(i);
        if (i > 0 && pathElement instanceof Path.ChildAssocElement)
        {
            int index = 1;  // for xpath index compatibility
            String searchPath = path.subPath(i).toPrefixString(namespaceService);
            List<NodeRef> siblings = searchService.selectNodes(nodeRef, searchPath, null, namespaceService, false);
            if (siblings.size() > 1)
            {
                ChildAssociationRef childAssoc = ((Path.ChildAssocElement)pathElement).getRef();
                NodeRef childRef = childAssoc.getChildRef();
                for (NodeRef sibling : siblings)
                {
                    if (sibling.equals(childRef))
                    {
                        childAssoc.setNthSibling(index);
                        break;
                    }
                    index++;
                }
            }
        }
    }
    
    return path;
}
 
Example 12
Source File: NodeChangeTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("serial")
private Path newPath(Path parent, final String name)
{
    Path path = new Path();
    if (parent != null)
    {
        for(Path.Element element: parent)
        {
            path.append(element);
        }
    }
    path.append(new Path.Element()
    {
        @Override
        public String getElementString()
        {
            return name;
        }

        @Override
        public Element getBaseNameElement(TenantService tenantService)
        {
           return this;
        }
    });
    return path;
}
 
Example 13
Source File: NodeBrowserPost.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Gets the current node primary parent reference
 * 
 * @return primary parent ref
 */
public NodeRef getPrimaryParent(NodeRef nodeRef)
{
    Path primaryPath = getNodeService().getPath(nodeRef);
    Path.Element element = primaryPath.last();
    NodeRef parentRef = ((Path.ChildAssocElement) element).getRef().getParentRef();
    return parentRef;
}
 
Example 14
Source File: FileFolderServiceImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Get the file or folder information from the root down to and including the node provided.
 * <ul>
 *   <li>The root node can be of any type and is not included in the path list.</li>
 *   <li>Only the primary path is considered.  If the target node is not a descendant of the
 *       root along purely primary associations, then an exception is generated.</li>
 *   <li>If an invalid type is encountered along the path, then an exception is generated.</li>
 * </ul>
 * 
 * @param rootNodeRef the start of the returned path, or null if the <b>store</b> root
 *        node must be assumed.
 * @param nodeRef a reference to the file or folder
 * @return Returns a list of file/folder infos from the root (excluded) down to and
 *         including the destination file or folder
 * @throws FileNotFoundException if the node could not be found
 */
@Override
public List<FileInfo> getNamePath(NodeRef rootNodeRef, NodeRef nodeRef) throws FileNotFoundException
{
    // check the root
    if (rootNodeRef == null)
    {
        rootNodeRef = nodeService.getRootNode(nodeRef.getStoreRef());
    }
    try
    {
        ArrayList<FileInfo> results = new ArrayList<FileInfo>(10);
        // get the primary path
        Path path = nodeService.getPath(nodeRef);
        // iterate and turn the results into file info objects
        boolean foundRoot = false;
        for (Path.Element element : path)
        {
            // ignore everything down to the root
            Path.ChildAssocElement assocElement = (Path.ChildAssocElement) element;
            final NodeRef childNodeRef = assocElement.getRef().getChildRef();
            if (childNodeRef.equals(rootNodeRef))
            {
                // just found the root - but we don't put in an entry for it
                foundRoot = true;
                continue;
            }
            else if (!foundRoot)
            {
                // keep looking for the root
                continue;
            }
            // we found the root and expect to be building the path up
            // Run as system as the user could not have access to all folders in the path, see ALF-13816
            FileInfo pathInfo = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<FileInfo>()
            {
                public FileInfo doWork() throws Exception
                {
                    return toFileInfo(childNodeRef, true);
                }
            }, AuthenticationUtil.getSystemUserName());
            
            // we can't append a path element to the results if there is already a (non-folder) file at the tail
            // since this would result in a path anomoly - file's cannot contain other files.
            if (!results.isEmpty() && !results.get(results.size()-1).isFolder())
            {
                throw new InvalidTypeException(
                            "File is not the last element in path: files cannot contain other files.");
            }
            results.add(pathInfo);
        }
        // check that we found the root
        if (!foundRoot)
        {
            throw new FileNotFoundException(nodeRef);
        }
        // done
        if (logger.isDebugEnabled())
        {
            logger.debug("Built name path for node: \n" +
                    "   root: " + rootNodeRef + "\n" +
                    "   node: " + nodeRef + "\n" +
                    "   path: " + results);
        }
        return results;
    }
    catch (InvalidNodeRefException e)
    {
        throw new FileNotFoundException(nodeRef);
    }
}
 
Example 15
Source File: ScriptNode.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return QName path to this node. This can be used for Lucene PATH: style queries
 */
public String getQnamePath()
{
    if (this.qnamePath == null)
    {
        final NamespaceService ns = this.services.getNamespaceService();
        final Map<String, String> cache = new HashMap<String, String>();
        final StringBuilder buf = new StringBuilder(128);
        final Path path = this.services.getNodeService().getPath(getNodeRef());
        for (final Path.Element e : path)
        {
            if (e instanceof Path.ChildAssocElement)
            {
                final QName qname = ((Path.ChildAssocElement)e).getRef().getQName();
                if (qname != null)
                {
                    String prefix = cache.get(qname.getNamespaceURI());
                    if (prefix == null)
                    {
                        // first request for this namespace prefix, get and cache result
                        Collection<String> prefixes = ns.getPrefixes(qname.getNamespaceURI());
                        prefix = prefixes.size() != 0 ? prefixes.iterator().next() : "";
                        cache.put(qname.getNamespaceURI(), prefix);
                    }
                    buf.append('/');
                    if(prefix.length() > 0)
                    {
                    	  buf.append(prefix).append(':');
                    }
                    buf.append(ISO9075.encode(qname.getLocalName()));
                }
            }
            else
            {
                buf.append('/').append(e.toString());
            }
        }
        this.qnamePath = buf.toString();
    }
    
    return this.qnamePath;
}
 
Example 16
Source File: HomeFolderProviderSynchronizerTest.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private String toPath(NodeRef root, NodeRef homeFolder)
{
    if (root == null || homeFolder == null)
    {
        return null;
    }
    
    if (root.equals(homeFolder))
    {
        return ".";
    }
    
    Path rootPath = nodeService.getPath(root);
    Path homeFolderPath = nodeService.getPath(homeFolder);
    int rootSize = rootPath.size();
    int homeFolderSize = homeFolderPath.size();
    if (rootSize >= homeFolderSize)
    {
        return null;
    }
    
    StringBuilder sb = new StringBuilder("");

    // Check homeFolder is under root
    for (int i=0; i < rootSize; i++)
    {
        if (!rootPath.get(i).equals(homeFolderPath.get(i)))
        {
            return null;
        }
    }
    
    // Build up path of sub folders
    for (int i = rootSize; i < homeFolderSize; i++)
    {
        Path.Element element = homeFolderPath.get(i);
        if (!(element instanceof Path.ChildAssocElement))
        {
            return null;
        }
        QName folderQName = ((Path.ChildAssocElement) element).getRef().getQName();
        if (sb.length() > 0)
        {
            sb.append('/');
        }
        sb.append(folderQName.getLocalName());
    }
    return sb.toString();
}
 
Example 17
Source File: HomeFolderProviderSynchronizer.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return the relative 'path' (a list of folder names) of the {@code homeFolder}
 * from the {@code root} or {@code null} if the homeFolder is not under the root
 * or is the root. An empty list is returned if the homeFolder is the same as the
 * root or the root is below the homeFolder.
 */
private List<String> getRelativePath(NodeRef root, NodeRef homeFolder)
{
    if (root == null || homeFolder == null)
    {
        return null;
    }
    
    if (root.equals(homeFolder))
    {
        return Collections.emptyList();
    }
    
    Path rootPath = nodeService.getPath(root);
    Path homeFolderPath = nodeService.getPath(homeFolder);
    int rootSize = rootPath.size();
    int homeFolderSize = homeFolderPath.size();
    if (rootSize >= homeFolderSize)
    {
        return Collections.emptyList();
    }
    
    // Check homeFolder is under root
    for (int i=0; i < rootSize; i++)
    {
        if (!rootPath.get(i).equals(homeFolderPath.get(i)))
        {
            return null;
        }
    }
    
    // Build up path of sub folders
    List<String> path = new ArrayList<String>();
    for (int i = rootSize; i < homeFolderSize; i++)
    {
        Path.Element element = homeFolderPath.get(i);
        if (!(element instanceof Path.ChildAssocElement))
        {
            return null;
        }
        QName folderQName = ((Path.ChildAssocElement) element).getRef().getQName();
        path.add(folderQName.getLocalName());
    }
    return path;
}