CommonBindValidator.java :  » Media » multimedia-portal » common » bind » Java Open Source

Java Open Source » Media » multimedia portal 
multimedia portal » common » bind » CommonBindValidator.java
/*
 *  Copyright 2010 demchuck.dima@gmail.com
 *
 *  Licensed 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 common.bind;

import common.beans.propertyEditors.StringTypesPropertyEditor;
import common.types.TypesCheckAndCorrect;
import java.beans.PropertyEditor;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.validation.Errors;
import org.springframework.web.bind.ServletRequestDataBinder;

/**
 * it enables checking of required fields and types checking and antispam code
 * if you want to add some extra functionality do not forget to call method of a superclass<br>
 * @author demchuck.dima@gmail.com
 */
public class CommonBindValidator extends ABindValidator{
  protected Map<String,PropertyEditor> editors;
  protected String[] required_fields;
    protected String[] allowed_fields;

  protected String anti_spam_code;

  protected String[] required_field_arrays;
  protected String required_field_arrays_marker;

  /*
   * Unused
   *  if want to use you must unkomment some lines below
   * types may not contain only a type name
   * [typeName][,][required]<br>
   * required - means that given field must be of type [typeName] and may not be null of an empty string
   * this is made for checking array types and collections ...<br>
   */

  /**
   * an empty method
   * @param binder
   */
  @Override
  protected void initBinder(ServletRequestDataBinder binder){
    binder.setBindEmptyMultipartFiles(false);
    binder.setRequiredFields(required_fields);
        binder.setAllowedFields(allowed_fields);
        if (editors!=null){
            Iterator<Entry<String,PropertyEditor>> i = editors.entrySet().iterator();
            while(i.hasNext()){
                Entry<String,PropertyEditor> e = i.next();
                binder.registerCustomEditor(String.class, e.getKey(), e.getValue());
            }
        }
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(false));
  }

  @Override
  protected void validate(Object command, Errors err, HttpServletRequest request) {
    if (required_field_arrays_marker!=null){
      String[] marker = request.getParameterValues(required_field_arrays_marker);
      if (marker==null){
        //err.rejectValue(required_field_arrays_marker,"required");
      }else{
        if (required_field_arrays!=null){
          for (int i=0;i<required_field_arrays.length;i++){
            for (int j=0;j<marker.length;j++){
              String s = request.getParameter(required_field_arrays[i]+"["+j+"]");
              if (s==null||s.equals("")){
                err.rejectValue(required_field_arrays[i], "required");
              }
            }
          }
        }
      }
    }
    if (anti_spam_code!=null){
      String code = request.getParameter(anti_spam_code);
      if (code!=null&&!code.equals(request.getSession().getAttribute("antiSpamCode")))
      {
                err.reject("anti_spam_code.different");
        common.CommonAttributes.addErrorMessage("anti_spam_code.different",request);
      }
      request.getSession().removeAttribute("antiSpamCode");
    }
  }

  public void setRequiredFields(String[] value){this.required_fields = value;}
  public void setAllowedFields(String[] value){this.allowed_fields = value;}
  /**
   * name of parameter with antispam code
   * @param name parameter
   */
  public void setAntiSpamCodeParam(String name){this.anti_spam_code = name;}
  /**
   * is used with setRequiredFieldId
   * prefixes to check suffix is its number in array [i]
   * an array of field values that might not be null
   * field[0], field[1], ...
   * @param value parameters prefixes to check
   */
  public void setRequiredFieldArrays(String[] value){this.required_field_arrays = value;}
  /**
   * name of parameter that is actually checked for existence
   * and its length is lenght of array in setRequiredFieldArrays
   * @param value parameter
   */
  public void setRequiredFieldArraysMarker(String value){this.required_field_arrays_marker = value;}

  //public static final String TYPES_SEPARATOR = ",";
  //public static final String REQUIRED_TYPE = "required";

  public void setFieldTypes(Map<String,String> types){
    TypesCheckAndCorrect tc = TypesCheckAndCorrect.get();
    Iterator<Entry<String,String>> i = types.entrySet().iterator();
    editors = new HashMap<String,PropertyEditor>();
    while(i.hasNext()){
      Entry<String,String> e = i.next();
      //String[] val = e.getValue().split(TYPES_SEPARATOR);
      //if (val.length<2){
        editors.put(e.getKey(), new StringTypesPropertyEditor(e.getValue(), tc, false));
      //}else{
      //  editors.put(e.getKey(), new StringTypesPropertyEditor(val[0], tc, val[1].equals(REQUIRED_TYPE)));
      //}
    }
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.