jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r1434626 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/
Date Thu, 17 Jan 2013 11:38:55 GMT
Author: jukka
Date: Thu Jan 17 11:38:54 2013
New Revision: 1434626

URL: http://svn.apache.org/viewvc?rev=1434626&view=rev
Log:
OAK-562: Clean up duplicate name conversion in plugins/nodetype

Avoid extra Oak-JCR-Oak name mappings in registerNodeTypes() by moving
the code that writes the type definition nodes from ReadWriteNTM to the
template classes that have direct access to the underlying Oak names.

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java?rev=1434626&r1=1434625&r2=1434626&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/AbstractNamedTemplate.java
Thu Jan 17 11:38:54 2013
@@ -47,7 +47,18 @@ abstract class AbstractNamedTemplate {
     }
 
     /**
-     * Returns the name of this template, or {@code null} if the name
+     * Returns the Oak name of this template, or {@code null} if the name
+     * has not yet been set.
+     *
+     * @return Oak name, or {@code null}
+     */
+    @CheckForNull
+    protected String getOakName() {
+        return oakName;
+    }
+
+    /**
+     * Returns the JCR name of this template, or {@code null} if the name
      * has not yet been set.
      *
      * @return JCR name, or {@code null}

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java?rev=1434626&r1=1434625&r2=1434626&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ItemDefinitionTemplateImpl.java
Thu Jan 17 11:38:54 2013
@@ -16,13 +16,22 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
+import static org.apache.jackrabbit.JcrConstants.JCR_AUTOCREATED;
+import static org.apache.jackrabbit.JcrConstants.JCR_MANDATORY;
+import static org.apache.jackrabbit.JcrConstants.JCR_NAME;
+import static org.apache.jackrabbit.JcrConstants.JCR_ONPARENTVERSION;
+import static org.apache.jackrabbit.JcrConstants.JCR_PROTECTED;
+
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
+import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.ItemDefinition;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.version.OnParentVersionAction;
 
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
 
 /**
@@ -58,6 +67,35 @@ abstract class ItemDefinitionTemplateImp
     }
 
     /**
+     * Writes the contents of this item definition to the given tree node.
+     * Used when registering new node types.
+     *
+     * @param tree an {@code nt:propertyDefinition} or
+     *             {@code nt:childNodeDefinition} node
+     * @throws RepositoryException if this definition could not be written
+     */
+    void writeTo(Tree tree) throws RepositoryException {
+        if (!residual) {
+            String oakName = getOakName();
+            if (oakName == null) {
+                throw new RepositoryException("Unnamed item definition");
+            }
+            tree.setProperty(JCR_NAME, oakName, Type.NAME);
+        }
+
+        // TODO avoid (in validator?) unbounded recursive auto creation.
+        // See 3.7.2.3.5 Chained Auto-creation (OAK-411)
+        tree.setProperty(JCR_AUTOCREATED, isAutoCreated);
+        tree.setProperty(JCR_MANDATORY, isMandatory);
+        tree.setProperty(JCR_PROTECTED, isProtected);
+        tree.setProperty(
+                JCR_ONPARENTVERSION,
+                OnParentVersionAction.nameFromValue(onParentVersion));
+    }
+
+    //------------------------------------------------------------< public >--
+
+    /**
      * Returns the name of this template, or {@code null} if the name
      * has not yet been set. The special name "*" is used for residual
      * item definitions.

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java?rev=1434626&r1=1434625&r2=1434626&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeDefinitionTemplateImpl.java
Thu Jan 17 11:38:54 2013
@@ -16,11 +16,20 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
+import static org.apache.jackrabbit.JcrConstants.JCR_DEFAULTPRIMARYTYPE;
+import static org.apache.jackrabbit.JcrConstants.JCR_REQUIREDPRIMARYTYPES;
+import static org.apache.jackrabbit.JcrConstants.JCR_SAMENAMESIBLINGS;
+
+import java.util.Arrays;
+
+import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.NodeDefinitionTemplate;
 import javax.jcr.nodetype.NodeType;
 
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
 
 class NodeDefinitionTemplateImpl extends ItemDefinitionTemplateImpl
@@ -45,6 +54,34 @@ class NodeDefinitionTemplateImpl extends
         setRequiredPrimaryTypeNames(definition.getRequiredPrimaryTypeNames());
     }
 
+    /**
+     * Writes the contents of this node definition to the given tree node.
+     * Used when registering new node types.
+     *
+     * @param tree an {@code nt:childNodeDefinition} node
+     * @throws RepositoryException if this definition could not be written
+     */
+    @Override
+    void writeTo(Tree tree) throws RepositoryException {
+        super.writeTo(tree);
+
+        tree.setProperty(JCR_SAMENAMESIBLINGS, allowSameNameSiblings);
+
+        if (requiredPrimaryTypeOakNames != null) {
+            tree.setProperty(
+                    JCR_REQUIREDPRIMARYTYPES,
+                    Arrays.asList(requiredPrimaryTypeOakNames), Type.NAMES);
+        }
+
+        if (defaultPrimaryTypeOakName != null) {
+            tree.setProperty(
+                    JCR_DEFAULTPRIMARYTYPE,
+                    defaultPrimaryTypeOakName, Type.NAME);
+        }
+    }
+
+    //------------------------------------------------------------< public >--
+
     @Override
     public boolean allowsSameNameSiblings() {
         return allowSameNameSiblings;
@@ -101,4 +138,10 @@ class NodeDefinitionTemplateImpl extends
                 getOakNamesOrThrowConstraintViolation(jcrNames);
     }
 
+    //------------------------------------------------------------< Object >--
+
+    public String toString() {
+        return String.format("PropertyDefinitionTemplate(%s)", getOakName());
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java?rev=1434626&r1=1434625&r2=1434626&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/NodeTypeTemplateImpl.java
Thu Jan 17 11:38:54 2013
@@ -16,22 +16,41 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
+import static org.apache.jackrabbit.JcrConstants.JCR_CHILDNODEDEFINITION;
+import static org.apache.jackrabbit.JcrConstants.JCR_HASORDERABLECHILDNODES;
+import static org.apache.jackrabbit.JcrConstants.JCR_ISMIXIN;
+import static org.apache.jackrabbit.JcrConstants.JCR_NODETYPENAME;
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYITEMNAME;
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+import static org.apache.jackrabbit.JcrConstants.JCR_PROPERTYDEFINITION;
+import static org.apache.jackrabbit.JcrConstants.JCR_SUPERTYPES;
+import static org.apache.jackrabbit.JcrConstants.NT_CHILDNODEDEFINITION;
+import static org.apache.jackrabbit.JcrConstants.NT_NODETYPE;
+import static org.apache.jackrabbit.JcrConstants.NT_PROPERTYDEFINITION;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_ABSTRACT;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_QUERYABLE;
+
+import java.util.Arrays;
 import java.util.List;
 
 import javax.annotation.Nonnull;
+import javax.jcr.RepositoryException;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.NodeDefinitionTemplate;
 import javax.jcr.nodetype.NodeTypeDefinition;
+import javax.jcr.nodetype.NodeTypeExistsException;
 import javax.jcr.nodetype.NodeTypeTemplate;
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.nodetype.PropertyDefinitionTemplate;
 
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
 
 import com.google.common.collect.Lists;
 
-final class NodeTypeTemplateImpl extends AbstractNamedTemplate
+class NodeTypeTemplateImpl extends AbstractNamedTemplate
         implements NodeTypeTemplate {
 
     private static final PropertyDefinition[] EMPTY_PROPERTY_DEFINITION_ARRAY =
@@ -53,11 +72,11 @@ final class NodeTypeTemplateImpl extends
     @Nonnull
     private String[] superTypeOakNames = new String[0];
 
-    private List<PropertyDefinitionTemplate> propertyDefinitionTemplates = null;
+    private List<PropertyDefinitionTemplateImpl> propertyDefinitionTemplates = null;
 
-    private List<NodeDefinitionTemplate> nodeDefinitionTemplates = null;
+    private List<NodeDefinitionTemplateImpl> nodeDefinitionTemplates = null;
 
-    public NodeTypeTemplateImpl(NameMapper mapper) {
+    NodeTypeTemplateImpl(NameMapper mapper) {
         super(mapper);
     }
 
@@ -96,6 +115,80 @@ final class NodeTypeTemplateImpl extends
         }
     }
 
+    /**
+     * Writes this node type as an {@code nt:nodeType} child of the given
+     * parent node. An exception is thrown if the child node already exists,
+     * unless the {@code allowUpdate} flag is set, in which case the existing
+     * node is overwritten.
+     *
+     * @param parent parent node under which to write this node type
+     * @param allowUpdate whether to overwrite an existing type
+     * @throws RepositoryException if this type could not be written
+     */
+    Tree writeTo(Tree parent, boolean allowUpdate) throws RepositoryException {
+        String oakName = getOakName();
+
+        Tree type = parent.getChild(oakName);
+        if (type != null) {
+            if (allowUpdate) {
+                type.remove();
+            } else {
+                throw new NodeTypeExistsException(
+                        "Node type " + getName() + " already exists");
+            }
+        }
+        type = parent.addChild(oakName);
+
+        type.setProperty(JCR_PRIMARYTYPE, NT_NODETYPE, Type.NAME);
+        type.setProperty(JCR_NODETYPENAME, oakName, Type.NAME);
+
+        if (superTypeOakNames != null && superTypeOakNames.length > 0) {
+            type.setProperty(
+                    JCR_SUPERTYPES,
+                    Arrays.asList(superTypeOakNames), Type.NAMES);
+        }
+
+        type.setProperty(JCR_IS_ABSTRACT, isAbstract);
+        type.setProperty(JCR_IS_QUERYABLE, queryable);
+        type.setProperty(JCR_ISMIXIN, isMixin);
+
+        // TODO fail (in validator?) if not orderable but a supertype is orderable
+        // See 3.7.6.7 Node Type Attribute Subtyping Rules (OAK-411)
+        type.setProperty(JCR_HASORDERABLECHILDNODES, isOrderable);
+
+        // TODO fail (in validator?) if a supertype specifies a different primary item
+        // See 3.7.6.7 Node Type Attribute Subtyping Rules (OAK-411)
+        if (primaryItemOakName != null) {
+            type.setProperty(JCR_PRIMARYITEMNAME, primaryItemOakName, Type.NAME);
+        }
+
+        // TODO fail (in validator?) on invalid item definitions
+        // See 3.7.6.8 Item Definitions in Subtypes (OAK-411)
+        if (propertyDefinitionTemplates != null) {
+            int pdn = 1;
+            for (PropertyDefinitionTemplateImpl pdt : propertyDefinitionTemplates) {
+                Tree tree = type.addChild(JCR_PROPERTYDEFINITION + pdn++);
+                tree.setProperty(
+                        JCR_PRIMARYTYPE, NT_PROPERTYDEFINITION, Type.NAME);
+                pdt.writeTo(tree);
+            }
+        }
+
+        if (nodeDefinitionTemplates != null) {
+            int ndn = 1;
+            for (NodeDefinitionTemplateImpl ndt : nodeDefinitionTemplates) {
+                Tree tree = type.addChild(JCR_CHILDNODEDEFINITION + ndn++);
+                tree.setProperty(
+                        JCR_PRIMARYTYPE, NT_CHILDNODEDEFINITION, Type.NAME);
+                ndt.writeTo(tree);
+            }
+        }
+
+        return type;
+    }
+
+    //------------------------------------------------------------< public >--
+
     @Override
     public boolean isMixin() {
         return isMixin;
@@ -171,7 +264,7 @@ final class NodeTypeTemplateImpl extends
     }
 
     @Override
-    public List<PropertyDefinitionTemplate> getPropertyDefinitionTemplates() {
+    public List<? extends PropertyDefinitionTemplate> getPropertyDefinitionTemplates()
{
         if (propertyDefinitionTemplates == null) {
             propertyDefinitionTemplates = Lists.newArrayList();
         }
@@ -189,11 +282,17 @@ final class NodeTypeTemplateImpl extends
     }
 
     @Override
-    public List<NodeDefinitionTemplate> getNodeDefinitionTemplates() {
+    public List<? extends NodeDefinitionTemplate> getNodeDefinitionTemplates() {
         if (nodeDefinitionTemplates == null) {
             nodeDefinitionTemplates = Lists.newArrayList();
         }
         return nodeDefinitionTemplates;
     }
 
+    //------------------------------------------------------------< Object >--
+
+    public String toString() {
+        return String.format("NodeTypeTemplate(%s)", getOakName());
+    }
+
 }
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java?rev=1434626&r1=1434625&r2=1434626&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/PropertyDefinitionTemplateImpl.java
Thu Jan 17 11:38:54 2013
@@ -17,15 +17,28 @@
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static org.apache.jackrabbit.JcrConstants.JCR_DEFAULTVALUES;
+import static org.apache.jackrabbit.JcrConstants.JCR_MULTIPLE;
+import static org.apache.jackrabbit.JcrConstants.JCR_REQUIREDTYPE;
+import static org.apache.jackrabbit.JcrConstants.JCR_VALUECONSTRAINTS;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_AVAILABLE_QUERY_OPERATORS;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_FULLTEXT_SEARCHABLE;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_QUERY_ORDERABLE;
+
+import java.util.Arrays;
 
 import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
 import javax.jcr.Value;
 import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.nodetype.PropertyDefinitionTemplate;
 import javax.jcr.query.qom.QueryObjectModelConstants;
 
+import org.apache.jackrabbit.oak.api.Tree;
+import org.apache.jackrabbit.oak.api.Type;
 import org.apache.jackrabbit.oak.namepath.NameMapper;
+import org.apache.jackrabbit.oak.plugins.memory.PropertyStates;
 
 class PropertyDefinitionTemplateImpl extends ItemDefinitionTemplateImpl
         implements PropertyDefinitionTemplate {
@@ -58,7 +71,7 @@ class PropertyDefinitionTemplateImpl ext
         super(mapper);
     }
 
-    public PropertyDefinitionTemplateImpl(
+    PropertyDefinitionTemplateImpl(
             NameMapper mapper, PropertyDefinition definition)
             throws ConstraintViolationException {
         super(mapper, definition);
@@ -71,6 +84,41 @@ class PropertyDefinitionTemplateImpl ext
         setDefaultValues(definition.getDefaultValues());
     }
 
+    /**
+     * Writes the contents of this property definition to the given tree node.
+     * Used when registering new node types.
+     *
+     * @param tree an {@code nt:propertyDefinition} node
+     * @throws RepositoryException if this definition could not be written
+     */
+    @Override
+    void writeTo(Tree tree) throws RepositoryException {
+        super.writeTo(tree);
+
+        tree.setProperty(
+                JCR_REQUIREDTYPE,
+                PropertyType.nameFromValue(requiredType).toUpperCase());
+        tree.setProperty(JCR_MULTIPLE, isMultiple);
+        tree.setProperty(JCR_IS_FULLTEXT_SEARCHABLE, fullTextSearchable);
+        tree.setProperty(JCR_IS_QUERY_ORDERABLE, queryOrderable);
+        tree.setProperty(
+                JCR_AVAILABLE_QUERY_OPERATORS,
+                Arrays.asList(queryOperators), Type.NAMES); // TODO: mapping?
+
+        if (valueConstraints != null) {
+            tree.setProperty(
+                    JCR_VALUECONSTRAINTS,
+                    Arrays.asList(valueConstraints), Type.STRINGS);
+        }
+
+        if (defaultValues != null) {
+            tree.setProperty(PropertyStates.createProperty(
+                    JCR_DEFAULTVALUES, Arrays.asList(defaultValues)));
+        }
+    }
+
+    //------------------------------------------------------------< public >--
+
     @Override
     public int getRequiredType() {
         return requiredType;
@@ -155,4 +203,10 @@ class PropertyDefinitionTemplateImpl ext
         }
     }
 
+    //------------------------------------------------------------< Object >--
+
+    public String toString() {
+        return String.format("PropertyDefinitionTemplate(%s)", getOakName());
+    }
+
 }

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java?rev=1434626&r1=1434625&r2=1434626&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java
(original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/nodetype/ReadWriteNodeTypeManager.java
Thu Jan 17 11:38:54 2013
@@ -16,62 +16,27 @@
  */
 package org.apache.jackrabbit.oak.plugins.nodetype;
 
+import static org.apache.jackrabbit.JcrConstants.JCR_SYSTEM;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_NODE_TYPES;
+import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.NODE_TYPES_PATH;
+
 import java.util.Arrays;
 import java.util.List;
 
 import javax.annotation.Nonnull;
-import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
-import javax.jcr.Value;
-import javax.jcr.nodetype.ItemDefinition;
 import javax.jcr.nodetype.NoSuchNodeTypeException;
-import javax.jcr.nodetype.NodeDefinition;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.NodeTypeDefinition;
-import javax.jcr.nodetype.NodeTypeExistsException;
 import javax.jcr.nodetype.NodeTypeIterator;
-import javax.jcr.nodetype.PropertyDefinition;
-import javax.jcr.version.OnParentVersionAction;
 
-import com.google.common.collect.Lists;
 import org.apache.jackrabbit.commons.iterator.NodeTypeIteratorAdapter;
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.Root;
 import org.apache.jackrabbit.oak.api.Tree;
 import org.apache.jackrabbit.oak.util.NodeUtil;
 
-import static org.apache.jackrabbit.JcrConstants.JCR_AUTOCREATED;
-import static org.apache.jackrabbit.JcrConstants.JCR_CHILDNODEDEFINITION;
-import static org.apache.jackrabbit.JcrConstants.JCR_DEFAULTPRIMARYTYPE;
-import static org.apache.jackrabbit.JcrConstants.JCR_DEFAULTVALUES;
-import static org.apache.jackrabbit.JcrConstants.JCR_HASORDERABLECHILDNODES;
-import static org.apache.jackrabbit.JcrConstants.JCR_ISMIXIN;
-import static org.apache.jackrabbit.JcrConstants.JCR_MANDATORY;
-import static org.apache.jackrabbit.JcrConstants.JCR_MULTIPLE;
-import static org.apache.jackrabbit.JcrConstants.JCR_NAME;
-import static org.apache.jackrabbit.JcrConstants.JCR_NODETYPENAME;
-import static org.apache.jackrabbit.JcrConstants.JCR_ONPARENTVERSION;
-import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYITEMNAME;
-import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
-import static org.apache.jackrabbit.JcrConstants.JCR_PROPERTYDEFINITION;
-import static org.apache.jackrabbit.JcrConstants.JCR_PROTECTED;
-import static org.apache.jackrabbit.JcrConstants.JCR_REQUIREDPRIMARYTYPES;
-import static org.apache.jackrabbit.JcrConstants.JCR_REQUIREDTYPE;
-import static org.apache.jackrabbit.JcrConstants.JCR_SAMENAMESIBLINGS;
-import static org.apache.jackrabbit.JcrConstants.JCR_SUPERTYPES;
-import static org.apache.jackrabbit.JcrConstants.JCR_SYSTEM;
-import static org.apache.jackrabbit.JcrConstants.JCR_VALUECONSTRAINTS;
-import static org.apache.jackrabbit.JcrConstants.NT_CHILDNODEDEFINITION;
-import static org.apache.jackrabbit.JcrConstants.NT_NODETYPE;
-import static org.apache.jackrabbit.JcrConstants.NT_PROPERTYDEFINITION;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_AVAILABLE_QUERY_OPERATORS;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_ABSTRACT;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_FULLTEXT_SEARCHABLE;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_QUERYABLE;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_IS_QUERY_ORDERABLE;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.JCR_NODE_TYPES;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.NODE_TYPES_PATH;
-import static org.apache.jackrabbit.oak.plugins.nodetype.NodeTypeConstants.RESIDUAL_NAME;
+import com.google.common.collect.Lists;
 
 /**
  * {@code ReadWriteNodeTypeManager} extends the {@link ReadOnlyNodeTypeManager}
@@ -173,113 +138,18 @@ public abstract class ReadWriteNodeTypeM
     private NodeType internalRegister(
             Tree types, NodeTypeDefinition ntd, boolean allowUpdate)
             throws RepositoryException {
-        String jcrName = ntd.getName();
-        String oakName = getOakName(jcrName);
-
-        Tree type = types.getChild(oakName);
-        if (type != null) {
-            if (allowUpdate) {
-                type.remove();
-            } else {
-                throw new NodeTypeExistsException("Node type " + jcrName + " already exists");
-            }
+        NodeTypeTemplateImpl template;
+        if (ntd instanceof NodeTypeTemplateImpl) {
+            template = (NodeTypeTemplateImpl) ntd;
+        } else {
+            // some external template implementation, copy before proceeding
+            template = new NodeTypeTemplateImpl(getNamePathMapper(), ntd);
         }
-        type = types.addChild(oakName);
-
+        Tree type = template.writeTo(types, allowUpdate);
         NodeUtil node = new NodeUtil(type, getNamePathMapper());
-        node.setName(JCR_PRIMARYTYPE, NT_NODETYPE);
-        node.setName(JCR_NODETYPENAME, jcrName);
-        String[] superTypeNames = ntd.getDeclaredSupertypeNames();
-        if (superTypeNames != null && superTypeNames.length > 0) {
-            node.setNames(JCR_SUPERTYPES, ntd.getDeclaredSupertypeNames());
-        }
-        node.setBoolean(JCR_IS_ABSTRACT, ntd.isAbstract());
-        node.setBoolean(JCR_IS_QUERYABLE, ntd.isQueryable());
-        node.setBoolean(JCR_ISMIXIN, ntd.isMixin());
-
-        // TODO fail if not orderable but a supertype is orderable. See 3.7.6.7 Node Type
Attribute Subtyping Rules (OAK-411)
-        node.setBoolean(JCR_HASORDERABLECHILDNODES, ntd.hasOrderableChildNodes());
-        String primaryItemName = ntd.getPrimaryItemName();
-
-        // TODO fail if a supertype specifies a different primary item. See 3.7.6.7 Node
Type Attribute Subtyping Rules (OAK-411)
-        if (primaryItemName != null) {
-            node.setName(JCR_PRIMARYITEMNAME, primaryItemName);
-        }
-
-        // TODO fail on invalid item definitions. See 3.7.6.8 Item Definitions in Subtypes
(OAK-411)
-        PropertyDefinition[] propertyDefinitions = ntd.getDeclaredPropertyDefinitions();
-        if (propertyDefinitions != null) {
-            int pdn = 1;
-            for (PropertyDefinition pd : propertyDefinitions) {
-                NodeUtil def = node.addChild(JCR_PROPERTYDEFINITION + pdn++, NT_PROPERTYDEFINITION);
-                internalRegisterPropertyDefinition(def, pd);
-            }
-        }
-
-        NodeDefinition[] nodeDefinitions = ntd.getDeclaredChildNodeDefinitions();
-        if (nodeDefinitions != null) {
-            int ndn = 1;
-            for (NodeDefinition nd : nodeDefinitions) {
-                NodeUtil def = node.addChild(JCR_CHILDNODEDEFINITION + ndn++, NT_CHILDNODEDEFINITION);
-                internalRegisterNodeDefinition(def, nd);
-            }
-        }
-
         return new NodeTypeImpl(this, getValueFactory(), node);
     }
 
-    private static void internalRegisterItemDefinition(
-            NodeUtil node, ItemDefinition def) {
-        String name = def.getName();
-        if (!RESIDUAL_NAME.equals(name)) {
-            node.setName(JCR_NAME, name);
-        }
-
-        // TODO avoid unbounded recursive auto creation. See 3.7.2.3.5 Chained Auto-creation
(OAK-411)
-        node.setBoolean(JCR_AUTOCREATED, def.isAutoCreated());
-        node.setBoolean(JCR_MANDATORY, def.isMandatory());
-        node.setBoolean(JCR_PROTECTED, def.isProtected());
-        node.setString(
-                JCR_ONPARENTVERSION,
-                OnParentVersionAction.nameFromValue(def.getOnParentVersion()));
-    }
-
-    private static void internalRegisterPropertyDefinition(
-            NodeUtil node, PropertyDefinition def) {
-        internalRegisterItemDefinition(node, def);
-
-        node.setString(
-                JCR_REQUIREDTYPE,
-                PropertyType.nameFromValue(def.getRequiredType()).toUpperCase());
-        node.setBoolean(JCR_MULTIPLE, def.isMultiple());
-        node.setBoolean(JCR_IS_FULLTEXT_SEARCHABLE, def.isFullTextSearchable());
-        node.setBoolean(JCR_IS_QUERY_ORDERABLE, def.isQueryOrderable());
-        node.setNames(JCR_AVAILABLE_QUERY_OPERATORS, def.getAvailableQueryOperators());
-
-        String[] constraints = def.getValueConstraints();
-        if (constraints != null) {
-            node.setStrings(JCR_VALUECONSTRAINTS, constraints);
-        }
-
-        Value[] values = def.getDefaultValues();
-        if (values != null) {
-            node.setValues(JCR_DEFAULTVALUES, values);
-        }
-    }
-
-    private static void internalRegisterNodeDefinition(NodeUtil node, NodeDefinition def)
{
-        internalRegisterItemDefinition(node, def);
-
-        node.setBoolean(JCR_SAMENAMESIBLINGS, def.allowsSameNameSiblings());
-        node.setNames(
-                JCR_REQUIREDPRIMARYTYPES,
-                def.getRequiredPrimaryTypeNames());
-        String defaultPrimaryType = def.getDefaultPrimaryTypeName();
-        if (defaultPrimaryType != null) {
-            node.setName(JCR_DEFAULTPRIMARYTYPE, defaultPrimaryType);
-        }
-    }
-
     private static Tree getOrCreateNodeTypes(Root root) {
         Tree types = root.getTree(NODE_TYPES_PATH);
         if (types == null) {



Mime
View raw message