You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by md...@apache.org on 2012/08/31 12:04:59 UTC

svn commit: r1379392 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java

Author: mduerig
Date: Fri Aug 31 10:04:58 2012
New Revision: 1379392

URL: http://svn.apache.org/viewvc?rev=1379392&view=rev
Log:
OAK-66: JCR Node Type Management
node type validation

Modified:
    jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java

Modified: jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java?rev=1379392&r1=1379391&r2=1379392&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java (original)
+++ jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java Fri Aug 31 10:04:58 2012
@@ -17,16 +17,19 @@
 package org.apache.jackrabbit.oak.plugins.type;
 
 import javax.jcr.RepositoryException;
+import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.nodetype.NodeType;
 import javax.jcr.nodetype.NodeTypeManager;
 
-import org.apache.jackrabbit.JcrConstants;
 import org.apache.jackrabbit.oak.api.CommitFailedException;
 import org.apache.jackrabbit.oak.api.CoreValue;
 import org.apache.jackrabbit.oak.api.PropertyState;
 import org.apache.jackrabbit.oak.spi.commit.Validator;
 import org.apache.jackrabbit.oak.spi.state.NodeState;
 
+import static org.apache.jackrabbit.JcrConstants.JCR_MIXINTYPES;
+import static org.apache.jackrabbit.JcrConstants.JCR_PRIMARYTYPE;
+
 class TypeValidator implements Validator {
     private final NodeTypeManager ntm;
 
@@ -34,56 +37,33 @@ class TypeValidator implements Validator
         this.ntm = ntm;
     }
 
-    private void checkTypeExists(PropertyState after) throws CommitFailedException {
-        if (JcrConstants.JCR_PRIMARYTYPE.equals(after.getName()) || JcrConstants.JCR_MIXINTYPES.equals(after.getName())) {
-            try {
-                for (CoreValue cv : after.getValues()) {
-                    String ntName = cv.getString();
-                    NodeType nt = ntm.getNodeType(ntName);
-                    if (nt.isAbstract()) {
-                        throw new CommitFailedException("Can't create node with abstract type: " + ntName);
-                    }
-                }
-            }
-            catch (RepositoryException e) {
-                throw new CommitFailedException(e);
-            }
-        }
-    }
-
     //-------------------------------------------------------< NodeValidator >
 
     @Override
-    public void propertyAdded(PropertyState after)
-            throws CommitFailedException {
-        checkTypeExists(after);
+    public void propertyAdded(PropertyState after) throws CommitFailedException {
+        validateType(after);
         // TODO: validate added property
     }
 
     @Override
-    public void propertyChanged(PropertyState before, PropertyState after)
-            throws CommitFailedException {
-        checkTypeExists(after);
+    public void propertyChanged(PropertyState before, PropertyState after) throws CommitFailedException {
+        validateType(after);
     }
 
     @Override
-    public void propertyDeleted(PropertyState before)
-            throws CommitFailedException {
+    public void propertyDeleted(PropertyState before) throws CommitFailedException {
         // TODO: validate removed property
     }
 
     @Override
-    public Validator childNodeAdded(String name, NodeState after)
-            throws CommitFailedException {
+    public Validator childNodeAdded(String name, NodeState after) throws CommitFailedException {
         // TODO: validate added child node
         // TODO: get the type for validating the child contents
         return this;
     }
 
     @Override
-    public Validator childNodeChanged(
-            String name, NodeState before, NodeState after)
-            throws CommitFailedException {
+    public Validator childNodeChanged(String name, NodeState before, NodeState after) throws CommitFailedException {
         // TODO: validate changed child node
         // TODO: get the type to validating the child contents
         return this;
@@ -95,4 +75,33 @@ class TypeValidator implements Validator
         return null;
     }
 
+    private void validateType(PropertyState after) throws CommitFailedException {
+        boolean primaryType = JCR_PRIMARYTYPE.equals(after.getName());
+        boolean mixinType = JCR_MIXINTYPES.equals(after.getName());
+        if (primaryType || mixinType) {
+            try {
+                for (CoreValue cv : after.getValues()) {
+                    String ntName = cv.getString();
+                    NodeType nt = ntm.getNodeType(ntName);
+                    if (nt.isAbstract()) {
+                        throwConstraintViolationException("Can't create node with abstract type: " + ntName);
+                    }
+                    if (primaryType && nt.isMixin()) {
+                        throwConstraintViolationException("Can't assign mixin for primary type: " + ntName);
+                    }
+                    if (mixinType && !nt.isMixin()) {
+                        throwConstraintViolationException("Can't assign primary type for mixin: " + ntName);
+                    }
+                }
+            }
+            catch (RepositoryException e) {
+                throw new CommitFailedException(e);
+            }
+        }
+    }
+
+    private static void throwConstraintViolationException(String message) throws CommitFailedException {
+        throw new CommitFailedException(new ConstraintViolationException(message));
+    }
+
 }