001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.fukurou.process;
017
018import org.opengion.fukurou.util.Argument;
019import org.opengion.fukurou.util.Closer;
020import org.opengion.fukurou.util.LogWriter;
021import org.opengion.fukurou.model.Formatter;
022import org.opengion.fukurou.db.ConnectionFactory;
023
024import java.util.Map ;
025import java.util.LinkedHashMap ;
026
027import java.sql.Connection;
028import java.sql.PreparedStatement;
029import java.sql.ParameterMetaData;
030import java.sql.ResultSet;
031import java.sql.SQLException;
032
033/**
034 * Process_DBCountFilter は、データベースの存在件数でフィルタリングする
035 * ChainProcess インターフェースの実装クラスです。
036 * 上流(プロセスチェインのデータは上流から下流へと渡されます。)から受け取った
037 * LineModel を元に、データベースの存在チェックを行い、下流への処理を振り分けます。
038 * 具体的には、指定する SELECT 文は、必ず、『select count(*) from ・・・』形式にして下さい。
039 * 検索カラムは、一つだけで、そこには数字が入ります。
040 *
041 * データベース接続先等は、ParamProcess のサブクラス(Process_DBParam)に
042 * 設定された接続(Connection)を使用します。
043 *
044 * 引数文字列中にスペースを含む場合は、ダブルコーテーション("") で括って下さい。
045 * 引数文字列の 『=』の前後には、スペースは挟めません。必ず、-key=value の様に
046 * 繋げてください。
047 *
048 * @og.formSample
049 *  Process_DBCountFilter -dbid=DBGE -sql="select count(*) from GEA03"
050 *
051 *   [ -dbid=DB接続ID           ] : -dbid=DBGE (例: Process_DBParam の -configFile で指定する DBConfig.xml ファイルで規定)
052 *   [ -sql=検索SQL文           ] : -sql="SELECT COUNT(*) FROM GEA03
053 *                                         WHERE SYSTEM_ID = [SYSTEM_ID]
054 *                                         AND CLM         = [CLM]
055 *                                         AND FGJ         = '1'"
056 *   [ -sqlFile=検索SQLファイル ] : -sqlFile=select.sql
057 *                                :   -sql や -sqlFile が指定されない場合は、エラーです。
058 *   [ -count=スルー条件        ] : -count=[0|1|2] は、検索値に応じたスルー条件。
059 *                                     0:0件時にスルー(処理を継続) つまり、なければ継続
060 *                                     1:1件時にスルー(処理を継続) つまり、あれば継続
061 *                                     2:2件以上ある場合にスルー   つまり、キー重複時に継続
062 *   [ -display=[false/true]    ] :結果を標準出力に表示する(true)かしない(false)か(初期値:false[表示しない])
063 *   [ -debug=[false/true]      ] :デバッグ情報を標準出力に表示する(true)かしない(false)か(初期値:false[表示しない])
064 *
065 * @version  4.0
066 * @author   Kazuhiko Hasegawa
067 * @since    JDK5.0,
068 */
069public class Process_DBCountFilter extends AbstractProcess implements ChainProcess {
070
071        private Connection      connection      = null;
072        private PreparedStatement pstmt = null ;
073        private ParameterMetaData pMeta = null;         // 5.1.1.0 (2009/11/11) setObject に、Type を渡す。(PostgreSQL対応)
074        private boolean useParamMetaData = false;       // 5.1.1.0 (2009/11/11) setObject に、Type を渡す。(PostgreSQL対応)
075
076        private String          dbid            = null;
077        private String          sql                     = null;
078        private int                     cntFlag         = -2;           // スルー条件 [0|1|2]
079        private boolean         display         = false;        // 表示しない
080        private boolean         debug           = false;        // 5.7.3.0 (2014/02/07) デバッグ情報
081
082        private int[]           clmNos          = null;         // ファイルのヘッダーのカラム番号
083        private boolean         firstRow        = true;         // 最初の一行目
084        private int                     count           = 0;
085
086        private static final Map<String,String> mustProparty   ;          // [プロパティ]必須チェック用 Map
087        private static final Map<String,String> usableProparty ;          // [プロパティ]整合性チェック Map
088
089        static {
090                mustProparty = new LinkedHashMap<String,String>();
091
092                usableProparty = new LinkedHashMap<String,String>();
093                usableProparty.put( "dbid",     "Process_DBParam の -configFile で指定する DBConfig.xml ファイルで規定" );
094                usableProparty.put( "sql",                      "カウントSQL文(sql or sqlFile 必須)" +
095                                                                        CR + "例: \"SELECT COUNT(*) FROM GEA03 " +
096                                                                        CR + "WHERE SYSTEM_ID = [SYSTEM_ID] " +
097                                                                        CR + "AND CLM = [CLM] AND FGJ = '1'\"" );
098                usableProparty.put( "sqlFile",          "検索SQLファイル(sql or sqlFile 必須)例: select.sql" );
099                usableProparty.put( "count",    "[0|1|2] は、検索値に応じたスルー条件" +
100                                                                        CR + "  0:0件時にスルー(処理を継続) つまり、なければ継続" +
101                                                                        CR + "  1:1件時にスルー(処理を継続) つまり、あれば継続" +
102                                                                        CR + "  2:2件以上ある場合にスルー   つまり、キー重複時に継続" );
103                usableProparty.put( "display",  "結果を標準出力に表示する(true)かしない(false)か" +
104                                                                                CR + "(初期値:false:表示しない)" );
105                usableProparty.put( "debug",    "デバッグ情報を標準出力に表示する(true)かしない(false)か" +
106                                                                                CR + "(初期値:false:表示しない)" );             // 5.7.3.0 (2014/02/07) デバッグ情報
107        }
108
109        /**
110         * デフォルトコンストラクター。
111         * このクラスは、動的作成されます。デフォルトコンストラクターで、
112         * super クラスに対して、必要な初期化を行っておきます。
113         *
114         */
115        public Process_DBCountFilter() {
116                super( "org.opengion.fukurou.process.Process_DBCountFilter",mustProparty,usableProparty );
117        }
118
119        /**
120         * プロセスの初期化を行います。初めに一度だけ、呼び出されます。
121         * 初期処理(ファイルオープン、DBオープン等)に使用します。
122         *
123         * @og.rev 5.1.2.0 (2010/01/01) setObject に ParameterMetaData の getParameterType を渡す。(PostgreSQL対応)
124         * @og.rev 5.3.8.0 (2011/08/01) useParamMetaData を ConnectionFactory経由で取得。(PostgreSQL対応)
125         *
126         * @param   paramProcess データベースの接続先情報などを持っているオブジェクト
127         */
128        public void init( final ParamProcess paramProcess ) {
129                Argument arg = getArgument();
130
131                sql                     = arg.getFileProparty("sql","sqlFile",true);
132                cntFlag         = arg.getProparty("count",cntFlag);
133                display         = arg.getProparty("display",display);
134                debug           = arg.getProparty("debug",debug);                               // 5.7.3.0 (2014/02/07) デバッグ情報
135//              if( debug ) { println( arg.toString() ); }                      // 5.7.3.0 (2014/02/07) デバッグ情報
136
137                dbid            = arg.getProparty("dbid");
138                connection      = paramProcess.getConnection( dbid );
139//              useParamMetaData = ApplicationInfo.useParameterMetaData( connection );  // 5.1.2.0 (2010/01/01)
140                useParamMetaData = ConnectionFactory.useParameterMetaData( dbid );      // 5.3.8.0 (2011/08/01)
141        }
142
143        /**
144         * プロセスの終了を行います。最後に一度だけ、呼び出されます。
145         * 終了処理(ファイルクローズ、DBクローズ等)に使用します。
146         *
147         * @og.rev 4.0.0.0 (2007/11/27) commit,rollback,remove 処理を追加
148         * @og.rev 5.1.2.0 (2010/01/01) pMeta のクリア
149         *
150         * @param   isOK トータルで、OKだったかどうか [true:成功/false:失敗]
151         */
152        public void end( final boolean isOK ) {
153                boolean flag = Closer.stmtClose( pstmt );
154                pstmt = null;
155                pMeta = null;           // 5.1.1.0 (2009/11/11)
156
157                ConnectionFactory.remove( connection,dbid );
158
159                if( !flag ) {
160                        String errMsg = "ステートメントをクローズ出来ません。";
161                        throw new RuntimeException( errMsg );
162                }
163        }
164
165        /**
166         * 引数の LineModel を処理するメソッドです。
167         * 変換処理後の LineModel を返します。
168         * 後続処理を行わない場合(データのフィルタリングを行う場合)は、
169         * null データを返します。つまり、null データは、後続処理を行わない
170         * フラグの代わりにも使用しています。
171         * なお、変換処理後の LineModel と、オリジナルの LineModel が、
172         * 同一か、コピー(クローン)かは、各処理メソッド内で決めています。
173         * ドキュメントに明記されていない場合は、副作用が問題になる場合は、
174         * 各処理ごとに自分でコピー(クローン)して下さい。
175         *
176         * @og.rev 5.1.2.0 (2010/01/01) setObject に ParameterMetaData の getParameterType を渡す。(PostgreSQL対応)
177         * @og.rev 5.3.8.0 (2011/08/01) useParamMetaData  setNull 対応(PostgreSQL対応)
178         * @og.rev 5.7.2.2 (2014/01/24) SQL実行エラーを少し詳細に出力します。
179         *
180         * @param       data ラインモデル オリジナルのLineModel
181         *
182         * @return      処理変換後のLineModel
183         */
184        public LineModel action( final LineModel data ) {
185                LineModel rtnData = data;
186
187                count++ ;
188                try {
189                        if( firstRow ) {
190                                pstmt = makePrepareStatement( data );
191                                if( useParamMetaData ) {
192                                        pMeta = pstmt.getParameterMetaData();
193                                }
194                                firstRow = false;
195                                if( display ) { println( data.nameLine() ); }           // 5.7.3.0 (2014/02/07) デバッグ情報
196                        }
197
198                        // 5.1.1.0 (2009/11/11) setObject に ParameterMetaData の getParameterType を渡す。(PostgreSQL対応)
199                        if( useParamMetaData ) {
200                                for( int i=0; i<clmNos.length; i++ ) {
201                                        int type = pMeta.getParameterType( i+1 );
202                                        // 5.3.8.0 (2011/08/01) setNull 対応
203//                                      pstmt.setObject( i+1,data.getValue(clmNos[i]),type );
204                                        Object val = data.getValue(clmNos[i]);
205                                        if( val == null || ( val instanceof String && ((String)val).isEmpty() ) ) {
206                                                pstmt.setNull( i+1, type );
207                                        }
208                                        else {
209                                                pstmt.setObject( i+1, val, type );
210                                        }
211                                }
212                        }
213                        else {
214                                for( int i=0; i<clmNos.length; i++ ) {
215                                        pstmt.setObject( i+1,data.getValue(clmNos[i]) );
216                                }
217                        }
218
219                        int cnt = -1;
220                        ResultSet result = null;
221                        try {
222                                result = pstmt.executeQuery();
223                                if( result.next() ) {                           // 1行目固定
224                                        cnt = result.getInt( 1 );               // 1カラム目固定
225                                }
226                        }
227                        finally {
228                                Closer.resultClose( result ) ;
229                        }
230
231                        if( ( cnt > 2  && cntFlag != 2 ) ||
232                                ( cnt <= 2 && cnt != cntFlag ) ) {
233                                        rtnData = null;         // 不一致
234                        }
235//                      if( display ) { printKey( count,cnt,data ); }
236                        if( display ) { println( data.dataLine() ); }           // 5.1.2.0 (2010/01/01) display の条件変更
237                }
238                catch (SQLException ex) {
239                        // 5.7.2.2 (2014/01/24) SQL実行エラーを少し詳細に出力します。
240                        String errMsg = "SQL を実行できませんでした。" + CR
241                                        + "errMsg=[" + ex.getMessage() + "]" + CR
242                                        + "errCode=[" + ex.getErrorCode() + "] State=[" + ex.getSQLState() + "]" + CR
243                                        + "dbid=[" + dbid + "]" + CR
244                                        + "sql =[" + sql + "]" + CR
245                                        + "data=[" + data.dataLine() + "]" + CR ;
246//                      String errMsg = "sql=[" + sql + "]" + CR +
247//                                              "errorCode=[" + ex.getErrorCode() + "] State=[" + ex.getSQLState() + "]" + CR ;
248                        throw new RuntimeException( errMsg,ex );
249                }
250                return rtnData;
251        }
252
253        /**
254         * 内部で使用する PreparedStatement を作成します。
255         * 引数指定の SQL または、LineModel から作成した SQL より構築します。
256         *
257         * @og.rev 5.7.2.2 (2014/01/24) SQL実行エラーを少し詳細に出力します。
258         *
259         * @param       data ラインモデル 処理対象のLineModel
260         *
261         * @return  PreparedStatementオブジェクト
262         */
263        private PreparedStatement makePrepareStatement( final LineModel data ) {
264
265                // カラム番号は、makeFormat の処理で設定しています。
266                Formatter format = new Formatter( data );
267                format.setFormat( sql );
268                sql = format.getQueryFormatString();
269                clmNos = format.getClmNos();
270
271                final PreparedStatement ps ;
272                try {
273                        ps = connection.prepareStatement( sql );
274                }
275                catch (SQLException ex) {
276                        // 5.7.2.2 (2014/01/24) SQL実行エラーを少し詳細に出力します。
277                        String errMsg = "PreparedStatement を取得できませんでした。" + CR
278                                        + "errMsg=[" + ex.getMessage() + "]" + CR
279                                        + "errCode=[" + ex.getErrorCode() + "] State=[" + ex.getSQLState() + "]" + CR
280                                        + "dbid=[" + dbid + "]" + CR
281                                        + "sql =[" + sql + "]" + CR
282                                        + "data=[" + data.dataLine() + "]" + CR ;
283//                      String errMsg = "PreparedStatement を取得できませんでした。" + CR
284//                                              + "sql=[" + sql + "]" + CR
285//                                              + "nameLine=[" + data.nameLine() + "]" ;
286                        throw new RuntimeException( errMsg,ex );
287                }
288
289                return ps;
290        }
291
292        /**
293         * プロセスの処理結果のレポート表現を返します。
294         * 処理プログラム名、入力件数、出力件数などの情報です。
295         * この文字列をそのまま、標準出力に出すことで、結果レポートと出来るような
296         * 形式で出してください。
297         *
298         * @return   処理結果のレポート
299         */
300        public String report() {
301                String report = "[" + getClass().getName() + "]" + CR
302                                + TAB + "DBID         : " + dbid + CR
303                                + TAB + "Output Count : " + count ;
304
305                return report ;
306        }
307
308        /**
309         * 画面出力用のフォーマットを作成します。
310         *
311         * @og.rev 5.7.3.0 (2014/02/07) 表示方法の変更のため、廃止
312         *
313         * @param       rowNo   データ読み取り件数
314         * @param       cnt     検索結果(の件数)
315         * @param       data ラインモデル
316         */
317//      private void printKey( final int rowNo , final int cnt , final LineModel data ) {
318//              StringBuilder buf = new StringBuilder();
319//
320//              buf.append( "row=[" ).append( rowNo ).append( "] : " );
321//              buf.append( "count=[" ).append( cnt ).append( "] " );
322//              for( int i=0; i < clmNos.length; i++ ) {
323//                      if( i == 0 ) { buf.append( "where " ); }
324//                      else         { buf.append( " and " );  }
325//                      buf.append( data.getName( clmNos[i] ) );
326//                      buf.append( " = " );
327//                      buf.append( data.getValue( clmNos[i] ) );
328//              }
329//
330//              println( buf.toString() );
331//      }
332
333        /**
334         * このクラスの使用方法を返します。
335         *
336         * @return      このクラスの使用方法
337         */
338        public String usage() {
339                StringBuilder buf = new StringBuilder();
340
341                buf.append( "Process_DBCountFilter は、データベースの存在件数でフィルタリングする"                     ).append( CR );
342                buf.append( "ChainProcess インターフェースの実装クラスです。"                                                            ).append( CR );
343                buf.append( "上流(プロセスチェインのデータは上流から下流へと渡されます。)から"                 ).append( CR );
344                buf.append( "受け取った LineModel を元に、データベースの存在チェックを行い、"                             ).append( CR );
345                buf.append( "下流への処理を振り分けます。"                                                                                                    ).append( CR );
346                buf.append( "存在チェックで指定する SELECT 文は、必ず、『select count(*) from ・・・』"               ).append( CR );
347                buf.append( "形式にして下さい。検索カラムは、一つだけで、そこには数字が入ります。"                        ).append( CR );
348                buf.append( CR );
349                buf.append( "データベース接続先等は、ParamProcess のサブクラス(Process_DBParam)に"                 ).append( CR );
350                buf.append( "設定された接続(Connection)を使用します。"                                                                                ).append( CR );
351                buf.append( CR );
352                buf.append( "引数文字列中に空白を含む場合は、ダブルコーテーション(\"\") で括って下さい。" ).append( CR );
353                buf.append( "引数文字列の 『=』の前後には、空白は挟めません。必ず、-key=value の様に"                ).append( CR );
354                buf.append( "繋げてください。"                                                                                                                          ).append( CR );
355                buf.append( CR ).append( CR );
356                buf.append( getArgument().usage() ).append( CR );
357
358                return buf.toString();
359        }
360
361        /**
362         * このクラスは、main メソッドから実行できません。
363         *
364         * @param       args    コマンド引数配列
365         */
366        public static void main( final String[] args ) {
367                LogWriter.log( new Process_DBCountFilter().usage() );
368        }
369}