Nikitin Mikhail
Nikitin Mikhail

Reputation: 3019

How to get a companyHome for nodeService.getChildAssocs in Alfresco?

Actually I'm trying to get files from the current task in alfresco but can't found any descryption of how to get the current node. I've found that I can get all the child nodeRefs I need using this:

List<ChildAssociationRef> children = nodeService.getChildAssocs(companyHome)

but here I only see that I need NodeRef nodeRef, QNamePattern typeQNamePattern, QNamePattern qnamePattern these are at least 3 parameters one of them is also a NodeRef and it turns out that I need a NodeRef to get NodeRef. As I understand the last nodeRef is something like the parent folder but how to get it I don't know too. It is also possible to get the NodeRef by PATH like this:

StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
   ResultSet rs = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home/app:user_homes/sys:boris/cm:mypics\"");
   NodeRef companyHomeNodeRef = null;
   try
   {
       if (rs.length() == 0)
       {
           throw new AlfrescoRuntimeException("Didn't find Company Home");
       }
       companyHomeNodeRef = rs.getNodeRef(0);
   }
   finally
   {
       rs.close();
   }

but I can't hardcode the path as the Alfresco can be run anywhere on server or other. It is also possible to get NodeRef using Lucene like this way:

        SearchParameters sp = new SearchParameters();
        sp.addStore(getStoreRef());
        sp.setLanguage(SearchService.LANGUAGE_LUCENE);
        sp.setQuery("TYPE:\"{http://www.alfresco.org/model/content/1.0}content\"");
        ResultSet results = null;
        try
        {
            results = serviceRegistry.getSearchService().query(sp);
            for(ResultSetRow row : results)
            {
                NodeRef currentNodeRef = row.getNodeRef();
                ...
            }
        }
        finally
        {
            if(results != null)
            {
                results.close();
            }
        }  

But this will return me all the Nodes existing in alfresco. Maybe there is a possibility to improve one of this methods to reach my wishes? Or maybe there are any other way?

UPD: here is the part of code where I'd like to get and use nodes:

        NodeService nodeService = getServiceRegistry().getNodeService();                    
        ContentService contentService = getServiceRegistry().getContentService();

        List<ChildAssociationRef> children = nodeService.getChildAssocs(companyHome);
         if (children.isEmpty()) {
            throw new AlfrescoRuntimeException("Workflow bpm_package does not contain any files");
        }
         for(ChildAssociationRef childAssoc: children){
                NodeRef childNodeRef = childAssoc.getChildRef();
                FileBinary = getFileBinary(childNodeRef, contentService);

further I'm going to perform file transformation. Hope this will shed some lite on the situation.

Upvotes: 0

Views: 2289

Answers (1)

Miki
Miki

Reputation: 2517

Try this way:

ServiceRegistry serviceRegistry = (ServiceRegistry) context.getBean("ServiceRegistry");

Node rootNode = session.getRootNode();
//obtaining root node company home 
Node companyHome = rootNode.getNode("app:company_home");
// getting noderef of company home node
NodeRef companyHomeRef = JCRNodeRef.getNodeRef(companyHome);

List<ChildAssociationRef> children = nodeService.getChildAssocs(companyHomeRef);
   for (ChildAssociationRef childAssoc : children) {
       NodeRef childNodeRef = childAssoc.getChildRef();
       // Use childNodeRef here.
   }

This way you programatically acquired NodeRef of CompanyHome node and via it you've got to the list of child nodes under Company Home.

Use these imports:

import org.alfresco.jcr.api.JCRNodeRef;
import org.alfresco.service.ServiceRegistry;
import javax.jcr.Node;
import javax.jcr.Session;
import org.springframework.context.ApplicationContext;

Upvotes: 3

Related Questions