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.report2;
017
018import java.io.File;
019// import java.util.Arrays;                                                                             // 8.0.0.0 (2021/07/31) Delete
020
021import org.opengion.fukurou.system.ThrowUtil ;                                  // 7.1.0.0 (2020/01/27)
022import org.opengion.fukurou.system.DateSet;                                             // 6.4.2.0 (2016/01/29)
023import org.opengion.fukurou.system.LogWriter;
024import org.opengion.fukurou.system.OgRuntimeException ;                 // 6.4.2.0 (2016/01/29)
025import org.opengion.fukurou.util.StringUtil;
026import org.opengion.fukurou.util.UnicodeCorrecter;                              // 5.9.3.3 (2015/12/26) package を、mail → util に移動のため
027import org.opengion.fukurou.db.DBFunctionName;
028import org.opengion.fukurou.db.DBUtil;
029import org.opengion.fukurou.db.ApplicationInfo;
030//import org.opengion.fukurou.mail.MailTX;                                              // 7.4.4.0 (2021/06/30) Delete
031import org.opengion.hayabusa.common.HybsSystem;
032import org.opengion.hayabusa.db.DBTableModel;
033import org.opengion.hayabusa.db.DBTableModelUtil;
034import org.opengion.hayabusa.resource.ResourceFactory;
035import org.opengion.hayabusa.resource.ResourceManager;
036import static org.opengion.fukurou.system.HybsConst.CR ;                // 6.1.0.0 (2014/12/26)
037
038/**
039 * DBからキューを作成するためのクラスです。
040 * キューはGE5xテーブルから作成されます。
041 *
042 * キュー生成時点(処理スレッドにスタックした時点)では、帳票データのテーブルモデルは作成されません。
043 * 帳票データは、各スレッドからset()メソッドを呼び出したタイミングで生成されます。
044 *
045 * 処理開始及び、完了のステータスは、GE50の完成フラグに更新されます。
046 * また、エラー発生時のメッセージは、GE56に更新されます。
047 *
048 * @og.group 帳票システム
049 *
050 * @version  4.0
051 * @author   Hiroki.Nakamura
052 * @since    JDK1.6
053 */
054public final class QueueManager_DB implements QueueManager {
055
056        /** コネクションにアプリケーション情報を追記するかどうか指定 */
057        private static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
058
059        private static final String DBID = HybsSystem.sys( "RESOURCE_DBID" );           // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対応
060
061        // 4.3.7.0 (2009/06/01) HSQLDB対応
062        // 5.1.4.0 (2010/03/01) データベース名 でなく、DBID名で検索するように変更します。
063        private static final String CON = DBFunctionName.getFunctionName( "CON", null );
064
065//      // 5.2.0.0 (2010/09/01) Ver4互換モード対応
066//      // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
067//      private static final String OUT_FILE = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "OUTFILE" : "OUT_FILE";
068//      private static final String OUT_DIR = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "OUTDIR" : "OUT_DIR";
069
070        // 4.3.3.6 (2008/11/15) マルチサーバ対応追加(GE12から処理対象デーモングループ取得)
071        // 4.3.7.0 (2009/06/01) HSQLDB対応
072        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
073        // 5.4.2.0 (2011/12/26) PRTID,PRGDIR,PRGFILE取得
074        // 5.9.2.2 (2015/11/20) GROUPID追加
075        // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
076        // 5.10.0.0 (2018/06/08) FGNOML(メール不要フラグ)を追加
077        // 7.4.4.0 (2021/06/30) openGionV8事前準備(GE54.FGNOML廃止)
078        // 8.0.1.0 (2021/10/29) 未使用のGE54_TRIGを廃止(HSQL,FSQL,BSQL廃止)
079        private static final String SQL_SELECT_GE50 =
080//              "SELECT A.SYSTEM_ID, A.YKNO, A.LISTID, A."+OUT_DIR+", A."+OUT_FILE+", A.PDF_PASSWD"
081                "SELECT A.SYSTEM_ID, A.YKNO, A.LISTID, A.OUT_DIR, A.OUT_FILE, A.PDF_PASSWD"
082                + ", B.LANG, B.FGRUN, B.DMN_GRP "
083//              + ", C.MODELDIR, C.MODELFILE, D.PRTNM, C.FGLOCAL, C.FGCUT, C.BSQL, C.HSQL, C.FSQL "     // 8.0.1.0 (2021/10/29) Modify
084                + ", C.MODELDIR, C.MODELFILE, D.PRTNM, C.FGLOCAL, C.FGCUT "
085                + " ,B.PRTID, B.PRGDIR, B.PRGFILE "
086                + " ,A.GROUPID "                                                        // 5.9.2.2 (2015/11/20)
087//              + " ,C.FGNOML "                                                         // 5.10.0.0 (2018/06/08)
088        //      + " , '0' as FGNOML "                                           // 5.10.0.0 (2018/06/08) , 6.9.8.1 (2018/06/11) 互換性の関係で、とりあえず、'0' を設定 7.4.4.0 (2021/06/30) Delete
089                + "FROM GE50 A "
090                + "INNER JOIN GE53 B "
091                + "ON A.SYSTEM_ID = B.SYSTEM_ID AND A.JOKEN = B.JOKEN "
092                + "INNER JOIN GE54 C "
093                + "ON A.SYSTEM_ID = C.SYSTEM_ID AND A.LISTID = C.LISTID "
094                + "LEFT OUTER JOIN GE55 D "
095                + "ON B.SYSTEM_ID = D.SYSTEM_ID AND B.PRTID = D.PRTID "
096                + "WHERE A.FGKAN='1' "
097                + "AND EXISTS ( SELECT 'X' FROM GE12 E "
098                +                               "WHERE  E.FGJ                           ='1' "
099                +                               "AND            E.SYSTEM_ID     = '"
100                +                               HybsSystem.sys( "SYSTEM_ID" )
101                +                               "' "
102                +                               "AND            E.CONTXT_PATH   = '"
103                +                               HybsSystem.sys( "HOST_URL" )
104                +                               "' "
105                +                               "AND            E.PARAM_ID              LIKE 'REPORT2_HANDLE_DAEMON_%' "
106                +                               "AND            E.PARAM                 = 'RUN_'" + CON + "A.SYSTEM_ID" + CON + "'_'" + CON + "B.DMN_GRP"
107                +                       ") "
108                + "ORDER BY "
109                + HybsSystem.sys( "REPORT_DAEMON_ORDER_BY" );
110
111        // 5.1.2.0 (2010/01/01) ページ数、データ数をGE50に更新する。
112        private static final String SQL_UPDATE_GE50 =
113                "UPDATE GE50 SET FGKAN = ?, DMN_NAME = ?, DMN_HOST = ?, SUDATA = ?, SUPAGE = ?, DYUPD = ? WHERE SYSTEM_ID = ? AND YKNO = ?";
114
115        // 7.1.0.0 (2020/01/27) GE50のUPDATEでエラー時に、最小限の情報のみ更新する。
116        private static final String SQL_UPDATE_GE50_ERR =
117                "UPDATE GE50 SET FGKAN = ?, DYUPD = ? WHERE SYSTEM_ID = ? AND YKNO = ?";
118
119        private static final String SQL_INSERT_GE56 =
120                "INSERT INTO GE56 ( FGJ, SYSTEM_ID, YKNO, ERRMSG, DYSET, DYUPD, USRSET, USRUPD, PGUPD ) "
121                + " VALUES ( '1', ?, ? ,? ,? ,? ,? ,? ,? )" ;
122
123        private static final int STATUS_COMPLETE        = 2;                    // 済
124        private static final int STATUS_EXECUTE         = 3;                    // 実行中
125        private static final int STATUS_ERROR           = 8;                    // アプリエラー
126
127        private static QueueManager manager = new QueueManager_DB();
128
129        /** アプリケーション情報 */
130        private static final ApplicationInfo APP_INFO;          // 6.4.1.1 (2016/01/16) appInfo → APP_INFO refactoring
131        static {
132                if( USE_DB_APPLICATION_INFO ) {
133                        APP_INFO = new ApplicationInfo();
134                        // ユーザーID,IPアドレス,ホスト名
135                        APP_INFO.setClientInfo( "ReportDaemon", HybsSystem.HOST_ADRS, HybsSystem.HOST_NAME );
136                        // 画面ID,操作,プログラムID
137                        APP_INFO.setModuleInfo( "ReportDaemon", "QueueManager", "QueueManager" );
138                }
139                else {
140                        APP_INFO = null;
141                }
142        }
143
144        /**
145         * インスタンスの生成を禁止します。
146         */
147        private QueueManager_DB() {}
148
149        /**
150         * インスタンスを返します。
151         *
152         * @return      帳票処理キューの管理マネージャ
153         */
154        public static QueueManager getInstance() {
155                return manager;
156        }
157
158        /**
159         * 帳票処理キューを作成します。
160         *
161         * @og.rev 4.3.0.0 (2008/07/15) スレッドIDにシステムIDを付加します。
162         * @og.rev 5.1.2.0 (2010/01/01) HSQL,FSQL,BSQLのセットを廃止します。(このクラス内でデータを直接分割)
163         * @og.rev 5.4.3.0 (2011/12/26) PRTIDの取得
164         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
165         * @og.rev 6.3.9.0 (2015/11/06) Use block level rather than method level synchronization.(PMD)
166         * @og.rev 5.9.2.2 (2015/11/20) GrpId,DmnGrp 追加
167         * @og.rev 5.10.0.0 (2018/06/08) FGNOML対応
168         * @og.rev 5.10.9.2 (2019/03/15) 5.10.9.0の対応修正
169         * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(GE54.FGNOML廃止)
170         * @og.rev 8.0.1.0 (2021/10/29) 未使用のGE54_TRIGを廃止(HSQL,FSQL,BSQL廃止)
171         */
172        public void create() {
173                // キューをスタックするまでの例外は、ScheduleTagでcatchされデーモンがスリープする。
174                final String[][] ge50vals = DBUtil.dbExecute( SQL_SELECT_GE50, new String[0], APP_INFO, DBID ); // 5.5.5.1 (2012/08/07)
175
176                // 6.3.9.0 (2015/11/06) 元々のsynchronizedの必要性が分からないが、帳票なのでとりあえず入れておきます。
177                synchronized( ge50vals ) {
178                        for( int i=0; i<ge50vals.length; i++ ) {
179                                final ExecQueue queue = new ExecQueue();
180                                queue.setSystemId(      ge50vals[i][0] );
181                                queue.setYkno(          ge50vals[i][1] );
182                                queue.setListId(        ge50vals[i][2] );
183//                              queue.setOutputName( new File( ge50vals[i][3] ).getAbsolutePath() , ge50vals[i][4] , ge50vals[i][7] , ge50vals[i][1] ); // 4.3.0.0 (2008/07/18) 要求番号を出力ファイル名に利用
184                                queue.setOutputName( HybsSystem.url2absPath( ge50vals[i][3] ) , ge50vals[i][4] , ge50vals[i][7] , ge50vals[i][1] );             // 5.10.9.2 (2019/03/15)
185                                queue.setPdfPasswd( ge50vals[i][5] );
186                                queue.setLang(          ge50vals[i][6] );
187                                queue.setOutputType( ge50vals[i][7] );
188                                queue.setThreadId(      ge50vals[i][0] + "_" + StringUtil.nval( ge50vals[i][8] , "_DEFALUT_" ) );       // 4.3.0.0 (2008/07/15)
189//                              queue.setTemplateName( new File( ge50vals[i][9] ).getAbsolutePath() + File.separator + ge50vals[i][10] );
190                                queue.setTemplateName( HybsSystem.url2absPath( ge50vals[i][9] ) + File.separator + ge50vals[i][10]);    // 5.10.9.2 (2019/03/15)
191                                queue.setPrinterName( ge50vals[i][11] );
192                                queue.setFglocal(       "1".equals( ge50vals[i][12] ) );
193                                queue.setFgcut(         "1".equals( ge50vals[i][13] ) );
194
195//                              queue.setPrtId(         ge50vals[i][17] );              // 5.4.3.0 8.0.1.0 (2021/10/29) Modify
196                                queue.setPrtId(         ge50vals[i][14] );
197//                              queue.setPrgDir(        ge50vals[i][18] );              // 5.4.3.0 8.0.1.0 (2021/10/29) Modify
198                                queue.setPrgDir(        ge50vals[i][15] );
199//                              queue.setPrgFile(       ge50vals[i][19] );              // 5.4.3.0 8.0.1.0 (2021/10/29) Modify
200                                queue.setPrgFile(       ge50vals[i][16] );
201
202//                              queue.setGrpId(         ge50vals[i][20] );              // 5.9.2.2 (2015/11/20) 8.0.1.0 (2021/10/29) Modify
203                                queue.setGrpId(         ge50vals[i][17] );
204                                queue.setDmnGrp(        ge50vals[i][8]  );              // 5.9.2.2 (2015/11/20)
205        //                      queue.setFgnoml(        ge50vals[i][21] );              // 5.10.0.0 (2018/06/08) 7.4.4.0 (2021/06/30) Delete
206
207                                queue.setManager( this );
208
209                                ExecThreadManager.insertQueue( queue );
210                        }
211                }
212        }
213
214        /**
215         * 帳票処理データをキューにセットします。
216         *
217         * @og.rev 5.1.2.0 (2010/01/01) HSQL,FSQL,BSQLのセットを廃止します。(このクラス内でデータを直接分割)
218         *
219         * @param       queue   ExecQueueオブジェクト
220         */
221        public void set( final ExecQueue queue ) {
222                final String systemId   = queue.getSystemId();
223                final String lang               = queue.getLang();
224                final String listId             = queue.getListId();
225                final String ykno               = queue.getYkno();
226
227                ResourceManager resource = null;
228                if( queue.isFglocal() ) {
229                        resource = ResourceFactory.newInstance( systemId, lang, false );
230                }
231                else {
232                        resource = ResourceFactory.newInstance( lang );
233                }
234
235                // ヘッダー情報の取得
236                final DBTableModel header = new DBTableModelCreator( systemId, listId, ykno, "H", resource ).getTable();
237
238                if( header != null && header.getRowCount() > 0 ) {
239                        queue.setHeader( header );
240                }
241
242                // フッター情報の取得
243                final DBTableModel footer = new DBTableModelCreator( systemId, listId, ykno, "F", resource ).getTable();
244                if( footer != null && footer.getRowCount() > 0 ) {
245                        queue.setFooter( footer );
246                }
247
248                // ボディー情報の取得
249                final DBTableModel body = new DBTableModelCreator( systemId, listId, ykno, "B", resource ).getTable();
250                // レイアウトテーブルがないと固定長を分割するSQL文が設定されず、DBTableModelがnullになる
251                if( body == null ) {
252//                      queue.addMsg( "[ERROR] DBTableModel doesn't exists! maybe Layout-Table(GE52) is not configured..." + CR );
253                        queue.addMsg( "[ERROR] DBTableModel doesn't exists! maybe Layout-Table(GE52) or Report-Data(GE51) is not configured..." + CR ); // 5.10.19.0 (2019/12/27)
254                        queue.setError();
255                        throw new OgRuntimeException();
256                }
257                if( body.getRowCount() <= 0 ) {
258                        queue.addMsg( "[ERROR] Database Body row count is Zero." + ykno + CR );
259                        queue.setError();
260                        throw new OgRuntimeException();
261                }
262                if( body.isOverflow() ) {
263                        queue.addMsg( "[ERROR]Database is Overflow. [" + body.getRowCount() + "]" + CR );
264                        queue.addMsg( "[ERROR]Check SystemParameter Data in DB_MAX_ROW_COUNT Overflow" + CR  );
265                        queue.setError();
266                        throw new OgRuntimeException();
267                }
268                queue.setBody( body );
269        }
270
271        /**
272         * キューを実行中の状態に更新します。
273         *
274         * @param       queue   ExecQueueオブジェクト
275         */
276        public void execute( final ExecQueue queue ) {
277                status( queue, STATUS_EXECUTE );
278        }
279
280        /**
281         * キューを完了済の状態に更新します。
282         *
283         * @param       queue   ExecQueueオブジェクト
284         */
285        public void complete( final ExecQueue queue ) {
286                status( queue, STATUS_COMPLETE );
287        }
288
289        /**
290         * キューをエラーの状態に更新します。
291         *
292         * @param       queue   ExecQueueオブジェクト
293         */
294        public void error( final ExecQueue queue ) {
295                status( queue, STATUS_ERROR );
296                insertErrorMsg( queue );
297        }
298
299        /**
300         * GE50の状況Cを更新します。
301         *
302         * @og.rev 4.2.4.1 (2008/07/09) 更新日時をセット
303         * @og.rev 5.1.2.0 (2010/01/01) 行数、ページ数も更新する
304         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
305         * @og.rev 6.4.2.0 (2016/01/29) DateSet.getDate( String ) を利用するように修正します。
306         * @og.rev 7.1.0.0 (2020/01/27) ここでDB登録エラーが発生すると実行中のまま止まるので、エラー処理を入れます。
307         *
308         * @param       queue   ExecQueueオブジェクト
309         * @param       status  状況C
310         */
311        private void status( final ExecQueue queue, final int status ) {
312
313                final String dyupd = DateSet.getDate( "yyyyMMddHHmmss" ) ;                      // 6.4.2.0 (2016/01/29)
314
315                final String[] args = new String[] {
316                          String.valueOf( status )                                              // FGKAN
317                        , queue.getThreadId()                                                   // DMN_NAME
318                        , HybsSystem.sys( "HOST_NAME" )                                 // DMN_HOST
319                        , String.valueOf( queue.getExecRowCnt() )               // SUDATA
320                        , String.valueOf( queue.getExecPagesCnt() )             // SUPAGE
321                        , dyupd                                                                                 // DYUPD
322                        , queue.getSystemId()                                                   // SYSTEM_ID
323                        , queue.getYkno()                                                               // YKNO
324                };
325
326                // 7.1.0.0 (2020/01/27) ここでDB登録エラーが発生すると実行中のまま止まるので、エラー処理を入れます。
327                try {
328                        DBUtil.dbExecute( SQL_UPDATE_GE50, args, APP_INFO, DBID );      // 5.5.5.1 (2012/08/07)
329                }
330                catch( final Throwable th ) {
331                        final String errMsg = "状況Cを更新時にエラーが発生しました。" + CR ;
332                        queue.addMsg( errMsg );
333                        queue.addMsg( ThrowUtil.ogStackTrace( th ) );
334                        LogWriter.log( errMsg );
335                        LogWriter.log( th );
336
337                        final String[] args2 = new String[] {
338                                  String.valueOf( STATUS_ERROR )                        // FGKAN
339                                , dyupd                                                                         // DYUPD
340                                , queue.getSystemId()                                           // SYSTEM_ID
341                                , queue.getYkno()                                                       // YKNO
342                        };
343
344                        DBUtil.dbExecute( SQL_UPDATE_GE50_ERR, args2, APP_INFO, DBID );
345
346                        if( status != STATUS_ERROR ) {
347                                insertErrorMsg( queue );                                        // エラー時以外は、エラーテーブルに書き込む。
348                        }
349                }
350        }
351
352        /**
353         * GE56にエラーメッセージを出力します。
354         *
355         * @og.rev 4.4.0.1 (2009/08/08) エラーメッセージ機能追加
356         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
357         * @og.rev 6.4.2.0 (2016/01/29) DateSet.getDate( String ) を利用するように修正します。
358         * @og.rev 5.10.0.0 (2018/06/08) メール送信条件を追加
359         * @og.rev 6.9.8.1 (2018/06/11) 帳票エラーメールの改修(メール不要フラグ=trueで不要、falseが必要)
360         * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(GE54.FGNOML廃止)
361         *
362         * @param       queue   ExecQueueオブジェクト
363         */
364        private void insertErrorMsg( final ExecQueue queue ) {
365                String errmsg = queue.getMsg();
366                if( errmsg.length() > 1300 ) {
367                        errmsg = errmsg.substring( errmsg.length() - 1300, errmsg.length() );
368                }
369
370                final String dyset = DateSet.getDate( "yyyyMMddHHmmss" ) ;                      // 6.4.2.0 (2016/01/29)
371
372                final String[] args
373                = new String[]{ queue.getSystemId(), queue.getYkno(), errmsg
374                                , dyset, dyset, "UNKNOWN", "UNKNOWN", "UNKNOWN" };
375
376                DBUtil.dbExecute( SQL_INSERT_GE56, args, APP_INFO, DBID );      // 5.5.5.1 (2012/08/07)
377
378//              sendMail( queue, errmsg ); // 4.4.0.1 (2009/08/08)
379                // 5.10.0.0 (2018/06/08) メール送信条件を追加
380//              if(!"1".equals(queue.getFgnoml())){
381                // 7.4.4.0 (2021/06/30) Delete
382        //      if( ! queue.isFgnoml() ) {              // 6.9.8.1 (2018/06/11)
383        //              sendMail( queue, errmsg );      // 4.4.0.1 (2009/08/08)
384        //      }
385        }
386
387        // /**
388        // * エラー情報のメール送信を行います。
389        // * エラーメールは、システムパラメータ の COMMON_MAIL_SERVER(メールサーバー)と
390        // * ERROR_MAIL_FROM_USER(エラーメール発信元)と、ERROR_MAIL_TO_USERS(エラーメール受信者)
391        // * がすべて設定されている場合に、送信されます。
392        // *
393        // * @og.rev 4.4.0.1 (2009/08/08) 追加
394        // * @og.rev 5.7.0.4 (2013/11/29) listIdの絞込み
395        // * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(GE54.FGNOML廃止)
396        // *
397        // * @param     queue           ExecQueueオブジェクト
398        // * @param     inErrMsg        エラーメッセージ
399        // */
400        // private void sendMail( final ExecQueue queue, final String inErrMsg ) {
401
402        //      final String   host = HybsSystem.sys( "COMMON_MAIL_SERVER" );
403        //      final String   from = HybsSystem.sys( "ERROR_MAIL_FROM_USER" );
404        //      final String[] to = StringUtil.csv2Array( HybsSystem.sys( "ERROR_MAIL_TO_USERS" ) );
405        //      final String   match_txt = HybsSystem.sys( "REPORT_ERRMAIL_REGEX" ); // 5.7.0.4 (2013/11/29)
406        //      if( host != null && from != null && to.length > 0 ) {
407        //              if( match_txt == null || match_txt.isEmpty()
408        //                              || queue.getListId() == null || queue.getListId().isEmpty()
409        //                              || queue.getListId().matches( match_txt )){     // 5.7.0.4 (2013/11/29)
410        //                      // 5.7.0.4 (2013/11/29) listid追加
411        //                      final String subject = "SYSTEM_ID=[" + queue.getSystemId() + "] , YKNO=[" + queue.getYkno() + "] , "
412        //                                                 + "THREAD_ID=[" + queue.getThreadId() + "] , DMN_HOST=[" + HybsSystem.HOST_NAME + "]"
413        //                                                 + "LISTID=["+ queue.getListId() + "]";
414        //                      try {
415        //                              final MailTX tx = new MailTX( host );
416        //                              tx.setFrom( from );
417        //                              tx.setTo( to );
418        //                              tx.setSubject( "帳票エラー:" + subject );
419        //                              tx.setMessage( inErrMsg );
420        //                              tx.sendmail();
421        //                      }
422        //                      catch( final Throwable ex ) {
423        //                              final String errMsg = "エラー時メール送信に失敗しました。" + CR
424        //                                                      + " SUBJECT:" + subject                                 + CR
425        //                                                      + " HOST:" + host                                               + CR
426        //                                                      + " FROM:" + from                                               + CR
427        //                                                      + " TO:"   + Arrays.toString( to )              + CR
428        //                                                      + ex.getMessage();              // 5.1.8.0 (2010/07/01) errMsg 修正
429        //                              LogWriter.log( errMsg );
430        //                              LogWriter.log( ex );
431        //                      }
432        //              }
433        //      }
434        // }
435
436        /**
437         * 帳票明細データを帳票レイアウトテーブルに従って分割し、その結果をDBTableModelとして
438         * 生成します。
439         * データの分割は、バイト数ベースで行われるため、エンコードを正しく指定する必要があります。
440         * エンコード指定は、システムリソースのDB_ENCODEで指定します。
441         *
442         * レイアウトテーブルが存在しない場合、又は、帳票データが存在しない場合、DBTableModelは
443         * nullで返されます。
444         *
445         * @og.rev 6.9.0.2 (2018/02/13) GE51の検索順(order by)を追加します。
446         */
447        public static final class DBTableModelCreator {
448//              // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
449//              // 5.2.0.0 (2010/09/01) Ver4互換モード対応
450//              private static final String CLM = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "COLUMN_NAME" : "CLM";
451//              private static final String TEXT_DATA = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "TEXT" : "TEXT_DATA";
452
453                // 5.2.0.0 (2010/09/01) Ver4互換モード対応
454                // 5.4.4.3 (2012/02/09) FGUSE条件追加対応
455                // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
456                private static final String SQL_SELECT_GE52 =
457//                      " select " + CLM + ", START_POS, USE_LENGTH"
458                        " select CLM, START_POS, USE_LENGTH"
459                        + " from GE52"
460                        + " where SYSTEM_ID = ?"
461                        + " and LISTID = ?"
462                        + " and KBTEXT = ?"
463                        + " and FGJ = '1'"
464                        + " and FGUSE = '1'" // 5.4.4.3
465                        + " order by SEQ";
466
467                // 5.2.0.0 (2010/09/01) Ver4互換モード対応
468                // 6.9.0.2 (2018/02/13) GE51の検索順(order by)を追加します。
469                // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
470                private static final String SQL_SELECT_GE51 =
471//                      " select " + TEXT_DATA
472                        " select TEXT_DATA"
473                        + " from GE51"
474                        + " where SYSTEM_ID = ?"
475                        + " and YKNO = ?"
476                        + " and KBTEXT = ?"
477                        + " and FGJ = '1'"
478                        + " order by SYSTEM_ID,YKNO,EDNO" ;                             // 6.9.0.2 (2018/02/13)
479
480                private static final String ENCODE = HybsSystem.sys( "DB_ENCODE" );
481
482                private final String systemId;
483                private final String listId;
484                private final String ykno;
485                private final String kbtext;
486                private final ResourceManager resource;
487
488                private DBTableModel table      ;
489
490                /**
491                 * コンストラクタです。
492                 *
493                 * @param sid システムID
494                 * @param lid 帳票ID
495                 * @param yk 要求NO
496                 * @param kt テキスト区分(H:ヘッダー F:フッター B:ボディー)
497                 * @param res リソースマネージャー
498                 */
499                public DBTableModelCreator( final String sid, final String lid, final String yk, final String kt, final ResourceManager res ) {
500                        systemId        = sid;
501                        listId          = lid;
502                        ykno            = yk;
503                        kbtext          = kt;
504                        resource        = res;
505                        create();
506                }
507
508                /**
509                 * 帳票データをレイアウト定義に従い分割します。
510                 *
511                 * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
512                 * @og.rev 5.9.3.1 (2015/12/18) SJIS-UTF変換時の波ダッシュ問題対応
513                 */
514                private void create() {
515                        final String[] ge52Where = new String[] { systemId, listId, kbtext } ;
516                        final String[][] ge52Vals = DBUtil.dbExecute( SQL_SELECT_GE52, ge52Where, APP_INFO, DBID );     // 5.5.5.1 (2012/08/07)
517                        if( ge52Vals == null || ge52Vals.length == 0 ) {
518                                return;
519                        }
520
521                        final String[] ge51Where = new String[] { systemId, ykno, kbtext } ;
522                        final String[][] ge51Vals = DBUtil.dbExecute( SQL_SELECT_GE51, ge51Where, APP_INFO, DBID );     // 5.5.5.1 (2012/08/07)
523                        if( ge51Vals == null || ge51Vals.length == 0 ) {
524                                return;
525                        }
526
527                        String[] clms = new String[ge52Vals.length];
528                        for( int i=0; i<ge52Vals.length; i++ ) {
529                                clms[i] = ge52Vals[i][0];
530                        }
531
532                        String[][] vals = new String[ge51Vals.length][ge52Vals.length];
533                        for( int i=0; i<ge51Vals.length; i++ ) {
534                                final byte[] bytes = StringUtil.makeByte( UnicodeCorrecter.correctToCP932( ge51Vals[i][0], ENCODE ), ENCODE ); // 5.9.3.1 (2015/12/18)
535                                for( int j=0; j<ge52Vals.length; j++ ) {
536                                        final int strpos = Integer.parseInt( ge52Vals[j][1] ) - 1;      // 6.0.2.4 (2014/10/17) メソッド間違い
537                                        int len = Integer.parseInt( ge52Vals[j][2] );                           // 6.0.2.4 (2014/10/17) メソッド間違い
538                                        if( strpos >= bytes.length ) {
539                                                vals[i][j] = "";
540                                        }
541                                        else {
542                                                if( strpos + len > bytes.length ) {
543                                                        len = bytes.length - strpos;
544                                                }
545                                                vals[i][j] = StringUtil.rTrim( StringUtil.makeString( bytes, strpos, len, ENCODE ) );
546                                        }
547                                }
548                        }
549                        table = DBTableModelUtil.makeDBTable( clms, vals, resource );
550                }
551
552                /**
553                 * 分割後のDBTableModelを返します。
554                 *
555                 * @return 分割後のDBTableModel
556                 */
557                public DBTableModel getTable() {
558                        return table;
559                }
560        }
561}