jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1387929 - /jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/cnd/CndImporter.java
Date Thu, 20 Sep 2012 09:21:07 GMT
Author: mduerig
Date: Thu Sep 20 09:21:07 2012
New Revision: 1387929

URL: http://svn.apache.org/viewvc?rev=1387929&view=rev
Log:
JCR-3430: CNDImporter should handle implied nt:base

Modified:
    jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/cnd/CndImporter.java

Modified: jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/cnd/CndImporter.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/cnd/CndImporter.java?rev=1387929&r1=1387928&r2=1387929&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/cnd/CndImporter.java
(original)
+++ jackrabbit/trunk/jackrabbit-jcr-commons/src/main/java/org/apache/jackrabbit/commons/cnd/CndImporter.java
Thu Sep 20 09:21:07 2012
@@ -19,7 +19,9 @@ package org.apache.jackrabbit.commons.cn
 import java.io.IOException;
 import java.io.Reader;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.RepositoryException;
@@ -27,15 +29,19 @@ import javax.jcr.Session;
 import javax.jcr.UnsupportedRepositoryOperationException;
 import javax.jcr.ValueFactory;
 import javax.jcr.Workspace;
+import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.InvalidNodeTypeDefinitionException;
 import javax.jcr.nodetype.NodeDefinitionTemplate;
 import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NodeTypeDefinition;
 import javax.jcr.nodetype.NodeTypeExistsException;
 import javax.jcr.nodetype.NodeTypeIterator;
 import javax.jcr.nodetype.NodeTypeManager;
 import javax.jcr.nodetype.NodeTypeTemplate;
 import javax.jcr.nodetype.PropertyDefinitionTemplate;
 
+import static org.apache.jackrabbit.JcrConstants.NT_BASE;
+
 /**
  * Utility class for importing compact node type definitions.
  * @see CompactNodeTypeDefReader
@@ -141,22 +147,20 @@ public final class CndImporter {
             CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry> cndReader
=
                 new CompactNodeTypeDefReader<NodeTypeTemplate, NamespaceRegistry>(cnd,
systemId, factory);
 
-            List<NodeTypeTemplate> ntts = cndReader.getNodeTypeDefinitions();
+            Map<String, NodeTypeTemplate> templates = new HashMap<String, NodeTypeTemplate>();
+            for (NodeTypeTemplate template : cndReader.getNodeTypeDefinitions()) {
+                templates.put(template.getName(), template);
+            }
 
-            NodeTypeIterator registered;
-            if (reregisterExisting) {
-                registered = nodeTypeManager.registerNodeTypes(ntts.toArray(new NodeTypeTemplate[ntts.size()]),
true);
-            } else {
-                List<NodeTypeTemplate> toRegister = new ArrayList<NodeTypeTemplate>(ntts.size());
-                for (NodeTypeTemplate ntt : ntts) {
-                    if (!nodeTypeManager.hasNodeType(ntt.getName())) {
-                        toRegister.add(ntt);
-                    }
+            List<NodeTypeTemplate> toRegister = new ArrayList<NodeTypeTemplate>(templates.size());
+            for (NodeTypeTemplate ntt : templates.values()) {
+                if (reregisterExisting || !nodeTypeManager.hasNodeType(ntt.getName())) {
+                    ensureNtBase(ntt, templates, nodeTypeManager);
+                    toRegister.add(ntt);
                 }
-
-                registered = nodeTypeManager.registerNodeTypes(toRegister.toArray(new NodeTypeTemplate[toRegister.size()]),
true);
             }
-
+            NodeTypeIterator registered = nodeTypeManager.registerNodeTypes(
+                    toRegister.toArray(new NodeTypeTemplate[toRegister.size()]), true);
             return toArray(registered);
         }
         finally {
@@ -164,6 +168,34 @@ public final class CndImporter {
         }
     }
 
+    private static void ensureNtBase(NodeTypeTemplate ntt, Map<String, NodeTypeTemplate>
templates,
+            NodeTypeManager nodeTypeManager) throws RepositoryException {
+        if (!ntt.isMixin() && !NT_BASE.equals(ntt.getName())) {
+            String[] supertypes = ntt.getDeclaredSupertypeNames();
+            if (supertypes.length == 0) {
+                ntt.setDeclaredSuperTypeNames(new String[] {NT_BASE});
+            } else {
+                // Check whether we need to add the implicit "nt:base" supertype
+                boolean needsNtBase = true;
+                for (String name : supertypes) {
+                    NodeTypeDefinition std = templates.get(name);
+                    if (std == null) {
+                        std = nodeTypeManager.getNodeType(name);
+                    }
+                    if (std != null && !std.isMixin()) {
+                        needsNtBase = false;
+                    }
+                }
+                if (needsNtBase) {
+                    String[] withNtBase = new String[supertypes.length + 1];
+                    withNtBase[0] = NT_BASE;
+                    System.arraycopy(supertypes, 0, withNtBase, 1, supertypes.length);
+                    ntt.setDeclaredSuperTypeNames(withNtBase);
+                }
+            }
+        }
+    }
+
     // -----------------------------------------------------< private >---
 
     private static NodeType[] toArray(NodeTypeIterator nodeTypes) {



Mime
View raw message