package jp.yoyoyoyo.webform.dto;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import jp.yoyoyoyo.webform.form.EntryForm;

public class RequestDAO {
	private DataSource source;
	
	public RequestDAO(){
		source = DaoUtil.getSource();
	}
	
	public int executeSP(EntryForm entryForm) throws Exception{
		CallableStatement objSql = null;
		Connection db = null;
		int ret = 0;
		
		try {
			db = source.getConnection();
			
			objSql = db.prepareCall("{call INSERT_REQUEST(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			
			objSql.registerOutParameter(1, java.sql.Types.INTEGER);
			objSql.setString(2, entryForm.getSeiKanji());
		    objSql.setString(3, entryForm.getMeiKanji());
		    objSql.setString(4, entryForm.getSeiKana());
		    objSql.setString(5, entryForm.getMeiKana());
		    objSql.setString(6, entryForm.getBirthDate());	
		    objSql.setInt(7, entryForm.getGender());
		    objSql.setString(8, entryForm.getGenderText());
		    objSql.setString(9, entryForm.getZip1());
		    objSql.setString(10, entryForm.getZip2());
		    objSql.setString(11, entryForm.getAddress1());
		    objSql.setString(12, entryForm.getAddress2());
		    objSql.setString(13, entryForm.getAddress3());
		    objSql.setString(14, entryForm.getAddress4());
		    objSql.setString(15, entryForm.getTel1());
		    objSql.setString(16, entryForm.getTel2());
		    objSql.setString(17, entryForm.getTel3());
		    objSql.setString(18, entryForm.getmTel1());
		    objSql.setString(19, entryForm.getmTel2());
		    objSql.setString(20, entryForm.getmTel3());
		    objSql.setString(21, entryForm.getEmail1());
		    objSql.setInt(22, entryForm.getOccupation());
		    objSql.setString(23, entryForm.getOccupationText());
		    objSql.setString(24, entryForm.getIpAddress());
		    objSql.setString(25, entryForm.getSessionId());
		    
		    objSql.executeUpdate();
		    
		    ret = objSql.getInt(1);
		    
		} catch (SQLException sqlex){
			sqlex.printStackTrace();
			throw new Exception();
		} finally {
			if(db != null){
				db.close();			
			}
		}

		return ret;
	}
	
	
}
