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