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         *
138         * @return 結果 (FGKAN_XX エラー時は、null)
139         */
140        public String execute() {
141                System.out.println();
142                System.out.println( "Process Start:" + HybsSystem.getDate( "yyyy/MM/dd HH:mm:ss" ) );
143                System.out.println( "  SYSTEM_ID=[" + SYSTEM_ID + "] YKNO=[" + YKNO + "] GROUPID=[" + GROUPID + "]" );
144                System.out.println( "  PRTID=[" + PRTID + "] LISTID=[" + LISTID + "] JOKEN=[" + JOKEN + "]" );
145                String  fgkan = null;
146
147                // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
148                if( USE_DB_APPLICATION_INFO ) {
149                        appInfo = new ApplicationInfo();
150                        // ユーザーID,IPアドレス,ホスト名
151                        appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
152                        // 画面ID,操作,プログラムID
153                        appInfo.setModuleInfo( "ReportProcessing",YKNO,LISTID );
154                }
155                else {
156                        appInfo = null;
157                }
158
159                boolean flag ;
160                try {
161                        flag = initialDataSet( appInfo );
162                        System.out.print( "INIT=" + flag + "," );
163                        String reportDir  = REPORT_URL + SYSTEM_ID + HybsSystem.FS + LISTID ;
164                        String modelFile = null;
165
166        // ================== FGRUN_1,2,3 は、帳票印刷処理 ===========================================
167                        // 3.8.0.0 (2005/06/07) 実行方法(FGRUN)に、EXCEL取込関連のフラグ値を追加。Stringからintに変更
168                        if( flag && ( FGRUN.equals( FGRUN_1 ) || FGRUN.equals( FGRUN_2 ) || FGRUN.equals( FGRUN_3 ) ) ) {
169                                System.out.println( "RUN_" + FGRUN );
170                                ReportConverter rc = new ReportConverter( SYSTEM_ID,YKNO,LISTID,reportDir,LANG,isDebug );
171
172                                // 3.8.0.0 (2005/06/07) MODELFILE のセットは、initialDataSet() で行う。
173                                flag = rc.initialDataSet();
174                                modelFile = rc.modelFile(); // 4.0.1.0 (2007/12/18)
175
176                                if( flag && ( FGRUN.equals( FGRUN_1 ) || FGRUN.equals( FGRUN_3 ) ) ) {
177                                        flag = rc.execute();
178                                }
179
180                                if( flag ) { fgkan = GE50Access.FG_OK; }
181                                errMsg.append( rc.getErrMsg() ).append( CR );
182                        }
183                        if( flag && ( FGRUN.equals( FGRUN_2 ) || FGRUN.equals( FGRUN_3 ) ) ) {
184                                System.out.println( "RUN_" + FGRUN );
185                                fgkan = null;
186                                // 3.8.0.5 (2005/08/26) デーモングループ追加
187                                ReportPrint rp = new ReportPrint( SYSTEM_ID,YKNO,PRTID,programFile,reportDir,modelFile,outputFile,DMN_GRP,isDebug );
188                                flag = rp.execute();
189                                if( flag ) { fgkan = GE50Access.FG_OK; }
190                                errMsg.append( rp.getErrMsg() ).append( CR );
191                        }
192
193        // ================== FGRUN_4,5,6 は、EXCEL取込処理 ===========================================
194                        // 3.8.0.0 (2005/06/07) EXCEL 登録機能を追加します。
195                        String excelinDir = EXCELIN_URL + SYSTEM_ID + HybsSystem.FS + LISTID;
196                        if( flag && ( FGRUN.equals( FGRUN_5 ) || FGRUN.equals( FGRUN_7 ) ) ) {
197                                System.out.println( "RUN_" + FGRUN );
198                                ExcelInsert ei = new ExcelInsert( SYSTEM_ID,YKNO,LISTID,excelinDir,isDebug );
199
200                                flag = ei.execute();
201                                if( flag ) { fgkan = GE50Access.FG_DBIN; }
202                                errMsg.append( ei.getErrMsg() ).append( CR );
203                        }
204                        if( flag && ( FGRUN.equals( FGRUN_6 ) || FGRUN.equals( FGRUN_7 ) ) ) {
205                                System.out.println( "RUN_" + FGRUN );
206                                fgkan = null;
207                                ProgramRun pr = new ProgramRun( SYSTEM_ID,YKNO,LISTID,isDebug );
208                                flag = pr.execute();
209                                if( flag ) { fgkan = GE50Access.FG_OK; }
210                                errMsg.append( pr.getErrMsg() ).append( CR );
211                        }
212
213        // ================== FGRUN_A,B,C,D は、RFID出力処理 ===========================================
214                        if( flag && ( FGRUN.equals( FGRUN_A ) || FGRUN.equals( FGRUN_B ) || FGRUN.equals( FGRUN_C ) || FGRUN.equals( FGRUN_D ) ) ) {
215                                System.out.println( "RUN_" + FGRUN );
216                                //RFIDPrintRequest rpr = new RFIDPrintRequest( SYSTEM_ID,YKNO,LISTID,LANG,FGRUN,PRTID,isDebug );
217                                // 4.3.3.0 (2008/10/01) RFIDPrintRequestのコンストラクタ引数追加
218                                // 5.4.3.9 (2012/01/25) 雛形ファイル名は新帳票のみ対応
219                                RFIDPrintRequest rpr = new RFIDPrintRequest( SYSTEM_ID,YKNO,LISTID,LANG,FGRUN,PRTID,PRGDIR,PRGFILE,OUT_DIR,null,isDebug );
220                                flag = rpr.initialDataSet();
221                                if( flag ) { flag = rpr.execute(); }
222                                //if( flag ) { fgkan = GE50Access.FG_RUN; }
223                                // 4.3.3.0 (2008/10/01) 完成フラグの取り方変更
224                                if( flag ) { fgkan = rpr.getFgkan(); }
225                                errMsg.append( rpr.getErrMsg() ).append( CR );
226                        }
227                        
228                        // 5.9.0.0 (2015/09/04)
229                        // ================== FGRUN_G,H,I は、CSV出力処理 =========================================== 
230                        if( flag && ( FGRUN.equals( FGRUN_G ) || FGRUN.equals( FGRUN_H ) || FGRUN.equals( FGRUN_I ) ) ) {
231                                System.out.println( "RUN_" + FGRUN );
232                                CSVPrintRequest rpr = new CSVPrintRequest( SYSTEM_ID,YKNO,LISTID,LANG,FGRUN,PRTID,PRGDIR,PRGFILE,OUT_DIR,null,isDebug );
233                                flag = rpr.initialDataSet();
234                                if( flag ) { flag = rpr.execute(); }
235                                if( flag ) { fgkan = rpr.getFgkan(); }
236                                errMsg.append( rpr.getErrMsg() ).append( CR );
237                        }
238                }
239                catch ( RuntimeException ex ) {
240                        errMsg.append( "ReportProcessing Execute Error! " ).append( CR ) ;
241                        errMsg.append( "==============================" ).append( CR );
242                        errMsg.append( StringUtil.stringStackTrace( ex ) ).append( CR ) ;
243                        flag = false;
244                }
245
246                return flag ? fgkan : null ;    // 正常なら、fgkan を、異常なら null を返します。
247        }
248
249        /**
250         * 内部変数をクリアします。
251         * このオブジェクトを再利用する場合は、必ずこのメソッドで内部変数の
252         * クリアが必要です。
253         * 注意:このクラス自身は、同期処理は入っていません
254         *
255         * @og.rev 3.8.0.5 (2005/08/26) デーモングループ追加
256         *
257         */
258        public void clear() {
259                SYSTEM_ID       = null;
260                YKNO            = null;
261                GROUPID         = null;
262                LISTID          = null;
263                JOKEN           = null;
264                OUT_DIR         = null;         // 5.1.0.0 (2009/11/04) OUTDIR ⇒ OUT_DIR
265                OUT_FILE        = null;         // 5.1.0.0 (2009/11/04) OUTFILE ⇒ OUT_FILE
266                outputFile      = null;
267                isDebug         = false;        // 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加
268
269                FGRUN           = null;
270                PRTID           = null;
271                LANG            = null;
272                PRGDIR          = null;
273                PRGFILE         = null;
274                DMN_GRP         = null;         // 3.8.0.5 (2005/08/26)
275                programFile     = null;
276
277                errMsg          = new StringBuilder() ;
278        }
279
280        /**
281         * システムID(SYSTEM_ID)をセットします。
282         *
283         * @param val システムID
284         */
285        public void setSystemId( final String val ) { SYSTEM_ID = val; }
286
287        /**
288         * 要求番号(YKNO)をセットします。
289         *
290         * @param val 要求番号
291         */
292        public void setYkno( final String val ) { YKNO = val; }
293
294        /**
295         * グループID(GROUPID)をセットします。
296         *
297         * @param val グループID
298         */
299        public void setGroupId( final String val ) { GROUPID = val; }
300
301        /**
302         * 帳票ID(LISTID)をセットします。
303         *
304         * @param val 帳票ID
305         */
306        public void setListId( final String val ) { LISTID = val; }
307
308        /**
309         * 処理条件(JOKEN)をセットします。
310         *
311         * @param val 処理条件
312         */
313        public void setJoken( final String val ) { JOKEN = val; }
314
315        /**
316         * プリンターID(PRTID)をセットします。
317         *
318         * @og.rev 3.8.5.0 (2006/03/06) PRTID が指定されていれば、その値を使用する。なければ NULL
319         * @og.rev 4.3.4.4 (2009/01/01) メソッド名変更
320         *
321         * @param val プリンターID
322         */
323        public void setPrtId( final String val ) { PRTID = val; }
324
325        /**
326         * 出力先ディレクトリ(OUT_DIR)をセットします。
327         *
328         * @param val 出力先ディレクトリ
329         */
330        public void setOutDir( final String val ) { OUT_DIR = val; }
331
332        /**
333         * 出力先ファイル(OUT_FILE)をセットします。
334         *
335         * @param val 出力先ファイル
336         */
337        public void setOutFile( final String val ) { OUT_FILE = val; }
338
339        /**
340         * debugフラグをセットします。
341         *
342         * @og.rev 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加します。
343         *
344         * @param       flag    デバッグフラグ[true:デバッグ/false:通常]
345         */
346        public void setDebug( final boolean flag ) { isDebug = flag; }
347
348        /**
349         * 初期データセットを行います。
350         * ここでは、GE53 テーブルより必要な情報を取得します。
351         *
352         * @og.rev 3.8.0.5 (2005/08/26) デーモングループ追加
353         * @og.rev 3.8.5.0 (2006/03/06) プリンタIDが引数から、渡される場合の対応
354         * @og.rev 3.8.6.0 (2006/09/29) 出力先フォルダの自動生成を行います。
355         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
356         * @og.rev 5.1.0.0 (2009/11/04) OUTDIR ⇒ OUT_DIR , OUTFILE ⇒ OUT_FILE カラム名変更
357         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
358         *
359         * @param       appInfo アプリ情報オブジェクト
360         *
361         * @return      結果[true:正常/false:異常]
362         */
363        private boolean initialDataSet( final ApplicationInfo appInfo ) {
364                String[] args = new String[] { SYSTEM_ID,JOKEN };
365                // fgrun,prtid,lang,prgdir,prgfile
366//              String[][] vals = DBUtil.dbExecute( GE53_SELECT,args,appInfo );                 // 3.8.7.0 (2006/12/15)
367                String[][] vals = DBUtil.dbExecute( GE53_SELECT,args,appInfo, DBID );   // 5.5.5.1 (2012/08/07)
368                if( vals == null || vals.length == 0 ) {
369                        errMsg.append( "Data does not exist in GE53 table." ).append( CR );
370                        errMsg.append( "==============================" ).append( CR );
371                        errMsg.append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , " );
372                        errMsg.append( "JOKEN=["     ).append( JOKEN     ).append( "]" );
373                        errMsg.append( CR );
374                        return false;
375                }
376
377                int row = 0;
378                // 検索結果が複数帰ったとき、SYSTEM_ID が 指定されている方のデータ(行)を採用する。
379                for( int i=0; i<vals.length; i++ ) {
380                        if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE53_SYSTEM_ID] ) ) { row = i; break; }
381                }
382
383                // 3.8.5.0 (2006/03/06) プリンタIDが引数から、渡される場合の対応
384                if( PRTID == null ) {
385                        PRTID = StringUtil.nval( vals[row][GE53_PRTID],PRTID   );
386                }
387
388                FGRUN   = StringUtil.nval( vals[row][GE53_FGRUN],       FGRUN   );
389                LANG    = StringUtil.nval( vals[row][GE53_LANG],        LANG    );
390                PRGDIR  = StringUtil.nval( vals[row][GE53_PRGDIR],      PRGDIR  );
391                PRGFILE = StringUtil.nval( vals[row][GE53_PRGFILE],     PRGFILE );
392                DMN_GRP = StringUtil.nval( vals[row][GE53_DMN_GRP],     DMN_GRP );      // 3.8.0.5 (2005/08/26)
393                programFile = PRGDIR + HybsSystem.FS + PRGFILE ;
394
395                if( ( OUT_DIR != null  && OUT_DIR.length() > 0 ) &&
396                        ( OUT_FILE != null && OUT_FILE.length() > 0 ) ) {
397                                outputFile  = OUT_DIR + HybsSystem.FS + OUT_FILE ;
398                                // 3.8.6.0 (2006/09/29) 出力先フォルダの自動生成を行います。
399                                File dir = new File(OUT_DIR);
400                                if( ! dir.exists() && ! dir.mkdirs() ) {
401                                        String errMsg = "ディレクトリの作成に失敗しました。[" + OUT_DIR + "]";
402                                        throw new HybsSystemException( errMsg );
403                                }
404                }
405
406                return true;
407        }
408
409        /**
410         * エラーが存在した場合に、エラーメッセージを返します。
411         *
412         * @return エラーメッセージ String
413         */
414        public String getErrMsg() {
415                return errMsg.toString();
416        }
417}