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.fukurou.util.StringUtil;
020import org.opengion.fukurou.util.Shell;
021import org.opengion.fukurou.util.ApplicationInfo;
022import org.opengion.fukurou.db.DBUtil;
023
024/**
025 * 【レポート出力】DBTableModelオブジェクトをレポート形式に返還するタグリブクラスです。
026 * このオブジェクトに、 データ(DBTableModel)と、コントローラ(DBTableReport クラス)を与えて、
027 * 外部からコントロールすることで、各種形式で データ(DBTableModel)を表示させることが
028 * 可能です。
029 *
030 * 各属性は、{@XXXX} 変数が使用できます。
031 * これは、ServletRequest から、xxxx をキーに値を取り出し,この変数に
032 * 割り当てます。つまり、このxxxxをキーにリクエストすれば、
033 * この変数に値をセットすることができます。
034 *
035 * http://localhost/query.jsp?KEY1=VLA1&KEY2=VAL2
036 *
037 * のようなリクエストで、{@KEY1} とすれば、 VAL1 がセットされます。
038 *
039 * @og.group 帳票システム
040 *
041 * @version  4.0
042 * @author   Kazuhiko Hasegawa
043 * @since    JDK5.0,
044 */
045public class ReportPrint {
046        // 3.6.1.0 (2005/01/05) Shell の タイムアウトを設定
047        private final int TIMEOUT = HybsSystem.sysInt( "REPORT_DAEMON_TIMEOUT" );
048
049        private static final String CR     = HybsSystem.CR ;
050        private final StringBuilder errMsg = new StringBuilder();
051
052        private final String    SYSTEM_ID       ;
053        private final String    YKNO            ;
054        private final String    PRTID           ;
055        private       String    PRTNM           ;
056        private final String    programFile     ;
057        private final String    htmlDir         ;
058        private final String    modelFile       ;
059        private final String    pdfFile         ;
060        private final String    DMN_GRP         ;               // 3.8.0.5 (2005/08/26)
061        private final boolean   DEBUG           ;       // 3.8.5.0 (2006/03/06) デバッグ用のフラグを追加
062
063        private String          shellCmd        = null;
064
065        // GE55 のプリンタ名を取得するSQL文です。
066        // 4.0.0 (2005/01/31) 共有 system_id を、考慮
067        private static final String GE55_SELECT =
068                "SELECT PRTNM,SYSTEM_ID" +
069                " FROM GE55" +
070                " WHERE FGJ = '1'" +
071                " AND  SYSTEM_ID IN (?,'**')" +
072                " AND  PRTID = ?" ;
073
074        private static final int GE55_PRTNM             = 0;
075        private static final int GE55_SYSTEM_ID = 1;
076
077        /** コネクションにアプリケーション情報を追記するかどうか指定 */
078        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
079
080        // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
081        private final ApplicationInfo appInfo;
082        private final String DBID = HybsSystem.sys( "RESOURCE_DBID" );          // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対応
083
084        /**
085         * コンストラクター
086         *
087         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
088         *
089         * @param system_id システムID
090         * @param ykno 要求番号
091         * @param prtId プリンターID
092         * @param prgFile 実行プログラムID
093         * @param inDir レポート入力ディレクトリ
094         * @param mdlFile テンポラリーファイル
095         * @param outFile 出力ファイル名
096         * @param dmnGrp  デーモングループ
097         * @param debug デバッグフラグ
098         */
099        public ReportPrint( final String system_id, final String ykno, final String prtId,
100                                                final String prgFile,final String inDir,String mdlFile,
101                                                final String outFile,final String dmnGrp,final boolean debug ) {
102                SYSTEM_ID       = system_id;
103                YKNO            = ykno;
104                PRTID           = prtId;
105                programFile     = prgFile ;
106                htmlDir         = inDir ;
107                modelFile       = mdlFile ; // 4.0.1.0 (2007/12/18)
108                pdfFile         = outFile ;
109                DMN_GRP         = dmnGrp;                               // 3.8.0.5 (2005/08/26)
110                DEBUG           = debug;
111
112                // 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
113                if( USE_DB_APPLICATION_INFO ) {
114                        appInfo = new ApplicationInfo();
115                        // ユーザーID,IPアドレス,ホスト名
116                        appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
117                        // 画面ID,操作,プログラムID
118                        appInfo.setModuleInfo( "ReportPrint",YKNO,PRTID );
119                }
120                else {
121                        appInfo = null;
122                }
123        }
124
125        /**
126         * レポート出力処理を実行します。
127         *
128         * @return 結果 [true:正常/false:異常]
129         */
130        public boolean execute() {
131                System.out.print( "ReportPrint Started ... " );
132                boolean flag ;
133
134                try {
135                        flag = initialDataSet();
136                        if( flag ) { System.out.print( "INIT," ); }
137
138                        if( flag ) {
139                                flag = makeShellCommand();
140                                if( flag ) { System.out.print( "SHELL," ); }
141                        }
142
143                        if( flag ) {
144                                flag = programRun();
145                                if( flag ) { System.out.print( "RUN," ); }
146                                // 3.8.5.3 (2006/06/30) 帳票処理実行時エラーの再実行
147        //                      else {
148        //                              System.out.println();
149        //                              System.out.println( "帳票印刷時にエラーが発生しました。リトライします。" );
150        //                              System.out.println( "YKNO=[" + YKNO + "],PRTID=[" + PRTID + "],DMN_GRP=[" + DMN_GRP + "]" );
151        //                              System.out.println();
152        //                              flag = programRun();
153        //                              if( flag ) { System.out.print( "RUN 2," ); }
154        //                              else {
155        //                                      System.out.println( "帳票印刷リトライに失敗しました。YKNO=[" + YKNO + "]" );
156        //                              }
157        //                      }
158                        }
159                }
160                catch ( RuntimeException ex ) {
161                        errMsg.append( "ReportPrint Execute Error! " ).append( CR ) ;
162                        errMsg.append( "==============================" ).append( CR );
163                        errMsg.append( StringUtil.stringStackTrace( ex ) ) ;
164                        errMsg.append( CR ) ;
165                        flag = false;
166                }
167
168                System.out.println( "End." );
169                return flag ;
170        }
171
172        /**
173         * 初期データセットを行います。
174         * ここでは、GE55 テーブルより必要な情報を取得します。
175         *
176         * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得の為,ApplicationInfoオブジェクトを設定
177         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
178         *
179         * @return 結果 [true:正常/false:異常]
180         */
181        private boolean initialDataSet() {
182                if( PRTID == null ) {
183                        errMsg.append( "PRTID columns does not exist in GE55 table." ).append( CR );
184                        errMsg.append( "==============================" ).append( CR );
185                        errMsg.append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , " );
186                        errMsg.append( "PRTID=["     ).append( PRTID     ).append( "]" );
187                        errMsg.append( CR );
188                        return false;
189                }
190
191                String[] args = new String[] { SYSTEM_ID,PRTID };
192                // prtnm,system_id
193//              String[][] vals = DBUtil.dbExecute( GE55_SELECT,args,appInfo );                 // 3.8.7.0 (2006/12/15)
194                String[][] vals = DBUtil.dbExecute( GE55_SELECT,args,appInfo, DBID );   // 5.5.5.1 (2012/08/07)
195                if( vals == null || vals.length == 0 ) {
196                        errMsg.append( "Data does not exist in GE55 table." ).append( CR );
197                        errMsg.append( "==============================" ).append( CR );
198                        errMsg.append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , " );
199                        errMsg.append( "PRTID=["     ).append( PRTID     ).append( "]" );
200                        errMsg.append( CR );
201                        return false;
202                }
203
204                int row = 0;
205                // 検索結果が複数帰ったとき、SYSTEM_ID が 指定されている方のデータ(行)を採用する。
206                for( int i=0; i<vals.length; i++ ) {
207                        if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE55_SYSTEM_ID] ) ) { row = i; break; }
208                }
209
210                PRTNM = StringUtil.nval( vals[row][GE55_PRTNM],PRTNM );
211
212                if( DEBUG ) {
213                        System.out.println( "SYSTEM_ID   =" + SYSTEM_ID   );
214                        System.out.println( "YKNO        =" + YKNO        );
215                        System.out.println( "PRTID       =" + PRTID       );
216                        System.out.println( "PRTNM       =" + PRTNM       );
217                        System.out.println( "programFile =" + programFile );
218                        System.out.println( "htmlDir     =" + htmlDir     );
219                        System.out.println( "pdfFile     =" + pdfFile     );
220                        System.out.println( "DMN_GRP     =" + DMN_GRP     );
221                        System.out.println( "GE55_SELECT =" + GE55_SELECT );
222                }
223
224                return true;
225        }
226
227        /**
228         * シェルコマンドの文字列を作成します。
229         *
230         * @og.rev 3.8.0.5 (2005/08/26) 引数にデーモングループを追加
231         * @og.rev 3.8.0.8 (2005/10/03) デーモングループのデフォルト値設定
232         * @og.rev 4.0.1.0 (2007/12/18) 帳票のテンポラリーファイルを追加
233         *
234         * @return 結果 [true:正常/false:異常]
235         */
236        private boolean makeShellCommand() {
237                StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_SMALL );
238
239                buf.append( programFile ).append( " " );                // 実行するコマンド
240                buf.append( "\"" ).append( htmlDir ).append( HybsSystem.FS );           // 入力HTMLファイル
241                buf.append( YKNO ).append( "_*.html\" " );              // 入力HTMLファイル
242                buf.append( "\"" ).append( PRTNM ).append( "\" " );     // プリンタ名
243                if( pdfFile != null ) {
244                        buf.append( "\"" ).append( pdfFile ).append( "\" " );           // PDFファイル名
245                }
246                else {
247                        // ダミーファイルを指定する必要がある。
248                        buf.append( "\"" ).append( htmlDir ).append( HybsSystem.FS );
249                        buf.append( YKNO ).append( ".xls\" " );         // ダミー出力ファイル
250                }
251
252                // 3.8.5.0 (2006/03/06) EXCELをオープンするファイル名に要求番号を使う
253//              buf.append( YKNO ).append( " " );
254                if( modelFile != null ) { // 4.0.1.0 (2007/12/18)
255                        buf.append( modelFile ).append( " " );          // モデルファイル名(4)
256                }
257                else {
258                        buf.append( "DMY_MDL" ).append( " " );          // ダミーモデルファイル名(4)
259                }
260
261                // 3.8.5.0 (2006/03/06) DMN_GRPは必須
262                buf.append( DMN_GRP ).append( " " );            // デーモングループ
263
264                buf.append( PRTID );            // プリンタID(この名前.pdf でPDFファイルが作成される。)
265
266                shellCmd = buf.toString();
267                System.out.println( CR + shellCmd + CR );
268
269                return true;
270        }
271
272        /**
273         * 実際のレポート出力処理を行います。
274         *
275         * @og.rev 3.1.9.0 (2003/05/16) Shell への stdout と stderr の取得設定は廃止。ShellTag では、有効。
276         * @og.rev 3.6.1.0 (2005/01/05) Shell の タイムアウトを設定
277         *
278         * @return 結果 [true:正常/false:異常]
279         */
280        private boolean programRun() {
281                Shell shell = new Shell();
282                shell.setCommand( shellCmd,true );              // BATCHプロセスで実行する
283                shell.setWait( true );                                  // プロセスの終了を待つ
284                shell.setTimeout( TIMEOUT );                    // 3.6.1.0 (2005/01/05) Shell の タイムアウトを設定
285
286                int rtnCode = shell.exec();                             // 0 は正常終了を示す
287
288                if( rtnCode != 0 ) {
289                        errMsg.append( "Shell Command exequte Error." ).append( CR );
290                        errMsg.append( "==============================" ).append( CR );
291                        errMsg.append( shellCmd ).append( CR );
292                        errMsg.append( shell.getStdoutData() ).append( CR );
293                        errMsg.append( shell.getStderrData() ).append( CR );
294                        errMsg.append( CR );
295                        return false;
296                }
297
298                return true;
299        }
300
301        /**
302         * エラーが存在した場合に、エラーメッセージを返します。
303         *
304         * @return エラーメッセージ String
305         */
306        public String getErrMsg() {
307                return errMsg.toString();
308        }
309}