You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by gp...@apache.org on 2009/04/09 18:32:54 UTC

svn commit: r763727 - in /myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval: ./ annotation/group/

Author: gpetracek
Date: Thu Apr  9 16:32:53 2009
New Revision: 763727

URL: http://svn.apache.org/viewvc?rev=763727&view=rev
Log:
restrict groups

Added:
    myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/None.java
Modified:
    myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/BeanValidationInterceptor.java
    myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/ExtValBeanValidationContext.java
    myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/BeanValidation.java

Modified: myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/BeanValidationInterceptor.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/BeanValidationInterceptor.java?rev=763727&r1=763726&r2=763727&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/BeanValidationInterceptor.java (original)
+++ myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/BeanValidationInterceptor.java Thu Apr  9 16:32:53 2009
@@ -36,6 +36,7 @@
 import org.apache.myfaces.extensions.validator.beanval.property.BeanValidationPropertyInformationKeys;
 import org.apache.myfaces.extensions.validator.beanval.annotation.group.BeanValidation;
 import org.apache.myfaces.extensions.validator.beanval.annotation.group.Group;
+import org.apache.myfaces.extensions.validator.beanval.annotation.group.None;
 import org.apache.myfaces.extensions.validator.beanval.annotation.extractor.DefaultGroupControllerScanningExtractor;
 import org.apache.myfaces.extensions.validator.beanval.validation.strategy.BeanValidationStrategyAdapter;
 import org.apache.myfaces.extensions.validator.internal.ToDo;
@@ -305,24 +306,28 @@
         Object firstBean = ExtValUtils.getELHelper().getBean(key[0]);
 
         List<Group> foundGroupsForCurrentView = new ArrayList<Group>();
+        List<Group> restrictedGroupsForCurrentView = new ArrayList<Group>();
 
         //extract bv-controller-annotation of
 
         //first bean
-        processClass(firstBean.getClass(), foundGroupsForCurrentView);
+        processClass(firstBean.getClass(), foundGroupsForCurrentView, restrictedGroupsForCurrentView);
 
         //first property
-        processFieldsAndProperties(key[0] + "." + key[1], firstBean, key[1], foundGroupsForCurrentView);
+        processFieldsAndProperties(key[0] + "." + key[1], firstBean, key[1],
+                foundGroupsForCurrentView, restrictedGroupsForCurrentView);
 
         //base object (of target property)
-        processClass(propertyDetails.getBaseObject().getClass(), foundGroupsForCurrentView);
+        processClass(propertyDetails.getBaseObject().getClass(),
+                foundGroupsForCurrentView, restrictedGroupsForCurrentView);
 
         //last property
         processFieldsAndProperties(
                 propertyDetails.getKey(),
                 propertyDetails.getBaseObject(),
                 propertyDetails.getProperty(),
-                foundGroupsForCurrentView);
+                foundGroupsForCurrentView,
+                restrictedGroupsForCurrentView);
 
         /*
          * add found groups to context
@@ -335,51 +340,77 @@
                         currentGroupClass, FacesContext.getCurrentInstance().getViewRoot().getViewId(), clientId);
             }
         }
+
+        /*
+         * add restricted groups
+         */
+        for (Group currentGroup : restrictedGroupsForCurrentView)
+        {
+            for (Class currentGroupClass : currentGroup.value())
+            {
+                ExtValBeanValidationContext.getCurrentInstance().restrictGroup(
+                        currentGroupClass, FacesContext.getCurrentInstance().getViewRoot().getViewId(), clientId);
+            }
+        }
     }
 
-    private void processClass(Class classToInspect, List<Group> foundGroupsForCurrentView)
+    private void processClass(Class classToInspect,
+                              List<Group> foundGroupsForCurrentView,
+                              List<Group> restrictedGroupsForCurrentView)
     {
         while (!Object.class.getName().equals(classToInspect.getName()))
         {
-            transferGroupValidationInformationToFoundGroups(classToInspect, foundGroupsForCurrentView);
+            transferGroupValidationInformationToFoundGroups(classToInspect,
+                    foundGroupsForCurrentView,
+                    restrictedGroupsForCurrentView);
 
-            processInterfaces(classToInspect, foundGroupsForCurrentView);
+            processInterfaces(classToInspect, foundGroupsForCurrentView, restrictedGroupsForCurrentView);
 
             classToInspect = classToInspect.getSuperclass();
         }
     }
 
-    private void processInterfaces(Class currentClass, List<Group> foundGroupsForCurrentView)
+    private void processInterfaces(Class currentClass,
+                                   List<Group> foundGroupsForCurrentView,
+                                   List<Group> restrictedGroupsForCurrentView)
     {
         for (Class currentInterface : currentClass.getInterfaces())
         {
-            transferGroupValidationInformationToFoundGroups(currentClass, foundGroupsForCurrentView);
+            transferGroupValidationInformationToFoundGroups(currentClass,
+                    foundGroupsForCurrentView,
+                    restrictedGroupsForCurrentView);
 
-            processInterfaces(currentInterface, foundGroupsForCurrentView);
+            processInterfaces(currentInterface, foundGroupsForCurrentView, restrictedGroupsForCurrentView);
         }
     }
 
-    private void transferGroupValidationInformationToFoundGroups(
-            Class classToInspect, List<Group> foundGroupsForCurrentView)
+    private void transferGroupValidationInformationToFoundGroups(Class classToInspect,
+                                                                 List<Group> foundGroupsForCurrentView,
+                                                                 List<Group> restrictedGroupsForCurrentView)
     {
         if (classToInspect.isAnnotationPresent(BeanValidation.class))
         {
             addGroupsForCurrentView(
                     (BeanValidation) classToInspect.getAnnotation(BeanValidation.class),
-                    foundGroupsForCurrentView);
+                    foundGroupsForCurrentView,
+                    restrictedGroupsForCurrentView);
         }
         else if (classToInspect.isAnnotationPresent(BeanValidation.List.class))
         {
             for(BeanValidation currentBeanValidation :
                     ((BeanValidation.List)classToInspect.getAnnotation(BeanValidation.List.class)).value())
             {
-                addGroupsForCurrentView(currentBeanValidation, foundGroupsForCurrentView);
+                addGroupsForCurrentView(currentBeanValidation,
+                        foundGroupsForCurrentView,
+                        restrictedGroupsForCurrentView);
             }
         }
     }
 
-    private void processFieldsAndProperties(
-            String key, Object base, String property, List<Group> foundGroupsForCurrentView)
+    private void processFieldsAndProperties(String key,
+                                            Object base,
+                                            String property, List<Group> foundGroupsForCurrentView,
+                                            List<Group> restrictedGroupsForCurrentView)
     {
         PropertyInformation propertyInformation = new DefaultGroupControllerScanningExtractor()
                 .extract(FacesContext.getCurrentInstance(), new PropertyDetails(key, base, property));
@@ -388,19 +419,24 @@
         {
             if (metaDataEntry.getValue() instanceof BeanValidation)
             {
-                addGroupsForCurrentView((BeanValidation) metaDataEntry.getValue(), foundGroupsForCurrentView);
+                addGroupsForCurrentView((BeanValidation) metaDataEntry.getValue(),
+                        foundGroupsForCurrentView, restrictedGroupsForCurrentView);
             }
             else if(metaDataEntry.getValue() instanceof BeanValidation.List)
             {
                 for(BeanValidation currentBeanValidation : ((BeanValidation.List)metaDataEntry.getValue()).value())
                 {
-                    addGroupsForCurrentView(currentBeanValidation, foundGroupsForCurrentView);
+                    addGroupsForCurrentView(currentBeanValidation,
+                            foundGroupsForCurrentView,
+                            restrictedGroupsForCurrentView);
                 }
             }
         }
     }
 
-    private void addGroupsForCurrentView(BeanValidation beanValidation, List<Group> foundGroupsForCurrentView)
+    private void addGroupsForCurrentView(BeanValidation beanValidation,
+                                         List<Group> foundGroupsForCurrentView,
+                                         List<Group> restrictedGroupsForCurrentView)
     {
         for (String currentViewId : beanValidation.viewId())
         {
@@ -408,6 +444,13 @@
                     currentViewId.equals("*"))
             {
                 foundGroupsForCurrentView.addAll(Arrays.asList(beanValidation.use()));
+
+                if(!(beanValidation.restrict().length == 1 &&
+                        beanValidation.restrict()[0].value().length == 1 &&
+                        beanValidation.restrict()[0].value()[0].equals(None.class)))
+                {
+                    restrictedGroupsForCurrentView.addAll(Arrays.asList(beanValidation.restrict()));
+                }
             }
         }
     }

Modified: myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/ExtValBeanValidationContext.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/ExtValBeanValidationContext.java?rev=763727&r1=763726&r2=763727&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/ExtValBeanValidationContext.java (original)
+++ myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/ExtValBeanValidationContext.java Thu Apr  9 16:32:53 2009
@@ -24,6 +24,8 @@
 import org.apache.myfaces.extensions.validator.internal.ToDo;
 import org.apache.myfaces.extensions.validator.internal.Priority;
 import org.apache.myfaces.extensions.validator.core.validation.message.resolver.MessageResolver;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 import javax.faces.context.FacesContext;
 import javax.validation.groups.Default;
@@ -40,6 +42,8 @@
  */
 public class ExtValBeanValidationContext
 {
+    protected final Log logger = LogFactory.getLog(getClass());
+
     private static final String KEY = ExtValBeanValidationContext.class.getName() + ":KEY";
 
     private static MessageInterpolator defaultMessageInterpolator = new DefaultMessageInterpolator(
@@ -48,7 +52,10 @@
     private static MessageResolver messageResolver;
 
     @ToDo(value = Priority.HIGH, description = "refactor to a pluggable GroupStorage")
-    private Map<String, List<Class>> currentGroups = new HashMap<String, List<Class>>();
+    private Map<String, List<Class>> addedGroups = new HashMap<String, List<Class>>();
+
+    @ToDo(value = Priority.HIGH, description = "refactor to a pluggable GroupStorage")
+    private Map<String, List<Class>> restrictedGroups = new HashMap<String, List<Class>>();
 
     @ToDo(value = Priority.HIGH,
             description = "idea: use it for impl. group support in a more extensible way - target: move it to the core")
@@ -94,12 +101,33 @@
 
     public void addGroup(Class groupClass, String viewId, String componentId)
     {
-        List<Class> groupList = this.currentGroups.get(getGroupKey(viewId, componentId));
+        addGroupToGroupStorage(groupClass, viewId, componentId, this.addedGroups);
+    }
+
+    public void restrictGroup(Class groupClass)
+    {
+        restrictGroup(groupClass, FacesContext.getCurrentInstance().getViewRoot().getViewId());
+    }
+
+    public void restrictGroup(Class groupClass, String viewId)
+    {
+        restrictGroup(groupClass, viewId, null);
+    }
+
+    public void restrictGroup(Class groupClass, String viewId, String componentId)
+    {
+        addGroupToGroupStorage(groupClass, viewId, componentId, this.restrictedGroups);
+    }
+
+    private void addGroupToGroupStorage(Class groupClass, String viewId, String componentId,
+                                        Map<String, List<Class>> groupStorage)
+    {
+        List<Class> groupList = groupStorage.get(getGroupKey(viewId, componentId));
 
         if(groupList == null)
         {
             groupList = new ArrayList<Class>();
-            this.currentGroups.put(getGroupKey(viewId, componentId), groupList);
+            groupStorage.put(getGroupKey(viewId, componentId), groupList);
         }
 
         if(!groupList.contains(groupClass))
@@ -115,19 +143,19 @@
 
     public void resetGroups(String viewId, String componentId)
     {
-        this.currentGroups.put(getGroupKey(viewId, componentId), new ArrayList<Class>());
+        this.addedGroups.put(getGroupKey(viewId, componentId), new ArrayList<Class>());
     }
 
     public Class[] getGroups()
     {
-        if(this.currentGroups.size() < 1)
+        if(this.addedGroups.size() < 1)
         {
             return new Class[] {Default.class};
         }
 
         List<Class> fullGroupList = new ArrayList<Class>();
 
-        for(Map.Entry<String, List<Class>> currentGroupEntry : this.currentGroups.entrySet())
+        for(Map.Entry<String, List<Class>> currentGroupEntry : this.addedGroups.entrySet())
         {
             fullGroupList.addAll(currentGroupEntry.getValue());
 
@@ -142,7 +170,7 @@
 
     public Class[] getGroups(String viewId, String componentId)
     {
-        if(this.currentGroups.size() < 1)
+        if(this.addedGroups.size() < 1)
         {
             if(!"true".equalsIgnoreCase(WebXmlParameter.DEACTIVATE_IMPLICIT_DEFAULT_GROUP_VALIDATION))
             {
@@ -152,13 +180,26 @@
         }
 
         String key = getGroupKey(viewId, null);
-        Class[] resultsForPage = buildResultFor(key);
+        List<Class> resultListForPage = buildResultFor(key, this.addedGroups);
 
         key = getGroupKey(viewId, componentId);
-        Class[] resultsForComponent = buildResultFor(key);
+        List<Class> resultListForComponent = buildResultFor(key, this.addedGroups);
+
+        //remove restricted groups
+        Class[] resultsForPage = filterResult(getGroupKey(viewId, null), resultListForPage);
+        Class[] resultsForComponent = filterResult(getGroupKey(viewId, componentId), resultListForComponent);
 
         if(resultsForPage.length == 0)
         {
+            if(resultsForComponent.length == 0)
+            {
+                if(this.logger.isDebugEnabled())
+                {
+                    this.logger.debug("no groups for group-validation available." +
+                            "maybe you restricted all groups or you aren't using groups." +
+                            "bean validation will use the default group for validation");
+                }
+            }
             return resultsForComponent;
         }
         else if(resultsForComponent.length == 0)
@@ -166,24 +207,32 @@
             return resultsForPage;
         }
 
-        return mergeResult(resultsForPage, resultsForComponent);
+        return mergeResults(resultsForPage, resultsForComponent);
     }
 
-    private Class[] buildResultFor(String key)
+    private List<Class> buildResultFor(String key, Map<String, List<Class>> groupStorage)
     {
-        List<Class> list = this.currentGroups.get(key);
-        int listSize = list != null ? list.size() : 0;
-        Class[] results = new Class[listSize];
+        List<Class> list = groupStorage.get(key);
+        return (list != null) ? list : new ArrayList<Class>();
+    }
+
+    private Class[] filterResult(String key, List<Class> addedGroups)
+    {
+        List<Class> restrictedGroups = buildResultFor(key, this.restrictedGroups);
+        List<Class> results = new ArrayList<Class>();
 
-        for(int i = 0; i < listSize; i++)
+        for(Class currentGroup : addedGroups)
         {
-            results[i] = this.currentGroups.get(key).get(i);
+            if(!restrictedGroups.contains(currentGroup))
+            {
+                results.add(currentGroup);
+            }
         }
 
-        return results;
+        return results.toArray(new Class[results.size()]);
     }
 
-    private Class[] mergeResult(Class[] resultsForPage, Class[] resultsForComponent)
+    private Class[] mergeResults(Class[] resultsForPage, Class[] resultsForComponent)
     {
         Class[] mergedResult = new Class[resultsForPage.length + resultsForComponent.length];
 
@@ -203,9 +252,10 @@
         removeGroup(groupClass, viewId, null);
     }
 
+    @ToDo(Priority.HIGH)
     public void removeGroup(Class groupClass, String viewId, String componentId)
     {
-        this.currentGroups.remove(getGroupKey(viewId, componentId));
+        this.addedGroups.remove(getGroupKey(viewId, componentId));
     }
 
     @ToDo(Priority.HIGH)

Modified: myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/BeanValidation.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/BeanValidation.java?rev=763727&r1=763726&r2=763727&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/BeanValidation.java (original)
+++ myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/BeanValidation.java Thu Apr  9 16:32:53 2009
@@ -45,6 +45,8 @@
 
     Group[] use() default @Group(Default.class);
 
+    Group[] restrict() default @Group(None.class);
+
     @Retention(RUNTIME) static @interface List
     {
         BeanValidation[] value();

Added: myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/None.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/None.java?rev=763727&view=auto
==============================================================================
--- myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/None.java (added)
+++ myfaces/extensions/validator/branches/beanval_integration/trunk/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/annotation/group/None.java Thu Apr  9 16:32:53 2009
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.extensions.validator.beanval.annotation.group;
+
+import org.apache.myfaces.extensions.validator.internal.UsageInformation;
+import org.apache.myfaces.extensions.validator.internal.UsageCategory;
+
+import java.lang.annotation.Target;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.TYPE;
+
+/**
+ * default group for restricted groups
+ *
+ * @author Gerhard Petracek
+ * @since 1.x.3
+ */
+
+@Target({METHOD, FIELD, TYPE})
+@Retention(RUNTIME)
+@UsageInformation(UsageCategory.INTERNAL)
+public @interface None
+{
+    public abstract Class<?>[] value();
+}
\ No newline at end of file