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