NodeType Registration

NodeType Registration

eXo JCR implementation supports two ways of Nodetypes registration:

  • From a NodeTypeValue POJO and
  • from an XML document (stream).

1 Interfaces and methods

1.1 ExtendedNodeTypeManager

The ExtendedNodeTypeManager (from JCR 1.11) interface provides the following methods related to registering node types

public static final int IGNORE_IF_EXISTS  = 0;

public static final int FAIL_IF_EXISTS    = 2;

public static final int REPLACE_IF_EXISTS = 4;


/**
 * Registers node type using value object.
 */
void registerNodeType(NodeTypeValue nodeTypeValue, int alreadyExistsBehaviour) throws RepositoryException;

/**
 * Registers all node types using XML binding value objects from xml stream.
 */
void registerNodeTypes(InputStream xml, int alreadyExistsBehaviour) throws RepositoryException;

/**
 * Return <code>NodeTypeValue</code> for a given nodetype name. Used for
 * nodetype update. Value can be edited and registered via
 * <code>registerNodeType(NodeTypeValue nodeTypeValue, int alreadyExistsBehaviour)</code>
 */
NodeTypeValue getNodeTypeValue(String ntName) throws NoSuchNodeTypeException, RepositoryException;

/**
 * Registers or updates the specified <code>Collection</code> of
 * <code>NodeTypeValue</code> objects.
 */
public NodeTypeIterator registerNodeTypes(Collection<NodeTypeValue> values,
                                            int alreadyExistsBehaviour) throws UnsupportedRepositoryOperationException,
                                                                       RepositoryException;

/**
 * Unregisters the specified node type.
 */
public void unregisterNodeType(String name) throws UnsupportedRepositoryOperationException,
                                             NoSuchNodeTypeException,
                                             RepositoryException;

/**
 * Unregisters the specified set of node types.<p/> Used to unregister a set
 * of node types with mutual dependencies.
 */
public void unregisterNodeTypes(String[] names) throws UnsupportedRepositoryOperationException,
                                                 NoSuchNodeTypeException,
                                                 RepositoryException;

1.2 NodeTypeValue

The NodeTypeValue interface represents a simple container structure used to define node types which are then registered through the ExtendedNodeTypeManager.registerNodeType method. Implementation of this interface doesn't contain any validation logic.

/**
 * @return Returns the declaredSupertypeNames.
 */
public List<String> getDeclaredSupertypeNames();

/**
 * @param declaredSupertypeNames
 *The declaredSupertypeNames to set.
 */
public void setDeclaredSupertypeNames(List<String> declaredSupertypeNames);

/**
 * @return Returns the mixin.
 */
public boolean isMixin();

/**
 * @param mixin
 *The mixin to set.
 */
public void setMixin(boolean mixin);

/**
 * @return Returns the name.
 */
public String getName();

/**
 * @param name
 *The name to set.
 */
public void setName(String name);

/**
 * @return Returns the orderableChild.
 */
public boolean isOrderableChild();

/**
 * @param orderableChild
 *The orderableChild to set.
 */
public void setOrderableChild(boolean orderableChild);

/**
 * @return Returns the primaryItemName.
 */
public String getPrimaryItemName();

/**
 * @param primaryItemName
 *The primaryItemName to set.
 */
public void setPrimaryItemName(String primaryItemName);

/**
 * @return Returns the declaredChildNodeDefinitionNames.
 */
public List<NodeDefinitionValue> getDeclaredChildNodeDefinitionValues();

/**
 * @param declaredChildNodeDefinitionNames
 *The declaredChildNodeDefinitionNames to set.
 */
public void setDeclaredChildNodeDefinitionValues(List<NodeDefinitionValue> declaredChildNodeDefinitionValues);

/**
 * @return Returns the declaredPropertyDefinitionNames.
 */
public List<PropertyDefinitionValue> getDeclaredPropertyDefinitionValues();

/**
 * @param declaredPropertyDefinitionNames
 *The declaredPropertyDefinitionNames to set.
 */
public void setDeclaredPropertyDefinitionValues(List<PropertyDefinitionValue> declaredPropertyDefinitionValues);

1.3 NodeDefinitionValue

The NodeDefinitionValue interface extends ItemDefinitionValue with the addition of write methods, enabling the characteristics of a child node definition to be set, after which the NodeDefinitionValue is added to a NodeTypeValue.

/**
 * @return Returns the declaredSupertypeNames.
 */
public List<String> getDeclaredSupertypeNames();

/**
 * @param declaredSupertypeNames
 *The declaredSupertypeNames to set.
 */
public void setDeclaredSupertypeNames(List<String> declaredSupertypeNames);

/**
 * @return Returns the mixin.
 */
public boolean isMixin();

/**
 * @param mixin
 *The mixin to set.
 */
public void setMixin(boolean mixin);

/**
 * @return Returns the name.
 */
public String getName();

/**
 * @param name
 *The name to set.
 */
public void setName(String name);

/**
 * @return Returns the orderableChild.
 */
public boolean isOrderableChild();

/**
 * @param orderableChild
 *The orderableChild to set.
 */
public void setOrderableChild(boolean orderableChild);

/**
 * @return Returns the primaryItemName.
 */
public String getPrimaryItemName();

/**
 * @param primaryItemName
 *The primaryItemName to set.
 */
public void setPrimaryItemName(String primaryItemName);

/**
 * @return Returns the declaredChildNodeDefinitionNames.
 */
public List<NodeDefinitionValue> getDeclaredChildNodeDefinitionValues();

/**
 * @param declaredChildNodeDefinitionNames
 *The declaredChildNodeDefinitionNames to set.
 */
public void setDeclaredChildNodeDefinitionValues(List<NodeDefinitionValue> declaredChildNodeDefinitionValues);

/**
 * @return Returns the declaredPropertyDefinitionNames.
 */
public List<PropertyDefinitionValue> getDeclaredPropertyDefinitionValues();

/**
 * @param declaredPropertyDefinitionNames
 *The declaredPropertyDefinitionNames to set.
 */
public void setDeclaredPropertyDefinitionValues(List<PropertyDefinitionValue> declaredPropertyDefinitionValues);

1.4 PropertyDefinitionValue

The PropertyDefinitionValue interface extends ItemDefinitionValue with the addition of write methods, enabling the characteristics of a child property definition to be set, after which the PropertyDefinitionValue is added to a NodeTypeValue.

/**
 * @return Returns the defaultValues.
 */
public List<String> getDefaultValueStrings();

/**
 * @param defaultValues The defaultValues to set.
 */
public void setDefaultValueStrings(List<String> defaultValues);

/**
 * @return Returns the multiple.
 */
public boolean isMultiple();

/**
 * @param multiple The multiple to set.
 */
public void setMultiple(boolean multiple);

/**
 * @return Returns the requiredType.
 */
public int getRequiredType();

/**
 * @param requiredType The requiredType to set.
 */
public void setRequiredType(int requiredType);

/**
 * @return Returns the valueConstraints.
 */
public List<String> getValueConstraints();

/**
 * @param valueConstraints The valueConstraints to set.
 */
public void setValueConstraints(List<String> valueConstraints);

1.5 ItemDefinitionValue

/**
 * @return Returns the autoCreate.
 */
public boolean isAutoCreate();

/**
 * @param autoCreate The autoCreate to set.
 */
public void setAutoCreate(boolean autoCreate);

/**
 * @return Returns the mandatory.
 */
public boolean isMandatory();

/**
 * @param mandatory The mandatory to set.
 */
public void setMandatory(boolean mandatory);

/**
 * @return Returns the name.
 */
public String getName();

/**
 * @param name The name to set.
 */
public void setName(String name);

/**
 * @return Returns the onVersion.
 */
public int getOnVersion();

/**
 * @param onVersion The onVersion to set.
 */
public void setOnVersion(int onVersion);

/**
 * @return Returns the readOnly.
 */
public boolean isReadOnly();

/**
 * @param readOnly The readOnly to set.
 */
public void setReadOnly(boolean readOnly);

2 Node type registration

eXo JCR implementation supports various methods of the node-type registration. The most used is registration from xml file on JCR startup?.

2.1 Run time registration from xml file.

ExtendedNodeTypeManager nodeTypeManager = (ExtendedNodeTypeManager) session.getWorkspace()
                                                             .getNodeTypeManager();
InputStream is = MyClass.class.getResourceAsStream("mynodetypes.xml");
nodeTypeManager.registerNodeTypes(is,ExtendedNodeTypeManager.IGNORE_IF_EXISTS );

2.2 Run time registration using NodeTypeValue.

ExtendedNodeTypeManager nodeTypeManager = (ExtendedNodeTypeManager) session.getWorkspace()
                                                             .getNodeTypeManager();
NodeTypeValue testNValue = new NodeTypeValue();

List<String> superType = new ArrayList<String>();
superType.add("nt:base");
testNValue.setName("exo:myNodeType");
testNValue.setPrimaryItemName("");
testNValue.setDeclaredSupertypeNames(superType);
List<PropertyDefinitionValue> props = new ArrayList<PropertyDefinitionValue>();
props.add(new PropertyDefinitionValue("*",
                                      false,
                                      false,
                                      1,
                                      false,
                                      new ArrayList<String>(),
                                      false,
                                      0,
                                      new ArrayList<String>()));
testNValue.setDeclaredPropertyDefinitionValues(props);

nodeTypeManager.registerNodeType(testNValue, ExtendedNodeTypeManager.FAIL_IF_EXISTS);

3 Change existing node type

If you want to replace existing node type definition you should pass ExtendedNodeTypeManager.REPLACE_IF_EXISTS as a second parameter for the method ExtendedNodeTypeManager.registerNodeType.

ExtendedNodeTypeManager nodeTypeManager = (ExtendedNodeTypeManager) session.getWorkspace()
                                                                           .getNodeTypeManager();
InputStream is = MyClass.class.getResourceAsStream("mynodetypes.xml");
.....
nodeTypeManager.registerNodeTypes(is,ExtendedNodeTypeManager.REPLACE_IF_EXISTS );

4 Remove node type

Warning: Removing node type is only possible when repository doesn't contains nodes of this type
nodeTypeManager.unregisterNodeType("myNodeType");

5 Practical How to

5.1 Add new PropertyDefinition

NodeTypeValue myNodeTypeValue = nodeTypeManager.getNodeTypeValue(myNodeTypeName);
List<PropertyDefinitionValue> props = new ArrayList<PropertyDefinitionValue>();
props.add(new PropertyDefinitionValue("tt",
                                      true,
                                      true,
                                      1,
                                      false,
                                      new ArrayList<String>(),
                                      false,
                                      PropertyType.STRING,
                                      new ArrayList<String>()));
myNodeTypeValue.setDeclaredPropertyDefinitionValues(props);

nodeTypeManager.registerNodeType(myNodeTypeValue, ExtendedNodeTypeManager.REPLACE_IF_EXISTS);

5.2 Add new child NodeDefinition

NodeTypeValue myNodeTypeValue = nodeTypeManager.getNodeTypeValue(myNodeTypeName);

List<NodeDefinitionValue> nodes = new ArrayList<NodeDefinitionValue>();
nodes.add(new NodeDefinitionValue("child",
                                      false,
                                      false,
                                      1,
                                      false,
                                      "nt:base",
                                      new ArrayList<String>(),
                                      false));
testNValue.setDeclaredChildNodeDefinitionValues(nodes);

nodeTypeManager.registerNodeType(myNodeTypeValue, ExtendedNodeTypeManager.REPLACE_IF_EXISTS);

5.3 Change or removing existing PropertyDefinition or child NodeDefinition

The main note what you should remember before changing or removing existing definition is consistency of the existing data. JCR do not allow you to change the node type in that way in which the the existing data would be incompatible with new node type. But if these changes are needed, you can do it in several phases, consistently changing the node type and the existing data.

Example

Add new residual property definition with name "downloadCount" to the existing node type "myNodeType".

There are two limitations that do not allow us to made the task with single call of registerNodeType method.

  1. Existing nodes of the type "myNodeType", wich does not contain properties "downloadCount" that conflicts with node type what we need.
  2. Registered node type "myNodeType" will not allow us to add properties "downloadCount" because it has no such specific properties.
To complete the tusk we need to make 3 steps

  1. We change the existing node type "myNodeType" by adding not mandatory property "downloadCount".
  2. We add to all existing nodes of the node type "myNodeType" property "downloadCount".
  3. We change definition of the property "downloadCount" of the node type "myNodeType" to mandatory.

5.4 Changing list of super types

NodeTypeValue testNValue = nodeTypeManager.getNodeTypeValue("exo:myNodeType");

List<String> superType  = testNValue.getDeclaredSupertypeNames();
superType.add("mix:versionable");
testNValue.setDeclaredSupertypeNames(superType);

nodeTypeManager.registerNodeType(testNValue, ExtendedNodeTypeManager.REPLACE_IF_EXISTS);

Tags:
Created by Sergey Kabashnyuk on 01/16/2009
Last modified by Nghia Nguyen on 06/15/2010

Products

generated on Thu Sep 02 15:25:03 UTC 2010

eXo Optional Modules

eXo Core Foundations


Copyright (c) 2000-2010. All Rights Reserved - eXo platform SAS
2.4.30451