myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r831893 - in /myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval: interceptor/ storage/ util/
Date Mon, 02 Nov 2009 13:20:03 GMT
Author: gpetracek
Date: Mon Nov  2 13:20:03 2009
New Revision: 831893

URL: http://svn.apache.org/viewvc?rev=831893&view=rev
Log:
small bv changes

Modified:
    myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/interceptor/ExtValBeanValidationMetaDataExtractionInterceptor.java
    myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/storage/ModelValidationEntry.java
    myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/util/BeanValidationUtils.java

Modified: myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/interceptor/ExtValBeanValidationMetaDataExtractionInterceptor.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/interceptor/ExtValBeanValidationMetaDataExtractionInterceptor.java?rev=831893&r1=831892&r2=831893&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/interceptor/ExtValBeanValidationMetaDataExtractionInterceptor.java
(original)
+++ myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/interceptor/ExtValBeanValidationMetaDataExtractionInterceptor.java
Mon Nov  2 13:20:03 2009
@@ -20,17 +20,21 @@
 
 import org.apache.myfaces.extensions.validator.internal.UsageInformation;
 import org.apache.myfaces.extensions.validator.internal.UsageCategory;
+import org.apache.myfaces.extensions.validator.internal.ToDo;
+import org.apache.myfaces.extensions.validator.internal.Priority;
 import org.apache.myfaces.extensions.validator.core.interceptor.MetaDataExtractionInterceptor;
 import org.apache.myfaces.extensions.validator.core.property.PropertyInformation;
 import org.apache.myfaces.extensions.validator.core.property.PropertyDetails;
 import org.apache.myfaces.extensions.validator.core.property.PropertyInformationKeys;
 import org.apache.myfaces.extensions.validator.core.ValidationModuleAware;
 import org.apache.myfaces.extensions.validator.core.InvocationOrder;
+import org.apache.myfaces.extensions.validator.core.storage.FacesInformationStorage;
 import org.apache.myfaces.extensions.validator.beanval.BeanValidationModuleKey;
 import org.apache.myfaces.extensions.validator.beanval.util.BeanValidationUtils;
 import org.apache.myfaces.extensions.validator.util.ExtValUtils;
 
 import javax.faces.component.UIComponent;
+import javax.faces.event.PhaseId;
 import java.util.Map;
 
 /**
@@ -68,7 +72,25 @@
      */
     private void processExtValBeanValidationMetaData(UIComponent uiComponent, PropertyDetails
propertyDetails)
     {
-        BeanValidationUtils.addMetaDataToContext(uiComponent, propertyDetails);
+        if(isRenderResponsePhase())
+        {
+            BeanValidationUtils.addMetaDataToContext(uiComponent, propertyDetails, false);
+        }
+        else
+        {
+            BeanValidationUtils.addMetaDataToContext(uiComponent, propertyDetails, true);
+        }
+    }
+
+    @ToDo(value = Priority.MEDIUM, description = "move to util class")
+    private boolean isRenderResponsePhase()
+    {
+        return PhaseId.RENDER_RESPONSE.equals(getFacesInformationStorage().getCurrentPhaseId());
+    }
+
+    private FacesInformationStorage getFacesInformationStorage()
+    {
+        return ExtValUtils.getStorage(FacesInformationStorage.class, FacesInformationStorage.class.getName());
     }
 
     public String[] getModuleKeys()

Modified: myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/storage/ModelValidationEntry.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/storage/ModelValidationEntry.java?rev=831893&r1=831892&r2=831893&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/storage/ModelValidationEntry.java
(original)
+++ myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/storage/ModelValidationEntry.java
Mon Nov  2 13:20:03 2009
@@ -37,9 +37,10 @@
     private UIComponent component;
     private List<Class> groups = new ArrayList<Class>();
     private List<Object> validationTargets = new ArrayList<Object>();
-    private List<String> validationTargetExpressions = new ArrayList<String>();
     private boolean displayMessageInline = false;
     private String customMessage = ModelValidation.DEFAULT_MESSAGE;
+    
+    //the original source where the extval-bv meta-data has been found
     private Object metaDataSourceObject;
     private String viewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
 
@@ -86,14 +87,6 @@
         }
     }
 
-    public void addValidationTargetExpression(String targetExpression)
-    {
-        if(!this.validationTargetExpressions.contains(targetExpression))
-        {
-            this.validationTargetExpressions.add(targetExpression);
-        }
-    }
-
     /*
      * generated
      */
@@ -122,11 +115,6 @@
         return validationTargets;
     }
 
-    public List<String> getValidationTargetExpressions()
-    {
-        return validationTargetExpressions;
-    }
-
     public Object getMetaDataSourceObject()
     {
         return metaDataSourceObject;

Modified: myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/util/BeanValidationUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/util/BeanValidationUtils.java?rev=831893&r1=831892&r2=831893&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/util/BeanValidationUtils.java
(original)
+++ myfaces/extensions/validator/branches/branch_for_jsf_1_1/validation-modules/bean-validation/src/main/java/org/apache/myfaces/extensions/validator/beanval/util/BeanValidationUtils.java
Mon Nov  2 13:20:03 2009
@@ -69,7 +69,8 @@
         return "true".equalsIgnoreCase(WebXmlParameter.ACTIVATE_MULTIPLE_VIOLATION_MESSAGES_PER_FIELD);
     }
 
-    public static void addMetaDataToContext(UIComponent component, PropertyDetails propertyDetails)
+    public static void addMetaDataToContext(
+            UIComponent component, PropertyDetails propertyDetails, boolean processModelValidation)
     {
         String[] key = propertyDetails.getKey().split("\\.");
 
@@ -87,7 +88,8 @@
                 foundGroupsForPropertyValidation,
                 restrictedGroupsForPropertyValidation,
                 modelValidationEntryList,
-                restrictedGroupsForModelValidation);
+                restrictedGroupsForModelValidation,
+                processModelValidation);
 
         //first property
         processFieldsAndProperties(key[0] + "." + key[1],
@@ -96,14 +98,16 @@
                 foundGroupsForPropertyValidation,
                 restrictedGroupsForPropertyValidation,
                 modelValidationEntryList,
-                restrictedGroupsForModelValidation);
+                restrictedGroupsForModelValidation,
+                processModelValidation);
 
         //base object (of target property)
         processClass(propertyDetails.getBaseObject(),
                 foundGroupsForPropertyValidation,
                 restrictedGroupsForPropertyValidation,
                 modelValidationEntryList,
-                restrictedGroupsForModelValidation);
+                restrictedGroupsForModelValidation,
+                processModelValidation);
 
         //last property
         processFieldsAndProperties(
@@ -113,7 +117,8 @@
                 foundGroupsForPropertyValidation,
                 restrictedGroupsForPropertyValidation,
                 modelValidationEntryList,
-                restrictedGroupsForModelValidation);
+                restrictedGroupsForModelValidation,
+                processModelValidation);
 
         ExtValBeanValidationContext extValBeanValidationContext = ExtValBeanValidationContext.getCurrentInstance();
         String currentViewId = FacesContext.getCurrentInstance().getViewRoot().getViewId();
@@ -134,7 +139,8 @@
                                      List<Class> foundGroupsForPropertyValidation,
                                      List<Class> restrictedGroupsForPropertyValidation,
                                      List<ModelValidationEntry> modelValidationEntryList,
-                                     List<Class> restrictedGroupsForModelValidation)
+                                     List<Class> restrictedGroupsForModelValidation,
+                                     boolean processModelValidation)
     {
         Class classToInspect = objectToInspect.getClass();
         while (!Object.class.getName().equals(classToInspect.getName()))
@@ -143,13 +149,15 @@
                     foundGroupsForPropertyValidation,
                     restrictedGroupsForPropertyValidation,
                     modelValidationEntryList,
-                    restrictedGroupsForModelValidation);
+                    restrictedGroupsForModelValidation,
+                    processModelValidation);
 
             processInterfaces(objectToInspect.getClass(), objectToInspect,
                     foundGroupsForPropertyValidation,
                     restrictedGroupsForPropertyValidation,
                     modelValidationEntryList,
-                    restrictedGroupsForModelValidation);
+                    restrictedGroupsForModelValidation,
+                    processModelValidation);
 
             classToInspect = classToInspect.getSuperclass();
         }
@@ -157,10 +165,12 @@
 
     private static void processFieldsAndProperties(String key,
                                                    Object base,
-                                                   String property, List<Class> foundGroupsForPropertyValidation,
+                                                   String property,
+                                                   List<Class> foundGroupsForPropertyValidation,
                                                    List<Class> restrictedGroupsForPropertyValidation,
                                                    List<ModelValidationEntry> modelValidationEntryList,
-                                                   List<Class> restrictedGroupsForModelValidation)
+                                                   List<Class> restrictedGroupsForModelValidation,
+                                                   boolean processModelValidation)
     {
         PropertyInformation propertyInformation = new DefaultGroupControllerScanningExtractor()
                 .extract(FacesContext.getCurrentInstance(), new PropertyDetails(key, base,
property));
@@ -170,27 +180,59 @@
             if (metaDataEntry.getValue() instanceof BeanValidation)
             {
                 tryToProcessMetaData((BeanValidation) metaDataEntry.getValue(),
-                        base,
+                        tryToCreateNewTarget(base, property),
                         foundGroupsForPropertyValidation,
                         restrictedGroupsForPropertyValidation,
                         modelValidationEntryList,
-                        restrictedGroupsForModelValidation);
+                        restrictedGroupsForModelValidation,
+                        processModelValidation);
             }
             else if (metaDataEntry.getValue() instanceof BeanValidation.List)
             {
                 for (BeanValidation currentBeanValidation : ((BeanValidation.List) metaDataEntry.getValue()).value())
                 {
                     tryToProcessMetaData(currentBeanValidation,
-                            base,
+                            tryToCreateNewTarget(base, property),
                             foundGroupsForPropertyValidation,
                             restrictedGroupsForPropertyValidation,
                             modelValidationEntryList,
-                            restrictedGroupsForModelValidation);
+                            restrictedGroupsForModelValidation,
+                            processModelValidation);
                 }
             }
         }
     }
 
+    private static Object tryToCreateNewTarget(Object base, String property)
+    {
+        Object result = getValueOfProperty(base, property);
+
+        if (result == null)
+        {
+            return base;
+        }
+
+        return result;
+    }
+
+    private static Object getValueOfProperty(Object base, String property)
+    {
+        property = property.substring(0, 1).toUpperCase() + property.substring(1, property.length());
+        Method targetMethod = ReflectionUtils.tryToGetMethod(base.getClass(), "get" + property);
+
+        if (targetMethod == null)
+        {
+            targetMethod = ReflectionUtils.tryToGetMethod(base.getClass(), "is" + property);
+        }
+
+        if (targetMethod == null)
+        {
+            throw new IllegalStateException(
+                    "class " + base.getClass() + " has no public get/is " + property.toLowerCase());
+        }
+        return ReflectionUtils.tryToInvokeMethod(base, targetMethod);
+    }
+
     private static void processFoundGroups(ExtValBeanValidationContext extValBeanValidationContext,
                                            String currentViewId,
                                            String clientId,
@@ -237,7 +279,10 @@
 
             if (modelValidationEntry.getGroups().length > 0)
             {
-                addTargetsForModelValidation(modelValidationEntry, propertyDetails.getBaseObject());
+                if(modelValidationEntry.getValidationTargets().isEmpty())
+                {
+                    modelValidationEntry.addValidationTarget(propertyDetails.getBaseObject());
+                }
                 modelValidationEntry.setComponent(component);
                 extValBeanValidationContext.addModelValidationEntry(modelValidationEntry);
             }
@@ -249,7 +294,8 @@
             List<Class> foundGroupsForPropertyValidation,
             List<Class> restrictedGroupsForPropertyValidation,
             List<ModelValidationEntry> modelValidationEntryList,
-            List<Class> restrictedGroupsForModelValidation)
+            List<Class> restrictedGroupsForModelValidation,
+            boolean processModelValidation)
     {
         if (objectToInspect.getClass().isAnnotationPresent(BeanValidation.class))
         {
@@ -258,7 +304,8 @@
                     foundGroupsForPropertyValidation,
                     restrictedGroupsForPropertyValidation,
                     modelValidationEntryList,
-                    restrictedGroupsForModelValidation);
+                    restrictedGroupsForModelValidation,
+                    processModelValidation);
         }
         else if (objectToInspect.getClass().isAnnotationPresent(BeanValidation.List.class))
         {
@@ -270,7 +317,8 @@
                         foundGroupsForPropertyValidation,
                         restrictedGroupsForPropertyValidation,
                         modelValidationEntryList,
-                        restrictedGroupsForModelValidation);
+                        restrictedGroupsForModelValidation,
+                        processModelValidation);
             }
         }
     }
@@ -280,7 +328,8 @@
                                           List<Class> foundGroupsForPropertyValidation,
                                           List<Class> restrictedGroupsForPropertyValidation,
                                           List<ModelValidationEntry> modelValidationEntryList,
-                                          List<Class> restrictedGroupsForModelValidation)
+                                          List<Class> restrictedGroupsForModelValidation,
+                                          boolean processModelValidation)
     {
         for (Class currentInterface : currentClass.getInterfaces())
         {
@@ -288,13 +337,15 @@
                     foundGroupsForPropertyValidation,
                     restrictedGroupsForPropertyValidation,
                     modelValidationEntryList,
-                    restrictedGroupsForModelValidation);
+                    restrictedGroupsForModelValidation,
+                    processModelValidation);
 
             processInterfaces(currentInterface, metaDataSourceObject,
                     foundGroupsForPropertyValidation,
                     restrictedGroupsForPropertyValidation,
                     modelValidationEntryList,
-                    restrictedGroupsForModelValidation);
+                    restrictedGroupsForModelValidation,
+                    processModelValidation);
         }
     }
 
@@ -303,7 +354,8 @@
                                         List<Class> foundGroupsForPropertyValidation,
                                         List<Class> restrictedGroupsForPropertyValidation,
                                         List<ModelValidationEntry> modelValidationEntryList,
-                                        List<Class> restrictedGroupsForModelValidation)
+                                        List<Class> restrictedGroupsForModelValidation,
+                                        boolean processModelValidation)
     {
         for (String currentViewId : beanValidation.viewIds())
         {
@@ -314,7 +366,8 @@
                         foundGroupsForPropertyValidation,
                         restrictedGroupsForPropertyValidation,
                         modelValidationEntryList,
-                        restrictedGroupsForModelValidation);
+                        restrictedGroupsForModelValidation,
+                        processModelValidation);
                 break;
             }
         }
@@ -331,58 +384,22 @@
                                                  List<Class> foundGroupsForPropertyValidation,
                                                  List<Class> restrictedGroupsForPropertyValidation,
                                                  List<ModelValidationEntry> modelValidationEntryList,
-                                                 List<Class> restrictedGroupsForModelValidation)
+                                                 List<Class> restrictedGroupsForModelValidation,
+                                                 boolean processModelValidation)
     {
-        if (isModelValidation(beanValidation))
+        if (processModelValidation && isModelValidation(beanValidation))
         {
             addModelValidationEntry(
                     beanValidation, metaDataSourceObject,
                     modelValidationEntryList, restrictedGroupsForModelValidation);
         }
-        else
+        else if(!isModelValidation(beanValidation))
         {
             processGroups(
                     beanValidation, foundGroupsForPropertyValidation, restrictedGroupsForPropertyValidation);
         }
     }
 
-    private static void addTargetsForModelValidation(ModelValidationEntry modelValidationEntry,
Object defaultTarget)
-    {
-        if (isDefaultTarget(modelValidationEntry))
-        {
-            modelValidationEntry.addValidationTarget(defaultTarget);
-        }
-        else
-        {
-            addValidationTargets(modelValidationEntry);
-        }
-    }
-
-    private static boolean isDefaultTarget(ModelValidationEntry modelValidationEntry)
-    {
-        return modelValidationEntry.getValidationTargetExpressions().size() == 1 &&
-                modelValidationEntry.getValidationTargetExpressions().iterator().next()
-                        .equals(ModelValidation.DEFAULT_TARGET);
-    }
-
-    private static void addValidationTargets(ModelValidationEntry modelValidationEntry)
-    {
-        Object target;
-        for (String modelValidationTargetExpression : modelValidationEntry.getValidationTargetExpressions())
-        {
-            target = resolveTarget(modelValidationEntry.getMetaDataSourceObject(), modelValidationTargetExpression);
-
-            if (target == null && LOG.isErrorEnabled())
-            {
-                LOG.error("target unreachable - source class: " +
-                        modelValidationEntry.getMetaDataSourceObject().getClass().getName()
+
-                        " target to resolve: " + modelValidationTargetExpression);
-            }
-
-            modelValidationEntry.addValidationTarget(target);
-        }
-    }
-
     private static boolean isValidationPermitted(BeanValidation beanValidation)
     {
         ELHelper elHelper = ExtValUtils.getELHelper();
@@ -427,9 +444,19 @@
         modelValidationEntry.setCustomMessage(beanValidation.modelValidation().message());
         modelValidationEntry.setMetaDataSourceObject(metaDataSourceObject);
 
+        Object validationTarget;
         for(String validationTargetExpression : beanValidation.modelValidation().validationTargets())
         {
-            modelValidationEntry.addValidationTargetExpression(validationTargetExpression);
+            if(ModelValidation.DEFAULT_TARGET.equals(validationTargetExpression))
+            {
+                continue;
+            }
+
+            validationTarget = tryToResolveValidationTargetExpression(validationTargetExpression);
+            if(validationTarget != null)
+            {
+                modelValidationEntry.addValidationTarget(validationTarget);
+            }
         }
 
         if (beanValidation.restrictGroups().length > 0)
@@ -437,9 +464,21 @@
             restrictedGroupsForModelValidation.addAll(Arrays.asList(beanValidation.restrictGroups()));
         }
 
+        if(modelValidationEntry.getValidationTargets().isEmpty())
+        {
+            modelValidationEntry.addValidationTarget(metaDataSourceObject);
+        }
+
         modelValidationEntryList.add(modelValidationEntry);
     }
 
+    private static Object tryToResolveValidationTargetExpression(String validationTargetExpression)
+    {
+        ValueBindingExpression valueBindingExpression = new ValueBindingExpression(validationTargetExpression);
+        return ExtValUtils.getELHelper()
+                .getValueOfExpression(FacesContext.getCurrentInstance(), valueBindingExpression);
+    }
+
     private static void processGroups(BeanValidation beanValidation,
                                       List<Class> foundGroupsForPropertyValidation,
                                       List<Class> restrictedGroupsForPropertyValidation)
@@ -452,60 +491,6 @@
         }
     }
 
-    private static Object resolveTarget(Object metaDataSourceObject, String modelValidationTargetExpression)
-    {
-        ELHelper elHelper = ExtValUtils.getELHelper();
-
-        if (elHelper.isELTermWellFormed(modelValidationTargetExpression))
-        {
-            if (elHelper.isELTermValid(FacesContext.getCurrentInstance(), modelValidationTargetExpression))
-            {
-                return elHelper.getValueOfExpression(
-                        FacesContext.getCurrentInstance(), new ValueBindingExpression(modelValidationTargetExpression));
-            }
-            else
-            {
-                if (LOG.isErrorEnabled())
-                {
-                    LOG.error("an invalid binding is used: " + modelValidationTargetExpression);
-                }
-            }
-        }
-
-        String[] properties = modelValidationTargetExpression.split("\\.");
-
-        Object result = metaDataSourceObject;
-        for (String property : properties)
-        {
-            result = getValueOfProperty(result, property);
-
-            if (result == null)
-            {
-                return null;
-            }
-        }
-
-        return result;
-    }
-
-    private static Object getValueOfProperty(Object base, String property)
-    {
-        property = property.substring(0, 1).toUpperCase() + property.substring(1, property.length());
-        Method targetMethod = ReflectionUtils.tryToGetMethod(base.getClass(), "get" + property);
-
-        if (targetMethod == null)
-        {
-            targetMethod = ReflectionUtils.tryToGetMethod(base.getClass(), "is" + property);
-        }
-
-        if (targetMethod == null)
-        {
-            throw new IllegalStateException(
-                    "class " + base.getClass() + " has no public get/is " + property.toLowerCase());
-        }
-        return ReflectionUtils.tryToInvokeMethod(base, targetMethod);
-    }
-
     public static void processConstraintViolations(FacesContext facesContext,
                                                    UIComponent uiComponent,
                                                    Object convertedObject,



Mime
View raw message