cancel
Showing results for 
Search instead for 
Did you mean: 

Find QName path from NodeRef for Lucene search (or Xpath search)

leonardo_celati
Champ in-the-making
Champ in-the-making
Within a Java Action I have a known NodeRef, I need to retrieve its full parent QName path:

Which is, from this:

/app:company_home/st:sites/cm:mysite/cm:documentLibrary/cm:mydoc.pdf


I need:

/app:company_home/st:sites/cm:mysite/cm:documentLibrary


To build a Lucene query like this:


PATH:"/app:company_home/st:sites/cm:mysite/cm:documentLibrary//*" AND + @cm\:name:"someOtherNodeName"


Or the full XPath search.
3 REPLIES 3

mdavid_cu
Champ in-the-making
Champ in-the-making
Maybe this DemoBean.java could be helpfull for you

<java>
public class DemoBean {   
   private NodeService nodeService;
   private NamespaceService namespaceService;
   
   public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }
   
   public void setNamespaceService(NamespaceService namespaceService) {
      this.namespaceService = namespaceService;
   }
   
   public String retrieveFullParentQNamePath(NodeRef nodeRef){
      
      ChildAssociationRef car = nodeService.getPrimaryParent(nodeRef);
      NodeRef parent = car.getParentRef();
      Path path = nodeService.getPath(parent);
      return path.toPrefixString(namespaceService);      
   }
   
   public NodeRef getSibling(NodeRef nodeRef, String siblingName){
      
      ChildAssociationRef car = nodeService.getPrimaryParent(nodeRef);
      NodeRef parent = car.getParentRef();
      return nodeService.getChildByName(parent, ContentModel.ASSOC_CONTAINS, siblingName);
   }
}
</java>

How do you make sure that nodeService is instantiated? I use the above code and and my nodeService is null while doing the same thing for the repository sets the repository correctly. Here's my code…


package org.example;

import java.util.HashMap;
import java.util.Map;

import org.alfresco.repo.model.Repository;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.DeclarativeWebScript;
import org.alfresco.service.cmr.repository.Path;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;

public class JavaDir extends DeclarativeWebScript
{
   private Repository repository;
        private NodeService nodeservice;

   public void setRepository(Repository repository)
   {
      this.repository = repository;
   }
       
        public void setNodeService(NodeService nodeService)
   {
      this.nodeservice = nodeService;
   }

   protected Map<String, Object> executeImpl(WebScriptRequest req,
         Status status, Cache cache)
         {
                          
                model.put("nodeservice", nodeservice);
      return model;
         }
}

aleksczajka
Champ in-the-making
Champ in-the-making
got the answer… make sure you've got your nodeService bean in your webscript's context.xml file


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN 2.0//EN'
  'http://www.springframework.org/dtd/spring-beans-2.0.dtd'>
<beans>
  <bean id="webscript.org.example.javadir.get"
    class="org.example.JavaDir" parent="webscript">
  <property name="repository" ref="repositoryHelper"/>
  <property name="nodeService"><ref bean="NodeService"/></property>
  </bean>
</beans>