private FacesMessage _getMinimumMessage(
      FacesContext context, UIComponent component, Object value, Object min) {
    Object msg = _getRawMinimumMessageDetail();
    Object label = ValidatorUtils.getComponentLabel(component);

    Object[] params = {label, value, min};

    return MessageFactory.getMessage(context, MINIMUM_MESSAGE_ID, msg, params, component);
  }
  private FacesMessage _getNotInRangeMessage(
      FacesContext context, UIComponent component, Object value, Object min, Object max) {
    Object msg = _getRawNotInRangeMessageDetail();
    Object label = ValidatorUtils.getComponentLabel(component);

    Object[] params = {label, value, min, max};

    return MessageFactory.getMessage(context, NOT_IN_RANGE_MESSAGE_ID, msg, params, component);
  }
Exemplo n.º 3
0
 /** @see Validator#validate(Object, org.springframework.validation.Errors) */
 @Override
 public void validate(final Object target, final Errors errors) {
   if (target instanceof Activity) {
     if (target != null) {
       ValidatorUtils.validateStringNotEmpty(
           target, errors, "siteId", "siteName", "target", "type");
     }
   } else {
     log.debug("Given object is not a Activity instance not validating");
   }
 }
 /**
  * Return the {@link ValueBinding} used to calculate the value for the specified attribute name,
  * if any.
  *
  * @param name Name of the attribute or property for which to retrieve a {@link ValueBinding}
  * @exception NullPointerException if <code>name</code> is <code>null</code>
  * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
  *     validator
  * @deprecated
  */
 public ValueBinding getValueBinding(String name) {
   return ValidatorUtils.getValueBinding(_facesBean, name);
 }
/** Implementation for <code>java.lang.Double</code> values. */
@JSFValidator(configExcluded = true)
public class DoubleRangeValidator extends javax.faces.validator.DoubleRangeValidator {

  public static final String VALIDATOR_ID = "org.apache.myfaces.trinidad.DoubleRange";

  /**
   * The message identifier of the {@link javax.faces.application.FacesMessage} to be created if the
   * maximum value check fails. The message format string for this message may optionally include
   * <code>{0}</code>, <code>{1}</code> and <code>{3}</code> placeholders, which will be replaced by
   * user input, component label and configured maximum value.
   */
  public static final String MAXIMUM_MESSAGE_ID =
      "org.apache.myfaces.trinidad.validator.DoubleRangeValidator.MAXIMUM";

  /**
   * The message identifier of the {@link javax.faces.application.FacesMessage} to be created if the
   * minimum value check fails. The message format string for this message may optionally include
   * <code>{0}</code>, <code>{1}</code> and <code>{2}</code> placeholders, which will be replaced by
   * user input, component label and configured minimum value.
   */
  public static final String MINIMUM_MESSAGE_ID =
      "org.apache.myfaces.trinidad.validator.DoubleRangeValidator.MINIMUM";

  /**
   * The message identifier of the {@link javax.faces.application.FacesMessage} to be created if the
   * maximum or minimum value check fails, and both the maximum and minimum values for this
   * validator have been set. The message format string for this message may optionally include
   * <code>{0}</code>, <code>{1}</code>, <code>{2}</code> and <code>{3}</code> placeholders, which
   * will be replaced by user input, component label, configured minimum value and configured
   * maximum value.
   */
  public static final String NOT_IN_RANGE_MESSAGE_ID =
      "org.apache.myfaces.trinidad.validator.DoubleRangeValidator.NOT_IN_RANGE";

  /** The message identifier of the FacesMessage to be created if the value cannot be converted */
  public static final String CONVERT_MESSAGE_ID =
      "org.apache.myfaces.trinidad.convert.DoubleConverter.CONVERT";

  /** Construct a {@link Validator} with no preconfigured limits. */
  public DoubleRangeValidator() {
    super();
  }

  /**
   * Construct a {@link Validator} with the specified preconfigured limit.
   *
   * @param maximum Maximum value to allow
   */
  public DoubleRangeValidator(double maximum) {
    super(maximum);
  }

  /**
   * Construct a {@link Validator} with the specified preconfigured limits.
   *
   * @param maximum Maximum value to allow
   * @param minimum Minimum value to allow
   */
  public DoubleRangeValidator(double maximum, double minimum) {
    super(maximum, minimum);
  }

  /**
   * Return the maximum value to be enforced by this {@link Validator} or null if it has not been
   * set.
   */
  @JSFProperty
  @Override
  public double getMaximum() {
    Object maxDouble = _facesBean.getProperty(_MAXIMUM_KEY);
    if (maxDouble == null) maxDouble = _MAXIMUM_KEY.getDefault();
    return ComponentUtils.resolveDouble(maxDouble);
  }

  /**
   * Set the maximum value to be enforced by this {@link Validator}.
   *
   * @param maximum The new maximum value
   */
  @Override
  public void setMaximum(double maximum) {
    _facesBean.setProperty(_MAXIMUM_KEY, Double.valueOf(maximum));
  }

  /**
   * Return the minimum value to be enforced by this {@link Validator}, or null if it has not been
   * set.
   */
  @JSFProperty
  @Override
  public double getMinimum() {
    Object minDouble = _facesBean.getProperty(_MINIMUM_KEY);
    if (minDouble == null) minDouble = _MINIMUM_KEY.getDefault();
    return ComponentUtils.resolveDouble(minDouble);
  }

  /**
   * Set the minimum value to be enforced by this {@link Validator}.
   *
   * @param minimum The new minimum value
   */
  @Override
  public void setMinimum(double minimum) {
    _facesBean.setProperty(_MINIMUM_KEY, Double.valueOf(minimum));
  }

  /**
   * Custom error message to be used, for creating detail part of the {@link FacesMessage}, when
   * input value exceeds the maximum value set. Overrides detail message identified by message id
   * {@link #MAXIMUM_MESSAGE_ID}
   *
   * @param maximumMessageDetail Custom error message.
   */
  public void setMessageDetailMaximum(String maximumMessageDetail) {
    _facesBean.setProperty(_MAXIMUM_MESSAGE_DETAIL_KEY, maximumMessageDetail);
  }

  /**
   * Return custom detail error message that was set for creating {@link FacesMessage}, for cases
   * where input value exceeds the <code>maximum</code> value set.
   *
   * @return Custom error message.
   * @see #setMessageDetailMaximum(String)
   */
  @JSFProperty
  public String getMessageDetailMaximum() {
    Object maxMsgDet = _facesBean.getProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
    return ComponentUtils.resolveString(maxMsgDet);
  }

  /**
   * Custom error message to be used, for creating detail part of the {@link FacesMessage}, when
   * input value is less the set <code>minimum</code> value. Overrides detail message identified by
   * message id {@link #MINIMUM_MESSAGE_ID}
   *
   * @param minimumMessageDetail Custom error message.
   */
  public void setMessageDetailMinimum(String minimumMessageDetail) {
    _facesBean.setProperty(_MINIMUM_MESSAGE_DETAIL_KEY, minimumMessageDetail);
  }

  /**
   * Return custom detail error message that was set for creating {@link FacesMessage}, for cases
   * where, input value is less than the <code>minimum</code> value set.
   *
   * @return Custom error message.
   * @see #setMessageDetailMinimum(String)
   */
  @JSFProperty
  public String getMessageDetailMinimum() {
    Object minMsgDet = _facesBean.getProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
    return ComponentUtils.resolveString(minMsgDet);
  }

  /**
   * Custom error message to be used, for creating detail part of the {@link FacesMessage}, when
   * input value is not with in the range, when <code>minimum</code> and <code>maximum</code> is
   * set. Overrides detail message identified by message id {@link #NOT_IN_RANGE_MESSAGE_ID}
   *
   * @param notInRangeMessageDetail Custom error message.
   */
  public void setMessageDetailNotInRange(String notInRangeMessageDetail) {
    _facesBean.setProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY, notInRangeMessageDetail);
  }

  /**
   * Return custom detail error message that was set for creating {@link FacesMessage}, for cases
   * where, input value exceeds the <code>maximum</code> value and is less than the <code>minimum
   * </code> value set.
   *
   * @return Custom error message.
   * @see #setMessageDetailNotInRange(String)
   */
  @JSFProperty
  public String getMessageDetailNotInRange() {
    Object notInRngMsg = _facesBean.getProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
    return ComponentUtils.resolveString(notInRngMsg);
  }

  /**
   * Custom hint maximum message. Overrides default hint message
   *
   * @param hintMaximum Custom hint message.
   */
  public void setHintMaximum(String hintMaximum) {
    _facesBean.setProperty(_HINT_MAXIMUM_KEY, hintMaximum);
  }

  /**
   * Return custom hint maximum message.
   *
   * @return Custom hint message.
   * @see #setHintMaximum(String)
   */
  @JSFProperty(tagExcluded = true)
  public String getHintMaximum() {
    Object obj = _facesBean.getProperty(_HINT_MAXIMUM_KEY);
    return ComponentUtils.resolveString(obj);
  }

  /**
   * Custom hint minimum message. Overrides default hint message
   *
   * @param hintMinimum Custom hint message.
   */
  @JSFProperty(tagExcluded = true)
  public void setHintMinimum(String hintMinimum) {
    _facesBean.setProperty(_HINT_MINIMUM_KEY, hintMinimum);
  }

  /**
   * Return custom hint minimum message.
   *
   * @return Custom hint message.
   * @see #setHintMinimum(String)
   */
  public String getHintMinimum() {
    Object obj = _facesBean.getProperty(_HINT_MINIMUM_KEY);
    return ComponentUtils.resolveString(obj);
  }

  /**
   * Custom hint notInRange message. Overrides default hint message
   *
   * @param hintNotInRange Custom hint message.
   */
  public void setHintNotInRange(String hintNotInRange) {
    _facesBean.setProperty(_HINT_NOT_IN_RANGE, hintNotInRange);
  }

  /**
   * Return custom hint notInRange message.
   *
   * @return Custom hint message.
   * @see #setHintNotInRange
   */
  @JSFProperty(tagExcluded = true)
  public String getHintNotInRange() {
    Object obj = _facesBean.getProperty(_HINT_NOT_IN_RANGE);
    return ComponentUtils.resolveString(obj);
  }

  @Override
  public void validate(FacesContext context, UIComponent component, Object value)
      throws ValidatorException {
    if ((context == null) || (component == null)) {
      throw new NullPointerException();
    }

    if (value == null) {
      return;
    }

    try {
      double doubleValue = _convertValueToDouble(value);
      double min = getMinimum();
      double max = getMaximum();

      if (isMaximumSet() && isMinimumSet()) {
        if (doubleValue < min || doubleValue > max) {
          throw new ValidatorException(_getNotInRangeMessage(context, component, value, min, max));
        }
      }

      if (isMaximumSet()) {
        if (doubleValue > max) {
          throw new ValidatorException(_getMaximumMessage(context, component, value, max));
        }
      }

      if (isMinimumSet()) {
        if (doubleValue < min) {
          throw new ValidatorException(_getMinimumMessage(context, component, value, min));
        }
      }
    } catch (NumberFormatException nfe) {
      FacesMessage msg = _getNotCorrectType(context);
      throw new ValidatorException(msg);
    }
  }

  //  StateHolder Methods
  @Override
  public Object saveState(FacesContext context) {
    return _facesBean.saveState(context);
  }

  @Override
  public void restoreState(FacesContext context, Object state) {
    _facesBean.restoreState(context, state);
  }

  /**
   * Set the {@link ValueExpression} used to calculate the value for the specified attribute if any.
   *
   * @param name Name of the attribute for which to set a {@link ValueExpression}
   * @param expression The {@link ValueExpression} to set, or <code>null</code> to remove any
   *     currently set {@link ValueExpression}
   * @exception NullPointerException if <code>name</code> is <code>null</code>
   * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
   *     converter
   */
  public void setValueExpression(String name, ValueExpression expression) {
    ValidatorUtils.setValueExpression(_facesBean, name, expression);
  }

  /**
   * Return the {@link ValueExpression} used to calculate the value for the specified attribute
   * name, if any.
   *
   * @param name Name of the attribute or property for which to retrieve a {@link ValueExpression}
   * @exception NullPointerException if <code>name</code> is <code>null</code>
   * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
   *     converter
   */
  public ValueExpression getValueExpression(String name) {
    return ValidatorUtils.getValueExpression(_facesBean, name);
  }

  /**
   * Set the {@link ValueBinding} used to calculate the value for the specified attribute if any.
   *
   * @param name Name of the attribute for which to set a {@link ValueBinding}
   * @param binding The {@link ValueBinding} to set, or <code>null</code> to remove any currently
   *     set {@link ValueBinding}
   * @exception NullPointerException if <code>name</code> is <code>null</code>
   * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
   *     validator
   * @deprecated
   */
  public void setValueBinding(String name, ValueBinding binding) {
    ValidatorUtils.setValueBinding(_facesBean, name, binding);
  }

  /**
   * Return the {@link ValueBinding} used to calculate the value for the specified attribute name,
   * if any.
   *
   * @param name Name of the attribute or property for which to retrieve a {@link ValueBinding}
   * @exception NullPointerException if <code>name</code> is <code>null</code>
   * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
   *     validator
   * @deprecated
   */
  public ValueBinding getValueBinding(String name) {
    return ValidatorUtils.getValueBinding(_facesBean, name);
  }

  @JSFProperty(istransient = true, tagExcluded = true)
  @Override
  public boolean isTransient() {
    return (_transientValue);
  }

  @Override
  public void setTransient(boolean transientValue) {
    _transientValue = transientValue;
  }

  protected boolean isMaximumSet() {
    return _facesBean.getProperty(_MAXIMUM_KEY) != null;
  }

  protected boolean isMinimumSet() {
    return _facesBean.getProperty(_MINIMUM_KEY) != null;
  }

  private FacesMessage _getNotCorrectType(FacesContext context) {
    return MessageFactory.getMessage(context, CONVERT_MESSAGE_ID);
  }

  /**
   * Try to call doubleValue() from java.lang.Number. Since not all "number" implement
   * java.lang.Number, we try to call string and parse the String representation of the "number". If
   * that fails we aren't working with a number so we throw a NumberFormatException
   *
   * @param value the number value
   * @return parsed number value
   * @throws NumberFormatException if the value is not a number
   */
  private double _convertValueToDouble(Object value) throws NumberFormatException {
    if (value instanceof Number) {
      return ((Number) value).doubleValue();
    } else {
      return Double.parseDouble(value.toString());
    }
  }

  private FacesMessage _getNotInRangeMessage(
      FacesContext context, UIComponent component, Object value, Object min, Object max) {
    Object msg = _getRawNotInRangeMessageDetail();
    Object label = ValidatorUtils.getComponentLabel(component);

    Object[] params = {label, value, min, max};

    return MessageFactory.getMessage(context, NOT_IN_RANGE_MESSAGE_ID, msg, params, component);
  }

  private Object _getRawNotInRangeMessageDetail() {
    return _facesBean.getRawProperty(_NOT_IN_RANGE_MESSAGE_DETAIL_KEY);
  }

  private FacesMessage _getMaximumMessage(
      FacesContext context, UIComponent component, Object value, Object max) {

    Object msg = _getRawMaximumMessageDetail();
    Object label = ValidatorUtils.getComponentLabel(component);

    Object[] params = {label, value, max};

    return MessageFactory.getMessage(context, MAXIMUM_MESSAGE_ID, msg, params, component);
  }

  private Object _getRawMaximumMessageDetail() {
    return _facesBean.getRawProperty(_MAXIMUM_MESSAGE_DETAIL_KEY);
  }

  private FacesMessage _getMinimumMessage(
      FacesContext context, UIComponent component, Object value, Object min) {
    Object msg = _getRawMinimumMessageDetail();
    Object label = ValidatorUtils.getComponentLabel(component);

    Object[] params = {label, value, min};

    return MessageFactory.getMessage(context, MINIMUM_MESSAGE_ID, msg, params, component);
  }

  private Object _getRawMinimumMessageDetail() {
    return _facesBean.getRawProperty(_MINIMUM_MESSAGE_DETAIL_KEY);
  }

  private static final FacesBean.Type _TYPE = new FacesBean.Type();

  private static final PropertyKey _MINIMUM_KEY =
      _TYPE.registerKey(
          "minimum",
          Double.class,
          // Don't rely on autoboxing: there's a method overload
          Double.valueOf(Double.MIN_VALUE));

  private static final PropertyKey _MAXIMUM_KEY =
      _TYPE.registerKey(
          "maximum",
          Double.class,
          // Don't rely on autoboxing: there's a method overload
          Double.valueOf(Double.MAX_VALUE));

  private static final PropertyKey _MAXIMUM_MESSAGE_DETAIL_KEY =
      _TYPE.registerKey("messageDetailMaximum", String.class);

  private static final PropertyKey _MINIMUM_MESSAGE_DETAIL_KEY =
      _TYPE.registerKey("messageDetailMinimum", String.class);

  private static final PropertyKey _NOT_IN_RANGE_MESSAGE_DETAIL_KEY =
      _TYPE.registerKey("messageDetailNotInRange", String.class);

  private static final PropertyKey _HINT_MAXIMUM_KEY =
      _TYPE.registerKey("hintMaximum", String.class);

  private static final PropertyKey _HINT_MINIMUM_KEY =
      _TYPE.registerKey("hintMinimum", String.class);

  private static final PropertyKey _HINT_NOT_IN_RANGE =
      _TYPE.registerKey("hintNotInRange", String.class);

  private FacesBean _facesBean = ValidatorUtils.getFacesBean(_TYPE);

  private boolean _transientValue = false;
}
 /**
  * Set the {@link ValueBinding} used to calculate the value for the specified attribute if any.
  *
  * @param name Name of the attribute for which to set a {@link ValueBinding}
  * @param binding The {@link ValueBinding} to set, or <code>null</code> to remove any currently
  *     set {@link ValueBinding}
  * @exception NullPointerException if <code>name</code> is <code>null</code>
  * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
  *     validator
  * @deprecated
  */
 public void setValueBinding(String name, ValueBinding binding) {
   ValidatorUtils.setValueBinding(_facesBean, name, binding);
 }
 /**
  * Return the {@link ValueExpression} used to calculate the value for the specified attribute
  * name, if any.
  *
  * @param name Name of the attribute or property for which to retrieve a {@link ValueExpression}
  * @exception NullPointerException if <code>name</code> is <code>null</code>
  * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
  *     converter
  */
 public ValueExpression getValueExpression(String name) {
   return ValidatorUtils.getValueExpression(_facesBean, name);
 }
 /**
  * Set the {@link ValueExpression} used to calculate the value for the specified attribute if any.
  *
  * @param name Name of the attribute for which to set a {@link ValueExpression}
  * @param expression The {@link ValueExpression} to set, or <code>null</code> to remove any
  *     currently set {@link ValueExpression}
  * @exception NullPointerException if <code>name</code> is <code>null</code>
  * @exception IllegalArgumentException if <code>name</code> is not a valid attribute of this
  *     converter
  */
 public void setValueExpression(String name, ValueExpression expression) {
   ValidatorUtils.setValueExpression(_facesBean, name, expression);
 }