Hi all,
using the following code, I am able to update an existing node type by
inserting a new property:

NodeTypeManager nodeTypeManager =
session.getWorkspace().getNodeTypeManager();

NodeType repositoryType = nodeTypeManager.getNodeType("testType");
NodeTypeTemplate repositoryTypeTemplate =
nodeTypeManager.createNodeTypeTemplate(repositoryType);
PropertyDefinitionTemplate testProperty =
nodeTypeManager.createPropertyDefinitionTemplate();
testProperty.setName("testProperty");
testProperty.setRequiredType(PropertyType.STRING);
testProperty.setMandatory(false);
testProperty.setMultiple(false);
testProperty.setDefaultValues(new Value[0]);
testProperty.setValueConstraints(new String[0]);
repositoryTypeTemplate.getPropertyDefinitionTemplates().add(testProperty);

nodeTypeManager.registerNodeType(repositoryTypeTemplate, true);
session.save();

I prefer this way, instead using CND
<https://jackrabbit.apache.org/jcr/node-type-notation.html> because I
understand it better and is cleaner, compared to having everything inside a
String (am I doing it correctly?).

Because of a bug I was tracking, I found out that, if you invoke this code
several times, it will insert a new property definition each time, so in
the repository you could end up with this (I removed a lot of stuff to keep
it small):
{
    "node": "testType",
    "path": "/jcr:system/jcr:nodeTypes/testType",
    "children": [
        {
            "node": "jcr:propertyDefinition",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition",
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[10]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[10]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[2]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[2]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[3]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[3]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[4]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[4]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[5]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[5]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[6]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[6]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[7]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[7]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[8]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[8]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "jcr:propertyDefinition[9]",
            "path":
"/jcr:system/jcr:nodeTypes/testType/jcr:propertyDefinition[9]",
            "mixins": [],
            "children": [],
            "properties": [
                *"jcr:name = testProperty"*
            ]
        },
        {
            "node": "rep:namedPropertyDefinitions",
            "path":
"/jcr:system/jcr:nodeTypes/testType/rep:namedPropertyDefinitions",
            "children": [
                {
                    "node": "jcr:created",
                    "path":
"/jcr:system/jcr:nodeTypes/testType/rep:namedPropertyDefinitions/jcr:created",
                },
                {
                    "node": "jcr:createdBy",
                    "path":
"/jcr:system/jcr:nodeTypes/testType/rep:namedPropertyDefinitions/jcr:createdBy",
                },
                {
                    "node": "rep:mixinTypes",
                    "path":
"/jcr:system/jcr:nodeTypes/testType/rep:namedPropertyDefinitions/rep:mixinTypes",
                },
                {
                    "node": "rep:primaryType",
                    "path":
"/jcr:system/jcr:nodeTypes/testType/rep:namedPropertyDefinitions/rep:primaryType",
                },
                {
                    "node": "testProperty",
                    "path":
"/jcr:system/jcr:nodeTypes/testType/rep:namedPropertyDefinitions/testProperty",
                        "properties": [
                            *"jcr:name = testProperty"*
                        ]
                    }],
                    "properties": ["jcr:primaryType =
rep:PropertyDefinitions"]
                }
            ],
            "properties": ["jcr:primaryType = rep:NamedPropertyDefinitions"]
        },
        {
            "node": "rep:residualChildNodeDefinitions",
            "path":
"/jcr:system/jcr:nodeTypes/testType/rep:residualChildNodeDefinitions",
            "children": [{
                "node": "nt:hierarchyNode",
                "path":
"/jcr:system/jcr:nodeTypes/testType/rep:residualChildNodeDefinitions/nt:hierarchyNode",
            }],
            "properties": ["jcr:primaryType = rep:ChildNodeDefinitions"]
        }
    ],
    "properties": [
        "jcr:nodeTypeName = testType",
        "rep:hasProtectedResidualChildNodes = false",
        "jcr:isAbstract = false",
        "jcr:hasOrderableChildNodes = false",
        "rep:supertypes = nt:folder,nt:hierarchyNode,nt:base,mix:created",
        "jcr:isMixin = false",
        "rep:hasProtectedResidualProperties = false",
        "rep:primarySubtypes = ",
        "jcr:isQueryable = true",
        "rep:mandatoryProperties = jcr:primaryType",
        "rep:protectedChildNodes = ",
        "jcr:supertypes = nt:folder",
        "rep:mandatoryChildNodes = ",
        "rep:protectedProperties =
jcr:primaryType,jcr:mixinTypes,jcr:created,jcr:createdBy",
        "jcr:primaryType = rep:NodeType",
        "rep:namedSingleValuedProperties =
testProperty,jcr:primaryType,jcr:created,jcr:createdBy"
    ]
}

I am not sure if I am doing something wrong, or whether there should be a
validation when inserting property definitions having a name that already
exists...

Any comment is appreciated as always :)

Jorge

Reply via email to