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.plugin.daemon;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.HybsSystemException;
020import org.opengion.hayabusa.report.GE50Access;
021import org.opengion.hayabusa.report.ReportProcessing;
022import org.opengion.fukurou.system.LogWriter;
023import org.opengion.fukurou.util.StringUtil;
024import org.opengion.fukurou.system.ThrowUtil;                                           // 6.4.2.0 (2016/01/29)
025import org.opengion.fukurou.util.HybsTimerTask;
026import org.opengion.fukurou.db.ApplicationInfo;
027import org.opengion.fukurou.db.DBUtil;
028import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
029
030import java.util.concurrent.ConcurrentMap;                                                      // 6.4.3.3 (2016/03/04)
031import java.util.concurrent.ConcurrentHashMap;                                          // 6.4.3.1 (2016/02/12) refactoring
032import java.util.Date;
033
034/**
035 * 【レポート出力】帳票要求テーブルを監視して、帳票処理プログラムを呼び出します。
036 * このクラスは、HybsTimerTask を継承した タイマータスククラスです。
037 * startDaemon() がタイマータスクによって、呼び出されます。
038 *
039 * @og.rev 4.3.4.4 (2009/01/01) プラグイン化
040 * @og.group デーモン
041 *
042 * @version  4.0
043 * @author   Kazuhiko Hasegawa
044 * @since    JDK5.0,
045 */
046public class Daemon_Report extends HybsTimerTask {
047        /** このプログラムのVERSION文字列を設定します。   {@value} */
048        private static final String VERSION = "6.4.3.3 (2016/03/04)" ;
049
050        /** コネクションにアプリケーション情報を追記するかどうか指定 */
051        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
052
053        // 3.7.0.0 (2005/01/18) 複数同時デーモン時の、同一帳票IDは処理できない。
054        // 実行中の帳票ID をセットする、static Map
055        /** 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。  */
056        private static final ConcurrentMap<String,String> USE_LISTID = new ConcurrentHashMap<>();               // 6.4.3.1 (2016/02/12)
057
058        // 3.8.5.0 (2006/03/06) EXCELをオープンするファイル名に要求番号を使う場合は、true
059        private static final boolean EXCEL_NAME_USE_YKNO = HybsSystem.sysBool( "REPORT_EXCEL_NAME_USE_YKNO" );
060
061        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
062        private static final String OUT_FILE = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "OUTFILE" : "OUT_FILE";
063        private static final String OUT_DIR = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "OUTDIR" : "OUT_DIR";
064
065        // 3.7.0.0 (2005/01/18) GE53 に DMN_GRP追加による、検索条件の変更
066        // 5.1.0.0 (2009/11/04) OUTDIR ⇒ OUT_DIR , OUTFILE ⇒ OUT_FILE
067        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
068
069        // 5.9.0.1 (2015/09/11)
070        private static final String GE50_SELECT =
071                "SELECT A.SYSTEM_ID,A.YKNO,A.GROUPID,A.LISTID,A.JOKEN,A."+OUT_DIR+",A."+OUT_FILE+",A.USRSET" +
072                " FROM GE50 A,GE53 B,GE54 C" +
073                " WHERE A.SYSTEM_ID = B.SYSTEM_ID" +
074                " AND A.JOKEN = B.JOKEN" +
075                " AND A.FGJ = '1'" +
076                " AND B.FGJ = '1'" +
077                " AND A.FGKAN = '1'" +
078                " AND A.SYSTEM_ID = C.SYSTEM_ID" +
079                " AND A.LISTID = C.LISTID"+
080                " AND C.FGJ = '1'";
081
082        private final String DBID = HybsSystem.sys( "RESOURCE_DBID" );          // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対応
083
084        // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
085        private ApplicationInfo appInfo ;
086        private ReportProcessing  rc    ;
087
088        private static final int LOOP_COUNTER = 24;             // カウンタを24回に設定
089        private int             loopCnt         ;               // 3.5.4.9 (2004/02/25) メッセージ出力時のループカウント
090
091        // 6.3.9.0 (2015/11/06) Variables should start with a lowercase character(PMD)
092        private String  geSELECT        ;               // 3.7.0.0 (2005/01/18) GE53 に DMN_GRP追加による、検索条件の変更
093        private String  prtID           ;               // 3.8.5.0 (2006/03/06) プリンタIDが、引数から渡される場合の対応
094        private String  dmnNAME         ;               // 3.8.5.0 (2006/03/06) デーモン名を設定します。
095        private boolean debug           ;               // 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加します。小文字に修正
096        private boolean running         = true; // 3.8.5.3 (2006/06/30) タイマータスクがキャンセルされた場合の停止フラグ
097
098        /**
099         * デフォルトコンストラクター
100         *
101         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
102         */
103        public Daemon_Report() { super(); }             // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
104
105        /**
106         * このタイマータスクによって初期化されるアクションです。
107         * パラメータを使用した初期化を行います。
108         *
109         * @og.rev 3.6.0.7 (2004/11/12) 新規追加
110         * @og.rev 3.7.0.0 (2005/01/18) 帳票定義マスタ(GE54)を参照するように仕様変更
111         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
112         * @og.rev 4.0.1.0 (2007/12/19) GE50の検索順をシステムリソースで設定可能にする
113         * @og.rev 5.7.3.2 (2014/02/28) GE53の検索条件修正
114         */
115        @Override
116        public void initDaemon() {
117
118                // 3.7.0.0 (2005/01/18) GE50, GE54 の USRUPD に、デーモン名をセットします。
119                // 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
120
121                // 3.8.5.0 (2006/03/06) デーモン名を設定します。
122                dmnNAME = getName();                                    // 6.3.9.0 (2015/11/06)
123
124                final StringBuilder buf = new StringBuilder( BUFFER_MIDDLE );
125                buf.append( GE50_SELECT );
126
127                // SYSTEM_ID は、指定がなければ、全件検索対象になります。
128                final String systemId = getValue( "SYSTEM_ID" );
129                if( ! StringUtil.isNull( systemId ) ) {
130                        buf.append( " AND A.SYSTEM_ID='" ).append( systemId ).append( '\'' );
131                }
132
133                // 3.8.5.0 (2006/03/06) DMN_GRP は、必須指定
134                // 5.1.9.0 (2010/08/01) Avoid if(x != y) ..; else ..;
135                final String dmnGroup = getValue( "DMN_GRP" );
136                if( StringUtil.isNull( dmnGroup ) ) {
137                        final String errMsg = "デーモングループは必須指定です。" ;
138                        throw new HybsSystemException( errMsg );
139                }
140                else {
141                        buf.append( " AND B.DMN_GRP='" ).append( dmnGroup ).append( '\'' );
142                }
143
144                // 3.8.5.0 (2006/03/06) GE50 の検索条件に、MODBASE と MODNO を使用する。
145                // デーモン起動時に 最大数(MODBASE)と余り番号(MODNO)を渡します。
146                // 最大数(MODBASE)を元に、検索時に、YKNOの余りを求め、これが、
147                // 引数の余り番号(MODNO)と一致する場合のみ、処理をします。
148                final String modBase    = StringUtil.nval( getValue( "MODBASE" ),null );
149                final String modNo      = StringUtil.nval( getValue( "MODNO" ),null );
150                if( modBase != null && modNo != null ) {
151                        buf.append( " AND MOD(A.YKNO," ).append( modBase ).append( ")=" ).append( modNo );
152                }
153
154                // 3.8.5.0 (2006/03/06) PRTID が指定されていれば、その値を使用する。なければ NULL
155                prtID = StringUtil.nval( getValue( "PRTID" ), null );                           // 6.3.9.0 (2015/11/06)
156
157                // 3.8.5.0 (2006/03/06) PRT_GRP が指定されていれば、振分条件検索時に使用する。
158                final String prtGgrp = getValue( "PRT_GRP" );
159                if( ! StringUtil.isNull( prtGgrp ) ) {
160                        buf.append( " AND B.PRTID='" ).append( prtGgrp ).append( '\'' ); // 5.7.3.2 (2014/02/28) GE53ではPRTIDにPRI_GRPが指定されている
161                }
162
163                buf.append( " ORDER BY " )                              // 4.0.1.0 (2007/12/19)
164                        .append( HybsSystem.sys( "REPORT_DAEMON_ORDER_BY" ) );
165
166                geSELECT = buf.toString() ;                             // 6.3.9.0 (2015/11/06)
167
168                // 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加します。
169                debug = StringUtil.nval( getValue( "DEBUG" ),debug ) ;
170
171                if( debug ) {
172                        System.out.println( "DMN_NAME=[" + dmnNAME + "]" );                             // 6.3.9.0 (2015/11/06)
173                        System.out.println( "MODNO=[" + modNo + "]" );
174                        System.out.println( "QUERY=[" + geSELECT + "]" );
175                        System.out.println( "EXCEL_NAME_USE_YKNO=[" + EXCEL_NAME_USE_YKNO + "]" );
176                }
177
178                // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
179                if( USE_DB_APPLICATION_INFO ) {
180                        appInfo = new ApplicationInfo();
181                        // ユーザーID,IPアドレス,ホスト名
182                        appInfo.setClientInfo( systemId,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
183                        // 画面ID,操作,プログラムID
184                        appInfo.setModuleInfo( "ReportDaemon",prtID,dmnNAME );                  // 6.3.9.0 (2015/11/06)
185                }
186                else {
187                        appInfo = null;
188                }
189        }
190
191        /**
192         * タイマータスクのデーモン処理の開始ポイントです。
193         *
194         * @og.rev 3.5.2.0 (2003/10/20) vals 変数を、ローカルに移動
195         * @og.rev 3.5.4.8 (2004/02/23) タイムスタンプを、10回に1回とする。
196         * @og.rev 3.6.0.0 (2004/09/17) タイムスタンプを、24回に1回とする。
197         * @og.rev 3.6.1.0 (2005/01/05) tyr ~ catch を Exception から Throwable に変更。
198         * @og.rev 3.7.0.0 (2005/01/18) 複数同時デーモンでも、同一帳票IDは処理できない為、スキップします。
199         * @og.rev 3.7.0.4 (2005/03/18) エラー発生時に vals が null なら、HybsSystemException を throw する。
200         * @og.rev 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
201         * @og.rev 3.8.0.0 (2005/06/07) EXCEL 取込時の完成フラグは、FG_DBIN とします。
202         * @og.rev 3.8.0.0 (2005/06/07) rc.execute() 実行結果を boolean ではなく、文字列(FGKAN_XX)で返します。
203         * @og.rev 3.8.5.0 (2006/03/06) EXCELファイル名に要求番号を使う場合は、帳票IDでの排他制御は不要。
204         * @og.rev 3.8.5.2 (2006/05/31) DEBUG 情報の強化
205         * @og.rev 3.8.6.0 (2006/06/30) タイマータスクがキャンセルされた場合の処理を追加(running フラグ)
206         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
207         * @og.rev 5.3.0.0 (2010/12/01) エラーハンドリングを修正
208         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
209         * @og.rev 5.7.0.4 (2013/11/29) listIdをGE50Accessに渡すようにする
210         * @og.rev 6.4.2.0 (2016/01/29) StringUtil#stringStackTrace(Throwable) を、ThrowUtil#ogStackTrace(String,Throwable) に置き換え。
211         * @og.rev 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
212         * @og.rev 6.4.3.3 (2016/03/04) Map#putIfAbsent で対応する。
213         */
214        @Override
215        protected void startDaemon() {
216                if( loopCnt % LOOP_COUNTER == 0 ) {
217                        loopCnt = 1;
218                        System.out.println();
219                        System.out.print( toString() + " " + new Date()  + " " );
220                }
221                else {
222                        System.out.print( "." );
223                        loopCnt++ ;
224                }
225
226                // 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
227                GE50Access ge50 = null ;
228
229                String listId = null;                           // 6.4.3.1 (2016/02/12) vals をローカル化して、listId を大域化します。
230                try {
231                        final String[][] vals = DBUtil.dbExecute( geSELECT,null,appInfo, DBID );                // 6.3.9.0 (2015/11/06)
232                        if( vals != null && vals.length > 0 ) {
233
234                                // 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
235                                // 毎回 オブジェクトを構築します。登録日付が初期化されます。
236                                ge50 = new GE50Access( null,null,dmnNAME ) ;                    // 6.3.9.0 (2015/11/06)
237
238                                if( rc == null ) { rc = new ReportProcessing(); }
239                                // 3.8.6.0 (2006/06/30) タイマータスクがキャンセルされた場合の処理を追加(running フラグ)
240                                for( int row=0; running && row<vals.length; row++ ) {   // 6.4.3.1 (2016/02/12)
241                                        // 3.7.0.0 (2005/01/18) 使用中の帳票IDのチェックと、使用時の登録
242                                        final String systemId = vals[row][0] ;
243                                        final String ykno     = vals[row][1] ;
244                                        listId                            = vals[row][3] ;                              // 6.4.3.1 (2016/02/12)
245                                        // 3.8.5.0 (2006/03/06) EXCELファイル名に要求番号を使う場合は、帳票IDでの排他制御は不要。
246                                        if( ! EXCEL_NAME_USE_YKNO ) {
247                                                // 6.4.3.3 (2016/03/04) Map#putIfAbsent で対応します。
248                                                // Map#putIfAbsent : 戻り値は、以前の値。追加有り、置換なし、削除なし
249                                                if( USE_LISTID.putIfAbsent( listId,"DUMMY" ) != null ) { continue; }    // 使用中なら、飛ばす。
250
251                                                // 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
252                                        }
253
254                                        // デバッグ情報を出力します。
255                                        if( debug ) {
256                                                System.out.println();
257                                                System.out.print( "[" + dmnNAME + "]:[" + ykno + "] START = " );                        // 6.3.9.0 (2015/11/06)
258                                                System.out.println( new Date() );
259                                        }
260
261                                        // 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
262                                        ge50.setSystemId( systemId );
263                                        ge50.setYkno( ykno );
264                                        ge50.updateGE50( GE50Access.FG_RUN );
265
266                                        ge50.setListId( listId ); // 5.7.0.4 (2013/11/29)
267
268                                        // system_id,ykno,groupid,listid,joken,outdir,outfile,usrset
269                                        rc.setSystemId( systemId        );
270                                        rc.setYkno(             ykno            );
271                                        rc.setGroupId(  vals[row][2]);
272                                        rc.setListId(   listId          );
273                                        rc.setJoken(    vals[row][4]);
274                                        rc.setPrtId(    prtID           );              // 6.3.9.0 (2015/11/06)
275                                        rc.setOutDir(   vals[row][5]);
276                                        rc.setOutFile(  vals[row][6]);
277                                        rc.setDebug(    debug           );              // 3.8.5.0 (2006/03/06) DEBUGを追加。
278
279                                        // 3.8.0.0 (2005/06/07) 実行結果を boolean ではなく、文字列(FGKAN_XX)で返します。
280                                        String fgkan = rc.execute();
281                                        if( fgkan == null ) {
282                                                fgkan = GE50Access.FG_ERR2 ;
283                                                final String errMsg = rc.getErrMsg();
284                                                // 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
285                                                ge50.insertErrorGE56( errMsg );
286                                        }
287
288                                        // 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
289                                        ge50.updateGE50( fgkan );
290
291                                        rc.clear();
292                                        // 3.8.5.0 (2006/03/06) EXCELファイル名に要求番号を使う場合は、帳票IDでの排他制御は不要。
293                                        if( ! EXCEL_NAME_USE_YKNO ) {
294                                                // 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
295                                                USE_LISTID.remove( listId );
296
297                                        }
298
299                                        // デバッグ情報を出力します。
300                                        if( debug ) {
301                                                System.out.println();
302                                                System.out.print( "[" + dmnNAME + "]:[" + ykno + "] END = " );                  // 6.3.9.0 (2015/11/06)
303                                                System.out.println( new Date() );
304                                        }
305                                }
306                        }
307                }
308                // 5.3.0.0 (2010/12/01) エラーハンドリングを修正
309                catch( final Throwable ex ) {           // 3.6.1.0 (2005/01/05)
310                        rc = null;
311
312                        final String errMsg = ThrowUtil.ogStackTrace( ex ) ;                                                            // 6.4.2.0 (2016/01/29)
313                        System.out.println( errMsg );
314                        LogWriter.log( errMsg );
315
316                        if( ! EXCEL_NAME_USE_YKNO ) {
317                                // 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。
318                                if( listId != null ) { USE_LISTID.remove( listId ); }
319
320                        }
321
322                        // 3.7.1.1 (2005/05/31) GE50Access を使用して、DB登録を行います。
323                        if( ge50 != null ) {
324                                ge50.insertErrorGE56( errMsg );
325                                ge50.updateGE50( GE50Access.FG_ERR1 );
326                        }
327                        // 6.4.3.1 (2016/02/12) 条件は異なるが、ほぼ、近いはず
328                        else if( listId == null ) {
329                                final String errMsg2 = "SQL=[" + geSELECT + "] , appInfo=[" + appInfo + "] , DBID=[" + DBID + "]";
330                                System.out.println( errMsg2 );
331                                LogWriter.log( errMsg2 );
332                                throw new HybsSystemException( errMsg + errMsg2 , ex );
333                        }
334
335                }
336        }
337
338        /**
339         * このタイマータスクのcancel() メソッドをオーバーライドします。
340         * HybsTimerTaskManager#cancelTask( int ) を実行します。
341         *
342         * @og.rev 3.8.5.3 (2006/06/30) 新規追加
343         *
344         * @return      スケジュールされている 1 回以上実行されない場合に true
345         * @see java.util.TimerTask#cancel()
346         */
347        @Override
348        public boolean cancel() {
349                running = false;
350                return super.cancel();
351        }
352}