1 Reply Latest reply on Apr 10, 2014 3:03 AM by Horia Chiorean

    Modeshape: Not able to add child node

    nimit Agrawal Newbie

      Hi

       

      I am not able to add a child node.

      I have defined 2 templates parent and a child.

       

      I can successfully add a parent node. when i am trying to add child node i am getting error.

       

      javax.jcr.nodetype.ConstraintViolationException: Unable to determine a valid node definition for the node "/parent/FirstParent/FirstChild" in workspace "default" of "testRepo"

       

      
      
      public class TestCase {
      
      
          @Inject
          private Repository repository;
      
      
      
      
          public void main(String[] args) throws LoginException, Exception {
              Session session = repository.login();
      
      
              session.getWorkspace().getNamespaceRegistry().registerNamespace("ns", "www.com");
      
      
              NodeTypeManager nodeTypeManager = session.getWorkspace().getNodeTypeManager();
      
      
              NodeTypeTemplate template1 = nodeTypeManager.createNodeTypeTemplate();
              template1.setName("ns:parent");
              template1.setOrderableChildNodes(false);
              template1.setQueryable(false);
      
      
              // Adding property definition
              PropertyDefinitionTemplate prop = nodeTypeManager.createPropertyDefinitionTemplate();
              prop.setName("host");
              prop.setRequiredType(PropertyType.STRING);
              prop.setMandatory(false);
              prop.setDefaultValues(new Value[] { session.getValueFactory().createValue("localhost") });
              template1.getPropertyDefinitionTemplates().add(prop);
      
      
              // Adding child node definition
              NodeDefinitionTemplate childTemplate = nodeTypeManager.createNodeDefinitionTemplate();
              childTemplate.setName("ns:child");
              childTemplate.setDefaultPrimaryTypeName("ns:parent");
              childTemplate.setMandatory(false);
              childTemplate.setProtected(false);
              template1.getNodeDefinitionTemplates().add(childTemplate);
      
      
              nodeTypeManager.registerNodeType(template1, true);
      
      
              NodeTypeTemplate template2 = nodeTypeManager.createNodeTypeTemplate();
              template2.setName("ns:child");
              template2.setOrderableChildNodes(false);
              template2.setQueryable(false);
              
              PropertyDefinitionTemplate prop2 = nodeTypeManager.createPropertyDefinitionTemplate();
              prop2.setName("host");
              prop2.setRequiredType(PropertyType.STRING);
              prop2.setMandatory(false);
              prop2.setDefaultValues(new Value[] { session.getValueFactory().createValue("localhost") });
              template2.getPropertyDefinitionTemplates().add(prop2);
      
      
              nodeTypeManager.registerNodeType(template2, true);
      
      
              Node rootNode = session.getRootNode().getNode("ns");
      
      
              Node firstParentNode = rootNode.addNode("FirstParent", "ns:parent");
      
      
              // Getting error in this line
              firstParentNode.addNode("FirstChild", "ns:child");
      
      
          }
      }
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      

       

       

       

      Thanks in advance

        • 1. Re: Modeshape: Not able to add child node
          Horia Chiorean Master
          1.         // Adding child node definition 
          2.         NodeDefinitionTemplate childTemplate = nodeTypeManager.createNodeDefinitionTemplate(); 
          3.         childTemplate.setName("ns:child"); 
          4.         childTemplate.setDefaultPrimaryTypeName("ns:parent"); 
          5.         childTemplate.setMandatory(false); 
          6.         childTemplate.setProtected(false); 
          7.         template1.getNodeDefinitionTemplates().add(childTemplate); 
          8.  
          9.         // Getting error in this line 
          10.         firstParentNode.addNode("FirstChild", "ns:child"); 

           

           

          Your child definition has the name: [ns:child] and the type [ns:parent]. This means that if you want such a child to "firstParentNode" your code should look like: firstParentNode.addNode("ns:child", "ns:parent") (you need to make the distinction between a NodeTypeTemplate and a NodeDefinitionTemplate)

           

          If you want to be able to use any name for the child node and have the [ns:child] primary type, your code should look like this:

           

          childTemplate.setName("*");  
          childTemplate.setDefaultPrimaryTypeName("ns:child");