Expand my Community achievements bar.

SOLVED

How to add custom object and custom metadata

Avatar

Level 6

I want to create new object type by extending existing nt:file/nt:folder. Can someone please let me know how can I create my own custom type and custom metadata?

After searching through Google I couldn't find good tutorial on how to do this. I found this thread but could not get much on how to implement new custom type and custom metadata. 

Thank you for your help!

1 Accepted Solution

Avatar

Correct answer by
Employee

Hi Sam,

In general, extensive use of node types isn't recommended. If you just want a bag of properties, you should use nt:unstructured and create whatever properties you need.

See http://www.quora.com/Repositories/When-using-a-Java-Content-Repository-Apache-JackRabbit-or-Adobe-CR... and http://wiki.apache.org/jackrabbit/DavidsModel

The specific scenario you describe, however, does require the use of a custom node type because nt:file and nt:resource are "strong" node types and do not support residual properties. The best thing to do would be to create a mixin node type which defines your properties (or just * to allow any property) and then add that mixin to the nt:resource node (see Rule #6 in David's Model linked to above).

Regards,

Justin

View solution in original post

11 Replies

Avatar

Level 7

Hello. Are you sure you are not referring to the node types ?

If so, you have the information of how to create/manage you own node types in this article that describes the built in node type administration:
http://dev.day.com/docs/en/crx/current/using_crx/nodetype_administration.html


For information about the custom node types within CQ, see this one:
http://dev.day.com/docs/en/cq/5-4/developing/custom_node_types.html

Regards
Johan
 

Avatar

Level 10

Have you read this sub topic: "CREATING NEW METADATA PROPERTY FOR ASSETS" located here: http://dev.day.com/docs/en/cq/current/dam/how_to_edit_metadata.html

Avatar

Level 6

Don't walkt that path. The "new node types" to hold data was very tempting for me too in the beginning and we did use it for some implementation. Today, with perspective and with the knowledge I know have about CQ5/AEM I would hesitate to use a custom node type. nt:unstructured is your friend.

/Ove

Avatar

Level 6

Thank you all for your help!

As per my requirement I have to create custom node type with corresponding metadata. So for that I opened "Node Type Administration" and then tried to create custom Node type(cq:mytype) as follows - 

[img]nodetype.jpg[/img]

 

But when I'm executing the below code, its throwing error -

Node myFolder = root.addNode("Folder","nt:folder"); 
        Node file = myFolder.addNode("Marketng_Document.doc","cq:mytype"); 
        Node content = file.addNode("jcr:content","nt:resource");
        Binary binary = session.getValueFactory().createBinary(stream);
        content.setProperty("jcr:data",binary);
        content.setProperty("jcr:mimeType","application/msword");
        session.save();

Error received - 

Exception in thread "main" javax.jcr.nodetype.ConstraintViolationException: not allowed to add node {}Marketng_Document.doc.doc:{http://www.day.com/jcr/cq/1.0}mytype is a mixin and cannot be used as primary node type.
    at org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeTypeImpl.checkAddNodeConstraints(EffectiveNodeTypeImpl.java:371)
    at org.apache.jackrabbit.jcr2spi.state.ItemStateValidator.checkAddNode(ItemStateValidator.java:388)
    at org.apache.jackrabbit.jcr2spi.state.SessionItemStateManager.addNodeState(SessionItemStateManager.java:435)
    at org.apache.jackrabbit.jcr2spi.state.SessionItemStateManager.visit(SessionItemStateManager.java:245)
    at org.apache.jackrabbit.jcr2spi.operation.AddNode.accept(AddNode.java:79)
    at org.apache.jackrabbit.jcr2spi.state.SessionItemStateManager.execute(SessionItemStateManager.java:215)
    at org.apache.jackrabbit.jcr2spi.NodeImpl.createNode(NodeImpl.java:1454)
    at org.apache.jackrabbit.jcr2spi.NodeImpl.addNode(NodeImpl.java:186)

 

Kindly let me know what I'm missing here. Thanks again for your help!

Avatar

Level 6

Thanks a lot for your reply. What I want is to upload files into CRX with custom object type and custom metadata. Like any other content management system, we uploads files to custom object/content model by extending the basic object/content model as per requirment. As per my knowledge nt:base is root type. So, for that I thought to extend the nt:file with custom metadata, for example customer_name, customer_address etc. 

I want to write following code to upload files into repository. 

Node myFolder = root.addNode("Folder","nt:folder");

            Node file = myFolder.addNode("Sumanta_Pakira_Resume2.doc","nt:file"); // Here I want to use my custom type, e.g cust:mytype

            Node content = file.addNode("jcr:content","nt:resource");

            Binary binary = session.getValueFactory().createBinary(stream);

            content.setProperty("jcr:data",binary);

            content.setProperty("jcr:mimeType","application/msword");

           content.setProperty("customer_name","Some Organization");// I want to set my own custom metadata.

            session.save(); 

The link that you posted here, I could not find how to achieve this requirement. Kindly let me know if I'm missing something here and how to implement this,

Thanks for your help!

Avatar

Employee

Ove-

While that is certainly true in general, if you want to add new properties to a nt:resource node (which is the OP's use case), you really don't have a choice.

Avatar

Correct answer by
Employee

Hi Sam,

In general, extensive use of node types isn't recommended. If you just want a bag of properties, you should use nt:unstructured and create whatever properties you need.

See http://www.quora.com/Repositories/When-using-a-Java-Content-Repository-Apache-JackRabbit-or-Adobe-CR... and http://wiki.apache.org/jackrabbit/DavidsModel

The specific scenario you describe, however, does require the use of a custom node type because nt:file and nt:resource are "strong" node types and do not support residual properties. The best thing to do would be to create a mixin node type which defines your properties (or just * to allow any property) and then add that mixin to the nt:resource node (see Rule #6 in David's Model linked to above).

Regards,

Justin

Avatar

Level 6

Just a small opinion. You should not use the cq: namespace for your custom node type but use your own namespace.

Avatar

Level 7

Valid points here from both Justin and Ove :) and I see that you have been reading up on the mixin nodes and that it would be the right way in your case.
The problem that I see here is that you have missed a small thing. The mixin node that you have created cannot be "used" directly. As Justin pointed out you add mixin nodes to other node-types.

The error you get is because you are trying to add attributes/values to a non-primary node. In your case you will have to use a standard node (like nt:base or nt:file) and then with the help of the method Node.addMixin(String mixinName) (http://www.day.com/specs/jcr/1.0/7.4.3_Assigning_Mixin_Node_Types.html) you add  the mixin-type that you created in the previous step. Something like 

...
Node file = myFolder.addNode("Marketng_Document.doc","nt:file");
file.addMixin("cq:myType");
....

 


I think that will solve that problem.
Regards
Johan

Avatar

Level 6

Thanks Johan, Ove, Justin!

this error was resolved after adding node.addMixin(). Now my doubt is, is it not possible to add Primary Node type itself as "cq:mytype'? Do I need to create CND file and then register it to achieve this?

Thanks again for your help!

Avatar

Level 7

Glad it worked.

As described in the above links, a mixin node only applies characteristics to another node, that is "...a type that further define properties and children that beyond those defined by the primary type.." (http://modeshape.wordpress.com/2010/01/29/the-shape-of-your-information/
As I have understood it, for a node to be able to have your own created one as the primary type,  you need to create another custom node that is not a mixin. 

Regarding the CND files, they are files which in you declare custom namespaces in.
You can create your custom namespace like sam: for a new type called sam:mytype. This can either be done in the Node Type Configurations window under Namespaces or via the CND files that you can deploy yourself. More info here (http://blogs.adobe.com/contentmanagement/tag/cnd-file/).

Then, after you have created your own namespace you can continue with creating maybe a mixin node type with that namespace (and maybe, if you want and feel like it's the right thing, even create your own custom node types and then apply the primary node type as the one you created... )