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 org.opengion.fukurou.db.DBUtil;
019import org.opengion.fukurou.db.ApplicationInfo;
020import org.opengion.fukurou.util.StringUtil;
021import org.opengion.hayabusa.common.HybsSystem;
022import org.opengion.hayabusa.db.DBTableModel;
023import org.opengion.hayabusa.report2.QueueManager_DB.DBTableModelCreator;
024import org.opengion.hayabusa.resource.ResourceFactory;
025import org.opengion.hayabusa.resource.ResourceManager;
026
027import static org.opengion.fukurou.system.HybsConst.CR ;                        // 5.9.0.0 (2015/09/04)
028import static org.opengion.fukurou.system.HybsConst.BUFFER_MIDDLE;      // 6.4.2.1 (2016/02/05)
029
030/**
031 * 帳票システムでCSV出力をする際に、データを加工、プラグインの呼び出しを行うクラスです。
032 *
033 * @og.group 帳票システム
034 *
035 * @version  4.0
036 * @author   Takahashi Masakazu
037 * @since    JDK8.0,
038 */
039public class CSVPrintRequest {
040        private final StringBuilder errMsg = new StringBuilder( BUFFER_MIDDLE );                // 6.4.2.1 (2016/02/05)
041
042        // DBTableModel に対して設定する情報
043        private ResourceManager resource        ;
044
045        // 受け渡し変数
046        private final String    SYSTEM_ID       ;
047        private final String    YKNO            ;
048        private final String    LISTID          ;
049        private final String    LANG            ;
050        private final String    FGRUN           ;       // G:CSV(print) H:CSV(Excel) I:CSV(PDF)
051        private final String    PRTID           ;
052        private final String    PRGDIR          ;
053        private final String    PRGFILE         ;
054        private final String    OUTDIR          ;
055        private final String    FILENAME        ;       // 雛形ファイル名
056        private final boolean   DEBUG           ;
057
058        private final String    GRPID           ;       // 5.9.2.2 (2015/11/20)
059        private final String    DMNGRP          ;       // 5.9.2.2 (2015/11/20)
060
061        // オブジェクト変数
062        private boolean         fgLOCAL         ;
063        private String          prtName         ;
064        private String          hostName        ;
065        private String          portnm          ;
066        private String          fgkan           = GE50Access.FG_ERR2;   // 初期値エラー
067        private String          option          ;                                               // 5.9.3.0 (2015/12/04)
068
069        private int                     bodyCount       ;                                               // 5.9.2.7 (2015/11/27) bodyCount 追加
070
071        // GE54 の帳票定義情報を取得するSQL文です。
072        private static final String GE54_SELECT =
073                "SELECT BSQL,FGLOCAL,SYSTEM_ID,HSQL,FSQL" +
074                " ,OPTIONS,FGCUT" +                                                                     // 5.9.3.0 (2015/12/04)
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        private static final int GE54_OPTIONS   = 5;                    // 5.9.3.0 (2015/12/04)
082        // private static final int GE54_FGCUT          = 6;                    // 5.9.3.0 (2015/12/04)
083
084        // 5.4.4.0 (2012/02/01) 互換モード対応漏れ
085        private static final String HOST = HybsSystem.sysBool( "VER4_COMPATIBLE_MODE" ) ? "HOST" : "HOST_ID";
086
087        // GE55 の出力先マスタ情報を取得するSQL文です。
088        // 5.1.0.0 (2009/11/04) HOST ⇒ HOST_ID
089        // 5.4.3.1 (2011/12/27) PORTNM
090        // 5.4.4.0 (2012/02/01) 互換モード
091        private static final String GE55_SELECT =
092                "SELECT PRTNM,"+HOST+",SYSTEM_ID,PORTNM" +
093                " FROM GE55" +
094                " WHERE FGJ = '1'" +
095                " AND  SYSTEM_ID IN (?,'**')" +
096                " AND  PRTID = ?" ;
097        private static final int GE55_PRTNM             = 0;
098        private static final int GE55_HOST_ID   = 1;
099        private static final int GE55_SYSTEM_ID = 2;
100        private static final int GE55_PORTNM    = 3;
101
102        // コネクションにアプリケーション情報を追記するかどうか指定
103        public static final boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
104        private final ApplicationInfo appInfo;
105        private final String DBID = HybsSystem.sys( "RESOURCE_DBID" );          // 5.5.5.1 (2012/08/07) リソース系DBID 付け忘れ対応
106
107        /**
108         * コンストラクター。
109         *
110         * 引数を受けとって、インスタンスを作成します。
111         * 
112         * @og.rev 5.9.2.2 (2015/11/20) grpid,dmngrp
113         *
114         * @param systemId システムID
115         * @param ykno 要求番号
116         * @param listId 帳票ID
117         * @param lang 言語
118         * @param fgrun 実行方法
119         * @param prtid プリンタID
120         * @param prgdir プログラムディレクトリ
121         * @param prgfile プログラムファイル
122         * @param outdir 出力ディレクトリ
123         * @param filename 出力ファイル
124         * @param grpid グループID
125         * @param dmngrp デーモングループ
126         * @param isDebug デバッグフラグ
127         */
128        public CSVPrintRequest( final String systemId, final String ykno
129                                                        , final String listId, final String lang, final String fgrun, final String prtid
130                                                        , final String prgdir, final String prgfile, final String outdir , final String filename
131                                                        , final String grpid, final String dmngrp, final boolean isDebug ) {    // 5.9.2.2 (2015/11/20)
132                SYSTEM_ID       = systemId;
133                YKNO            = ykno;
134                LISTID          = listId;
135                LANG            = lang;
136                FGRUN           = fgrun;
137                PRTID           = prtid;
138                PRGDIR          = prgdir;
139                PRGFILE         = prgfile;
140                OUTDIR          = outdir;
141                FILENAME        = filename;
142                GRPID           = grpid;
143                DMNGRP          = dmngrp;
144                DEBUG           = isDebug;
145
146                // アクセスログ取得の為,ApplicationInfoオブジェクトを設定
147                if( USE_DB_APPLICATION_INFO ) {
148                        appInfo = new ApplicationInfo();
149                        // ユーザーID,IPアドレス,ホスト名
150                        appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
151                        // 画面ID,操作,プログラムID
152                        appInfo.setModuleInfo( "ReportConverter",YKNO,LISTID );
153                }
154                else {
155                        appInfo = null;
156                }
157        }
158
159        /**
160         * 初期データセットを行います。
161         * ここでは、GE54,GE58,GE55 テーブルより必要な情報を取得します。
162         *
163         * @og.rev 5.9.3.0 (2015/12/04) 
164         *
165         * @return 結果 [true:正常/false:異常]
166         */
167        public boolean initialDataSet() {
168
169                // ===== GE54 から帳票定義情報を取得します =========================================
170                final String[] args = new String[] { SYSTEM_ID,LISTID };
171                final String[][] vals = DBUtil.dbExecute( GE54_SELECT,args,appInfo, DBID );             // 5.5.5.1 (2012/08/07)
172                if( vals == null || vals.length == 0 ) {
173                        errMsg.append( "Data does not exist in GE54 table." ).append( CR )
174                                .append( "==============================" ).append( CR )
175                                .append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , LISTID=[" ).append( LISTID ).append( ']' )
176                                .append( CR );
177                        return false;
178                }
179
180                // 検索結果が複数帰ったとき、SYSTEM_ID が 指定されている方のデータ(行)を採用する。
181                int row = 0;
182                for( int i=0; i<vals.length; i++ ) {
183                        if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE54_SYSTEM_ID] ) ) { row = i; break; }
184                }
185
186                // GE54のbSQLは廃止となっているので下方で別で取る
187
188                // ローカルリソースフラグ
189                final String localFlag = vals[row][GE54_FGLOCAL];
190                fgLOCAL = localFlag != null && "1".equals( localFlag.trim() ) ;
191
192                option = vals[row][GE54_OPTIONS];               // 5.9.3.0 (2015/12/04)
193
194                // ===== GE55 から出力先マスタ情報を取得します =========================================
195                final String[] argsGe55 = new String[] { SYSTEM_ID,PRTID };
196                final String[][] valsGe55 = DBUtil.dbExecute( GE55_SELECT,argsGe55,appInfo, DBID );
197                if( valsGe55 == null || valsGe55.length == 0 ) {
198                        errMsg.append( "Data does not exist in GE55 table." ).append( CR )
199                                .append( "==============================" ).append( CR )
200                                .append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , PRTID=[" ).append( PRTID ).append( ']' )
201                                .append( CR );
202                        return false;
203                }
204
205                // 検索結果が複数帰ったとき、SYSTEM_ID が 指定されている方のデータ(行)を採用する。
206                int rowGe55 = 0;
207                for( int i=0; i<vals.length; i++ ) {
208                        if( SYSTEM_ID.equalsIgnoreCase( vals[i][GE55_SYSTEM_ID] ) ) { rowGe55 = i; break; }
209                }
210
211                prtName = valsGe55[rowGe55][GE55_PRTNM];
212                hostName = valsGe55[rowGe55][GE55_HOST_ID];
213                portnm = valsGe55[rowGe55][GE55_PORTNM];
214
215                return true;
216        }
217
218        /**
219         * 発行用データを作成し、発行リクエストの処理を行います。
220         *
221         * @og.rev 5.4.3.0 (2011/12/26) _DEFAULT対応
222         * @og.rev 5.4.3.4 (2012/01/12) listid追加
223         * @og.rev 5.4.3.9 (2012/01/25) FILENAME追加
224         * @og.rev 5.9.2.2 (2015/11/20) GRPID,DEMGRP
225         * @og.rev 5.9.2.3 (2015/11/27) rowCount
226         * @og.rev 5.9.2.7 (2015/11/27) bodyCount 追加
227         * @og.rev 5.9.3.0 (2015/12/04) option
228         *
229         * @return 結果 [true:正常/false:異常]
230         */
231        public boolean execute() {
232                System.out.print( "CSV RequestData Creating ... " );
233
234                // FGLOCAL 指定時は、SYSTEM_ID を指定してリソース作成
235                if( fgLOCAL ) {
236                        // ローカルリソース指定時は、SYSTEM_ID,LANG を使用します。先読みは、使用しません。
237                        resource = ResourceFactory.newInstance( SYSTEM_ID,LANG,false );
238                }
239                else {
240                        // 従来と互換性のあるモード(ローカルリソースは使用しない。
241                        resource = ResourceFactory.newInstance( LANG );
242                }
243
244                // ボディー情報の取得
245                DBTableModelCreator tmc = new DBTableModelCreator( SYSTEM_ID, LISTID, YKNO, "B", resource );
246                final DBTableModel table = tmc.getTable();
247                if( table.getRowCount() <= 0 ) {
248                        errMsg.append( "Database Body row count is Zero." ).append( CR )
249                                .append( "==============================" ).append( CR )
250                                .append( "SYSTEM_ID=" ).append( SYSTEM_ID )
251                                .append( ",LISTID="   ).append( LISTID )
252                                .append( ",YKNO="     ).append( YKNO )
253                                .append( CR );
254                        return false;
255                }
256                // 検索時の最大件数での打ち切りをエラーとする。
257                if( table.isOverflow() ) {
258                        errMsg.append( "Database is Overflow. [" )
259                                .append( table.getRowCount() )
260                                .append( ']' ).append( CR ).append( CR )
261                                .append( "==============================" ).append( CR )
262                                .append( "Check SystemParameter Data in DB_MAX_ROW_COUNT Overflow" )
263                                .append( CR );
264                        return false;
265                }
266
267                bodyCount = table.getRowCount(); // 5.9.2.3 (2015/11/27)
268
269                // ヘッダフッタも渡す
270                tmc = new DBTableModelCreator( SYSTEM_ID, LISTID, YKNO, "H", resource );
271                final DBTableModel tableH = tmc.getTable();
272                tmc = new DBTableModelCreator( SYSTEM_ID, LISTID, YKNO, "F", resource );
273                final DBTableModel tableF = tmc.getTable();
274
275                // 発行用クラスを実行する。
276                CSVPrintPointService service = null;
277                try {
278                        service = (CSVPrintPointService)StringUtil.newInstance( HybsSystem.sys( "REPORT_CSV_SERVICE_CLASS" ) );
279                        service.setYkno( YKNO );
280                        service.setSystemId( SYSTEM_ID );
281                        service.setFgrun( FGRUN );
282                        service.setHostName( hostName );
283                        service.setPrinterName( prtName );
284                        service.setTable( table );
285                        service.setTableH( tableH );
286                        service.setTableF( tableF );
287                        service.setPrgDir( PRGDIR );
288                        service.setPrgFile( PRGFILE );
289                        service.setOutDir( OUTDIR );
290                        service.setPrtId( PRTID );
291                        service.setPortnm( portnm );
292                        service.setListId( LISTID );
293                        service.setModelname( FILENAME );
294                        service.setGrpId( GRPID );                      // 5.9.2.2 (2015/11/20)
295                        service.setDmnGrp( DMNGRP );            // 5.9.2.2 (2015/11/20)
296                        service.setOption( option );            // 5.9.3.0 (2015/12/04)
297
298                        final boolean flag = service.execute();
299
300                        fgkan = service.getFgkan();     
301
302                        if( DEBUG ) {
303                                System.out.println( service );
304                        }
305
306                        if( ! flag ){ 
307                                errMsg.append( service.getErrMsg() );
308                                return false;
309                        }
310                }
311                catch( final Throwable ex ) {
312                        fgkan = GE50Access.FG_ERR2; // エラー時はアプリエラーにしておく
313                        errMsg.append( "CSV Print Request Execution Error. " ).append( CR )
314                                .append( "==============================" ).append( CR )
315                                .append( "SYSTEM_ID=[" ).append( SYSTEM_ID ).append( "] , YKNO=[" ).append( YKNO ).append( ']' ).append( CR )
316                                .append( HybsSystem.sys( "REPORT_CSV_SERVICE_CLASS" ) ).append( CR )
317                                .append( ex.toString() )
318                                .append( CR );
319                        if( service != null ) { errMsg.append( service.getErrMsg() ); }         // 5.5.2.6 (2012/05/25) findbugs対応
320                        return false;
321                }
322
323                System.out.println( "End." );
324                return true ;
325        }
326
327        /**
328         * エラーが存在した場合に、エラーメッセージを返します。
329         *
330         * @return エラーメッセージ String
331         */
332        public String getErrMsg() {
333                return errMsg.toString();
334        }
335
336        /**
337         * 完了フラグを返します。
338         *
339         *
340         * @return 完了フラグ String
341         */
342        public String getFgkan() {
343                return fgkan;
344        }
345
346        /**
347         * 処理件数(bodyの件数)を返します。
348         * 
349         * @og.rev 5.9.2.7 (2015/11/27) bodyCount 追加
350         *
351         * @return 処理件数 int
352         */
353        public int getBodyCount() {
354                return bodyCount;
355        }
356}