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.report;
017
018import java.util.concurrent.ConcurrentMap;                                                              // 6.4.3.3 (2016/03/04)
019import java.util.concurrent.ConcurrentHashMap;                                                  // 6.4.3.1 (2016/02/12) refactoring
020
021import org.opengion.fukurou.db.DBUtil;
022import org.opengion.fukurou.db.ApplicationInfo;
023import org.opengion.fukurou.util.StringUtil;
024import org.opengion.hayabusa.common.HybsSystem;
025import org.opengion.hayabusa.db.DBTableModel;
026import org.opengion.hayabusa.report2.QueueManager_DB.DBTableModelCreator;
027import org.opengion.hayabusa.resource.ResourceFactory;
028import org.opengion.hayabusa.resource.ResourceManager;
029import static org.opengion.fukurou.system.HybsConst.CR ;                                // 6.1.0.0 (2014/12/26)
030import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;              // 6.1.0.0 (2014/12/26) refactoring
031
032/**
033 * DBTableModelオブジェクトをレポート形式に返還するタグリブクラスです。
034 * このオブジェクトに、 データ(DBTableModel)と、コントローラ(DBTableReport クラス)を与えて、
035 * 外部からコントロールすることで、各種形式で データ(DBTableModel)を表示させることが
036 * 可能です。
037 *
038 * @og.group 帳票システム
039 *
040 * @version  4.0
041 * @author   Hiroki Nakamura
042 * @since    JDK5.0,
043 */
044public class RFIDPrintRequest {
045        private final StringBuilder errMsg = new StringBuilder( BUFFER_MIDDLE );
046
047        // DBTableModel に対して設定する情報
048        private ResourceManager resource        ;
049
050        // 受け渡し変数
051        private final String    SYSTEM_ID       ;
052        private final String    YKNO            ;
053        private final String    LISTID          ;
054        private final String    LANG            ;
055        private final String    FGRUN           ;
056        private final String    PRTID           ;
057        private final String    PRGDIR          ;               // 4.3.3.0 (2008/10/01) 板金RFID対応で追加
058        private final String    PRGFILE         ;               // 4.3.3.0 (2008/10/01) 追加
059        private final String    OUTDIR          ;               // 4.3.3.0 (2008/10/01) 追加
060        private final boolean   DEBUG           ;
061        private final String    FILENAME        ;               // 5.4.3.9 (2012/01/25)
062
063        // オブジェクト変数
064        private boolean         fgLOCAL         ;
065        private String          prtName         ;
066        private String          hostName        ;
067        private String          portnm          ;
068        private String          fgkan           = GE50Access.FG_ERR2;   // 4.3.3.0 (2008/10/01) 初期値エラー
069        /** 6.4.3.1 (2016/02/12) PMD refactoring. HashMap → ConcurrentHashMap に置き換え。  */
070        private final ConcurrentMap<String,String[]> rfidLayoutMap = new ConcurrentHashMap<>();         // 6.4.3.3 (2016/03/04)
071
072        // GE54 の帳票定義情報を取得するSQL文です。
073        private static final String GE54_SELECT =
074                "SELECT BSQL,FGLOCAL,SYSTEM_ID" +
075                " FROM GE54" +
076                " WHERE FGJ = '1'" +
077                " AND  SYSTEM_ID IN (?,'**')" +
078                " AND  LISTID = ?" ;
079        private static final int GE54_FGLOCAL   = 1;
080        private static final int GE54_SYSTEM_ID = 2;
081
082//      // 5.4.4.0 (2012/02/01) 互換モード対応漏れ
083//      // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
084//      private static final String HOST = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "HOST" : "HOST_ID";
085
086        // GE55 の出力先マスタ情報を取得するSQL文です。
087        // 5.1.0.0 (2009/11/04) HOST ⇒ HOST_ID
088        // 5.4.3.1 (2011/12/27) PORTNM
089        // 5.4.4.0 (2012/02/01) 互換モード
090        // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
091        private static final String GE55_SELECT =
092//              "SELECT PRTNM,"+HOST+",SYSTEM_ID,PORTNM" +
093                "SELECT PRTNM,HOST_ID,SYSTEM_ID,PORTNM" +
094                " FROM GE55" +
095                " WHERE FGJ = '1'" +
096                " AND  SYSTEM_ID IN (?,'**')" +
097                " AND  PRTID = ?" ;
098        private static final int GE55_PRTNM             = 0;
099        private static final int GE55_HOST_ID   = 1;
100        private static final int GE55_SYSTEM_ID = 2;
101        private static final int GE55_PORTNM    = 3;
102
103//      // 5.2.0.0 (2010/09/01) Ver4互換モード対応
104//      // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
105//      private static final String CLM = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "COLUMN_NAME" : "CLM";
106
107        // GE58 の帳票レイアウトテーブルを取得するSQL文です。
108        // 5.2.0.0 (2010/09/01) Ver4互換モード対応
109        // 6.9.5.0 (2018/04/23) VER4_COMPATIBLE_MODE 廃止
110        private static final String GE58_SELECT =
111                "SELECT KBOUT,DATA1,DATA2,DATA3,DATA4,DATA5,DATA6,DATA7,DATA8" +
112//              " ,DATA9,DATA10,DATA11,DATA12,DATA13,DATA14,DATA15,"+CLM +
113                " ,DATA9,DATA10,DATA11,DATA12,DATA13,DATA14,DATA15,CLM" +
114                " FROM GE58 WHERE SYSTEM_ID IN (?,'**')" +
115                " AND LISTID = ?";
116
117        public static final int GE58_KBOUT              = 0;
118        public static final int GE58_DATA1              = 1;
119        public static final int GE58_DATA2              = 2;
120        public static final int GE58_DATA3              = 3;
121        public static final int GE58_DATA4              = 4;
122        public static final int GE58_DATA5              = 5;
123        public static final int GE58_DATA6              = 6;
124        public static final int GE58_DATA7              = 7;
125        public static final int GE58_DATA8              = 8;
126        public static final int GE58_DATA9              = 9;
127        public static final int GE58_DATA10             = 10;
128        public static final int GE58_DATA11             = 11;
129        public static final int GE58_DATA12             = 12;
130        public static final int GE58_DATA13             = 13;
131        public static final int GE58_DATA14             = 14;
132        public static final int GE58_DATA15             = 15;
133        public static final int GE58_CLM_NAME   = 16;
134
135        // コネクションにアプリケーション情報を追記するかどうか指定
136        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
137        private final ApplicationInfo appInfo;
138        private final String DBID = HybsSystem.sys( "RESOURCE_DBID" );          // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対応
139
140        /**
141         * コンストラクター
142         * 引数を受けとって、インスタンスを作成します。
143         * @param systemId システムID
144         * @param ykno 要求番号
145         * @param listId 帳票ID
146         * @param lang 言語
147         * @param fgrun 実行方法
148         * @param prtid プリンタID
149         * @param prgdir プログラムディレクトリ
150         * @param prgfile プログラムファイル
151         * @param outdir 出力ディレクトリ
152         * @param layoutFile レイアウトファイル
153         * @param isDebug デバッグフラグ
154         *
155         * @og.rev 4.3.3.0 (2008/10/01) 板金 RFID対応。引数にprgdir,prgfile,outdir追加
156         * @og.rev 5.4.3.1 (2011/12/27) portnm追加
157         * @og.rev 5.4.3.9 (2012/01/25) 雛形ファイル名追加
158         *
159         */
160        public RFIDPrintRequest( final String systemId, final String ykno
161                        , final String listId, final String lang, final String fgrun, final String prtid
162                        , final String prgdir, final String prgfile, final String outdir , final String layoutFile, final boolean isDebug ) {
163                SYSTEM_ID       = systemId;
164                YKNO            = ykno;
165                LISTID          = listId;
166                LANG            = lang;
167                FGRUN           = fgrun;
168                PRTID           = prtid;
169                PRGDIR          = prgdir;                                       // 4.3.3.0 (2008/10/01)
170                PRGFILE         = prgfile;                                      // 4.3.3.0 (2008/10/01)
171                OUTDIR          = outdir;                                       // 4.3.3.0 (2008/10/01)
172                DEBUG           = isDebug;
173                FILENAME        = layoutFile;                           // 5.4.3.9 (2012/01/25)
174
175                // アクセスログ取得の為,ApplicationInfoオブジェクトを設定
176                if( USE_DB_APPLICATION_INFO ) {
177                        appInfo = new ApplicationInfo();
178                        // ユーザーID,IPアドレス,ホスト名
179                        appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
180                        // 画面ID,操作,プログラムID
181                        appInfo.setModuleInfo( "ReportConverter",YKNO,LISTID );
182                }
183                else {
184                        appInfo = null;
185                }
186        }
187
188        /**
189         * 初期データセットを行います。
190         * ここでは、GE54,GE58,GE55 テーブルより必要な情報を取得します。
191         *
192         * @og.rev 4.3.3.0 (2008/10/01) 板金 RFID対応(PRGDIR,PRGFILE追加等)
193         * @og.rev 5.4.3.0 (2011/12/26) GE54のbSQLは取らないようにする
194         * @og.rev 5.4.3.9 (2012/01/25) GE58はなくてもよい
195         * @og.rev 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対策
196         * @og.rev 6.4.3.3 (2016/03/04) ConcurrentHashMap の not null制限のチェック追加
197         *
198         * @return 結果 [true:正常/false:異常]
199         */
200        public boolean initialDataSet() {
201
202                // ===== GE54 から帳票定義情報を取得します =========================================
203                final String[] args = new String[] { SYSTEM_ID,LISTID };
204                final String[][] vals = DBUtil.dbExecute( GE54_SELECT,args,appInfo, DBID );             // 5.5.5.1 (2012/08/07)
205                if( vals == null || vals.length == 0 ) {
206                        errMsg.append( "Data does not exist in GE54 table." ).append( CR )
207                                .append( "==============================" ).append( CR )
208                                .append( "SYSTEM_ID=["  ).append( SYSTEM_ID )
209                                .append( "] , LISTID=[" ).append( LISTID    ).append( ']' )             // 6.0.2.5 (2014/10/31) char を append する。
210                                .append( CR );
211                        return false;
212                }
213
214                // 検索結果が複数帰ったとき、SYSTEM_ID が 指定されている方のデータ(行)を採用する。
215                int row = 0;
216                for( int i=0; i<vals.length; i++ ) {
217                        if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE54_SYSTEM_ID] ) ) { row = i; break; }
218                }
219
220                // ローカルリソースフラグ
221                // 4.3.3.0 (2008/10/01)
222                // fgLOCAL = ( vals[row][GE54_FGLOCAL]  != null && vals[row][GE54_FGLOCAL].trim().equals( "1" ) ) ? true : false ;
223                final String localFlag = vals[row][GE54_FGLOCAL];
224                fgLOCAL = localFlag != null && "1".equals( localFlag.trim() ) ;
225
226                // ===== GE58 から帳票定義情報を取得します =========================================
227                final String[] argsGe58 = new String[] { SYSTEM_ID,LISTID };
228                final String[][] valsGe58 = DBUtil.dbExecute( GE58_SELECT,argsGe58,appInfo, DBID );             // 5.5.5.1 (2012/08/07)
229
230                for( int i=0; i<valsGe58.length; i++ ) {
231                        // 6.4.3.3 (2016/03/04) ConcurrentHashMap の not null制限
232                        final String key  = valsGe58[i][GE58_CLM_NAME];                         // 最低限、valsGe58[i] は、null ではない。
233                        if( key != null ) {
234                                rfidLayoutMap.put( key, valsGe58[i] );
235                        }
236                }
237
238                // ===== GE55 から出力先マスタ情報を取得します =========================================
239                final String[] argsGe55 = new String[] { SYSTEM_ID,PRTID };
240                final String[][] valsGe55 = DBUtil.dbExecute( GE55_SELECT,argsGe55,appInfo, DBID );             // 5.5.5.1 (2012/08/07)
241                if( valsGe55 == null || valsGe55.length == 0 ) {
242                        errMsg.append( "Data does not exist in GE55 table." ).append( CR )
243                                .append( "==============================" ).append( CR )
244                                .append( "SYSTEM_ID=[" ).append( SYSTEM_ID )
245                                .append( "] , PRTID=[" ).append( PRTID     ).append( ']' )              // 6.0.2.5 (2014/10/31) char を append する。
246                                .append( CR );
247                        return false;
248                }
249
250                // 検索結果が複数帰ったとき、SYSTEM_ID が 指定されている方のデータ(行)を採用する。
251                int rowGe55 = 0;
252                for( int i=0; i<vals.length; i++ ) {
253                        if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE55_SYSTEM_ID] ) ) { rowGe55 = i; break; }
254                }
255
256                prtName = valsGe55[rowGe55][GE55_PRTNM];
257                hostName = valsGe55[rowGe55][GE55_HOST_ID];
258                portnm = valsGe55[rowGe55][GE55_PORTNM];
259
260                return true;
261        }
262
263        /**
264         * RFID発行用データを作成し、発行リクエストの処理を行います。
265         *
266         * @og.rev 5.4.3.0 (2011/12/26) _DEFAULT対応
267         * @og.rev 5.4.3.4 (2012/01/12) listid追加
268         * @og.rev 5.4.3.9 (2012/01/25) FILENAME追加
269         * @og.rev 6.4.3.1 (2016/02/12) PMD refactoring. Map → ConcurrentMap に置き換え。
270         *
271         * @return 結果 [true:正常/false:異常]
272         */
273        public boolean execute() {
274                System.out.print( "RRID RequestData Creating ... " );
275
276                // FGLOCAL 指定時は、SYSTEM_ID を指定してリソース作成
277                if( fgLOCAL ) {
278                        // ローカルリソース指定時は、SYSTEM_ID,LANG を使用します。先読みは、使用しません。
279                        resource = ResourceFactory.newInstance( SYSTEM_ID,LANG,false );
280                }
281                else {
282                        // 従来と互換性のあるモード(ローカルリソースは使用しない。
283                        resource = ResourceFactory.newInstance( LANG );
284                }
285
286                // ボディー情報の取得
287                // 5.2.3.0 tableのとり方変更
288                final DBTableModelCreator tmc = new DBTableModelCreator( SYSTEM_ID, LISTID, YKNO, "B", resource );
289                final DBTableModel table = tmc.getTable();
290                if( table.getRowCount() <= 0 ) {
291                        errMsg.append( "Database Body row count is Zero." ).append( CR );
292                        errMsg.append( "==============================" ).append( CR );
293                        // 5.5.2.4 (2012/05/16) 廃止の消し忘れ。
294                        errMsg.append( "SYSTEM_ID=" ).append( SYSTEM_ID );
295                        errMsg.append( ",LISTID="   ).append( LISTID );
296                        errMsg.append( ",YKNO="     ).append( YKNO );
297                        errMsg.append( CR );
298                        return false;
299                }
300                // 検索時の最大件数での打ち切りをエラーとする。
301                if( table.isOverflow() ) {
302                        errMsg.append( "Database is Overflow. [" );
303                        errMsg.append( table.getRowCount() );
304                        errMsg.append( ']' ).append( CR ).append( CR );         // 6.0.2.5 (2014/10/31) char を append する。
305                        errMsg.append( "==============================" ).append( CR );
306                        errMsg.append( "Check SystemParameter Data in DB_MAX_ROW_COUNT Overflow" );
307                        errMsg.append( CR );
308                        return false;
309                }
310
311                // RFID発行用クラスを実行する。
312                RFIDPrintPointService service = null;
313                try {
314                        service = (RFIDPrintPointService)StringUtil.newInstance( HybsSystem.sys( "REPORT_RFID_SERVICE_CLASS" ) );
315                        service.setYkno( YKNO );
316                        service.setSystemId( SYSTEM_ID );
317                        service.setFgrun( FGRUN );
318                        service.setHostName( hostName );
319                        service.setPrinterName( prtName );
320                        service.setTable( table );
321                        service.setLayout( rfidLayoutMap );     // 6.4.3.1 (2016/02/12) ConcurrentMap 系は、key,val ともに not null 制限です。
322                        service.setPrgDir( PRGDIR );            // 4.3.3.0 (2008/10/01) 板金 RFID対応
323                        service.setPrgFile( PRGFILE );          // 4.3.3.0 (2008/10/01)
324                        service.setOutDir( OUTDIR );            // 4.3.3.0 (2008/10/01)
325                        service.setPrtId( PRTID );                      // 5.4.3.0 (2011/12/26)
326                        service.setPortnm( portnm );            // 5.4.3.1 (2011/12/27)
327                        service.setListId( LISTID );            // 5.4.3.4 (2012/01/12)
328                        service.setLayoutFile( FILENAME );      // 5.4.3.9 (2012/01/25)
329
330                        // service.execute();
331                        final boolean flag = service.execute(); // 4.3.3.0 (2008/10/01) booleanを返す
332
333                        fgkan = service.getFgkan();                     // 4.3.3.0 (2008/10/01) FGKANを受け取るようにする
334
335                        if( DEBUG ) {
336                                System.out.println( service );
337                        }
338
339                        if( ! flag ){ // 4.3.3.0 (2008/10/01)
340                                errMsg.append( service.getErrMsg() );
341                                return false;
342                        }
343                }
344                catch( final Throwable ex ) {
345                        fgkan = GE50Access.FG_ERR2; // 4.3.3.0 (2008/10/01) エラー時はアプリエラーにしておく
346                        errMsg.append( "RFID Print Request Execution Error. " ).append( CR )
347                                .append( "==============================" ).append( CR )
348                                .append( "SYSTEM_ID=[" ).append( SYSTEM_ID )
349                                .append( "] , YKNO=["  ).append( YKNO ).append( ']' ).append( CR )              // 6.0.2.5 (2014/10/31) char を append する。
350                                .append( HybsSystem.sys( "REPORT_RFID_SERVICE_CLASS" ) ).append( CR )
351                                .append( ex.toString() )
352                                .append( CR );
353                        if( service != null ) { errMsg.append( service.getErrMsg() ); }                         // 5.5.2.6 (2012/05/25) findbugs対応
354                        return false;
355                }
356
357                System.out.println( "End." );
358                return true ;
359        }
360
361        /**
362         * エラーが存在した場合に、エラーメッセージを返します。
363         *
364         * @return エラーメッセージ String
365         * @og.rtnNotNull
366         */
367        public String getErrMsg() {
368                return errMsg.toString();
369        }
370
371        /**
372         * 完了フラグを返します。
373         *
374         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID メソッド追加
375         *
376         * @return 完了フラグ String
377         */
378        public String getFgkan() {
379                return fgkan;
380        }
381}