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