How to add custom object and custom metadata | Community
Skip to main content
Level 6
October 16, 2015
Solved

How to add custom object and custom metadata

  • October 16, 2015
  • 11 replies
  • 4283 views

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!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by JustinEd3

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-CRX-for-example-should-you-be-creating-your-own-node-types-or-have-an-unstructured-free-for-all 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

11 replies

Ojjis
Level 7
October 16, 2015

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
 

smacdonald2008
Level 10
October 16, 2015

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

Level 6
October 16, 2015

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

Level 6
October 16, 2015

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!

Level 6
October 16, 2015

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!

Adobe Employee
October 16, 2015

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.

JustinEd3Adobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

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-CRX-for-example-should-you-be-creating-your-own-node-types-or-have-an-unstructured-free-for-all 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

Level 6
October 16, 2015

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

Ojjis
Level 7
October 16, 2015

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

Level 6
October 16, 2015

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!