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        @Override       // QueueManager
169        public void create() {
170                // キューをスタックするまでの例外は、ScheduleTagでcatchされデーモンがスリープする。
171//              final String[][] ge50vals = DBUtil.dbExecute( SQL_SELECT_GE50, new String[0], APP_INFO, DBID ); // 5.5.5.1 (2012/08/07)
172                final String[][] ge50vals = DBUtil.dbExecute( SQL_SELECT_GE50, null, APP_INFO, DBID );                  // 8.0.3.0 (2021/12/17)
173
174                // 6.3.9.0 (2015/11/06) 元々のsynchronizedの必要性が分からないが、帳票なのでとりあえず入れておきます。
175                synchronized( ge50vals ) {
176//                      for( int i=0; i<ge50vals.length; i++ ) {
177                        for( final String[] ge50row : ge50vals ) {
178                                final ExecQueue queue = new ExecQueue();
179                                queue.setSystemId(      ge50row[0] );   // [0]:SYSTEM_ID
180                                queue.setYkno(          ge50row[1] );   // [1]:YKNO
181                                queue.setListId(        ge50row[2] );   // [2]:LISTID
182                                                                                                                //              [3]:OUT_DIR [4]:OUT_FILE  [7]:FGRUN  [1]:YKNO
183                                queue.setOutputName( HybsSystem.url2absPath(ge50row[3]),ge50row[4],ge50row[7],ge50row[1]);      // 5.10.9.2 (2019/03/15)
184                                queue.setPdfPasswd( ge50row[5] );       // [5]:PDF_PASSWD
185                                queue.setLang(          ge50row[6] );   // [6]:LANG
186                                queue.setOutputType(ge50row[7] );       // [7]:FGRUN
187                                                                                // [0]:SYSTEM_ID                                           [8]:DMN_GRP
188                                queue.setThreadId(      ge50row[0] + "_" + StringUtil.nval( ge50row[8] , "_DEFALUT_" ) );       // 4.3.0.0 (2008/07/15)
189                                                                                                        //                                [9]:MODELDIR            [10]:MODELFILE
190                                queue.setTemplateName( HybsSystem.url2absPath( ge50row[9] ) + FS + ge50row[10]);                // 8.0.3.0 (2021/12/17)
191                                queue.setPrinterName( ge50row[11] );                            // [11]:PRTNM
192                                queue.setFglocal(       "1".equals( ge50row[12] ) );    // [12]:FGLOCAL
193                                queue.setFgcut(         "1".equals( ge50row[13] ) );    // [13]:FGCUT
194
195//                              queue.setSystemId(      ge50vals[i][0] );       // [0]:SYSTEM_ID
196//                              queue.setYkno(          ge50vals[i][1] );       // [1]:YKNO
197//                              queue.setListId(        ge50vals[i][2] );       // [2]:LISTID
198//                                                                                                              //                         [3]:OUT_DIR     [4]:OUT_FILE   [7]:FGRUN      [1]:YKNO
199//                              queue.setOutputName( HybsSystem.url2absPath(ge50vals[i][3]),ge50vals[i][4],ge50vals[i][7],ge50vals[i][1]);      // 5.10.9.2 (2019/03/15)
200//                              queue.setPdfPasswd( ge50vals[i][5] );   // [5]:PDF_PASSWD
201//                              queue.setLang(          ge50vals[i][6] );       // [6]:LANG
202//                              queue.setOutputType(ge50vals[i][7] );   // [7]:FGRUN
203//                                                                                      // [0]:SYSTEM_ID                                                   [8]:DMN_GRP
204//                              queue.setThreadId(      ge50vals[i][0] + "_" + StringUtil.nval( ge50vals[i][8] , "_DEFALUT_" ) );       // 4.3.0.0 (2008/07/15)
205//                                                                                                              //                                [9]:MODELDIR                    [10]:MODELFILE
206//                              queue.setTemplateName( HybsSystem.url2absPath( ge50vals[i][9] ) + FS + ge50vals[i][10]);                // 8.0.3.0 (2021/12/17)
207//                              queue.setPrinterName( ge50vals[i][11] );        // [11]:PRTNM
208//                              queue.setFglocal(       "1".equals( ge50vals[i][12] ) );        // [12]:FGLOCAL
209//                              queue.setFgcut(         "1".equals( ge50vals[i][13] ) );        // [13]:FGCUT
210
211//                              queue.setPrtId(         ge50vals[i][17] );              // 5.4.3.0 8.0.1.0 (2021/10/29) Modify
212        //                      queue.setPrtId(         ge50vals[i][14] );              // 8.0.3.0 (2021/12/17) Delete
213//                              queue.setPrgDir(        ge50vals[i][18] );              // 5.4.3.0 8.0.1.0 (2021/10/29) Modify
214        //                      queue.setPrgDir(        ge50vals[i][15] );              // 8.0.3.0 (2021/12/17) Delete
215//                              queue.setPrgFile(       ge50vals[i][19] );              // 5.4.3.0 8.0.1.0 (2021/10/29) Modify
216        //                      queue.setPrgFile(       ge50vals[i][16] );              // 8.0.3.0 (2021/12/17) Delete
217
218//                              queue.setGrpId(         ge50vals[i][20] );              // 5.9.2.2 (2015/11/20) 8.0.1.0 (2021/10/29) Modify
219        //                      queue.setGrpId(         ge50vals[i][17] );              // 8.0.3.0 (2021/12/17) Delete
220        //                      queue.setDmnGrp(        ge50vals[i][8]  );              // 5.9.2.2 (2015/11/20) 8.0.3.0 (2021/12/17) Delete
221        //                      queue.setFgnoml(        ge50vals[i][21] );              // 5.10.0.0 (2018/06/08) 7.4.4.0 (2021/06/30) Delete
222
223                                queue.setManager( this );
224
225                                ExecThreadManager.insertQueue( queue );
226                        }
227                }
228        }
229
230        /**
231         * 帳票処理データをキューにセットします。
232         *
233         * @og.rev 5.1.2.0 (2010/01/01) HSQL,FSQL,BSQLのセットを廃止します。(このクラス内でデータを直接分割)
234         *
235         * @param       queue   ExecQueueオブジェクト
236         */
237        @Override       // QueueManager
238        public void set( final ExecQueue queue ) {
239                final String systemId   = queue.getSystemId();
240                final String lang               = queue.getLang();
241                final String listId             = queue.getListId();
242                final String ykno               = queue.getYkno();
243
244                ResourceManager resource = null;
245                if( queue.isFglocal() ) {
246                        resource = ResourceFactory.newInstance( systemId, lang, false );
247                }
248                else {
249                        resource = ResourceFactory.newInstance( lang );
250                }
251
252                // ヘッダー情報の取得
253                final DBTableModel header = new DBTableModelCreator( systemId, listId, ykno, "H", resource ).getTable();
254                if( header != null && header.getRowCount() > 0 ) {
255                        queue.setHeader( header );
256                }
257
258                // フッター情報の取得
259                final DBTableModel footer = new DBTableModelCreator( systemId, listId, ykno, "F", resource ).getTable();
260                if( footer != null && footer.getRowCount() > 0 ) {
261                        queue.setFooter( footer );
262                }
263
264                // ボディー情報の取得 (8.0.3.0 (2021/12/17))
265//              final DBTableModel body = new DBTableModelCreator( systemId, listId, ykno, "B", resource ).getTable();
266                final DBTableModelCreator creator = new DBTableModelCreator( systemId, listId, ykno, "B", resource );
267                final DBTableModel body = creator.getTable();
268                // レイアウトテーブルがないと固定長を分割するSQL文が設定されず、DBTableModelがnullになる
269                if( body == null ) {
270//                      queue.addMsg( "[ERROR] DBTableModel doesn't exists! maybe Layout-Table(GE52) is not configured..." + CR );
271                        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)
272                        queue.setError();
273                        throw new OgRuntimeException();
274                }
275                if( body.getRowCount() <= 0 ) {
276                        queue.addMsg( "[ERROR]QM;Database Body row count is Zero." + ykno );
277                        queue.setError();
278                        throw new OgRuntimeException();
279                }
280                if( body.isOverflow() ) {
281                        queue.addMsg( "[ERROR]QM;Database is Overflow. [" + body.getRowCount() + "]" );
282                        queue.addMsg( "[ERROR]QM;Check SystemParameter Data in DB_MAX_ROW_COUNT Overflow" );
283                        queue.setError();
284                        throw new OgRuntimeException();
285                }
286                queue.setBody( body );
287                queue.setBodyTypes( creator.getBodyTypes() );           // 8.0.3.0 (2021/12/17)
288        }
289
290        /**
291         * キューを実行中の状態に更新します。
292         *
293         * @param       queue   ExecQueueオブジェクト
294         */
295        @Override       // QueueManager
296        public void execute( final ExecQueue queue ) {
297                status( queue, STATUS_EXECUTE );
298        }
299
300        /**
301         * キューを完了済の状態に更新します。
302         *
303         * @param       queue   ExecQueueオブジェクト
304         */
305        @Override       // QueueManager
306        public void complete( final ExecQueue queue ) {
307                status( queue, STATUS_COMPLETE );
308        }
309
310        /**
311         * キューをエラーの状態に更新します。
312         *
313         * @param       queue   ExecQueueオブジェクト
314         */
315        @Override       // QueueManager
316        public void error( final ExecQueue queue ) {
317                status( queue, STATUS_ERROR );
318                insertErrorMsg( queue );
319        }
320
321        /**
322         * GE50の状況Cを更新します。
323         *
324         * @og.rev 4.2.4.1 (2008/07/09) 更新日時をセット
325         * @og.rev 5.1.2.0 (2010/01/01) 行数、ページ数も更新する
326         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
327         * @og.rev 6.4.2.0 (2016/01/29) DateSet.getDate( String ) を利用するように修正します。
328         * @og.rev 7.1.0.0 (2020/01/27) ここでDB登録エラーが発生すると実行中のまま止まるので、エラー処理を入れます。
329         *
330         * @param       queue   ExecQueueオブジェクト
331         * @param       status  状況C
332         */
333        private void status( final ExecQueue queue, final int status ) {
334
335                final String dyupd = DateSet.getDate( "yyyyMMddHHmmss" ) ;                      // 6.4.2.0 (2016/01/29)
336
337                final String[] args = new String[] {
338                          String.valueOf( status )                                              // FGKAN
339                        , queue.getThreadId()                                                   // DMN_NAME
340                        , HybsSystem.sys( "HOST_NAME" )                                 // DMN_HOST
341                        , String.valueOf( queue.getExecRowCnt() )               // SUDATA
342                        , String.valueOf( queue.getExecPagesCnt() )             // SUPAGE
343                        , dyupd                                                                                 // DYUPD
344                        , queue.getSystemId()                                                   // SYSTEM_ID
345                        , queue.getYkno()                                                               // YKNO
346                };
347
348                // 7.1.0.0 (2020/01/27) ここでDB登録エラーが発生すると実行中のまま止まるので、エラー処理を入れます。
349                try {
350                        DBUtil.dbExecute( SQL_UPDATE_GE50, args, APP_INFO, DBID );      // 5.5.5.1 (2012/08/07)
351                }
352                catch( final Throwable th ) {
353                        final String errMsg = "状況Cを更新時にエラーが発生しました。";
354                        queue.addMsg( errMsg );
355                        queue.addMsg( ThrowUtil.ogStackTrace( th ) );
356                        LogWriter.log( errMsg );
357                        LogWriter.log( th );
358
359                        final String[] args2 = new String[] {
360                                  String.valueOf( STATUS_ERROR )                        // FGKAN
361                                , dyupd                                                                         // DYUPD
362                                , queue.getSystemId()                                           // SYSTEM_ID
363                                , queue.getYkno()                                                       // YKNO
364                        };
365
366                        DBUtil.dbExecute( SQL_UPDATE_GE50_ERR, args2, APP_INFO, DBID );
367
368                        if( status != STATUS_ERROR ) {
369                                insertErrorMsg( queue );                                        // エラー時以外は、エラーテーブルに書き込む。
370                        }
371                }
372        }
373
374        /**
375         * GE56にエラーメッセージを出力します。
376         *
377         * @og.rev 4.4.0.1 (2009/08/08) エラーメッセージ機能追加
378         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
379         * @og.rev 6.4.2.0 (2016/01/29) DateSet.getDate( String ) を利用するように修正します。
380         * @og.rev 5.10.0.0 (2018/06/08) メール送信条件を追加
381         * @og.rev 6.9.8.1 (2018/06/11) 帳票エラーメールの改修(メール不要フラグ=trueで不要、falseが必要)
382         * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(GE54.FGNOML廃止)
383         *
384         * @param       queue   ExecQueueオブジェクト
385         */
386        private void insertErrorMsg( final ExecQueue queue ) {
387                String errmsg = queue.getMsg();
388                if( errmsg.length() > 1300 ) {
389                        errmsg = errmsg.substring( errmsg.length() - 1300, errmsg.length() );
390                }
391
392                final String dyset = DateSet.getDate( "yyyyMMddHHmmss" ) ;                      // 6.4.2.0 (2016/01/29)
393
394                final String[] args = new String[]{ queue.getSystemId(), queue.getYkno(), errmsg
395                                                                                        , dyset, dyset, "UNKNOWN", "UNKNOWN", "UNKNOWN" };
396
397                DBUtil.dbExecute( SQL_INSERT_GE56, args, APP_INFO, DBID );      // 5.5.5.1 (2012/08/07)
398
399//              sendMail( queue, errmsg ); // 4.4.0.1 (2009/08/08)
400                // 5.10.0.0 (2018/06/08) メール送信条件を追加
401//              if(!"1".equals(queue.getFgnoml())){
402                // 7.4.4.0 (2021/06/30) Delete
403        //      if( ! queue.isFgnoml() ) {              // 6.9.8.1 (2018/06/11)
404        //              sendMail( queue, errmsg );      // 4.4.0.1 (2009/08/08)
405        //      }
406        }
407
408        // /**
409        // * エラー情報のメール送信を行います。
410        // * エラーメールは、システムパラメータ の COMMON_MAIL_SERVER(メールサーバー)と
411        // * ERROR_MAIL_FROM_USER(エラーメール発信元)と、ERROR_MAIL_TO_USERS(エラーメール受信者)
412        // * がすべて設定されている場合に、送信されます。
413        // *
414        // * @og.rev 4.4.0.1 (2009/08/08) 追加
415        // * @og.rev 5.7.0.4 (2013/11/29) listIdの絞込み
416        // * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(GE54.FGNOML廃止)
417        // *
418        // * @param     queue           ExecQueueオブジェクト
419        // * @param     inErrMsg        エラーメッセージ
420        // */
421        // private void sendMail( final ExecQueue queue, final String inErrMsg ) {
422
423        //      final String   host = HybsSystem.sys( "COMMON_MAIL_SERVER" );
424        //      final String   from = HybsSystem.sys( "ERROR_MAIL_FROM_USER" );
425        //      final String[] to = StringUtil.csv2Array( HybsSystem.sys( "ERROR_MAIL_TO_USERS" ) );
426        //      final String   match_txt = HybsSystem.sys( "REPORT_ERRMAIL_REGEX" ); // 5.7.0.4 (2013/11/29)
427        //      if( host != null && from != null && to.length > 0 ) {
428        //              if( match_txt == null || match_txt.isEmpty()
429        //                              || queue.getListId() == null || queue.getListId().isEmpty()
430        //                              || queue.getListId().matches( match_txt )){     // 5.7.0.4 (2013/11/29)
431        //                      // 5.7.0.4 (2013/11/29) listid追加
432        //                      final String subject = "SYSTEM_ID=[" + queue.getSystemId() + "] , YKNO=[" + queue.getYkno() + "] , "
433        //                                                 + "THREAD_ID=[" + queue.getThreadId() + "] , DMN_HOST=[" + HybsSystem.HOST_NAME + "]"
434        //                                                 + "LISTID=["+ queue.getListId() + "]";
435        //                      try {
436        //                              final MailTX tx = new MailTX( host );
437        //                              tx.setFrom( from );
438        //                              tx.setTo( to );
439        //                              tx.setSubject( "帳票エラー:" + subject );
440        //                              tx.setMessage( inErrMsg );
441        //                              tx.sendmail();
442        //                      }
443        //                      catch( final Throwable ex ) {
444        //                              final String errMsg = "エラー時メール送信に失敗しました。" + CR
445        //                                                      + " SUBJECT:" + subject                                 + CR
446        //                                                      + " HOST:" + host                                               + CR
447        //                                                      + " FROM:" + from                                               + CR
448        //                                                      + " TO:"   + Arrays.toString( to )              + CR
449        //                                                      + ex.getMessage();              // 5.1.8.0 (2010/07/01) errMsg 修正
450        //                              LogWriter.log( errMsg );
451        //                              LogWriter.log( ex );
452        //                      }
453        //              }
454        //      }
455        // }
456
457        /**
458         * 帳票明細データを帳票レイアウトテーブルに従って分割し、その結果をDBTableModelとして
459         * 生成します。
460         * データの分割は、バイト数ベースで行われるため、エンコードを正しく指定する必要があります。
461         * エンコード指定は、システムリソースのDB_ENCODEで指定します。
462         *
463         * レイアウトテーブルが存在しない場合、又は、帳票データが存在しない場合、DBTableModelは
464         * nullで返されます。
465         *
466         * @og.rev 6.9.0.2 (2018/02/13) GE51の検索順(order by)を追加します。
467         * @og.rev 8.0.3.0 (2021/12/17) create()とgetTable()を一体化する。
468         */
469        public static final class DBTableModelCreator {
470//              // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
471//              // 5.2.0.0 (2010/09/01) Ver4互換モード対応
472//              private static final String CLM = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "COLUMN_NAME" : "CLM";
473//              private static final String TEXT_DATA = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "TEXT" : "TEXT_DATA";
474
475                // 5.2.0.0 (2010/09/01) Ver4互換モード対応
476                // 5.4.4.3 (2012/02/09) FGUSE条件追加対応
477                // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
478                private static final String SQL_SELECT_GE52 =
479//                      " select " + CLM + ", START_POS, USE_LENGTH"
480                        " select CLM, START_POS, USE_LENGTH"
481                        + " from GE52"
482                        + " where SYSTEM_ID = ?"
483                        + " and LISTID = ?"
484                        + " and KBTEXT = ?"                                                             // ※ GE52の KBTEXT は、like検索しない。
485                        + " and FGJ = '1'"
486                        + " and FGUSE = '1'"                                                    // 5.4.4.3
487                        + " order by SEQ";
488
489                // 5.2.0.0 (2010/09/01) Ver4互換モード対応
490                // 6.9.0.2 (2018/02/13) GE51の検索順(order by)を追加します。
491                // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
492                private static final String SQL_SELECT_GE51 =
493//                      " select " + TEXT_DATA
494                        " select KBTEXT,TEXT_DATA"
495                        + " from GE51"
496                        + " where SYSTEM_ID = ?"
497                        + " and YKNO = ?"
498//                      + " and KBTEXT = ?"
499                        + " and KBTEXT like ? || '%'"                                   // 8.0.3.0 (2021/12/17) B+数値を検索する
500                        + " and FGJ = '1'"
501                        + " order by SYSTEM_ID,YKNO,EDNO" ;                             // 6.9.0.2 (2018/02/13)
502
503                private static final String ENCODE = HybsSystem.sys( "DB_ENCODE" );
504
505                private String[] kbtxt ;                                                        // 8.0.3.0 (2021/12/17)
506
507                private final String systemId;
508                private final String listId;
509                private final String ykno;
510                private final String kbtext;
511                private final ResourceManager resource;
512
513//              private DBTableModel table      ;
514
515                /**
516                 * コンストラクタです。
517                 *
518                 * @param sid システムID
519                 * @param lid 帳票ID
520                 * @param yk 要求NO
521                 * @param kt テキスト区分(H:ヘッダー F:フッター B:ボディー)
522                 * @param res リソースマネージャー
523                 */
524                public DBTableModelCreator( final String sid, final String lid, final String yk, final String kt, final ResourceManager res ) {
525                        systemId        = sid;
526                        listId          = lid;
527                        ykno            = yk;
528                        kbtext          = kt;
529                        resource        = res;
530//                      create();
531                }
532
533                /**
534                 * 帳票データをレイアウト定義に従い分割しDBTableModelを返します。
535                 *
536                 * @og.rev 8.0.3.0 (2021/12/17) create()とgetTable()を一体化する。
537                 *
538                 * @return 分割後のDBTableModel
539                 */
540//              private void create() {
541                public DBTableModel getTable() {
542                        final String[] ge52Where = new String[] { systemId, listId, kbtext } ;
543                        final String[][] ge52Vals = DBUtil.dbExecute( SQL_SELECT_GE52, ge52Where, APP_INFO, DBID );     // 5.5.5.1 (2012/08/07)
544                        if( ge52Vals == null || ge52Vals.length == 0 ) {
545                                return null;
546                        }
547
548                        final String[] ge51Where = new String[] { systemId, ykno, kbtext } ;
549                        final String[][] ge51Vals = DBUtil.dbExecute( SQL_SELECT_GE51, ge51Where, APP_INFO, DBID );     // 5.5.5.1 (2012/08/07)
550                        if( ge51Vals == null || ge51Vals.length == 0 ) {
551                                return null;
552                        }
553
554                        String[] clms = new String[ge52Vals.length];
555                        for( int j=0; j<ge52Vals.length; j++ ) {
556                                clms[j] = ge52Vals[j][0];                               // [0]:CLM
557                        }
558
559                        String[][] vals = new String[ge51Vals.length][ge52Vals.length];
560                        kbtxt = new String[ge51Vals.length];
561                        for( int i=0; i<ge51Vals.length; i++ ) {
562                                kbtxt[i] = ge51Vals[i][0];              // [0]:KBTEXT   8.0.3.0 (2021/12/17)
563                                                                                                                                                                                                //    [1]:TEXT_DATA
564                                final byte[] bytes = StringUtil.makeByte( UnicodeCorrecter.correctToCP932( ge51Vals[i][1], ENCODE ), ENCODE ); // 5.9.3.1 (2015/12/18)
565                                // [0]:CLM, [1]:START_POS, [2]:USE_LENGTH
566                                for( int j=0; j<ge52Vals.length; j++ ) {
567                                        final int strpos = Integer.parseInt( ge52Vals[j][1] ) - 1;      // [1]:START_POS
568                                        int len = Integer.parseInt( ge52Vals[j][2] );                           // [2]:USE_LENGTH
569                                        if( strpos >= bytes.length ) {
570                                                vals[i][j] = "";
571                                        }
572                                        else {
573                                                if( strpos + len > bytes.length ) {
574                                                        len = bytes.length - strpos;
575                                                }
576                                                vals[i][j] = StringUtil.rTrim( StringUtil.makeString( bytes, strpos, len, ENCODE ) );
577                                        }
578                                }
579                        }
580//                      table = DBTableModelUtil.makeDBTable( clms, vals, resource );
581                        return DBTableModelUtil.makeDBTable( clms, vals, resource );
582                }
583
584                /**
585                 * GE51のKBTEXT配列を返します。
586                 *
587                 * @og.rev 8.0.3.0 (2021/12/17) KBTEXT配列を返します。
588                 *
589                 * @return GE51のKBTEXT配列
590                 */
591                public String[] getBodyTypes() {
592                        return kbtxt;
593                }
594
595//              /**
596//               * 分割後のDBTableModelを返します。
597//               *
598//               * @og.rev 8.0.3.0 (2021/12/17) create()とgetTable()を一体化する。
599//               *
600//               * @return 分割後のDBTableModel
601//               */
602//              public DBTableModel getTable() {
603//                      return table;
604//              }
605        }
606}