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;
019
020import org.opengion.fukurou.util.FileUtil;
021import org.opengion.fukurou.util.StringUtil;
022import org.opengion.fukurou.util.ZipArchive;                            // 6.0.0.0 (2014/04/11) ZIP API変更
023import static org.opengion.fukurou.system.HybsConst.CR ;                // 6.1.0.0 (2014/12/26)
024import org.opengion.hayabusa.common.HybsSystem;
025import org.opengion.hayabusa.common.HybsSystemException;
026import org.opengion.hayabusa.report.CSVPrintRequest;            // 5.9.0.0 (2015/09/04)
027import org.opengion.hayabusa.report.ExcelInsert;
028import org.opengion.hayabusa.report.ProgramRun;
029import org.opengion.hayabusa.report.RFIDPrintRequest;
030import static org.opengion.fukurou.system.HybsConst.FS;         // 6.1.0.0 (2014/12/26) refactoring
031
032/**
033 * 帳票要求に設定された実行方法により、各種出力、Excel取り込み、RFID出力処理を行います。
034 * 1.出力
035 *  雛形ファイルを一時ディレクトリに展開した後、帳票データを埋め込み、最後にOpenOffice.orgの
036 *  プロセスを利用して出力を行います。
037 *  対応している出力方法は、印刷、PDF出力、Excel出力です。
038 *  一時ディレクトリは、システムリソースのREPORT_FILE_URLで定義されたディレクトリです。
039 *  これが定義されていない場合は、システムリソースのFILE_URLで定義されたディレクト以下の/REPORTに
040 *  展開されます。
041 *  一時ファイルは、処理が正常に終了した場合、削除されます。(ODS出力のみにした場合は、出力直前の
042 *  ODSファイルは残ります)
043 *  処理でエラーが発生した場合は、一時ファイルはデバッグのため、削除されません。
044 * 2.取り込み
045 *  旧帳票システムの取り込み処理及びその後のPG起動を行います。
046 * 3.RFID出力
047 *  旧帳票システムのRFID出力処理を行います。
048 *
049 * 実行方法により、出力、入力、RFID出力を行います。
050 *
051 * @og.group 帳票システム
052 *
053 * @version  4.0
054 * @author   Hiroki.Nakamura
055 * @since    JDK1.6
056 */
057public class ExecProcess {
058
059        /** 帳票処理キュー */
060        private final ExecQueue queue;
061
062        /** 出力タイプ */
063        private final String type;
064
065        private final long start = System.currentTimeMillis();                                  // 6.3.9.1 (2015/11/27) 修飾子を、なし → private に変更。
066        private final boolean debug; // 4.3.0.0 (2008/07/15) デバッグの追加
067
068        /**
069         * コンストラクタ
070         *
071         * @og.rev 4.3.0.0 (2008/07/15)引数にdebugを追加
072         *
073         * @param qu            ExecQueueオブジェクト
074         * @param debugFlag デバッグフラグ[true/false]
075         */
076        public ExecProcess( final ExecQueue qu , final boolean debugFlag ) {
077                queue = qu;
078                type = qu.getOutputType();
079                debug = debugFlag;
080        }
081
082        /**
083         * 帳票処理プロセスを実行します。
084         *
085         * @og.rev 4.3.0.0 (2008/07/15) debugの追加
086         * @og.rev 4.3.3.4 (2008/11/01) ODS出力追加
087         * @og.rev 5.1.2.0 (2010/01/01) 256シートを超えた場合の対応
088         */
089        public void process() {
090                // 処理開始
091                addDebugMsg( "[INFO]EXEC-TIME:START=" + start );
092
093                // 5.1.2.0 (2010/01/01) 基本的には1回で終了。256シートを超える場合のみ内部でfalseを立てる(2回目を処理させる)
094                queue.setEnd( true );
095
096                /*
097                 * ======================================================================
098                 * = 出力処理
099                 * ======================================================================
100                 */
101                // パース
102                if( ExecQueue.OUT_ODS_ONLY.equals( type )
103                                || ExecQueue.OUT_ODS_PRINT.equals( type ) || ExecQueue.OUT_ODS_PDF.equals( type ) || ExecQueue.OUT_ODS_EXCEL.equals( type )
104                                || ExecQueue.OUT_ODS_PRINT_PDF.equals( type ) || ExecQueue.OUT_ODS_ODS.equals( type ) ) {
105                        parse();
106                }
107
108                // 印刷
109                if( ExecQueue.OUT_PRINT_ONLY.equals( type ) || ExecQueue.OUT_ODS_PRINT.equals( type ) ) {
110                        output( "PRINT" );
111                }
112                // PDF出力
113                else if( ExecQueue.OUT_ODS_PDF.equals( type ) ) {
114                        output( "PDF" );
115                }
116                // EXCEL出力
117                else if( ExecQueue.OUT_ODS_EXCEL.equals( type ) ) {
118                        output( "EXCEL" );
119                }
120                // 印刷 + PDF出力
121                else if( ExecQueue.OUT_ODS_PRINT_PDF.equals( type ) ) {
122                        output( "PRINT", "PDF" );
123                }
124                // 4.3.3.4 (2008/11/01) 追加 ODS出力
125                else if( ExecQueue.OUT_ODS_ODS.equals( type ) ) {
126                        output( "ODS" );
127                }
128
129                /*
130                 * ======================================================================
131                 * = 取込処理
132                 * ======================================================================
133                 */
134                // 取込
135                if( ExecQueue.IN_INPUT_ONLY.equals( type ) || ExecQueue.IN_INPUT_EXEC.equals( type ) ) {
136                        input();
137                }
138
139                // PG起動
140                if( ExecQueue.IN_EXEC_ONLY.equals( type ) || ExecQueue.IN_INPUT_EXEC.equals( type ) ) {
141                        pgexec();
142                }
143
144                /*
145                 * ======================================================================
146                 * = RFID出力処理
147                 * ======================================================================
148                 */
149                // RFID出力
150                if( ExecQueue.RFID_PRINT.equals( type ) || ExecQueue.RFID_ALLPRINT.equals( type )
151                                || ExecQueue.RFID_ALLERASE.equals( type ) || ExecQueue.RFID_SEQERASE.equals( type ) ) {
152                        rfid();
153                }
154
155                /*
156                 * ======================================================================
157                 * = CSV出力処理
158                 * 5.9.4.2 (2016/01/13) Excel2追加
159                 * ======================================================================
160                 */
161                if( ExecQueue.CSV_PRINT.equals( type ) || ExecQueue.CSV_PRINT_EXCEL.equals( type )
162                                || ExecQueue.CSV_PRINT_PDF.equals( type ) || ExecQueue.CSV_PRINT_EXCEL2.equals( type ) ) {
163                        csv();
164                }
165
166                addDebugMsg( "[INFO]EXEC-TIME:END=" + System.currentTimeMillis() );
167        }
168
169        /**
170         * 雛形ファイルを解析し、帳票データを挿入します。
171         *
172         * @og.rev 6.0.0.0 (2014/04/11) ZIP API変更
173         */
174        private void parse() {
175                final File templateFile = new File( queue.getTemplateName() + ".ods" );         // 6.0.0.0 (2014/04/11) ZIP API変更
176
177                final String tmp =
178                        HybsSystem.url2dir( StringUtil.nval( HybsSystem.sys( "REPORT_FILE_URL" ) ,HybsSystem.sys( "FILE_URL" ) + "REPORT/" ) )
179                        + queue.getSystemId() + File.separator + queue.getListId() + File.separator + queue.getYkno();
180                final String tmpdir = tmp + File.separator;
181                final File tmpdirFile = new File( tmp + File.separator );               // 6.0.0.0 (2014/04/11) ZIP API変更
182                final File tmpodsFile = new File( tmp + ".ods" );                               // 6.0.0.0 (2014/04/11) ZIP API変更
183
184                // 一時ファイルを削除(エラー発生時の前のファイルを削除)
185                FileUtil.deleteFiles( tmpdirFile );                                             // 6.0.0.0 (2014/04/11) ZIP API変更
186
187                // 雛形ODSをテンポラリフォルダに解凍
188                ZipArchive.unCompress( tmpdirFile, templateFile );              // 6.0.0.0 (2014/04/11) ZIP API変更
189                addDebugMsg( "[INFO]EXEC-TIME:UNCOMP=" + ( System.currentTimeMillis() - start ) );
190
191                // DBTableModelのセット
192                queue.setData();
193                addDebugMsg( "[INFO]EXEC-TIME:DATA=" + ( System.currentTimeMillis() - start ) );
194
195                // content.xml,meta.xmlのパース
196                final OdsContentParser contentParser = new OdsContentParser( queue, tmpdir );
197                contentParser.exec();
198                addDebugMsg( "[INFO]EXEC-TIME:PARSE=" + ( System.currentTimeMillis() - start ) );
199
200                // 雛形ODSを再圧縮
201                ZipArchive.compress( tmpdirFile, tmpodsFile );                  // 6.0.0.0 (2014/04/11) ZIP API変更
202                addDebugMsg( "[INFO]EXEC-TIME:COMP=" + ( System.currentTimeMillis() - start ) );
203
204                // 一時ファイルを削除
205                FileUtil.deleteFiles( tmpdirFile );                                             // 6.0.0.0 (2014/04/11) ZIP API変更
206                addDebugMsg( "[INFO]EXEC-TIME:DELETE=" + ( System.currentTimeMillis() - start ) );
207        }
208
209        /**
210         * 出力処理を行います。
211         *
212         * @og.rev 4.2.3.1 (2008/06/04) 中間ファイルの存在チェックを追加
213         * @og.rev 4.3.0.0 (2008/07/18) プリント時のプリンタ名チェック追加
214         * @og.rev 4.3.0.0 (2008/07/18) 出力ファイル名を指定していない場合に要求番号にする
215         * @og.rev 4.3.3.4 (2008/11/01) ODS出力追加
216         * @og.rev 5.1.2.0 (2010/01/01) 例外発生時にCalcオブジェクトをCloseしていないバグを修正
217         * @og.rev 5.1.6.0 (2010/05/01) 変換クラスの大幅見直しによる修正(元のコードも削除します)
218         *
219         * @param String... types
220         */
221        private void output( final String... types ) {
222                final String tmpods =
223                        HybsSystem.url2dir( StringUtil.nval( HybsSystem.sys( "REPORT_FILE_URL" ) ,HybsSystem.sys( "FILE_URL" ) + "REPORT/" ) )
224                        + queue.getSystemId() + File.separator + queue.getListId() + File.separator + queue.getYkno() + ".ods";
225
226                // 4.2.3.1 (2008/06/04) 中間ファイルの存在チェック
227                if( ! new File( tmpods ).exists() ){
228                        queue.addMsg( "中間ファイルが存在しません。" + tmpods + CR );
229                        throw new HybsSystemException();
230                }
231
232                // 変換クラスの生成
233                final DocConverter_OOO dc = new DocConverter_OOO( tmpods );
234                try {
235                        // 起動
236                        dc.open();
237                        addDebugMsg( "[INFO]EXEC-TIME:OPEN=" + ( System.currentTimeMillis() - start ) );
238
239                        for( int i=0; i<types.length; i++ ) {
240                                if( "PRINT".equals( types[i] ) ) {
241                                        // 4.3.0.0 (2008/07/18) プリント時のプリンタ名チェック
242                                        if( queue.getPrinterName() == null || queue.getPrinterName().isEmpty() ){
243                                                queue.addMsg( "出力先マスタが正しく設定されていません。" + CR );
244                                                throw new Exception();
245                                        }
246                                        dc.print( queue.getPrinterName() );
247                                }
248                                else if( "PDF".equals( types[i] ) ) {
249                                        dc.pdf( queue.getOutputName(), queue.getPdfPasswd() );
250                                }
251                                else if( "EXCEL".equals( types[i] ) ) {
252                                        dc.xls( queue.getOutputName() );
253                                }
254                                // 4.3.3.4 (2008/11/01) 追加
255                                else if( "ODS".equals( types[i] ) ) {
256                                        dc.ods( queue.getOutputName() );
257                                }
258                                addDebugMsg( "[INFO]EXEC-TIME:EXEC["+types[i]+"]=" + ( System.currentTimeMillis() - start ) );
259                        }
260
261                        // Calcを閉じる
262                        dc.close();
263                        addDebugMsg( "[INFO]EXEC-TIME:RELEASE=" + ( System.currentTimeMillis() - start ) );
264                }
265                catch( final Throwable th ) {
266                        // Calcを閉じる(エラー発生時)
267                        dc.close( true );
268                        queue.addMsg( "[INFO]EXEC-TIME:ERROR=" + ( System.currentTimeMillis() - start ) + CR );
269                        throw new HybsSystemException( th );
270                }
271                // 一時ファイルの削除
272                FileUtil.deleteFiles( new File( tmpods ) );
273                addDebugMsg( "[INFO]EXEC-TIME:DELETE=" + ( System.currentTimeMillis() - start ) );
274        }
275
276        /**
277         * 取込処理を行います。
278         *
279         * @og.rev 4.3.0.0 (2008/07/15) debugの追加
280         */
281        private void input() {
282                boolean flag = false;
283
284                // エクセル入力の基底となるパス
285                final String excelinUrl = HybsSystem.url2dir( StringUtil.nval( HybsSystem.sys( "EXCEL_IN_FILE_URL" ), HybsSystem.sys( "FILE_URL" ) + "EXCELIN/" ) );
286                final String excelinDir = excelinUrl + queue.getSystemId() + FS + queue.getListId();
287
288                final ExcelInsert ei = new ExcelInsert( queue.getSystemId(), queue.getYkno(), queue.getListId(), excelinDir, false );
289                flag = ei.execute();
290                if( !flag ) {
291                        queue.addMsg( ei.getErrMsg() );
292                        queue.addMsg( "取り込み処理に失敗しました" );
293                        throw new HybsSystemException();
294                }
295                addDebugMsg( "[INFO]EXEC-TIME:INPUT=" + ( System.currentTimeMillis() - start ) );
296        }
297
298        /**
299         * Excel取込後のPG起動処理を行います。
300         *
301         * @og.rev 4.3.0.0 (2008/07/15) debugの追加
302         */
303        private void pgexec() {
304                boolean flag = false;
305
306                final ProgramRun pr = new ProgramRun( queue.getSystemId(), queue.getYkno(), queue.getListId(), false );
307                flag = pr.execute();
308                if( !flag ) {
309                        queue.addMsg( "取り込み後のPG起動に失敗しました" );
310                        queue.addMsg( pr.getErrMsg() );
311                        throw new HybsSystemException();
312                }
313                addDebugMsg( "[INFO]EXEC-TIME:PGEXEC=" + ( System.currentTimeMillis() - start ) );
314        }
315
316        /**
317         * RFID出力処理を行います。
318         *
319         * @og.rev 4.3.0.0 (2008/07/15) debugの追加
320         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
321         * @og.rev 5.4.3.9 (2012/01/25) 雛形ファイル名
322         */
323        private void rfid() {
324                boolean flag = false;
325
326                // RFIDPrintRequest rpr = new RFIDPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrinterName(), false );
327                // 4.3.3.0 (2008/10/01) 板金RFID対応。
328                // 5.4.3.9 (2012/01/25) 雛形ファイル名を渡す
329                final RFIDPrintRequest rpr = new RFIDPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrtId()
330                                                                                                         ,queue.getPrgDir(), queue.getPrgFile(), queue.getOutputName(),queue.getTemplateName(), false );
331                flag = rpr.initialDataSet();
332                if( flag ) {
333                        flag = rpr.execute();
334                }
335                if( !flag ) {
336                        queue.addMsg( "RFID出力処理に失敗しました" );
337                        queue.addMsg( rpr.getErrMsg() );
338                        throw new HybsSystemException();
339                }
340                addDebugMsg( "[INFO]EXEC-TIME:RFID=" + ( System.currentTimeMillis() - start ) );
341        }
342
343        /**
344         * CSV出力処理を行います。
345         *
346         * @og.rev 5.9.0.0 (2015/09/04)
347         * @og.rev 5.9.2.2 (2015/11/22) grpid,demgrp
348         * @og.rev 5.9.2.3 (2015/11/27) 件数対応
349         */
350        private void csv() {
351                boolean flag = false;
352
353                final CSVPrintRequest rpr = new CSVPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrtId()
354                                                                                                         ,queue.getPrgDir(), queue.getPrgFile(), queue.getOutputName(), queue.getTemplateName(), queue.getGrpId(), queue.getDmnGrp(), debug );
355                flag = rpr.initialDataSet();
356                if( flag ) {
357                        flag = rpr.execute();
358                }
359                if( !flag ) {
360                        queue.addMsg( "CSV出力処理に失敗しました" );
361                        queue.addMsg( rpr.getErrMsg() );
362                        throw new HybsSystemException();
363                }
364                queue.setExecRowCnt( rpr.getBodyCount() ); // 5.9.2.3 (2015/11/27)
365
366                addDebugMsg( "[INFO]EXEC-TIME:CSV=" + ( System.currentTimeMillis() - start ) );
367        }
368
369        /**
370         * デバッグ用のメッセージを出力します。
371         *
372         * @param       msg     メッセージ
373         */
374        private void addDebugMsg( final String msg ) {
375                if( debug ){
376                        queue.addMsg( msg + CR );
377                }
378        }
379}