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