Java Code Examples for org.alfresco.service.cmr.repository.Path#size()

The following examples show how to use org.alfresco.service.cmr.repository.Path#size() . 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: ViewXMLExporter.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
public void startNode(NodeRef nodeRef)
{
    try
    {
        AttributesImpl attrs = new AttributesImpl(); 

        Path path = nodeService.getPath(nodeRef);
        if (path.size() > 1)
        {
            // a child name does not exist for root
            Path.ChildAssocElement pathElement = (Path.ChildAssocElement)path.last();
            QName childQName = pathElement.getRef().getQName();
            attrs.addAttribute(NamespaceService.REPOSITORY_VIEW_1_0_URI, CHILDNAME_LOCALNAME, CHILDNAME_QNAME.toPrefixString(), null, toPrefixString(childQName));
        }
        
        QName type = nodeService.getType(nodeRef);
        contentHandler.startElement(type.getNamespaceURI(), type.getLocalName(), toPrefixString(type), attrs);
    }
    catch (SAXException e)
    {
        throw new ExporterException("Failed to process start node event - node ref " + nodeRef.toString(), e);
    }
}
 
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: 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 5
Source File: BasicCorrespondingNodeResolverImpl.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param store StoreRef
 * @param parentPath Path
 * @return NodeRef
 */
private NodeRef resolveParentPath(StoreRef store, Path parentPath)
{
    if (log.isDebugEnabled()) 
    {
        log.debug("Trying to resolve parent path " + parentPath);
    }
    NodeRef node = nodeService.getRootNode(store);
    int index = 1;
    while (index < parentPath.size())
    {
        Element element = parentPath.get(index++);
        QName name = QName.createQName(element.getElementString());
        List<ChildAssociationRef> children = nodeService.getChildAssocs(node, RegexQNamePattern.MATCH_ALL, name);

        if (children.isEmpty())
        {
            if (log.isDebugEnabled())
            {
                log.debug("Failed to resolve path element " + element.getElementString());
            }
            return null;
        }
        if (log.isDebugEnabled()) 
        {
            log.debug("Resolved path element " + element.getElementString());
        }
        node = children.get(0).getChildRef();
    }
    return node;
}
 
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: UnitTestTransferManifestNodeFactory.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Get mapped path
 */
public Path getMappedPath(Path from)
{
    Path to = new Path();

    /**
     * 
     */
    Path source = new Path();
    for (int i = 0; i < from.size(); i++)
    {
        // Source steps through each element of from.
        source.append(from.get(i));
        boolean replacePath = false;
        for (Pair<Path, Path> xx : getPathMap())
        {
            // Can't use direct equals because of mismatched node refs (which we don't care about)
            if (xx.getFirst().toString().equals(source.toString()))
            {
                to = xx.getSecond().subPath(xx.getSecond().size() - 1);
                replacePath = true;
                break;
            }
        }
        if (!replacePath)
        {
            to.append(from.get(i));
        }
    }

    return to;
}
 
Example 12
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;
}
 
Example 13
Source File: ExporterComponent.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Determine if specified Node Reference is within the set of nodes to be exported
 * 
 * @param nodeRef  node reference to check
 * @return  true => node reference is within export set
 */
private boolean isWithinExport(NodeRef nodeRef, ExporterCrawlerParameters parameters)
{
    boolean isWithin = false;

    try
    {
        // Current strategy is to determine if node is a child of the root exported node
        for (NodeRef exportRoot : context.getExportList())
        {
            if (nodeRef.equals(exportRoot) && parameters.isCrawlSelf() == true)
            {
                // node to export is the root export node (and root is to be exported)
                isWithin = true;
            }
            else
            {
                // locate export root in primary parent path of node
                Path nodePath = nodeService.getPath(nodeRef);
                for (int i = nodePath.size() - 1; i >= 0; i--)
                {
                    Path.ChildAssocElement pathElement = (Path.ChildAssocElement) nodePath.get(i);
                    if (pathElement.getRef().getChildRef().equals(exportRoot))
                    {
                        isWithin = true;
                        break;
                    }
                }
            }
        }
    }
    catch (AccessDeniedException accessErr)
    {
        // use default if this occurs
    }
    catch (InvalidNodeRefException nodeErr)
    {
        // use default if this occurs
    }

    return isWithin;
}
 
Example 14
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 15
Source File: NodesImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public PathInfo lookupPathInfo(NodeRef nodeRefIn, ChildAssociationRef archivedParentAssoc)
{

    List<ElementInfo> pathElements = new ArrayList<>();
    Boolean isComplete = Boolean.TRUE;
    final Path nodePath;
    final int pathIndex;

    if (archivedParentAssoc != null)
    {
        if (permissionService.hasPermission(archivedParentAssoc.getParentRef(), PermissionService.READ).equals(AccessStatus.ALLOWED)
                && nodeService.exists(archivedParentAssoc.getParentRef()))
        {
            nodePath = nodeService.getPath(archivedParentAssoc.getParentRef());
            pathIndex = 1;// 1 => we want to include the given node in the path as well.
        }
        else
        {
            //We can't return a valid path
            return null;
        }
    }
    else
    {
        nodePath = nodeService.getPath(nodeRefIn);
        pathIndex = 2; // 2 => as we don't want to include the given node in the path as well.
    }

    for (int i = nodePath.size() - pathIndex; i >= 0; i--)
    {
        Element element = nodePath.get(i);
        if (element instanceof Path.ChildAssocElement)
        {
            ChildAssociationRef elementRef = ((Path.ChildAssocElement) element).getRef();
            if (elementRef.getParentRef() != null)
            {
                NodeRef childNodeRef = elementRef.getChildRef();
                if (permissionService.hasPermission(childNodeRef, PermissionService.READ) == AccessStatus.ALLOWED)
                {
                    Serializable nameProp = nodeService.getProperty(childNodeRef, ContentModel.PROP_NAME);
                    String type = getNodeType(childNodeRef).toPrefixString(namespaceService);
                    Set<QName> aspects = nodeService.getAspects(childNodeRef);
                    List<String> aspectNames = mapFromNodeAspects(aspects, EXCLUDED_NS, EXCLUDED_ASPECTS);
                    pathElements.add(0, new ElementInfo(childNodeRef.getId(), nameProp.toString(), type, aspectNames));
                }
                else
                {
                    // Just return the pathInfo up to the location where the user has access
                    isComplete = Boolean.FALSE;
                    break;
                }
            }
        }
    }

    String pathStr = null;
    if (pathElements.size() > 0)
    {
        StringBuilder sb = new StringBuilder(120);
        for (PathInfo.ElementInfo e : pathElements)
        {
            sb.append("/").append(e.getName());
        }
        pathStr = sb.toString();
    }
    else
    {
        // There is no path element, so set it to null in order to be
        // ignored by Jackson during serialisation
        isComplete = null;
    }
    return new PathInfo(pathStr, isComplete, pathElements);
}