Author: alexsmirnov
Date: 2008-07-11 20:42:05 -0400 (Fri, 11 Jul 2008)
New Revision: 9551
Added:
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
Modified:
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/DataBean.java
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/MinMaxBean.java
trunk/sandbox/samples/beanValidatorSample/src/main/webapp/pages/index.jsp
trunk/sandbox/ui/beanValidator/src/main/config/component/beanValidator.xml
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java
Log:
graph validator component.
Modified:
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/DataBean.java
===================================================================
---
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/DataBean.java 2008-07-11
17:00:31 UTC (rev 9550)
+++
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/DataBean.java 2008-07-12
00:42:05 UTC (rev 9551)
@@ -9,6 +9,8 @@
import java.util.List;
import java.util.ListIterator;
+import org.hibernate.validator.Max;
+
/**
* @author asmirnov
*
@@ -27,6 +29,14 @@
backend.add(new MinMaxBean());
}
+ @Max(value=20,message="Total value should be less then 20")
+ public int getTotal(){
+ int total = 0;
+ for (Validable bean : backend) {
+ total += bean.getIntValue();
+ }
+ return total;
+ }
/**
* @param o
* @return
Modified:
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/MinMaxBean.java
===================================================================
---
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/MinMaxBean.java 2008-07-11
17:00:31 UTC (rev 9550)
+++
trunk/sandbox/samples/beanValidatorSample/src/main/java/org/richfaces/MinMaxBean.java 2008-07-12
00:42:05 UTC (rev 9551)
@@ -55,7 +55,7 @@
public String getIntDescription() {
// TODO Auto-generated method stub
- return "Integer Value, fron 2 to 10";
+ return "Integer Value, valid values from 2 to 10";
}
}
Modified: trunk/sandbox/samples/beanValidatorSample/src/main/webapp/pages/index.jsp
===================================================================
--- trunk/sandbox/samples/beanValidatorSample/src/main/webapp/pages/index.jsp 2008-07-11
17:00:31 UTC (rev 9550)
+++ trunk/sandbox/samples/beanValidatorSample/src/main/webapp/pages/index.jsp 2008-07-12
00:42:05 UTC (rev 9551)
@@ -16,18 +16,19 @@
<h:form id="form">
<rich:panel>
<f:facet name="header">
- <h2>Single input field with label and message</h2>
+ <h2>Single input field with label and message. Validated by AJAX on every
char.</h2>
</f:facet>
<h:outputLabel for="ltext"
value="#{lengthBean.textDescription}" />
<h:inputText id="ltext" value="#{lengthBean.text}">
- <v:ajaxValidator event="onblur" />
+ <v:ajaxValidator event="onkeyup" />
</h:inputText>
<rich:message for="ltext" showDetail="true"
showSummary="true" />
</rich:panel>
- <h2>Input fields with label and message in the JSF dataTable</h2>
+ <h2>Input fields with label and message in the JSF dataTable. Each field
validated by AJAX on "onblur" event</h2>
+ <v:graphValidator value="#{data}" >
<h:dataTable value="#{data}" var="bean"
id="table">
<f:facet name="header">
- <h:outputText value="Validate values in the data table" />
+ <h:outputText value="Validate values in the data table. Total sum for an all
integer values validated for a value less then 20" />
</f:facet>
<h:column>
@@ -51,9 +52,10 @@
<rich:message for="intValue" showDetail="true"
showSummary="true" />
</h:column>
</h:dataTable>
+ </v:graphValidator>
<h:commandButton value="Non-AJAX Post submit"></h:commandButton>
</h:form>
- <rich:messages />
+ <rich:messages />
</f:view>
</body>
</html>
Modified: trunk/sandbox/ui/beanValidator/src/main/config/component/beanValidator.xml
===================================================================
--- trunk/sandbox/ui/beanValidator/src/main/config/component/beanValidator.xml 2008-07-11
17:00:31 UTC (rev 9550)
+++ trunk/sandbox/ui/beanValidator/src/main/config/component/beanValidator.xml 2008-07-12
00:42:05 UTC (rev 9551)
@@ -58,6 +58,30 @@
<defaultvalue>true</defaultvalue>
</property>
</component>
+ <component>
+ <name>org.richfaces.GraphValidator</name>
+ <family>org.richfaces.GraphValidator</family>
+ <classname>
+ org.richfaces.component.html.HtmlGraphValidator
+ </classname>
+ <superclass>org.richfaces.component.UIGraphValidator</superclass>
+ <description><![CDATA[]]></description>
+ <tag>
+ <name>graphValidator</name>
+ <classname>org.richfaces.taglib.html.jsp.GraphValidator</classname>
+ <superclass>
+ org.ajax4jsf.webapp.taglib.UIComponentTagBase
+ </superclass>
+ <!-- <superclass>
+ org.ajax4jsf.webapp.taglib.UIComponentTagBase
+ </superclass>
+ -->
+ <displayname>Bean validation @ and @validation support</displayname>
+ <icon>icon</icon>
+ <test />
+ </tag>
+ &ui_component_attributes;
+ </component>
<validator generate="false">
<id>org.richfaces.BeanValidator</id>
<classname>
Added:
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
===================================================================
---
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
(rev 0)
+++
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java 2008-07-12
00:42:05 UTC (rev 9551)
@@ -0,0 +1,150 @@
+/**
+ *
+ */
+
+package org.richfaces.component;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javax.el.ValueExpression;
+import javax.faces.FacesException;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.EditableValueHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIComponentBase;
+import javax.faces.component.UIInput;
+import javax.faces.component.UIMessage;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.FacesEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.validator.Validator;
+
+import org.ajax4jsf.component.AjaxComponent;
+import org.ajax4jsf.component.AjaxContainer;
+import org.ajax4jsf.component.AjaxSupport;
+import org.ajax4jsf.component.EventValueExpression;
+import org.ajax4jsf.context.AjaxContext;
+import org.ajax4jsf.event.AjaxEvent;
+import org.ajax4jsf.event.AjaxListener;
+import org.ajax4jsf.renderkit.AjaxContainerRenderer;
+import org.ajax4jsf.renderkit.AjaxRendererUtils;
+import org.richfaces.event.ValidationEvent;
+import org.richfaces.validator.BeanValidator;
+import org.richfaces.validator.FacesBeanValidator;
+
+import com.sun.facelets.FaceletException;
+
+/**
+ * JSF component class
+ *
+ */
+public abstract class UIGraphValidator extends UIComponentBase {
+
+ public static final String COMPONENT_TYPE = "org.richfaces.GraphValidator";
+
+ public static final String COMPONENT_FAMILY = "org.richfaces.GraphValidator";
+
+ /**
+ * Get object for validation
+ *
+ * @return
+ */
+ public abstract Object getValue();
+
+ /**
+ * Set object for validation
+ *
+ * @param newvalue
+ */
+ public abstract void setValue(Object newvalue);
+
+ /**
+ * Get set of profiles for validation
+ *
+ * @return
+ */
+ public abstract Set<String> getProfile();
+
+ /**
+ * Set set of profiles for validation
+ *
+ * @param newvalue
+ */
+ public abstract void setProfile(Set<String> newvalue);
+
+ @Override
+ public void processUpdates(FacesContext context) {
+ super.processUpdates(context);
+ Object value = getValue();
+ if (null != value) {
+ BeanValidator beanValidator = BeanValidator.getInstance(context);
+ String[] messages = beanValidator.validateGraph(context, value,getProfile());
+ if (null != messages) {
+ context.renderResponse();
+ // send all validation messages.
+ for (String msg : messages) {
+ // TODO - create Summary message ?
+ context.addMessage(null, new FacesMessage(
+ FacesMessage.SEVERITY_ERROR, msg, msg));
+ }
+ }
+
+ }
+ }
+
+ @Override
+ public void encodeBegin(FacesContext context) throws IOException {
+ super.encodeBegin(context);
+ Validator validator =
context.getApplication().createValidator(FacesBeanValidator.BEAN_VALIDATOR_TYPE);
+ setupValidators(this,validator);
+ }
+
+ @Override
+ public void encodeChildren(FacesContext context) throws IOException {
+ if(isRendered()){
+ for (UIComponent child : getChildren()) {
+ if(child.isRendered()){
+ child.encodeAll(context);
+ }
+ }
+ }
+ }
+
+ private void setupValidators(UIComponent component,
+ Validator validator) {
+ Iterator<UIComponent> facetsAndChildren = component.getFacetsAndChildren();
+ while (facetsAndChildren.hasNext()) {
+ UIComponent child = facetsAndChildren.next();
+ if (child instanceof EditableValueHolder) {
+ EditableValueHolder input = (EditableValueHolder) child;
+ setupValidator(input,validator);
+ }
+ setupValidators(child, validator);
+ }
+ }
+
+ /**
+ * @param input
+ */
+ private void setupValidator(EditableValueHolder input,Validator validator) {
+ Validator[] validators = input.getValidators();
+ for (int i = 0; i < validators.length; i++) {
+ if(validators[i] instanceof FacesBeanValidator){
+ return;
+ }
+ }
+ input.addValidator(validator);
+ }
+
+ @Override
+ public boolean getRendersChildren() {
+ return true;
+ }
+
+}
Property changes on:
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/component/UIGraphValidator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java
===================================================================
---
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java 2008-07-11
17:00:31 UTC (rev 9550)
+++
trunk/sandbox/ui/beanValidator/src/main/java/org/richfaces/validator/BeanValidator.java 2008-07-12
00:42:05 UTC (rev 9551)
@@ -8,6 +8,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
+import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.el.ELContext;
@@ -109,13 +110,8 @@
elContext.getELResolver());
ELContextWrapper wrappedElContext = new ELContextWrapper(elContext,
validationResolver);
- // TODO - check null viewRoot and null Locale.
- if (null == context.getViewRoot()) {
- throw new FacesException(VIEW_ROOT_IS_NOT_INITIALIZED);
- } else if (null == context.getViewRoot().getLocale()) {
- throw new FacesException(LOCALE_IS_NOT_SET);
- }
- wrappedElContext.setLocale(context.getViewRoot().getLocale());
+ Locale locale = calculateLocale(context);
+ wrappedElContext.setLocale(locale);
// TODO - handle ELExceptions ?
try {
target.setValue(wrappedElContext, value);
@@ -129,6 +125,16 @@
}
}
+ protected Locale calculateLocale(FacesContext context) {
+ if (null == context.getViewRoot()) {
+ throw new FacesException(VIEW_ROOT_IS_NOT_INITIALIZED);
+ } else if (null == context.getViewRoot().getLocale()) {
+ throw new FacesException(LOCALE_IS_NOT_SET);
+ }
+ Locale locale = context.getViewRoot().getLocale();
+ return locale;
+ }
+
// Method for checking input parameters for prevent NPE
private void checkInputParameters(FacesContext context,
ValueExpression target, Object value) {
@@ -140,10 +146,10 @@
/**
* Validate bean property for a new value. TODO - localization ?
*
- * @param base -
- * bean
- * @param property -
- * bean property name.
+ * @param base
+ * - bean
+ * @param property
+ * - bean property name.
* @param value
* new value.
* @return null for a valid value, array of the validation messages
@@ -165,6 +171,24 @@
}
}
+ @SuppressWarnings("unchecked")
+ public String[] validateGraph(FacesContext context, Object value, Set<String>
profiles) {
+ String validationMessages[] = null;
+ ClassValidator<Object> validator = (ClassValidator<Object>)
getValidator(value
+ .getClass(), calculateLocale(context));
+ if (validator.hasValidationRules()) {
+ InvalidValue[] invalidValues = validator.getInvalidValues(value);
+ if(null != invalidValues && invalidValues.length >0){
+ validationMessages = new String[invalidValues.length];
+ for (int i = 0; i < invalidValues.length; i++) {
+ InvalidValue invalidValue = invalidValues[i];
+ validationMessages[i] = invalidValue.getMessage();
+ }
+ }
+ }
+ return validationMessages;
+ }
+
/**
* Validate bean property of the base object aganist new value
*
@@ -222,12 +246,13 @@
* locale - user locale @return ResourceBundle instance
*/
private ResourceBundle getCurrentResourceBundle(Locale locale) {
- if(null == FacesContext.getCurrentInstance() || null ==
FacesContext.getCurrentInstance().getApplication()){
+ if (null == FacesContext.getCurrentInstance()
+ || null == FacesContext.getCurrentInstance().getApplication()) {
throw new FacesException(FACES_CONTEXT_IS_NULL);
}
String appBundle = FacesContext.getCurrentInstance().getApplication()
.getMessageBundle();
- if(null == appBundle || null == locale){
+ if (null == appBundle || null == locale) {
return null;
}
ResourceBundle bundle = ResourceBundle.getBundle(appBundle, locale);
@@ -236,8 +261,9 @@
/*
* Method for create new instance of ClassValidator, if same not in cache.
- * @param beanClass - Class to validate @param locale - user Locale,
- * used during validation process @return ClassValidator instance
+ *
+ * @param beanClass - Class to validate @param locale - user Locale, used
+ * during validation process @return ClassValidator instance
*/
@SuppressWarnings("unchecked")
private ClassValidator<? extends Object> createValidator(
@@ -378,10 +404,11 @@
/**
* Constructor for ValidatorKey object
*
- * @param validatableClass -
- * class to validate
- * @param locale -
- * User locale to determine Resource bundle, used during validation process
+ * @param validatableClass
+ * - class to validate
+ * @param locale
+ * - User locale to determine Resource bundle, used during
+ * validation process
*/
public ValidatorKey(Class<? extends Object> validatableClass,
Locale locale) {
@@ -389,7 +416,9 @@
this.locale = locale;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see java.lang.Object#hashCode()
*/
@Override
@@ -405,7 +434,9 @@
return result;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
@@ -430,6 +461,5 @@
return true;
}
-
}
}