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