UniqueRecordValidator.java :  » Portal » stringbeans-3.5 » com » nabhinc » portlet » mvcportlet » validator » Java Open Source

Java Open Source » Portal » stringbeans 3.5 
stringbeans 3.5 » com » nabhinc » portlet » mvcportlet » validator » UniqueRecordValidator.java
/* 
 * (C) Copyright 2004 Nabh Information Systems, Inc. 
 * 
 * All copyright notices regarding Nabh's products MUST remain
 * intact in the scripts and in the outputted HTML.
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */
package com.nabhinc.portlet.mvcportlet.validator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;

import javax.portlet.PortletException;
import javax.portlet.PortletRequest;

import org.w3c.dom.Element;

import com.nabhinc.portlet.mvcportlet.core.ControllerPortletConfig;
import com.nabhinc.portlet.mvcportlet.core.RequestConfig;
import com.nabhinc.util.StringUtil;
import com.nabhinc.util.XMLUtil;
import com.nabhinc.util.db.DBConfigUtil;
import com.nabhinc.util.db.DBParamUtil;
import com.nabhinc.util.db.DBUtil;

/**
 * 
 * 
 * @author Padmanabh Dabke
 * (c) 2004 Nabh Information Systems, Inc. All Rights Reserved.
 */
public class UniqueRecordValidator extends BaseValidator {
  //private DataSource urvDataSource = null;
  private String[] urvCreateParams = null;
  private int[] urvCreateParamTypes = null;
  private String urvCreateSQL = null;
  private String[] urvEditParams = null;
  private int[] urvEditParamTypes = null;
  private String urvEditSQL = null;
  private String urvIDParam = null;
  
  public UniqueRecordValidator() {
    setName(ValidatorConstants.UNIQUE_RECORD);
  }
  
    public void init(Element config, ControllerPortletConfig cpConfig) throws PortletException {
        super.init(config, cpConfig);
    
    // Get ID param
    urvIDParam = XMLUtil.getSubElementText(config, "id-param");
    if (urvIDParam == null) {
      throw new PortletException("Missing required parameter: id-param");
    }
    // Process create parameters
    urvCreateSQL = XMLUtil.getSubElementText(config, "create-sql");
    if (urvCreateSQL == null) {
      throw new PortletException("Missing required parameter: create-sql");
    }
  
    String params = XMLUtil.getSubElementText(config, "create-params");
    if (params != null) {
      urvCreateParams = StringUtil.split(params, ",");
      String paramTypes = XMLUtil.getSubElementText(config, "create-param-types");
      if (paramTypes == null) {
        throw new PortletException("You must specify param-types if you specify params.");
      } 
      String[] typeArray = StringUtil.split(paramTypes, ",");
      if (typeArray.length != urvCreateParams.length) {
        throw new PortletException("Number of param-types must be equal to number of params.");
      } 
      
      urvCreateParamTypes = new int[typeArray.length];
      for (int i=0; i<typeArray.length; i++) {
              try {
                urvCreateParamTypes[i] = DBConfigUtil.getSQLType(typeArray[i]);
              } catch (Exception ex) {
                throw new PortletException("Failed to parse parameter type.", ex);
              }
      }
    }
    
    // Process edit parameters
    urvEditSQL = XMLUtil.getSubElementText(config, "edit-sql");
    if (urvEditSQL == null) {
      throw new PortletException("Missing required parameter: edit-sql");
    }
  
    params = XMLUtil.getSubElementText(config, "edit-params");
    if (params != null) {
      urvEditParams = StringUtil.split(params, ",");
      String paramTypes = XMLUtil.getSubElementText(config, "edit-param-types");
      if (paramTypes == null) {
        throw new PortletException("You must specify param types if you specify params.");
      } 
      
      String[] typeArray = StringUtil.split(paramTypes, ",");
      if (typeArray.length != urvEditParams.length) {
        throw new PortletException("Number of param types must be equal to number of params.");
      } 
      
      urvEditParamTypes = new int[typeArray.length];
      for (int i=0; i<typeArray.length; i++) {
              try {
                urvEditParamTypes[i] = DBConfigUtil.getSQLType(typeArray[i]);
              } catch (Exception ex) {
                throw new PortletException("Failed to parse parameter type.", ex);
              }
      }
    }

  }

  /* (non-Javadoc)
   * @see com.nabhinc.portlet.mvcportlet.core.FormFieldValidator#validate(java.lang.String, com.nabhinc.portlet.mvcportlet.core.Form)
   */
  public boolean validate(String fieldValue, PortletRequest request,
  RequestConfig config) throws PortletException {
    if (fieldValue == null || fieldValue.trim().equals("")) {
      return true;
    }
    
    Connection conn = null;
    PreparedStatement st = null;
    ResultSet results = null;
    String idParamVal = request.getParameter(urvIDParam);
    String sql = urvCreateSQL;
    String[] params = urvCreateParams;
    int[] paramTypes = urvCreateParamTypes;
    
    if (idParamVal != null) {
      sql = urvEditSQL;
      params = urvEditParams;
      paramTypes = urvEditParamTypes;
    }
    try {
      conn = bvConfig.getDataSource().getConnection();
      st = conn.prepareStatement(sql);
      if (params != null) {
        for (int i=0; i<params.length; i++) {
          /*
          String defaul = null;
          if (form != null && (! params[i].startsWith("$"))) {
              if (form.getField(params[i]) != null)
                  defaul = form.getField(params[i]).getDefaultValue();
          }
          */
          DBParamUtil.setSQLParam(st,i,params[i],paramTypes[i],request,
            -1);
        }
      }
    
      results = st.executeQuery();
      return ! results.next();
      
    } catch (SQLException sqe) {
      throw new PortletException("Database exception.", sqe);
    } catch (ParseException pex) {
      throw new PortletException("Malformed request parameter.", pex);
    } finally {
      DBUtil.close(results);
      DBUtil.close(st);
      DBUtil.close(conn);
    }

  }

}
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.