package okiniiri;
import java.io.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
import javax.sql.DataSource;
import java.sql.*;
import java.util.HashMap;
import java.util.ArrayList;
public class SearchAction2 extends Action{
//------------------------------------------------------------------------------------
/**
* エンコードを変換する(文字化けを防ぐ為)
* @param strVal : 変換前文字列データ
* @return :変換文字列データ
* @exception UnsupportedEncoding : サポートされていないエンコーディング
*///--------------------------------------------------------------------------------
public String strEncode(String strVal) throws UnsupportedEncodingException{
if(strVal == null){
return (null);
}
else{
return(new String(strVal.getBytes("ISO8859_1"),"Windows-31J"));
}
}
//-----------------------------------------------------------------------------------
/**
* お気に入りテーブル情報検索処理
*
* フォーム情報(キー)を元にfavoritesテーブルを検索し、
* 検索結果はArrayListのfavoritesに格納しリターンする
*
* @param form : SearchFormオブジェクト
* @return favorites : 検索結果リスト
*///------------------------------------------------------------------------------------
protected ArrayList getFavorites(ActionForm form){
Favorite favorite = null;
ArrayList favorites = new ArrayList();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource) context.getAttribute(org.apache.struts.Globals.DATA_SOURCE_KEY);
try{
//フォームジョウホウ(サイト名とカテゴリ)を取得する。
SearchForm2 sForm= (SearchForm2)form;
String f_Sitename = strEncode(sForm.getSitename());
String f_Categoryid = strEncode(sForm.getCategoryid());
conn = dataSource.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from favorites" + "where" + "favorites.sitename= + '" +f_Sitename + "'"+" or favorites.categoryid= '"+ f_Categoryid +"' order by favorites.categoryid asc");
while(rs.next()){
favorite = new Favorite();
favorite.setSitename(rs.getString("sitename"));
favorite.setUrl(rs.getString("url"));
favorite.setCategoryid(rs.getString("categoryid"));
favorite.setNote(rs.getString("note"));
favorites.add(favorite);
}
}
catch(SQLException e){
System.out.println(e.getMessage());
}
catch(Exception e){
System.out.println(e.getMessage());
}
finally{
if(rs != null){
try{
rs.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
rs = null;
}
if(stmt != null){
try{
stmt.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
stmt = null;
}
if(conn !=null){
try{
conn.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
conn = null;
}
}
//1レコードもヒットしなかった場合はオブジェクトをnullにして返却
if(favorites.size() == 0){
favorites = null;
System.out.println("SearchActionの検索結果はNULLです");
}
return favorites;
}
//--------------------------------------------------------------------------------
/** SearchActionのメイン実行部
*
* SearchActionが呼び出されるとこのexecute()メソッドが実行される
* Http専用のカスタムアクションを定義している
* @param mapping : アクションBeanのすべての設定情報が格納されるオブジェクト
* 今回のサンプルではSearchActionの処理が終わったあとで
* 適切なビューへのターゲットを指定している
* 設定内容はstruts-config.xmlを確認
* @param form : このアクションに関連付けられるフォームBeanオブジェクト
* @param request : HttpServletRequestオブジェクト
* @param reqaponse : HttpServlteResponseオブジェクト
* @Exception ServletException : Servletエラー
* @exception IOExcetion : 入出力エラー
*///---------------------------------------------------------------------------------
public ActionForward execute(ActionMapping mapping, Action form, HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException{
//デフォルトでターゲットにsuccessを設定する
String target = "Search_ok";
FavoritesActionMapping favoritesMapping = (FavoritesActionMapping)mapping;
/*********************************************************************************
* 各種チェックロジック
******************************************************************************/
//ユーザーのログインを必要とするアクションか
if(favoritesMapping.isLoginRequired()) {
//セッション情報の取得
HttpSession session = request.getSession();
if(session.getAttribute("USER") == null){
//ユーザーはログインしていない
target = "login";
ActionErrors errors = new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("errors.login.required"));
//見つかったらエラーを元のフォームに報告する
if(!errors.isEmpty()){
saveErrors(request,errors);
}
return(mapping.findForward("target"));
}
}
//cancelボタンが押されたら検索リストに報告する
if(isCancelled(request)){
return (mapping.findForward("search_cancel"));
}
/*************************************************************************
* メイン実行部
**************************************************************************/
try {
ArrayList favorites = null;
//フォームデータをもとに検索を実行
favorites = getFavorites(form);
if(favorites == null){
//検索に失敗した場合
target = new String("search_fail");
}
else{
request.setAttribute("favorites",favorites);
}
}
catch(Exception e){
System.out.println(e.getMessage());
}
//適切なビューに転送する。
return(mapping.findForward(target));
}
}
|