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.hayabusa.report;
017
018import org.opengion.hayabusa.common.HybsSystem;
019import org.opengion.hayabusa.common.HybsSystemException;
020import org.opengion.fukurou.system.DateSet;                                             // 6.4.2.0 (2016/01/29)
021import org.opengion.fukurou.util.StringUtil;
022import org.opengion.fukurou.db.ApplicationInfo;
023import org.opengion.fukurou.system.ThrowUtil ;                                  // 6.4.2.0 (2016/01/29)
024import org.opengion.fukurou.db.DBUtil;
025import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
026import static org.opengion.fukurou.system.HybsConst.FS ;                                // 6.1.0.0 (2014/12/26)
027import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.1.0.0 (2014/12/26) refactoring
028
029import java.io.File;
030
031/**
032 * DBTableModelオブジェクトをレポート形式に返還するタグリブクラスです。
033 * このオブジェクトに、 データ(DBTableModel)と、コントローラ(DBTableReport クラス)を与えて、
034 * 外部からコントロールすることで、各種形式で データ(DBTableModel)を表示させることが
035 * 可能です。
036 *
037 * @og.group 帳票システム
038 *
039 * @version  4.0
040 * @author   Kazuhiko Hasegawa
041 * @since    JDK5.0,
042 */
043public class ReportProcessing {
044
045        private static final String FGRUN_1     = "1";  // HTMLのみ
046        private static final String FGRUN_2     = "2";  // 印刷のみ
047        private static final String FGRUN_3     = "3";  // 出力+印刷
048
049        // 3.8.0.0 (2005/06/07) 実行方法(FGRUN)に、EXCEL取込関連のフラグ値を追加。
050        private static final String FGRUN_5     = "5";  // 取込のみ
051        private static final String FGRUN_6     = "6";  // PG起動のみ
052        private static final String FGRUN_7     = "7";  // 取込+PG起動
053
054        private static final String FGRUN_A     = "A";  // RFID(通常印刷) 4.0.3.0 (2007/12/26)
055        private static final String FGRUN_B     = "B";  // RFID(全印刷)   4.0.3.0 (2007/12/26)
056        private static final String FGRUN_C     = "C";  // RFID(全消去)   4.0.3.0 (2007/12/26)
057        private static final String FGRUN_D     = "D";  // RFID(連続消去) 4.1.1.0 (2008/02/21)
058
059        private static final String FGRUN_G     = "G";  // CSV(印刷)  5.9.0.0 (2015/09/04)
060        private static final String FGRUN_H     = "H";  // CSV(Excel) 5.9.0.0 (2015/09/04)
061        private static final String FGRUN_I     = "I";  // CSV(PDF)   5.9.0.0 (2015/09/04)
062        private static final String FGRUN_J     = "J";  // CSV(XLSX)  5.9.4.2 (2016/01/13)
063
064        // 印刷時に使用するテンポラリフォルダ名
065        private final String REPORT_URL =
066                HybsSystem.url2dir( StringUtil.nval(
067                                                                HybsSystem.sys( "REPORT_FILE_URL" ) ,
068                                                                HybsSystem.sys( "FILE_URL"   ) + "REPORT/" ) ) ;
069
070        // 3.8.0.0 (2005/06/07) 追加
071        // EXCEL取込時に使用するテンポラリフォルダ名。ファイル名は、要求番号.xls
072        private final String EXCELIN_URL =
073                HybsSystem.url2dir( StringUtil.nval(
074                                                                HybsSystem.sys( "EXCEL_IN_FILE_URL" ) ,
075                                                                HybsSystem.sys( "FILE_URL"   ) + "EXCELIN/" ) ) ;
076
077        private final StringBuilder errMsg = new StringBuilder( BUFFER_MIDDLE );
078
079        // 各種DBを検索するときに、キー情報
080        private String          SYSTEM_ID       ;
081        private String          YKNO            ;
082        private String          GROUPID         ;
083        private String          LISTID          ;
084        private String          JOKEN           ;
085        private String          OUT_DIR         ;               // 5.1.0.0 (2009/11/04) OUTDIR ⇒ OUT_DIR
086        private String          OUT_FILE        ;               // 5.1.0.0 (2009/11/04) OUTFILE ⇒ OUT_FILE
087        private String          outputFile      ;
088        private boolean         isDebug         ;               // 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加
089
090        // GE53 検索結果
091        private String          FGRUN           ;
092        private String          PRTID           ;
093        private String          LANG            = "ja";
094        private String          PRGDIR          ;
095        private String          PRGFILE         ;
096        private String          DMN_GRP         ;               // 3.8.0.5 (2005/08/26)
097        private String          programFile     ;
098
099        // GE53 の帳票振分情報を取得するSQL文です。
100        // 3.8.0.5 (2005/08/26) デーモングループ追加
101        // 4.0.0 (2005/01/31) 共有 system_id を、考慮
102        private static final String GE53_SELECT =
103                "SELECT FGRUN,PRTID,LANG,PRGDIR,PRGFILE,SYSTEM_ID,DMN_GRP" +
104                " FROM GE53" +
105                " WHERE FGJ = '1'" +
106                " AND  SYSTEM_ID IN (?,'**')" +
107                " AND  JOKEN = ?" ;
108
109        private static final int GE53_FGRUN             = 0;
110        private static final int GE53_PRTID             = 1;
111        private static final int GE53_LANG              = 2;
112        private static final int GE53_PRGDIR    = 3;
113        private static final int GE53_PRGFILE   = 4;
114        private static final int GE53_SYSTEM_ID = 5;
115        private static final int GE53_DMN_GRP   = 6;
116
117        /** コネクションにアプリケーション情報を追記するかどうか指定 */
118        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
119
120        // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
121        private ApplicationInfo appInfo;
122        private final String DBID = HybsSystem.sys( "RESOURCE_DBID" );          // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対応
123
124        /**
125         * デフォルトコンストラクター
126         *
127         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
128         */
129        public ReportProcessing() { super(); }          // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
130
131        /**
132         * 処理を実行します。
133         * このメソッドを実行すると、レポート変換、レポート出力を順次処理していきます。
134         *
135         * @og.rev 3.0.0.4 (2003/02/26) FGRUN が PRT のみのときは,MODELFILE のみセットして終了する。
136         * @og.rev 3.8.0.0 (2005/06/07) ReportConverter の initialDataSet() を呼び出して,MODELFILE をセットする。
137         * @og.rev 3.8.0.0 (2005/06/07) EXCEL 登録機能を追加します。
138         * @og.rev 3.8.0.0 (2005/06/07) 実行結果を boolean ではなく、文字列(FGKAN_XX)で返します。
139         * @og.rev 3.8.0.5 (2005/08/26) デーモングループ追加
140         * @og.rev 3.8.0.9 (2005/10/17) エラーメッセージ強化
141         * @og.rev 3.8.5.0 (2006/03/06) 標準画面出力メッセージの変更。日付、プリンタIDの表示
142         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
143         * @og.rev 4.0.3.0 (2007/12/26) RFID対応(実行方法='A','B','C'の処理追加)
144         * @og.rev 4.1.1.0 (2008/02/21) RFID対応(実行方法='D'の処理追加)
145         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応(PRGDIR,PRGFILEの渡し等)
146         * @og.rev 5.4.3.9 (2012/01/25) RFIDの雛形ファイル名私は旧帳票は無視する
147         * @og.rev 5.9.0.0 (2015/09/04) CSV出力対応
148         * @og.rev 5.9.2.2 (2015/11/20) CSV出力にgrpid,dmngrpを追加
149         * @og.rev 5.9.4.2 (2016/01/13) FGRUN_J追加
150         * @og.rev 6.4.2.0 (2016/01/29) StringUtil#stringStackTrace(Throwable) を、ThrowUtil#ogStackTrace(Throwable) に置き換え。
151         * @og.rev 6.4.2.0 (2016/01/29) DateSet.getDate( String ) を利用するように修正します。
152         * @og.rev 6.4.2.1 (2016/02/05) メソッド名変更 ReportConverter#modelFile() → ReportConverter#getModelFile() 。
153         *
154         * @return 結果 (FGKAN_XX エラー時は、null)
155         */
156        public String execute() {
157                System.out.println();
158                System.out.println( "Process Start:" + DateSet.getDate( "yyyy/MM/dd HH:mm:ss" ) );                                              // 6.4.2.0 (2016/01/29)
159                System.out.println( "  SYSTEM_ID=[" + SYSTEM_ID + "] YKNO=[" + YKNO + "] GROUPID=[" + GROUPID + "]" );
160                System.out.println( "  PRTID=[" + PRTID + "] LISTID=[" + LISTID + "] JOKEN=[" + JOKEN + "]" );
161                String  fgkan = null;
162
163                // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
164                if( USE_DB_APPLICATION_INFO ) {
165                        appInfo = new ApplicationInfo();
166                        // ユーザーID,IPアドレス,ホスト名
167                        appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
168                        // 画面ID,操作,プログラムID
169                        appInfo.setModuleInfo( "ReportProcessing",YKNO,LISTID );
170                }
171                else {
172                        appInfo = null;
173                }
174
175                boolean flag ;
176                try {
177                        flag = initialDataSet( appInfo );
178                        System.out.print( "INIT=" + flag + "," );
179                        final String reportDir  = REPORT_URL + SYSTEM_ID + FS + LISTID ;
180                        String modelFile = null;
181
182        // ================== FGRUN_1,2,3 は、帳票印刷処理 ===========================================
183                        // 3.8.0.0 (2005/06/07) 実行方法(FGRUN)に、EXCEL取込関連のフラグ値を追加。Stringからintに変更
184                        if( flag && ( FGRUN.equals( FGRUN_1 ) || FGRUN.equals( FGRUN_2 ) || FGRUN.equals( FGRUN_3 ) ) ) {
185                                System.out.println( "RUN_" + FGRUN );
186                                final ReportConverter rc = new ReportConverter( SYSTEM_ID,YKNO,LISTID,reportDir,LANG,isDebug );
187
188                                // 3.8.0.0 (2005/06/07) MODELFILE のセットは、initialDataSet() で行う。
189                                flag = rc.initialDataSet();
190                                modelFile = rc.getModelFile();          // 6.4.2.1 (2016/02/05) メソッド名変更
191
192                                if( flag && ( FGRUN.equals( FGRUN_1 ) || FGRUN.equals( FGRUN_3 ) ) ) {
193                                        flag = rc.execute();
194                                }
195
196                                if( flag ) { fgkan = GE50Access.FG_OK; }
197                                errMsg.append( rc.getErrMsg() ).append( CR );
198                        }
199                        if( flag && ( FGRUN.equals( FGRUN_2 ) || FGRUN.equals( FGRUN_3 ) ) ) {
200                                System.out.println( "RUN_" + FGRUN );
201                                fgkan = null;
202                                // 3.8.0.5 (2005/08/26) デーモングループ追加
203                                final ReportPrint rp = new ReportPrint( SYSTEM_ID,YKNO,PRTID,programFile,reportDir,modelFile,outputFile,DMN_GRP,isDebug );
204                                flag = rp.execute();
205                                if( flag ) { fgkan = GE50Access.FG_OK; }
206                                errMsg.append( rp.getErrMsg() ).append( CR );
207                        }
208
209        // ================== FGRUN_4,5,6 は、EXCEL取込処理 ===========================================
210                        // 3.8.0.0 (2005/06/07) EXCEL 登録機能を追加します。
211                        final String excelinDir = EXCELIN_URL + SYSTEM_ID + FS + LISTID;
212                        if( flag && ( FGRUN.equals( FGRUN_5 ) || FGRUN.equals( FGRUN_7 ) ) ) {
213                                System.out.println( "RUN_" + FGRUN );
214                                final ExcelInsert ei = new ExcelInsert( SYSTEM_ID,YKNO,LISTID,excelinDir,isDebug );
215
216                                flag = ei.execute();
217                                if( flag ) { fgkan = GE50Access.FG_DBIN; }
218                                errMsg.append( ei.getErrMsg() ).append( CR );
219                        }
220                        if( flag && ( FGRUN.equals( FGRUN_6 ) || FGRUN.equals( FGRUN_7 ) ) ) {
221                                System.out.println( "RUN_" + FGRUN );
222                                fgkan = null;
223                                final ProgramRun pr = new ProgramRun( SYSTEM_ID,YKNO,LISTID,isDebug );
224                                flag = pr.execute();
225                                if( flag ) { fgkan = GE50Access.FG_OK; }
226                                errMsg.append( pr.getErrMsg() ).append( CR );
227                        }
228
229        // ================== FGRUN_A,B,C,D は、RFID出力処理 ===========================================
230                        if( flag && ( FGRUN.equals( FGRUN_A ) || FGRUN.equals( FGRUN_B ) || FGRUN.equals( FGRUN_C ) || FGRUN.equals( FGRUN_D ) ) ) {
231                                System.out.println( "RUN_" + FGRUN );
232                                //RFIDPrintRequest rpr = new RFIDPrintRequest( SYSTEM_ID,YKNO,LISTID,LANG,FGRUN,PRTID,isDebug );
233                                // 4.3.3.0 (2008/10/01) RFIDPrintRequestのコンストラクタ引数追加
234                                // 5.4.3.9 (2012/01/25) 雛形ファイル名は新帳票のみ対応
235                                final RFIDPrintRequest rpr = new RFIDPrintRequest( SYSTEM_ID,YKNO,LISTID,LANG,FGRUN,PRTID,PRGDIR,PRGFILE,OUT_DIR,null,isDebug );
236                                flag = rpr.initialDataSet();
237                                if( flag ) { flag = rpr.execute(); }
238                                //if( flag ) { fgkan = GE50Access.FG_RUN; }
239                                // 4.3.3.0 (2008/10/01) 完成フラグの取り方変更
240                                if( flag ) { fgkan = rpr.getFgkan(); }
241                                errMsg.append( rpr.getErrMsg() ).append( CR );
242                        }
243
244        // ================== FGRUN_G,H,I,J は、CSV出力処理 ===========================================       
245                        // 5.9.0.0 (2015/09/04)
246                        // 5.9.4.2 (2016/01/13) FGRUN_J追加
247                        if( flag && ( FGRUN.equals( FGRUN_G ) || FGRUN.equals( FGRUN_H ) || FGRUN.equals( FGRUN_I ) || FGRUN.equals( FGRUN_J ) ) ) {
248                                System.out.println( "RUN_" + FGRUN );
249                                final CSVPrintRequest rpr = new CSVPrintRequest( SYSTEM_ID,YKNO,LISTID,LANG,FGRUN,PRTID,PRGDIR,PRGFILE,OUT_DIR,null,GROUPID,DMN_GRP,isDebug );  // 5.9.2.2 (2015/11/20)
250                                flag = rpr.initialDataSet();
251                                if( flag ) { flag = rpr.execute(); }
252                                if( flag ) { fgkan = rpr.getFgkan(); }
253                                errMsg.append( rpr.getErrMsg() ).append( CR );
254                        }
255                }
256                catch( final RuntimeException ex ) {
257                        errMsg.append( "ReportProcessing Execute Error! " ).append( CR ) ;
258                        errMsg.append( "==============================" ).append( CR );
259                        errMsg.append( ThrowUtil.ogStackTrace( ex ) ).append( CR ) ;                            // 6.4.2.0 (2016/01/29)
260                        flag = false;
261                }
262
263                return flag ? fgkan : null ;    // 正常なら、fgkan を、異常なら null を返します。
264        }
265
266        /**
267         * 内部変数をクリアします。
268         * このオブジェクトを再利用する場合は、必ずこのメソッドで内部変数の
269         * クリアが必要です。
270         * 注意:このクラス自身は、同期処理は入っていません
271         *
272         * @og.rev 3.8.0.5 (2005/08/26) デーモングループ追加
273         *
274         */
275        public void clear() {
276                SYSTEM_ID       = null;
277                YKNO            = null;
278                GROUPID         = null;
279                LISTID          = null;
280                JOKEN           = null;
281                OUT_DIR         = null;         // 5.1.0.0 (2009/11/04) OUTDIR ⇒ OUT_DIR
282                OUT_FILE        = null;         // 5.1.0.0 (2009/11/04) OUTFILE ⇒ OUT_FILE
283                outputFile      = null;
284                isDebug         = false;        // 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加
285
286                FGRUN           = null;
287                PRTID           = null;
288                LANG            = null;
289                PRGDIR          = null;
290                PRGFILE         = null;
291                DMN_GRP         = null;         // 3.8.0.5 (2005/08/26)
292                programFile     = null;
293
294                errMsg.setLength(0);    // 6.1.0.0 (2014/12/26) refactoring
295        }
296
297        /**
298         * システムID(SYSTEM_ID)をセットします。
299         *
300         * @param val システムID
301         */
302        public void setSystemId( final String val ) { SYSTEM_ID = val; }
303
304        /**
305         * 要求番号(YKNO)をセットします。
306         *
307         * @param val 要求番号
308         */
309        public void setYkno( final String val ) { YKNO = val; }
310
311        /**
312         * グループID(GROUPID)をセットします。
313         *
314         * @param val グループID
315         */
316        public void setGroupId( final String val ) { GROUPID = val; }
317
318        /**
319         * 帳票ID(LISTID)をセットします。
320         *
321         * @param val 帳票ID
322         */
323        public void setListId( final String val ) { LISTID = val; }
324
325        /**
326         * 処理条件(JOKEN)をセットします。
327         *
328         * @param val 処理条件
329         */
330        public void setJoken( final String val ) { JOKEN = val; }
331
332        /**
333         * プリンターID(PRTID)をセットします。
334         *
335         * @og.rev 3.8.5.0 (2006/03/06) PRTID が指定されていれば、その値を使用する。なければ NULL
336         * @og.rev 4.3.4.4 (2009/01/01) メソッド名変更
337         *
338         * @param val プリンターID
339         */
340        public void setPrtId( final String val ) { PRTID = val; }
341
342        /**
343         * 出力先ディレクトリ(OUT_DIR)をセットします。
344         *
345         * @param val 出力先ディレクトリ
346         */
347        public void setOutDir( final String val ) { OUT_DIR = val; }
348
349        /**
350         * 出力先ファイル(OUT_FILE)をセットします。
351         *
352         * @param val 出力先ファイル
353         */
354        public void setOutFile( final String val ) { OUT_FILE = val; }
355
356        /**
357         * debugフラグをセットします。
358         *
359         * @og.rev 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加します。
360         *
361         * @param       flag    デバッグフラグ[true:デバッグ/false:通常]
362         */
363        public void setDebug( final boolean flag ) { isDebug = flag; }
364
365        /**
366         * 初期データセットを行います。
367         * ここでは、GE53 テーブルより必要な情報を取得します。
368         *
369         * @og.rev 3.8.0.5 (2005/08/26) デーモングループ追加
370         * @og.rev 3.8.5.0 (2006/03/06) プリンタIDが引数から、渡される場合の対応
371         * @og.rev 3.8.6.0 (2006/09/29) 出力先フォルダの自動生成を行います。
372         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
373         * @og.rev 5.1.0.0 (2009/11/04) OUTDIR ⇒ OUT_DIR , OUTFILE ⇒ OUT_FILE カラム名変更
374         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
375         *
376         * @param       appInfo アプリ情報オブジェクト
377         *
378         * @return      結果[true:正常/false:異常]
379         */
380        private boolean initialDataSet( final ApplicationInfo appInfo ) {
381                final String[] args = new String[] { SYSTEM_ID,JOKEN };
382                // fgrun,prtid,lang,prgdir,prgfile
383                final String[][] vals = DBUtil.dbExecute( GE53_SELECT,args,appInfo, DBID );     // 5.5.5.1 (2012/08/07)
384                if( vals == null || vals.length == 0 ) {
385                        errMsg.append( "Data does not exist in GE53 table." ).append( CR )
386                                .append( "==============================" ).append( CR )
387                                .append( "SYSTEM_ID=[" ).append( SYSTEM_ID )
388                                .append( "] , JOKEN=[" ).append( JOKEN     ).append( ']' )              // 6.0.2.5 (2014/10/31) char を append する。
389                                .append( CR );
390                        return false;
391                }
392
393                int row = 0;
394                // 検索結果が複数帰ったとき、SYSTEM_ID が 指定されている方のデータ(行)を採用する。
395                for( int i=0; i<vals.length; i++ ) {
396                        if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE53_SYSTEM_ID] ) ) { row = i; break; }
397                }
398
399                // 3.8.5.0 (2006/03/06) プリンタIDが引数から、渡される場合の対応
400                if( PRTID == null ) {
401                        PRTID = StringUtil.nval( vals[row][GE53_PRTID],PRTID   );
402                }
403
404                FGRUN   = StringUtil.nval( vals[row][GE53_FGRUN],       FGRUN   );
405                LANG    = StringUtil.nval( vals[row][GE53_LANG],        LANG    );
406                PRGDIR  = StringUtil.nval( vals[row][GE53_PRGDIR],      PRGDIR  );
407                PRGFILE = StringUtil.nval( vals[row][GE53_PRGFILE],     PRGFILE );
408                DMN_GRP = StringUtil.nval( vals[row][GE53_DMN_GRP],     DMN_GRP );      // 3.8.0.5 (2005/08/26)
409                programFile = PRGDIR + FS + PRGFILE ;
410
411                if( OUT_DIR != null  && OUT_DIR.length() > 0 &&
412                        OUT_FILE != null && OUT_FILE.length() > 0 ) {
413                                outputFile  = OUT_DIR + FS + OUT_FILE ;
414                                // 3.8.6.0 (2006/09/29) 出力先フォルダの自動生成を行います。
415                                final File dir = new File(OUT_DIR);
416                                if( ! dir.exists() && ! dir.mkdirs() ) {
417                                        final String errMsg = "ディレクトリの作成に失敗しました。[" + OUT_DIR + "]";
418                                        throw new HybsSystemException( errMsg );
419                                }
420                }
421
422                return true;
423        }
424
425        /**
426         * エラーが存在した場合に、エラーメッセージを返します。
427         *
428         * @return エラーメッセージ String
429         * @og.rtnNotNull
430         */
431        public String getErrMsg() {
432                return errMsg.toString();
433        }
434}