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     */
016    package org.opengion.hayabusa.report;
017    
018    import org.opengion.hayabusa.common.HybsSystem;
019    import org.opengion.fukurou.util.StringUtil;
020    import org.opengion.fukurou.util.Shell;
021    import org.opengion.fukurou.util.ApplicationInfo;
022    import 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     */
045    public 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 実行ã?ログラãƒ?D
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,æ“ä½?プログラãƒ?D
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( "帳票å°åˆ·ãƒªãƒˆãƒ©ã‚¤ã«å¤±æ•—ã—ã¾ã—ãŸã€?KNO=[" + 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    }