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.hayabusa.db.DBTableModelUtil;
020    import org.opengion.hayabusa.db.DBTableModel;
021    import org.opengion.hayabusa.resource.ResourceFactory;
022    import org.opengion.hayabusa.resource.ResourceManager;
023    import org.opengion.fukurou.util.StringUtil;
024    import org.opengion.fukurou.util.FileUtil;
025    import org.opengion.fukurou.util.ApplicationInfo;
026    import org.opengion.fukurou.db.DBUtil;
027    
028    import java.io.File;
029    
030    /**
031     * 【レポã?トå?力ã?DBTableModelオブジェクトをレポã?ト形式に返還するタグリブクラスですã?
032     * こã?オブジェクトにã€?ãƒ??タ(DBTableModel)と、コントローラ(DBTableReport クラス)を与えてã€?
033     * 外部からコントロールすることで、各種形式で ãƒ??タ(DBTableModel)を表示させることã�?
034     * 可能ですã?
035     *
036     * @og.group 帳票シスãƒ?ƒ 
037     *
038     * @version  4.0
039     * @author   Kazuhiko Hasegawa
040     * @since    JDK5.0,
041     */
042    public class ReportConverter {
043    
044            private static final String CR     = HybsSystem.CR ;
045    
046            private final StringBuilder errMsg = new StringBuilder();
047    
048            // DBTableReport に対して設定する情報
049            private String[]        headerKeys              = null;   // 固定部の key 部åˆ?‚’æŒ?®šするã?カンマでè¤?•°æŒ?®šできるã€?
050            private String[]        headerVals              = null;   // 固定部の key に対応するå?を指定するã?
051            private String[]        footerKeys              = null;   // 繰り返し部の終äº?¾Œに表示する key 部åˆ?‚’æŒ?®šするã?カンマでè¤?•°æŒ?®šできるã€?
052            private String[]        footerVals              = null;   // 繰り返し部の終äº?¾Œに表示する key に対する値を指定するã?
053            private boolean         pageEndCut              = false;  // ボディー部(繰り返し部)がなくなったときに、それ以降ã?ペã?ジをå?力するかæŒ?®šするã?
054            private File            templateFile    = null;         // 3.8.0.0 (2005/06/07)
055            private File            firstTemplateFile = null;       // 3.8.0.0 (2005/06/07)
056            private DBTableModel    table           = null;
057            private ResourceManager resource        = null;         // 4.0.0 (2005/01/31)
058    
059            // 受け渡し変数
060            private final String    SYSTEM_ID       ;
061            private final String    YKNO            ;
062            private final String    LISTID          ;
063            private final String    HTML_DIR        ;
064            private final String    LANG            ;
065            private final boolean   DEBUG           ;       // 3.8.5.0 (2006/03/06) ãƒ?ƒ�ãƒ?‚°用のフラグを追åŠ?
066    
067            // GE54 帳票定義マスタ
068            private String          modelDIR        = null;
069            private String          modelFILE       = null;
070            private String          hSQL            = null;
071            private String          fSQL            = null;
072            private String          bSQL            = null;
073            private boolean         fgLOCAL         = false;        // 0:未使用 1:使用 4.0.0 (2005/01/31)
074            private boolean         fgDIRECT        = false;        // 0:未使用 1:使用 4.0.0 (2005/01/31)
075    
076            // GE54 の帳票定義æƒ??を取得するSQLæ–?�§すã?
077            // 4.0.0 (2005/01/31) 共æœ?system_id をã?è€??
078            private static final String GE54_SELECT =
079                    "SELECT MODELDIR,MODELFILE,FGCUT,HSQL,FSQL,BSQL,FGLOCAL,FGDIRECT,SYSTEM_ID" +   // 4.0.0 (2005/01/31)
080                    " FROM GE54" +
081                    " WHERE FGJ = '1'" +
082                    " AND  SYSTEM_ID IN (?,'**')" +
083                    " AND  LISTID = ?" ;
084    
085            private static final int GE54_MODELDIR  = 0;
086            private static final int GE54_MODELFILE = 1;
087            private static final int GE54_FGCUT             = 2;
088            private static final int GE54_HSQL              = 3;
089            private static final int GE54_FSQL              = 4;
090            private static final int GE54_BSQL              = 5;
091            private static final int GE54_FGLOCAL   = 6;
092            private static final int GE54_FGDIRECT  = 7;
093            private static final int GE54_SYSTEM_ID = 8;
094    
095            /** コネクションにアプリケーションæƒ??を追記するかどã�?�‹æŒ?®?*/
096            public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
097    
098            // 3.8.7.0 (2006/12/15) アクセスログ取得ã?為,ApplicationInfoオブジェクトを設å®?
099            private final ApplicationInfo appInfo;
100            private final String DBID = HybsSystem.sys( "RESOURCE_DBID" );          // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対å¿?
101    
102            /**
103             * コンストラクター
104             * 引数を受けとって、インスタンスを作æ?しますã?
105             *
106             * @og.rev 3.0.0.4 (2003/02/26) FGRUN ã�?PRT のみのときã??ŒMODELFILE のみセãƒ?ƒˆして終äº?�™るã?
107             * @og.rev 3.8.0.0 (2005/06/07) FGRUNは?Œ使用しなã�?
108             * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得ã?為,ApplicationInfoオブジェクトを設å®?
109             *
110             * @param system_id シスãƒ?ƒ ID
111             * @param ykno 要求番号
112             * @param listId 帳票ID
113             * @param tempDir 出力ディレクトリ
114             * @param lang è¨?ª?
115             * @param debug ãƒ?ƒ�ãƒ?‚°フラグè¨?ª?
116             */
117            public ReportConverter( final String system_id, final String ykno, final String listId,
118                                                             final String tempDir,final String lang,final boolean debug ) {
119                    SYSTEM_ID       = system_id;
120                    YKNO            = ykno;
121                    LISTID          = listId;
122                    HTML_DIR        = tempDir;
123                    LANG            = lang;
124                    DEBUG           = debug;
125    
126                    // 3.8.7.0 (2006/12/15) アクセスログ取得ã?為,ApplicationInfoオブジェクトを設å®?
127                    if( USE_DB_APPLICATION_INFO ) {
128                            appInfo = new ApplicationInfo();
129                            // ユーザーID,IPアドレス,ホスト名
130                            appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
131                            // 画面ID,操ä½?プログラãƒ?D
132                            appInfo.setModuleInfo( "ReportConverter",YKNO,LISTID );
133                    }
134                    else {
135                            appInfo = null;
136                    }
137            }
138    
139            /**
140             * 変換処ç�?‚’実行しますã?
141             *
142             * @og.rev 3.0.0.4 (2003/02/26) FGRUN ã�?PRT のみのときã??ŒMODELFILE のみセãƒ?ƒˆして終äº?�™るã?
143             * @og.rev 3.5.4.9 (2004/02/25) 存在チェãƒ?‚¯エラー(原因不æ?)の暫定対å¿?
144             * @og.rev 3.8.0.0 (2005/06/07) initialDataSet() ã‚?ReportProcessing 側で呼び出しますã?
145             *
146             * @return 結果 [true:正常/false:異常]
147             */
148            public boolean execute() {
149                    System.out.print( "ReportConverter Started ... " );
150                    boolean flag = true;
151    
152                    try {
153                            // 雛形ファイルのチェãƒ?‚¯
154                            // 3.8.0.0 (2005/06/07) 存在チェãƒ?‚¯は、FileUtil.checkFile で行うã€?
155                            if( flag ) {
156                                    System.out.print( "MDL CK," );
157                                    templateFile = FileUtil.checkFile( modelDIR, modelFILE + ".html" );
158                                    flag = ( templateFile != null ) ;               // チェãƒ?‚¯の結果ã�?null ならã?見つからなかったã?
159    
160                                    if( !flag ) {
161                                            errMsg.append( "ModelFile Not Found Error !" ).append( CR );
162                                            errMsg.append( "MODELDIR=" ).append( modelDIR ).append( CR );
163                                            errMsg.append( "MODELFILE=" ).append( modelFILE + ".html" ).append( CR );
164                                    }
165                            }
166    
167                            // ファーストã?ージ雛形ファイルのチェãƒ?‚¯(なくても良ã�?
168                            // 3.8.0.0 (2005/06/07) 存在チェãƒ?‚¯は、FileUtil.checkFile で行うã€?
169                            if( flag ) {
170                                    // チェãƒ?‚¯はã€?¼‘回のみ行うã€?
171                                    firstTemplateFile = FileUtil.checkFile( modelDIR, modelFILE + "_FIRST.html",1 );
172                            }
173    
174                            // ヘッãƒ?¼Œフãƒ?‚¿, ボディー部の SQL を実行しますã?
175                            if( flag ) {
176    //                              flag = getHeaderFooter();
177                                    flag = isHeaderFooter();
178                                    if( flag ) { System.out.print( "HF SQL," ); }
179                            }
180    
181                            // 帳票変換処ç�?‚’実行しますã?
182                            if( flag ) {
183                                    flag = reportRun();
184                                    if( flag ) { System.out.print( "RPT RUN," ); }
185                            }
186                    }
187                    catch ( RuntimeException ex ) {
188                            errMsg.append( "ReportConverter Execute Exception Error!" ).append( CR );
189                            errMsg.append( "==============================" ).append( CR );
190                            errMsg.append( StringUtil.stringStackTrace( ex ) ) ;
191                            errMsg.append( CR ) ;
192                            flag = false;
193                    }
194    
195                    System.out.println( "End." );
196                    return flag ;
197            }
198    
199            /**
200             * 初期ãƒ??タセãƒ?ƒˆを行いますã?
201             * ここでは、GE54 ãƒ??ブルよりå¿?¦�なæƒ??を取得しますã?
202             *
203             * @og.rev 3.8.0.0 (2005/06/07) initialDataSet() ã‚?ReportProcessing 側で呼び出しますã?
204             * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得ã?為,ApplicationInfoオブジェクトを設å®?
205             * @og.rev 4.0.0.0 (2005/01/31) ローカルリソースフラグとãƒ?‚¤レクトアクセスフラグを追åŠ?
206             * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対ç­?
207             *
208             * @return 結果 [true:正常/false:異常]
209             */
210            public boolean initialDataSet() {
211                    String[] args = new String[] { SYSTEM_ID,LISTID };
212                    // modeldir,modelfile,fgcut,hsql,fsql,bsql,fglocal,fgdirect,system_id
213    //              String[][] vals = DBUtil.dbExecute( GE54_SELECT,args,appInfo );                         // 3.8.7.0 (2006/12/15)
214                    String[][] vals = DBUtil.dbExecute( GE54_SELECT,args,appInfo, DBID );           // 5.5.5.1 (2012/08/07)
215                    if( vals == null || vals.length == 0 ) {
216                            errMsg.append( "Data does not exist in GE54 table." ).append( CR );
217                            errMsg.append( "==============================" ).append( CR );
218                            errMsg.append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , " );
219                            errMsg.append( "LISTID=["    ).append( LISTID    ).append( "]" );
220                            errMsg.append( CR );
221                            return false;
222                    }
223    
224                    int row = 0;
225                    // 検索結果がè¤?•°帰ったときã?SYSTEM_ID ã�?æŒ?®šされてã�?‚‹方のãƒ??タ(è¡?を採用するã€?
226                    for( int i=0; i<vals.length; i++ ) {
227                            if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE54_SYSTEM_ID] ) ) { row = i; break; }
228                    }
229    
230                    modelDIR        = StringUtil.nval( vals[row][GE54_MODELDIR],modelDIR );
231                    modelFILE       = StringUtil.nval( vals[row][GE54_MODELFILE],modelFILE );
232                    pageEndCut      = StringUtil.nval( vals[row][GE54_FGCUT],pageEndCut );  // boolean タイãƒ?
233    
234                    if( DEBUG ) {
235                            System.out.println( "MODELDIR   = [" + modelDIR   + "]" );
236                            System.out.println( "MODELFILE  = [" + modelFILE  + "]" );
237                            System.out.println( "pageEndCut = [" + pageEndCut + "]" );
238                    }
239    
240                    hSQL = StringUtil.nval( vals[row][GE54_HSQL],hSQL );
241                    fSQL = StringUtil.nval( vals[row][GE54_FSQL],fSQL );
242                    bSQL = StringUtil.nval( vals[row][GE54_BSQL],bSQL );
243                    if( bSQL == null || bSQL.length() == 0 ) {
244                            errMsg.append( "Body SQL Columns does not exist in GE54 table." ).append( CR );
245                            errMsg.append( "==============================" ).append( CR );
246                            errMsg.append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , " );
247                            errMsg.append( "LISTID=["    ).append( LISTID    ).append( "] " );
248                            errMsg.append( CR );
249                            return false;
250                    }
251    
252                    // 4.0.0 (2005/01/31) ローカルリソースフラグとãƒ?‚¤レクトアクセスフラグを追åŠ?
253                    fgLOCAL = ( vals[row][GE54_FGLOCAL]  != null && vals[row][GE54_FGLOCAL].trim().equals( "1" ) ) ? true : false ;
254                    fgDIRECT= ( vals[row][GE54_FGDIRECT] != null && vals[row][GE54_FGDIRECT].trim().equals( "1" ) ) ? true : false ;
255    
256                    return true;
257            }
258    
259            /**
260             * ヘッãƒ??フッターæƒ??の取得を行いますã?
261             * GE54 の HSQL,FSQL,BSQL のコマンドを実行して、データを取得しますã?
262             * ヘッãƒ??æƒ??、フãƒ?‚¿ーæƒ??がなくても異常とは判断されませんがã?
263             * ボディæƒ??が存在しなã�??合ã?、エラーとなりますã?
264             *
265             * @og.rev 3.0.0.1 (2003/02/14) ヘッãƒ??、フãƒ?‚¿ーæƒ??ã�?null のときã?処ç�?¿½åŠ??
266             * @og.rev 3.0.1.3 (2003/03/11) 検索時ã?æœ?¤§件数での打ちåˆ?‚Šをエラーとするã€?
267             * @og.rev 3.8.7.0 (2006/12/15) アクセスログ取得ã?為,ApplicationInfoオブジェクトを設å®?
268             * @og.rev 4.0.0.0 (2005/01/31) lang â‡?ResourceManager へ変更 , メソãƒ?ƒ‰名変更
269             *
270             * @return 結果 [true:正常/false:異常]
271             */
272    //      private boolean getHeaderFooter() {
273            private boolean isHeaderFooter() {
274                    // 4.0.0 (2005/01/31) FGLOCAL æŒ?®š時は、SYSTEM_ID を指定してリソース作æ?
275                    if( fgLOCAL ) {
276                            // ローカルリソースæŒ?®š時は、SYSTEM_ID,LANG を使用しますã?先読みは、使用しませんã€?
277                            resource = ResourceFactory.newInstance( SYSTEM_ID,LANG,false ); // 4.0.0 (2005/01/31)
278                    }
279                    else {
280                            // 従来と互換性のあるモーãƒ?ローカルリソースは使用しなã�??
281                            resource = ResourceFactory.newInstance( LANG ); // 4.0.0 (2005/01/31)
282                    }
283    
284                    // 4.0.0 (2005/01/31) FGDIRECT æŒ?®š時は、where 条件は、null になりますã?
285                    String[] where = ( fgDIRECT ) ? null : new String[] { SYSTEM_ID , YKNO } ;
286    
287                    // ヘッãƒ??æƒ??の取å¾?
288                    DBTableModel header = DBTableModelUtil.makeDBTable( hSQL, where, resource, appInfo );           // 3.8.7.0 (2006/12/15)
289                    if( header != null && header.getRowCount() > 0 ) {
290                            headerKeys              = header.getNames();
291                            Object[] obj    = header.getValues(0);
292                            headerVals              = new String[obj.length];
293                            for( int i=0; i<obj.length; i++ ) {
294                                    headerVals[i] = obj[i].toString().trim();
295                            }
296                    }
297    
298                    // フッターæƒ??の取å¾?
299                    DBTableModel footer = DBTableModelUtil.makeDBTable( fSQL, where, resource ,appInfo );           // 3.8.7.0 (2006/12/15)
300                    if( footer != null && footer.getRowCount() > 0 ) {
301                            footerKeys              = footer.getNames();
302                            Object[] obj    = footer.getValues(0);
303                            footerVals              = new String[obj.length];
304                            for( int i=0; i<obj.length; i++ ) {
305                                    footerVals[i] = obj[i].toString().trim();
306                            }
307                    }
308    
309                    // ボディーæƒ??の取å¾?
310                    table = DBTableModelUtil.makeDBTable( bSQL, where, resource ,appInfo );         // 3.8.7.0 (2006/12/15)
311                    if( table.getRowCount() <= 0 ) {
312                            errMsg.append( "Database Body row count is Zero." ).append( CR );
313                            errMsg.append( "==============================" ).append( CR );
314                            errMsg.append( bSQL );
315                            errMsg.append( CR );
316                            return false;
317                    }
318                    // 3.0.1.3 (2003/03/11) 検索時ã?æœ?¤§件数での打ちåˆ?‚Šをエラーとするã€?
319                    if( table.isOverflow() ) {
320                            errMsg.append( "Database is Overflow. [" );
321                            errMsg.append( table.getRowCount() );
322                            errMsg.append( "]" ).append( CR ).append( CR );
323                            errMsg.append( "==============================" ).append( CR );
324                            errMsg.append( "Check SystemParameter Data in DB_MAX_ROW_COUNT Overflow" );
325                            errMsg.append( CR );
326                            return false;
327                    }
328    
329                    return true;
330            }
331    
332            /**
333             * 実際のレポã?ト変換処ç�?‚’行いますã?
334             *
335             * @og.rev 3.5.4.3 (2004/01/05) HTMLDBTableReport のクラス名変更ã€?
336             * @og.rev 3.6.0.0 (2004/09/17) メソãƒ?ƒ‰名ã?変更。setInputFile â‡?setTemplateFile
337             * @og.rev 3.6.1.0 (2005/01/05) 帳票?©?¤(LISTID)をセãƒ?ƒˆしますã?
338             * @og.rev 3.8.5.1 (2006/04/28) setOutputFileKey の代わりに、setYkno を使用しますã?
339             * @og.rev 4.0.0.0 (2005/01/31) lang â‡?ResourceManager へ変更
340             *
341             * @return 結果 [true:正常/false:異常]
342             */
343            private boolean reportRun() {
344                    DBTableReport report = new DBTableReport_HTML();
345    
346                    report.setDBTableModel( table );
347                    report.setTemplateFile( templateFile );                         // 3.6.0.0 (2004/09/17)
348                    report.setFirstTemplateFile( firstTemplateFile );       // 3.6.0.0 (2004/09/17)
349                    report.setOutputDir( HTML_DIR );
350                    report.setOutputFileKey( YKNO );        // 要求番号をファイル名として使用しますã?
351                    report.setYkno( YKNO );                                                         // 3.8.5.1 (2006/04/28)
352                    report.setHeaderKeys( headerKeys );
353                    report.setHeaderVals( headerVals );
354                    report.setFooterKeys( footerKeys );
355                    report.setFooterVals( footerVals );
356                    report.setPageEndCut( pageEndCut );
357                    report.setResourceManager( resource );          // 4.0.0 (2005/01/31)
358                    report.setListId( LISTID );             // 3.6.1.0 (2005/01/05)
359                    report.writeReport();
360    
361                    return true;
362            }
363    
364            /**
365             * modelFILE を返しますã?
366             *
367             * @return modelFILE�
368             */
369            public String modelFile() {
370                    return modelFILE ;
371            }
372    
373            /**
374             * エラーが存在した場合に、エラーメãƒ?‚»ージを返しますã?
375             *
376             * @return エラーメãƒ?‚»ージ String
377             */
378            public String getErrMsg() {
379                    return errMsg.toString();
380            }
381    }