myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gpetra...@apache.org
Subject svn commit: r696868 - in /myfaces/extensions/validator/branches/jsf_1.1: core/src/main/java/org/apache/myfaces/extensions/validator/core/ core/src/main/java/org/apache/myfaces/extensions/validator/core/validation/strategy/ core/src/main/java/org/apache...
Date Fri, 19 Sep 2008 00:23:01 GMT
Author: gpetracek
Date: Thu Sep 18 17:23:01 2008
New Revision: 696868

URL: http://svn.apache.org/viewvc?rev=696868&view=rev
Log:
mechanism to join client-side validation of component libs for required fields via annotations

Added:
    myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/validation/strategy/RequiredAttributeStrategy.java
Modified:
    myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRenderKit.java
    myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRendererWrapper.java
    myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValValidationPhaseListener.java
    myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/util/ValidationUtils.java
    myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JoinValidationStrategy.java
    myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JpaValidationStrategy.java
    myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/LengthStrategy.java
    myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/RequiredStrategy.java

Modified: myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRenderKit.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRenderKit.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRenderKit.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRenderKit.java
Thu Sep 18 17:23:01 2008
@@ -38,7 +38,7 @@
 @UsageInformation(UsageCategory.INTERNAL)
 public class ExtValRenderKit extends RenderKit
 {
-    private RenderKit wrapped;
+    protected RenderKit wrapped;
 
     public static final String ID = "EXTVAL_RENDERKIT";
 
@@ -61,14 +61,14 @@
         }
         else
         {
-            wrapped.addRenderer(family, rendererType, new ExtValRendererWrapper(renderer));
+            wrapped.addRenderer(family, rendererType, createWrapper(renderer));
         }
     }
 
     public Renderer getRenderer(String family, String rendererType)
     {
         Renderer renderer = wrapped.getRenderer(family, rendererType);
-        return renderer instanceof ExtValRendererWrapper ? renderer : new ExtValRendererWrapper(renderer);
+        return renderer instanceof ExtValRendererWrapper ? renderer : createWrapper(renderer);
     }
 
     public ResponseStateManager getResponseStateManager()
@@ -85,4 +85,10 @@
     {
         return wrapped.createResponseStream(outputStream);
     }
+
+    @UsageInformation({UsageCategory.REUSE, UsageCategory.CUSTOMIZABLE})
+    protected Renderer createWrapper(Renderer renderer)
+    {
+        return new ExtValRendererWrapper(renderer);
+    }
 }

Modified: myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRendererWrapper.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRendererWrapper.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRendererWrapper.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValRendererWrapper.java
Thu Sep 18 17:23:01 2008
@@ -25,6 +25,7 @@
 import org.apache.myfaces.extensions.validator.util.ReflectionUtils;
 
 import javax.faces.component.UIComponent;
+import javax.faces.component.EditableValueHolder;
 import javax.faces.context.FacesContext;
 import javax.faces.convert.ConverterException;
 import javax.faces.render.Renderer;
@@ -48,7 +49,7 @@
 public class ExtValRendererWrapper extends Renderer
 {
     private static Boolean isAlternativeAvailable = null;
-    private Renderer wrapped;
+    protected Renderer wrapped;
 
     public ExtValRendererWrapper(Renderer wrapped)
     {
@@ -63,6 +64,11 @@
     public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
         throws IOException
     {
+        if(ValidationUtils.isValueOfComponentRequired(facesContext, uiComponent))
+        {
+            ((EditableValueHolder)uiComponent).setRequired(true);
+        }
+
         wrapped.encodeBegin(facesContext, uiComponent);
     }
 

Modified: myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValValidationPhaseListener.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValValidationPhaseListener.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValValidationPhaseListener.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/ExtValValidationPhaseListener.java
Thu Sep 18 17:23:01 2008
@@ -47,10 +47,8 @@
         RenderKitFactory renderKitFactory = (RenderKitFactory)
             FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
 
-        String renderKitId;
-        RenderKit renderKit;
-        renderKitId = facesContext.getViewRoot().getRenderKitId();
-        renderKit = renderKitFactory.getRenderKit(FacesContext.getCurrentInstance(), renderKitId);
+        String renderKitId = facesContext.getViewRoot().getRenderKitId();
+        RenderKit renderKit = renderKitFactory.getRenderKit(FacesContext.getCurrentInstance(),
renderKitId);
         renderKitFactory.addRenderKit(ExtValRenderKit.ID, new ExtValRenderKit(renderKit));
         facesContext.getViewRoot().setRenderKitId(ExtValRenderKit.ID);
     }

Added: myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/validation/strategy/RequiredAttributeStrategy.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/validation/strategy/RequiredAttributeStrategy.java?rev=696868&view=auto
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/validation/strategy/RequiredAttributeStrategy.java
(added)
+++ myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/core/validation/strategy/RequiredAttributeStrategy.java
Thu Sep 18 17:23:01 2008
@@ -0,0 +1,32 @@
+/*
+ * 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.core.validation.strategy;
+
+import java.lang.annotation.Annotation;
+
+/**
+ * Interface for validation strategies which validate if an input is required
+ *
+ * @author Gerhard Petracek
+ * @since 1.x.1
+ */
+public interface RequiredAttributeStrategy
+{
+    boolean markedAsRequired(Annotation annotation);
+}

Modified: myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/util/ValidationUtils.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/util/ValidationUtils.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/util/ValidationUtils.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/core/src/main/java/org/apache/myfaces/extensions/validator/util/ValidationUtils.java
Thu Sep 18 17:23:01 2008
@@ -23,6 +23,7 @@
 import org.apache.myfaces.extensions.validator.core.annotation.AnnotationEntry;
 import org.apache.myfaces.extensions.validator.core.annotation.extractor.AnnotationExtractor;
 import org.apache.myfaces.extensions.validator.core.validation.strategy.ValidationStrategy;
+import org.apache.myfaces.extensions.validator.core.validation.strategy.RequiredAttributeStrategy;
 import org.apache.myfaces.extensions.validator.internal.UsageInformation;
 import org.apache.myfaces.extensions.validator.internal.UsageCategory;
 
@@ -69,4 +70,34 @@
             }
         }
     }
+
+    public static boolean isValueOfComponentRequired(FacesContext facesContext, UIComponent
uiComponent)
+    {
+        if (!(uiComponent instanceof EditableValueHolder))
+        {
+            return false;
+        }
+
+        ValidationStrategy validationStrategy;
+
+        AnnotationExtractor annotationExtractor = FactoryUtils
+            .getAnnotationExtractorFactory().create();
+        for (AnnotationEntry entry : annotationExtractor
+            .extractAnnotations(facesContext, uiComponent))
+        {
+            validationStrategy = FactoryUtils
+                .getValidationStrategyFactory().create(
+                entry.getAnnotation());
+
+            if (validationStrategy != null && validationStrategy instanceof RequiredAttributeStrategy)
+            {
+                if(((RequiredAttributeStrategy)validationStrategy).markedAsRequired(entry.getAnnotation()))
+                {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
 }

Modified: myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JoinValidationStrategy.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JoinValidationStrategy.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JoinValidationStrategy.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JoinValidationStrategy.java
Thu Sep 18 17:23:01 2008
@@ -24,16 +24,18 @@
 import org.apache.myfaces.extensions.validator.core.annotation.extractor.AnnotationExtractor;
 import org.apache.myfaces.extensions.validator.core.validation.strategy.AbstractValidatorAdapter;
 import org.apache.myfaces.extensions.validator.core.validation.strategy.ValidationStrategy;
+import org.apache.myfaces.extensions.validator.core.validation.strategy.RequiredAttributeStrategy;
 import org.apache.myfaces.extensions.validator.util.FactoryUtils;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.validator.ValidatorException;
+import java.lang.annotation.Annotation;
 
 /**
  * @author Gerhard Petracek
  */
-public class JoinValidationStrategy extends AbstractValidatorAdapter
+public class JoinValidationStrategy extends AbstractValidatorAdapter implements RequiredAttributeStrategy
 {
     public void processValidation(FacesContext facesContext,
             UIComponent uiComponent, AnnotationEntry annotationEntry,
@@ -68,4 +70,33 @@
             }
         }
     }
+
+    public boolean markedAsRequired(Annotation annotation)
+    {
+        AnnotationExtractor extractor = new DefaultPropertyScanningAnnotationExtractor();
+
+        String[] targetExpressions = ((JoinValidation)annotation).value();
+
+        ValidationStrategy validationStrategy;
+
+        for (String targetExpression : targetExpressions)
+        {
+            for (AnnotationEntry entry : extractor.extractAnnotations(
+                FacesContext.getCurrentInstance(), targetExpression))
+            {
+                validationStrategy = FactoryUtils
+                        .getValidationStrategyFactory().create(
+                                entry.getAnnotation());
+
+                if (validationStrategy != null && validationStrategy instanceof RequiredAttributeStrategy)
+                {
+                    if(((RequiredAttributeStrategy)validationStrategy).markedAsRequired(entry.getAnnotation()))
+                    {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
 }

Modified: myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JpaValidationStrategy.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JpaValidationStrategy.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JpaValidationStrategy.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/JpaValidationStrategy.java
Thu Sep 18 17:23:01 2008
@@ -20,6 +20,7 @@
 
 import org.apache.myfaces.extensions.validator.core.annotation.AnnotationEntry;
 import org.apache.myfaces.extensions.validator.core.validation.strategy.AbstractValidationStrategy;
+import org.apache.myfaces.extensions.validator.core.validation.strategy.RequiredAttributeStrategy;
 import org.apache.myfaces.extensions.validator.internal.ToDo;
 import org.apache.myfaces.extensions.validator.internal.Priority;
 
@@ -35,7 +36,7 @@
 /**
  * @author Gerhard Petracek
  */
-public class JpaValidationStrategy extends AbstractValidationStrategy
+public class JpaValidationStrategy extends AbstractValidationStrategy implements RequiredAttributeStrategy
 {
 
     private static final String VALIDATE_REQUIRED = "required";
@@ -67,6 +68,20 @@
         }
     }
 
+    @ToDo(value = Priority.MEDIUM, description = "check")
+    public boolean markedAsRequired(Annotation annotation)
+    {
+        if(annotation instanceof Column && !((Column) annotation).nullable())
+        {
+            return true;
+        }
+        else if(annotation instanceof Basic && !((Basic)annotation).optional())
+        {
+            return true;
+        }
+        return false;
+    }
+
     private void validateColumnAnnotation(Column column, Object convertedObject)
             throws ValidatorException
     {

Modified: myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/LengthStrategy.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/LengthStrategy.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/LengthStrategy.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/LengthStrategy.java
Thu Sep 18 17:23:01 2008
@@ -21,18 +21,19 @@
 import org.apache.myfaces.extensions.validator.baseval.annotation.Length;
 import org.apache.myfaces.extensions.validator.core.annotation.AnnotationEntry;
 import org.apache.myfaces.extensions.validator.core.validation.strategy.AbstractValidatorAdapter;
+import org.apache.myfaces.extensions.validator.core.validation.strategy.RequiredAttributeStrategy;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.validator.LengthValidator;
 import javax.faces.validator.ValidatorException;
+import java.lang.annotation.Annotation;
 
 /**
  * @author Gerhard Petracek
  */
-public class LengthStrategy extends AbstractValidatorAdapter
+public class LengthStrategy extends AbstractValidatorAdapter implements RequiredAttributeStrategy
 {
-
     protected void processValidation(FacesContext facesContext,
             UIComponent uiComponent, AnnotationEntry annotationEntry,
             Object convertedObject) throws ValidatorException
@@ -46,4 +47,9 @@
 
         lengthValidator.validate(facesContext, uiComponent, convertedObject);
     }
+
+    public boolean markedAsRequired(Annotation annotation)
+    {
+        return ((Length)annotation).minimum() > 0;
+    }
 }
\ No newline at end of file

Modified: myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/RequiredStrategy.java
URL: http://svn.apache.org/viewvc/myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/RequiredStrategy.java?rev=696868&r1=696867&r2=696868&view=diff
==============================================================================
--- myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/RequiredStrategy.java
(original)
+++ myfaces/extensions/validator/branches/jsf_1.1/validation-modules/property-validation/src/main/java/org/apache/myfaces/extensions/validator/baseval/strategy/RequiredStrategy.java
Thu Sep 18 17:23:01 2008
@@ -21,6 +21,7 @@
 import org.apache.myfaces.extensions.validator.baseval.annotation.Required;
 import org.apache.myfaces.extensions.validator.core.annotation.AnnotationEntry;
 import org.apache.myfaces.extensions.validator.core.validation.strategy.AbstractValidationStrategy;
+import org.apache.myfaces.extensions.validator.core.validation.strategy.RequiredAttributeStrategy;
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
@@ -30,9 +31,8 @@
 /**
  * @author Gerhard Petracek
  */
-public class RequiredStrategy extends AbstractValidationStrategy
+public class RequiredStrategy extends AbstractValidationStrategy implements RequiredAttributeStrategy
 {
-
     public void processValidation(FacesContext facesContext,
             UIComponent uiComponent, AnnotationEntry annotationEntry,
             Object convertedObject) throws ValidatorException
@@ -45,8 +45,13 @@
         }
     }
 
+    public boolean markedAsRequired(Annotation annotation)
+    {
+        return true;
+    }
+
     protected String getValidationErrorMsgKey(Annotation annotation)
     {
         return ((Required) annotation).validationErrorMsgKey();
     }
-}
\ No newline at end of file
+}



Mime
View raw message