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

The following examples show how to use org.alfresco.service.cmr.repository.Path#ChildAssocElement . 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: 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 2
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 3
Source File: FilenameFilteringInterceptor.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
private boolean isSystemPath(NodeRef parentNodeRef, String filename)
{
    boolean ret = false;
    Path path = nodeService.getPath(parentNodeRef);

    Iterator<Element> it = path.iterator();
    while(it.hasNext())
    {
        Path.ChildAssocElement elem = (Path.ChildAssocElement)it.next();
        QName qname = elem.getRef().getQName();
        if(qname != null && systemPaths.isFiltered(qname.getLocalName()))
        {
            ret = true;
            break;
        }
    }

    return ret;
}
 
Example 4
Source File: HiddenAspect.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks whether the node is on a hidden path
 *
 * @param nodeRef NodeRef
 * @return the matching filter, or null if no match
 */
public HiddenFileInfo onHiddenPath(NodeRef nodeRef)
{
    HiddenFileInfo ret = null;
    // TODO would be nice to check each part of the path in turn, bailing out if a match is found
    Path path = nodeService.getPath(nodeRef);
    nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);

    Iterator<Element> it = path.iterator();
    while(it.hasNext())
    {
        Path.ChildAssocElement elem = (Path.ChildAssocElement)it.next();
        QName qname = elem.getRef().getQName();
        if(qname != null)
        {
            ret = isHidden(qname.getLocalName());
            if(ret != null)
            {
                break;
            }
        }
    }

    return ret;
}
 
Example 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
Source File: HiddenAspect.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
private HiddenFileInfo findMatch(NodeRef nodeRef)
{
    HiddenFileInfo ret = null;
    Path path = null;
    String name = null;

    OUTER: for(HiddenFileInfo filter : filters)
    {
        if (Client.cmis.equals(FileFilterMode.getClient()) && filter instanceof ConfigurableHiddenFileInfo)
        {
            if (((ConfigurableHiddenFileInfo) filter).isCmisDisableHideConfig())
            {
                continue;
            }
        }
    	if(filter.cascadeHiddenAspect() || filter.cascadeIndexControlAspect())
    	{
    		if(path == null)
    		{
    			path = nodeService.getPath(nodeRef);
    		}

            // TODO would be nice to check each part of the path in turn, bailing out if a match is found
            Iterator<Element> it = path.iterator();
            while(it.hasNext())
            {
                Path.ChildAssocElement elem = (Path.ChildAssocElement)it.next();
                QName qname = elem.getRef().getQName();
                if(qname != null)
                {
        			if(filter.isHidden(qname.getLocalName()))
        			{
        				ret = filter;
                        break OUTER;
                    }
                }
    		}
    	}
    	else
    	{
    		if(name == null)
    		{
    			name = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
    		}

         if(filter.isHidden(name))
         {
         	ret = filter;
         	break;
         }
    	}
    }

    return ret;
}
 
Example 13
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 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);
}