You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by "Shah.Abhishek" <en...@gmail.com> on 2013/01/25 16:16:41 UTC

How to add ChildNodeDefinition to NodeTypeTemplate while creating a custom NodeType using NodeTypeManager?

Hi,

I *need to* create a Custom Node type using the NodeTypeManager and other
api available in Jackrabbit 2.4.2. I can successfully create a custom node
type but am unable to add ChildNodeDefinitions to that node type. 

JCR docs specify that one can use NodeDefinitionTemplate:
*"NodeTypeManager.createNodeDefinitionTemplate() returns an empty
NodeDefinitionTemplate which can then be used to create a child node
definition and attached to a NodeTypeTemplate"*

Here,
When I try to create a new ChildNodeDefinition and try to register the
NodeTypeTemplate after appending the ChildNodeDefinition, it throws and
exception saying that "No such node type is defined" (talking about the
childNodeDefinition as it isn't registered.

And considering that I have to append a the Definition of a registered
NodeType, I don't have any way to get the Definition of a registered
NodeType. Even the NodeTypeManager returns the NodeType by using
NodeTypeManager.getNodeType("NodeTypeName") method.

Also adding a NodeType instance to NodeDefinitionList of NodeTypeTemplate
isn't an option as it gives ClassCastException.

Please, kindly help me out on this.


Below attached is my code:

NodeTypeManager nodeTypeManager = sessionImpl.getNodeTypeManager();
				try{
				
sessionImpl.getWorkspace().getNamespaceRegistry().registerNamespace("new",
"www.demo.com/new");
				}catch(Exception err)
				{
					System.out.println("EXCEPTION: Namespace is already registered!!");
				}
				
                                System.out.println("Create custom Node
Type");
				
				/* Create node type */
				NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
				nodeType.setName("new:contentType");
				

				/* Create a new property */
				PropertyDefinitionTemplate customProperty =
nodeTypeManager.createPropertyDefinitionTemplate();
				PropertyDefinitionTemplate customProperty_temp =
nodeTypeManager.createPropertyDefinitionTemplate();
				PropertyDefinitionTemplate customProperty_Two =
nodeTypeManager.createPropertyDefinitionTemplate();
	
				customProperty.setName("my_custom_property");
				customProperty.setRequiredType(PropertyType.LONG);
				customProperty_temp.setName("new:prop");
				customProperty_temp.setRequiredType(PropertyType.STRING);
				customProperty_temp.setMandatory(true);
				
				customProperty_Two.setName("new:property");
				customProperty_Two.setRequiredType(PropertyType.STRING);
				
				/* Add property to node type */
				nodeType.getPropertyDefinitionTemplates().add(customProperty);
				nodeType.getPropertyDefinitionTemplates().add(customProperty_temp);
				
                                //Complete child node part has been
commented, don't refer to it

				/*//TESTING FOR CHILD NODE TYPES
				
				 * Creating a definition for child node
				 
				NodeTypeTemplate childNode = nodeTypeManager.createNodeTypeTemplate();
				childNode.setName("new:childContentType");
				nodeTypeManager.registerNodeType(childNode, true);
				
				NodeDefinitionTemplate childNodeDefinition =
nodeTypeManager.createNodeDefinitionTemplate();
				//NodeDefinitionTemplate childNodeDefinition = (NodeDefinitionTemplate)
nodeTypeManager.getNodeType("tcs:childContentType");
				
				try{
					//nodeType.getNodeDefinitionTemplates().add(childNodeDefinition);
				}catch(Exception err)
				{
					System.out.println("CLASS-CAST-EXCEPTION!!");
				}
				
				 * End of child node definition creation and registration
				 */


				/* Register node type */

                                 //Even this won't work, not even if you try
to update the registered nodetype
                                // as it throws 
                                /*
java.lang.ArrayStoreException:
org.apache.jackrabbit.spi.commons.nodetype.NodeTypeTemplateImpl
                                   at java.util.LinkedList.toArray(Unknown
Source)
                               */
				//nodeType.getNodeDefinitionTemplates().add(nodeType);

				try{
					nodeTypeManager.registerNodeType(nodeType, true);
				}catch(Exception err)
				{
					System.out.println("Problem in registering a node type!");
				}




--
View this message in context: http://jackrabbit.510166.n4.nabble.com/How-to-add-ChildNodeDefinition-to-NodeTypeTemplate-while-creating-a-custom-NodeType-using-NodeTypeMa-tp4657488.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: How to add ChildNodeDefinition to NodeTypeTemplate while creating a custom NodeType using NodeTypeManager?

Posted by "Shah.Abhishek" <en...@gmail.com>.
It worked. Thanks for helping.

It seems that I had misunderstood the concept. We just need to specify the
name and required conditions, rest will be done by Jackrabbit. Thanks again.

regards,
Abhishek



--
View this message in context: http://jackrabbit.510166.n4.nabble.com/How-to-add-ChildNodeDefinition-to-NodeTypeTemplate-while-creating-a-custom-NodeType-using-NodeTypeMa-tp4657488p4657527.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: How to add ChildNodeDefinition to NodeTypeTemplate while creating a custom NodeType using NodeTypeManager?

Posted by Alexander Klimetschek <ak...@adobe.com>.
On 28.01.2013, at 05:28, Shah.Abhishek <en...@gmail.com> wrote:

> Is there any way using which I can get the NodeDefinition of a previously
> registered NodeType using NodeTypeManager?

AFAICS, the only thing in the NodeDefinitionTemplate that refers to an exisiting node type is the name, which is just a string set via setName(). So you would use a fresh NodeTypeManager.createNodeDefinitionTemplate() and set the name and then the other properties as needed. They are defined here by the parent node type, not from the exisiting node type (mandatory, autocreated, onparentversion etc.)

Cheers,
Alex

Re: How to add ChildNodeDefinition to NodeTypeTemplate while creating a custom NodeType using NodeTypeManager?

Posted by "Shah.Abhishek" <en...@gmail.com>.
Hi Alex,

Thanks for your reply.

I have already gone the link that you have shown. Even the
doChildNodeDefinition() method parses the CND format. This leaves me with
two options,
1) I first create a CND file by taking the user inputs and then use it to
register nodeTypes (doable but not approved by supervisor)
2) I ask the user to create a CND file and then use it to register (client
would never agree to that!)

Unfortunately, I won't be able to follow either.

Is there any way using which I can get the NodeDefinition of a previously
registered NodeType using NodeTypeManager?

Once again, thanks for your reply.



--
View this message in context: http://jackrabbit.510166.n4.nabble.com/How-to-add-ChildNodeDefinition-to-NodeTypeTemplate-while-creating-a-custom-NodeType-using-NodeTypeMa-tp4657488p4657494.html
Sent from the Jackrabbit - Users mailing list archive at Nabble.com.

Re: How to add ChildNodeDefinition to NodeTypeTemplate while creating a custom NodeType using NodeTypeManager?

Posted by Alexander Klimetschek <ak...@adobe.com>.
On 25.01.2013, at 16:16, Shah.Abhishek <en...@gmail.com> wrote:

> I *need to* create a Custom Node type using the NodeTypeManager and other
> api available in Jackrabbit 2.4.2. I can successfully create a custom node
> type but am unable to add ChildNodeDefinitions to that node type. 

The CND format importer code might help:

http://svn.apache.org/repos/asf/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/cnd/CompactNodeTypeDefReader.java

Have a look at the places where Lexer.CHILD_NODE_DEFINITION is used and at the method doChildNodeDefinition(). It uses AbstractNodeTypeDefinitionBuilder(s) that come from TemplateBuilderFactory.java, but under the hood it's working with NodeTypeTemplate.

HTH,
Alex