jackrabbit-oak-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdue...@apache.org
Subject svn commit: r1383881 - /jackrabbit/oak/trunk/oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/type/TypeValidator.java
Date Wed, 12 Sep 2012 11:09:08 GMT
Author: mduerig
Date: Wed Sep 12 11:09:08 2012
New Revision: 1383881

URL: http://svn.apache.org/viewvc?rev=1383881&view=rev
Log:
OAK-66: JCR Node Type Management
better error reporting

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=1383881&r1=1383880&r2=1383881&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
Wed Sep 12 11:09:08 2012
@@ -60,7 +60,7 @@ class TypeValidator implements Validator
     private EffectiveNodeType parentType;
 
     @Nonnull
-    private EffectiveNodeType getParentType() throws CommitFailedException {
+    private EffectiveNodeType getParentType() throws RepositoryException {
         if (parentType == null) {
             parentType = getEffectiveNodeType(parent);
         }
@@ -76,51 +76,60 @@ class TypeValidator implements Validator
 
     @Override
     public void propertyAdded(PropertyState after) throws CommitFailedException {
-        validateType(after);
-        if (!getParentType().canSetProperty(after)) {
-            throwConstraintViolationException(
-                    "Can't add property " + after.getName() + " at " + parent.getPath());
+        try {
+            checkType(after);
+            getParentType().checkSetProperty(after);
+        }
+        catch (RepositoryException e) {
+            throw new CommitFailedException(
+                    "Cannot add property '" + after.getName() + "' at " + parent.getPath(),
e);
         }
     }
 
     @Override
     public void propertyChanged(PropertyState before, PropertyState after) throws CommitFailedException
{
-        validateType(after);
-        if (!getParentType().canSetProperty(after)) {
-            throwConstraintViolationException(
-                    "Can't set property " + after.getName() + " at " + parent.getPath());
+        try {
+            checkType(after);
+            getParentType().checkSetProperty(after);
+        }
+        catch (RepositoryException e) {
+            throw new CommitFailedException(
+                    "Cannot set property '" + after.getName() + "' at " + parent.getPath(),
e);
         }
     }
 
     @Override
     public void propertyDeleted(PropertyState before) throws CommitFailedException {
-        if (!getParentType().canRemoveProperty(before)) {
-            throwConstraintViolationException(
-                    "Can't delete property " + before.getName() + " at " + parent.getPath());
+        try {
+            getParentType().checkRemoveProperty(before);
+        }
+        catch (RepositoryException e) {
+            throw new CommitFailedException(
+                    "Cannot remove property '" + before.getName() + "' at " + parent.getPath(),
e);
         }
     }
 
     @Override
     public Validator childNodeAdded(String name, NodeState after) throws CommitFailedException
{
-        PropertyState type = after.getProperty(JCR_PRIMARYTYPE);
-        if (type == null || type.getValues().isEmpty()) {
-            if (!getParentType().canAddChildNode(name)) {
-                throwConstraintViolationException(
-                        "Can't add node " + name + " at " + parent.getPath());
+        try {
+            PropertyState type = after.getProperty(JCR_PRIMARYTYPE);
+            if (type == null || type.getValues().isEmpty()) {
+                getParentType().canAddChildNode(name);
+            }
+            else {
+                String ntName = type.getValues().get(0).getString();
+                getParentType().checkAddChildNode(name, ntName);
             }
+
+            ReadOnlyTree addedTree = new ReadOnlyTree(parent, name, after);
+            EffectiveNodeType addedType = getEffectiveNodeType(addedTree);
+            addedType.checkMandatoryItems(addedTree);
+            return new TypeValidator(ntm, new ReadOnlyTree(parent, name, after));
         }
-        else {
-            String ntName = type.getValues().get(0).getString();
-            if (!getParentType().canAddChildNode(name, ntName)) {
-                throwConstraintViolationException(
-                        "Can't add node " + name + " at " + parent.getPath());
-            }
+        catch (RepositoryException e) {
+            throw new CommitFailedException(
+                    "Cannot add node '" + name + "' at " + parent.getPath(), e);
         }
-
-        ReadOnlyTree addedTree = new ReadOnlyTree(parent, name, after);
-        EffectiveNodeType addedType = getEffectiveNodeType(addedTree);
-        addedType.checkMandatoryItems(addedTree);
-        return new TypeValidator(ntm, new ReadOnlyTree(parent, name, after));
     }
 
     @Override
@@ -130,96 +139,76 @@ class TypeValidator implements Validator
 
     @Override
     public Validator childNodeDeleted(String name, NodeState before) throws CommitFailedException
{
-        if (!getParentType().canRemoveNode(name)) {
-            throwConstraintViolationException(
-                    "Can't delete node " + name + " at " + parent.getPath());
+        try {
+            getParentType().checkRemoveNode(name);
+            return null;
+        }
+        catch (RepositoryException e) {
+            throw new CommitFailedException(
+                    "Cannot remove node '" + name + "' at " + parent.getPath(), e);
         }
-        return null;
     }
 
     //------------------------------------------------------------< private >---
 
-    private void validateType(PropertyState after) throws CommitFailedException {
+    private void checkType(PropertyState after) throws RepositoryException {
         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);
-                    }
+            for (CoreValue cv : after.getValues()) {
+                String ntName = cv.getString();
+                NodeType nt = ntm.getNodeType(ntName);
+                if (nt.isAbstract()) {
+                    throw new ConstraintViolationException("Can't create node with abstract
type: " + ntName);
+                }
+                if (primaryType && nt.isMixin()) {
+                    throw new ConstraintViolationException("Can't assign mixin for primary
type: " + ntName);
+                }
+                if (mixinType && !nt.isMixin()) {
+                    throw new ConstraintViolationException("Can't assign primary type for
mixin: " + ntName);
                 }
-            }
-            catch (RepositoryException e) {
-                throwConstraintViolationException(e);
             }
         }
     }
 
-    private NodeType getPrimaryType(Tree tree) throws CommitFailedException {
-        try {
-            PropertyState jcrPrimaryType = tree.getProperty(JCR_PRIMARYTYPE);
-            if (jcrPrimaryType != null) {
-                for (CoreValue typeName : jcrPrimaryType.getValues()) {
-                    String ntName = typeName.getString();
-                    NodeType type = ntm.getNodeType(ntName);
-                    if (type == null) {
-                        log.warn("Could not find node type {} for item at {}", ntName, tree.getPath());
-                    }
-                    return type;
+    private NodeType getPrimaryType(Tree tree) throws RepositoryException {
+        PropertyState jcrPrimaryType = tree.getProperty(JCR_PRIMARYTYPE);
+        if (jcrPrimaryType != null) {
+            for (CoreValue typeName : jcrPrimaryType.getValues()) {
+                String ntName = typeName.getString();
+                NodeType type = ntm.getNodeType(ntName);
+                if (type == null) {
+                    log.warn("Could not find node type {} for item at {}", ntName, tree.getPath());
                 }
+                return type;
             }
-            log.warn("Item at {} has no primary type. Assuming nt:unstructured", tree.getPath());
-            return ntm.getNodeType(NT_UNSTRUCTURED);
-        }
-        catch (RepositoryException e) {
-            return throwConstraintViolationException(e);
         }
+        log.warn("Item at {} has no primary type. Assuming nt:unstructured", tree.getPath());
+        return ntm.getNodeType(NT_UNSTRUCTURED);
     }
 
-    private List<NodeType> getMixinTypes(Tree tree) throws CommitFailedException {
-        try {
-            List<NodeType> types = Lists.newArrayList();
-            PropertyState jcrMixinType = tree.getProperty(JCR_MIXINTYPES);
-            if (jcrMixinType != null) {
-                for (CoreValue typeName : jcrMixinType.getValues()) {
-                    String ntName = typeName.getString();
-                    NodeType type = ntm.getNodeType(ntName);
-                    if (type == null) {
-                        log.warn("Could not find mixin type {} for item at {}", ntName, tree.getPath());
-                    }
-                    else {
-                        types.add(type);
-                    }
+    private List<NodeType> getMixinTypes(Tree tree) throws RepositoryException {
+        List<NodeType> types = Lists.newArrayList();
+        PropertyState jcrMixinType = tree.getProperty(JCR_MIXINTYPES);
+        if (jcrMixinType != null) {
+            for (CoreValue typeName : jcrMixinType.getValues()) {
+                String ntName = typeName.getString();
+                NodeType type = ntm.getNodeType(ntName);
+                if (type == null) {
+                    log.warn("Could not find mixin type {} for item at {}", ntName, tree.getPath());
+                }
+                else {
+                    types.add(type);
                 }
             }
-            return types;
-        }
-        catch (RepositoryException e) {
-            return throwConstraintViolationException(e);
         }
+        return types;
     }
 
-    private EffectiveNodeType getEffectiveNodeType(Tree tree) throws CommitFailedException
{
+    private EffectiveNodeType getEffectiveNodeType(Tree tree) throws RepositoryException
{
         return new EffectiveNodeType(getPrimaryType(tree), getMixinTypes(tree));
     }
 
-    private static <T> T throwConstraintViolationException(String message) throws CommitFailedException
{
-        throw new CommitFailedException(new ConstraintViolationException(message));
-    }
-
-    private static <T> T throwConstraintViolationException(RepositoryException cause)
throws CommitFailedException {
-        throw new CommitFailedException(cause);
-    }
-
     private static class EffectiveNodeType {
         private final Iterable<NodeType> allTypes;
 
@@ -227,81 +216,88 @@ class TypeValidator implements Validator
             this.allTypes = Iterables.concat(mixinTypes, Collections.singleton(primaryType));
         }
 
-        public boolean canSetProperty(PropertyState property) {
-            return property.isArray()
-                ? canSetProperty(property.getName(), property.getValues())
-                : canSetProperty(property.getName(), property.getValue());
+        public void checkSetProperty(PropertyState property) throws ConstraintViolationException
{
+            if (property.isArray()) {
+                checkSetProperty(property.getName(), property.getValues());
+            }
+            else {
+                checkSetProperty(property.getName(), property.getValue());
+            }
         }
 
-        private boolean canSetProperty(final String propertyName, final List<CoreValue>
values) {
+        private void checkSetProperty(final String propertyName, final List<CoreValue>
values)
+                throws ConstraintViolationException {
             Value[] jcrValues = jcrValues(values);
             for (NodeType nodeType : allTypes) {
                 if (nodeType.canSetProperty(propertyName, jcrValues)) {
-                    return true;
+                    return;
                 }
             }
-            return false;
+            throw new ConstraintViolationException("Cannot set property '" + propertyName
+ "' to '" + values + '\'');
         }
 
-        private boolean canSetProperty(final String propertyName, final CoreValue value)
{
+        private void checkSetProperty(final String propertyName, final CoreValue value)
+                throws ConstraintViolationException {
             Value jcrValue = jcrValue(value);
             for (NodeType nodeType : allTypes) {
                 if (nodeType.canSetProperty(propertyName, jcrValue)) {
-                    return true;
+                    return;
                 }
             }
-            return false;
+            throw new ConstraintViolationException("Cannot set property '" + propertyName
+ "' to '" + value + '\'');
         }
 
-        public boolean canRemoveProperty(PropertyState property) {
+        public void checkRemoveProperty(PropertyState property) throws ConstraintViolationException
{
             final String name = property.getName();
             for (NodeType nodeType : allTypes) {
                 if (nodeType.canRemoveProperty(name)) {
-                    return true;
+                    return;
                 }
             }
-            return false;
+            throw new ConstraintViolationException("Cannot remove property '" + property.getName()
+ '\'');
         }
 
-        public boolean canRemoveNode(final String name) {
+        public void checkRemoveNode(final String name) throws ConstraintViolationException
{
             for (NodeType nodeType : allTypes) {
                 if (nodeType.canRemoveProperty(name)) {
-                    return true;
+                    return;
                 }
             }
-            return false;
+            throw new ConstraintViolationException("Cannot remove node '" + name + '\'');
         }
 
-        public boolean canAddChildNode(final String name) {
+        public void canAddChildNode(final String name) throws ConstraintViolationException
{
             for (NodeType nodeType : allTypes) {
                 if (nodeType.canAddChildNode(name)) {
-                    return true;
+                    return;
                 }
             }
-            return false;
+            throw new ConstraintViolationException("Cannot add node '" + name + '\'');
         }
 
-        public boolean canAddChildNode(final String name, final String ntName) {
+        public void checkAddChildNode(final String name, final String ntName) throws ConstraintViolationException
{
             for (NodeType nodeType : allTypes) {
                 if (nodeType.canAddChildNode(name, ntName)) {
-                    return true;
+                    return;
                 }
             }
-            return false;
+            throw new ConstraintViolationException("Cannot add node '" + name + "' of type
'" + ntName + '\'');
         }
 
-        public void checkMandatoryItems(final ReadOnlyTree tree) throws CommitFailedException
{
+        public void checkMandatoryItems(final ReadOnlyTree tree) throws ConstraintViolationException
{
             for (NodeType nodeType : allTypes) {
                 for (PropertyDefinition pd : nodeType.getPropertyDefinitions()) {
                     String name = pd.getName();
                     if (pd.isMandatory() && !pd.isProtected() && tree.getProperty(name)
== null) {
-                        throwConstraintViolationException(name + " in " + nodeType.getName()
+ " is mandatory");
+                        throw new ConstraintViolationException(
+                                "Property '" + name + "' in '" + nodeType.getName() + "'
is mandatory");
                     }
                 }
                 for (NodeDefinition nd : nodeType.getChildNodeDefinitions()) {
                     String name = nd.getName();
                     if (nd.isMandatory() && !nd.isProtected() && tree.getChild(name)
== null) {
-                        throwConstraintViolationException(name + " in " + nodeType.getName()
+ " is mandatory");
+                        throw new ConstraintViolationException(
+                                "Node '" + name + "' in '" + nodeType.getName() + "' is mandatory");
                     }
                 }
             }



Mime
View raw message