/**
  * Configuration method invoked by framework while initializing Class. Following are the
  * parameters used :
  *
  * <p>
  *
  * <blockquote>
  *
  * <pre>
  * resource-file - oneinc-resource.properties
  * validation-file - validation-rules.xml
  * </pre>
  *
  * </blockquote>
  *
  * <p>This method initialize the ValidatorResources by reading the validation-file passed and then
  * initialize different forms defined in this file. It also initialize the validation resource
  * bundle i.e. oneinc-resource.properties from where user friendly message would be picked in case
  * of validation failure.
  *
  * <p>Member-variable needs to be initialized/assigned once while initializing the class, and is
  * being passed from the XML is initialized in this method.
  *
  * <p>In XML, use the following tag to define the property:
  *
  * <p>&lt;property name="resource-file" value="oneinc-resource" /&gt;
  *
  * <p>corresponding code to get the property is as follow:
  *
  * <p>String resourceName = cfg.get("resource-file");
  *
  * @param cfg - Configuration object holds the property defined in the XML.
  * @throws ConfigurationException
  */
 public void setConfiguration(Configuration cfg) throws ConfigurationException {
   try {
     String resourceName = cfg.get("resource-file");
     apps = ResourceBundle.getBundle(resourceName);
     validationFileName = cfg.get("validation-file");
     InputStream in = null;
     in = BaseValidation.class.getClassLoader().getResourceAsStream(validationFileName);
     try {
       resources = new ValidatorResources(in);
       form = resources.getForm(Locale.getDefault(), "ValidateBean");
       provForm = resources.getForm(Locale.getDefault(), "ValidateProvBean");
     } finally {
       if (in != null) {
         in.close();
       }
     }
     prepareTxnMap();
   } catch (FileNotFoundException e) {
     throw new ConfigurationException(e.getLocalizedMessage(), e);
   } catch (IOException e) {
     throw new ConfigurationException(e.getLocalizedMessage(), e);
   } catch (Exception e) {
     throw new ConfigurationException(e.getLocalizedMessage(), e);
   }
 }
 /**
  * Iterate over the validation result to check for any validation failure. For the very first
  * validation failure, it reads the oneinc-resource.properties encapsulated in member variable
  * apps and set the user friendly message along with the IRC and throws the
  * OneIncValidationException.
  *
  * @param bean - Bean to be validated
  * @param results - contains the validation result
  * @param resources2 - ValidatorResources object prepared for validation-rules.xml
  * @param form - contains the mapping for field and their validation rule.
  * @throws CWValidationException - if validation gets fail.
  */
 @SuppressWarnings({"unchecked", "deprecation"})
 private void checkResults(
     Serializable bean, ValidatorResults results, ValidatorResources resources2, Form form)
     throws CWValidationException {
   Iterator propertyNames = results.getPropertyNames().iterator();
   while (propertyNames.hasNext()) {
     String propertyName = (String) propertyNames.next();
     ValidatorResult result = results.getValidatorResult(propertyName);
     Map actionMap = result.getActionMap();
     Iterator keys = actionMap.keySet().iterator();
     Field field = form.getField(propertyName);
     String prettyFieldName = apps.getString(field.getArg(0).getKey());
     while (keys.hasNext()) {
       String actName = (String) keys.next();
       ValidatorAction action = resources.getValidatorAction(actName);
       if (!result.isValid(actName)) {
         String message = apps.getString(action.getMsg());
         Object[] args = {prettyFieldName};
         String formatMsg = MessageFormat.format(message, args);
         int colonIndex = formatMsg.indexOf(',');
         String imfResponseCode = null;
         String msg = null;
         if (colonIndex > 0) {
           imfResponseCode = formatMsg.substring(0, colonIndex);
           msg = formatMsg.substring(colonIndex);
         }
         throw new CWValidationException(msg, imfResponseCode);
       }
     }
   }
 }
Example #3
0
  public int doStartTag() throws JspException {

    try {
      this.requestContext = new RequestContext((HttpServletRequest) this.pageContext.getRequest());
    } catch (RuntimeException ex) {
      throw ex;
    } catch (Exception ex) {
      pageContext.getServletContext().log("Exception in custom tag", ex);
    }

    // Look up this key to see if its a field of the current form
    boolean requiredField = false;
    boolean validationError = false;

    ValidatorResources resources = getValidatorResources();

    Locale locale = pageContext.getRequest().getLocale();

    if (locale == null) {
      locale = Locale.getDefault();
    }

    // get the name of the bean from the key
    String formName = key.substring(0, key.indexOf('.'));
    String fieldName = key.substring(formName.length() + 1);

    if (resources != null) {
      Form form = resources.getForm(locale, formName);

      if (form != null) {
        Field field = form.getField(fieldName);

        if (field != null) {
          if (field.isDependency("required") || field.isDependency("validwhen")) {
            requiredField = true;
          }
        }
      }
    }

    Errors errors = requestContext.getErrors(formName, false);
    List fes = null;
    if (errors != null) {
      fes = errors.getFieldErrors(fieldName);
      // String errorMsg = getErrorMessages(fes);
    }

    if (fes != null && fes.size() > 0) {
      validationError = true;
    }

    // Retrieve the message string we are looking for
    String message = null;
    try {
      message = getMessageSource().getMessage(key, null, locale);
    } catch (NoSuchMessageException nsm) {
      message = "???" + key + "???";
    }

    String cssClass = null;
    if (styleClass != null) {
      cssClass = styleClass;
    } else if (requiredField) {
      cssClass = "required";
    }

    String cssErrorClass = (errorClass != null) ? errorClass : "error";
    StringBuilder label = new StringBuilder();

    if ((message == null) || "".equals(message.trim())) {
      label.append("");
    } else {
      label.append("<label for=\"").append(fieldName).append("\"");

      if (cssClass != null) {
        label.append(" class=\"").append(cssClass);
        if (validationError) {
          label.append(" ").append(cssErrorClass);
        }
      }

      label.append("\">").append(message);
      label.append((requiredField) ? " <span class=\"required\">*</span>" : "");
      label.append((colon) ? ":" : "");
      label.append("</label>");
    }

    // Print the retrieved message to our output writer
    try {
      writeMessage(label.toString());
    } catch (IOException io) {
      io.printStackTrace();
      throw new JspException("Error writing label: " + io.getMessage());
    }

    // Continue processing this page
    return (SKIP_BODY);
  }