package jp.test1.mst;
 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.*;
import java.io.*;
 

import jp.test1.beans.*;
import jp.test1.system.*;
 

import org.apache.log4j.Logger;
 

//CSVデータ処理クラス
public class CsvDataTakeInMst {
 

         private static Logger logger = Logger.getLogger(CsvDataTakeInMst.class.getName());
         private DBManager dbm = null;
 

         public CsvDataTakeInMst(DBManager dbm){
 

                   this.dbm = dbm;
 

         }
 

         //生徒テストデータ 更新SQL
         public int update(CsvDataTakeInDataRetention csvDataMst) throws SQLException{
 

                   logger.debug("Start update SQL.");
 

                   StringBuffer sb = new StringBuffer();
 

                   sb.append("UPDATE a_test_results_dat SET ");
                   sb.append("score=);
                   sb.append(csvDataMst.getScore());
                   sb.append(,");
                   sb.append("update_ymd=);
                   sb.append(UtilDateTime.getDateYMD());
                   sb.append(");
                   sb.append(" WHERE test1_cd=);
                   sb.append(csvDataMst.getTest1Cd());
                   sb.append( AND ");
                   sb.append("test2_cd=);
                   sb.append(csvDataMst.getTest2Cd());
                   sb.append(");
 

                   //更新するSQL実行
                   String sql = sb.toString();
                   int num = dbm.executeUpdateSql(sql);
 

                   logger.debug("Finish updete SQL.");
 

                   return num;
         }
 

         //生徒テストデータ 追加SQL
         public int insert(CsvDataTakeInDataRetention csvDataMst) throws SQLException{
 

                   logger.debug("Start insert SQL.");
 

                   StringBuffer sb = new StringBuffer();
                   sb.append("INSERT INTO a_test_results_dat(");
                   sb.append("test1_cd");
                   sb.append(",test2_cd");
                   sb.append(",score");
                   sb.append(", insert_ymd");
                   sb.append(") VALUES (");
                   sb.append(csvDataMst.getTest1Cd());
                   sb.append(",);
                   sb.append(csvDataMst.getTest2Cd());
                   sb.append(,);
                   sb.append(csvDataMst.getScore());
                   sb.append(,);
                   sb.append(UtilDateTime.getDateYMD());
                   sb.append()");
 

                   //登録するSQL実行
                   String sql = sb.toString();
                   int num = dbm.executeUpdateSql(sql);
 

                   logger.debug("Finish insert SQL.");
 

                   return num;
 

         }
 

         //CSV読込を飛ばした時の処理
                   public void textWrite(String line,String fileName){
 

                            //PropertiesManager.Javaを取得、実行
                            PropertiesManager propMng = PropertiesManager.getInstance();
 

                            /*
                             *
                             * テキストファイル名設定
                             * ここで設定したファイル名があれば追 加上書き書込み
                             * 存在しなければ作成して書込み
                             *
                             */
                            File file = new File(propMng.getProperty("PATH.DIR.EXPORT.ROOT") + fileName + "_" + UtilDateTime.getDateYMD() + ".txt");
 

                            try{
 

                                     // 出力ストリームの生成(追記モード)
                                     FileOutputStream fos = new FileOutputStream(file,true);
                                     OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
                                     PrintWriter filewriter = new PrintWriter(osw);
 

                                     //テキストファイルに書き込む
                                     filewriter.write(line + " ");
 

                                     filewriter.close();
 

                                     logger.debug("CSVをテキストファイルに書き込みました");
 

                            }catch(IOException e){
 

                                     logger.error("例外発生(IOException):",e);
 

                            }catch(NullPointerException e){
 

                                     logger.error("例外発生(NullPointerException):",e);
 

                            }
 

                   }
 

         //ファイル名から拡 張子を取得する
         public  String getSuffix(String fileName) {
 

             if (fileName == null)
 

                 return null;
 

             int point = fileName.lastIndexOf(".");
 

             if (point != -1) {
 

                 return fileName.substring(point + 1);
 

             }
 

             return fileName;
 

         }
 

         //ファイル名から拡 張子を取り除いたファイル名を取得する
         public static String getPreffix(String fileName) {
 

             if (fileName == null)
 

                 return null;
 

             int point = fileName.lastIndexOf(".");
 

             if (point != -1) {
 

                 return fileName.substring(0, point);
 

             }
 

             return fileName;
         }
 

}