cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with getChildByName

dwighth
Champ on-the-rise
Champ on-the-rise
Hi.
I always get a null from NodeService.getChildByName() and FileFolderService.resolveNamePath().  Since FileFolderService.resolveNamePath() uses NodeService.getChildByName(), I suspect the problem is with the latter.

I'm trying to get Company Home as my test, but to no avail. I've tried every combination of qualified association type name and child name that I can think of, but none of them works. The following is the combination that I think is the correct one:


nodeService.getChildByName(root, ContentModel.ASSOC_CONTAINS, "app:company_home");

I'm working with 2.1 and the JCR methods seem to work for me, so I have a work around, but I'd rather be using the FileFolderService methods.

Any ideas?

Thanks in advance;
Dwight
15 REPLIES 15

derek
Star Contributor
Star Contributor
Inject these properties  (from repository.properties😞

spaces.store=workspace://SpacesStore
spaces.company_home.childname=app:company_home
So:

\\ in setter: StoreRef spacesStoreRef = new StoreRef(spaceStore);
\\ in setter: QName companyHomePath = QName.createQName(namespaceService, spacesCompanyHomeChildName);
NodeRef workspaceStoreRootNode = nodeService.getRootNode(spacesStoreRef)
List<ChildAssociationRef> assocRefs = nodeService.getChildAssocs(workspaceStoreRootNode, ContentModel.ASSOC_CHILDREN, companyHomePath);
It'll be much more efficient than the XPath cm:name query.  You can then use the getChildByName query on the NodeService from Company Home downwards.

kdejaeger
Champ in-the-making
Champ in-the-making
Ok thanks.

It seems the company home ref is found like this then.


   assocRefs.get(0).getChildRef(); //company home
Thanks for the help.

agatap
Champ in-the-making
Champ in-the-making
And if any other newbie like me wonders how to inject a value from a property file into a bean:
I have found a repository-properties bean in the core-services-context.xml (part of the config.jar).
It is backed by the PropertyPlaceholderConfigurer, a kind of postprocessing bean.

<!– load common properties –>
    <bean id="repository-properties"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders">
            <value>true</value>
        </property>
        <property name="locations">
            <list>
                <value>classpath:alfresco/repository.properties</value>
                <value>classpath:alfresco/version.properties</value>
                <value>classpath:alfresco/domain/transaction.properties</value>
            </list>
        </property>
    </bean>

To inject one of the property values from any of the files held by the repository-properties bean, you simply need to do add the following to your bean definition in the context.xml file:

   <!–  Injecting properties from the repository.properties config file –>
      <property name="companyHome" value="${spaces.company_home.childname}"/>
… and of course rememer to define the setCompanyHome() method in your java class.
Best,
Agata

agatap
Champ in-the-making
Champ in-the-making
I have problems following the example, where I inject the properties from repository.properties into my java code.
I am logged in as admin, and my java class is as follows:

public void setCompanyHome(String spacesCompanyHomeChildName){
      companyHomePath = QName.createQName( NamespaceService.APP_MODEL_1_0_URI, spacesCompanyHomeChildName);
   }
   
   public void setSpacesStore(String spaceStore){
      spacesStoreRef = new StoreRef(spaceStore);
   }
NodeRef getCompanyHomeFolder(){
      NodeRef workspaceStoreRootNode = nodeService.getRootNode(spacesStoreRef);
      List<ChildAssociationRef> assocRefs = nodeService.getChildAssocs(workspaceStoreRootNode,
            ContentModel.ASSOC_CHILDREN, companyHomePath);
      NodeRef homeFolderRef = assocRefs.get(0).getChildRef();
      return homeFolderRef;
   }

The problem is that assocRefs is empty - and I therefore have no childReferences 😞
Any ideas what went wrong here?
I work on 2.1C, with MySql.

Best regards
Agata

derek
Star Contributor
Star Contributor
Can you show the values for spacesCompanyHomeChildName and the workspaceStoreRootNode.
Also, what is the result of:
select id, parent_node_id, child_node_id, type_qname, qname from alf_child_assoc limit 10

agatap
Champ in-the-making
Champ in-the-making
I have solved the problem - the value of the injected properties is
spacesCompanyHomeChildName=app:company_home
workspaceStoreRootNode=SpacesStore

The problem was, that the QName needs to be created with a URI and a local name  - ie without the "app:". In this case the hardcoded string will work:

QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "company_home");
-this gives the QName {http://www.alfresco.org/model/application/1.0}company_home.

Trying to be DRY, and use Spring for dependency injections, I injected the nameSpaceDAO bean, and ended up with the following code - I dont know if its the easiest way to do it?

public void setSpacesStore(String spaceStore) {
      spacesStoreRef = new StoreRef(spaceStore);
   }

   public void setNameSpaceDAO(NamespacePrefixResolver prefixResolver) {
      this.prefixResolver = prefixResolver;
   }

   public void init() {
      companyHome = QName.createQName(spacesCompanyHomeChildName,
            prefixResolver);
      LOG.debug("Compnay home path = " + spacesCompanyHomeChildName
            + "QName of company home is now " + companyHome);
   }

NodeRef getCompanyHomeFolder() {
      NodeRef workspaceStoreRootNode = nodeService
            .getRootNode(spacesStoreRef);
      List<ChildAssociationRef> assocRefs = nodeService.getChildAssocs(
            workspaceStoreRootNode, ContentModel.ASSOC_CHILDREN,
            companyHome);
return assocRefs.get(0).getChildRef();
   }


Now I have all other kind of problems, but its another story  :roll:
Best,
Agata