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)
027//import org.opengion.hayabusa.report.ExcelInsert;                      // 7.4.4.0 (2021/06/30) Delete
028//import org.opengion.hayabusa.report.ProgramRun;                       // 7.4.4.0 (2021/06/30) Delete
029//import org.opengion.hayabusa.report.RFIDPrintRequest;         // 7.4.4.0 (2021/06/30) Delete
030// import static org.opengion.fukurou.system.HybsConst.FS;              // 6.1.0.0 (2014/12/26) refactoring // 8.0.0.0 (2021/07/31) Delete
031
032/**
033 * 帳票要求に設定された実行方法により、各種出力、Excel取り込み処理を行います。
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 *
047 * 実行方法により、出力、入力を行います。
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        private final long start = System.currentTimeMillis();                                  // 6.3.9.1 (2015/11/27) 修飾子を、なし → private に変更。
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        //       * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(EXCEL取込廃止)
131        //       * ======================================================================
132        //       */
133                // 取込 7.4.4.0 (2021/06/30) Delete
134        //      if( ExecQueue.IN_INPUT_ONLY.equals( type ) || ExecQueue.IN_INPUT_EXEC.equals( type ) ) {
135        //              input();
136        //      }
137
138                // PG起動 7.4.4.0 (2021/06/30) Delete
139        //      if( ExecQueue.IN_EXEC_ONLY.equals( type ) || ExecQueue.IN_INPUT_EXEC.equals( type ) ) {
140        //              pgexec();
141        //      }
142
143        //      /*
144        //       * ======================================================================
145        //       * = RFID出力処理
146        //       * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(RFID出力廃止)
147        //       * ======================================================================
148        //       */
149                // RFID出力 7.4.4.0 (2021/06/30) Delete
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        // * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(EXCEL取込廃止)
281        // */
282        // 7.4.4.0 (2021/06/30) Delete
283        // private void input() {
284        //      boolean flag = false;
285
286        //      // エクセル入力の基底となるパス
287        //      final String excelinUrl = HybsSystem.url2dir( StringUtil.nval( HybsSystem.sys( "EXCEL_IN_FILE_URL" ), HybsSystem.sys( "FILE_URL" ) + "EXCELIN/" ) );
288        //      final String excelinDir = excelinUrl + queue.getSystemId() + FS + queue.getListId();
289
290        //      final ExcelInsert ei = new ExcelInsert( queue.getSystemId(), queue.getYkno(), queue.getListId(), excelinDir, false );
291        //      flag = ei.execute();
292        //      if( !flag ) {
293        //              queue.addMsg( ei.getErrMsg() );
294        //              queue.addMsg( "取り込み処理に失敗しました" );
295        //              throw new HybsSystemException();
296        //      }
297        //      addDebugMsg( "[INFO]EXEC-TIME:INPUT=" + ( System.currentTimeMillis() - start ) );
298        // }
299
300        // /**
301        // * Excel取込後のPG起動処理を行います。
302        // *
303        // * @og.rev 4.3.0.0 (2008/07/15) debugの追加
304        // * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(EXCEL取込廃止)
305        // */
306        // 7.4.4.0 (2021/06/30) Delete
307        // private void pgexec() {
308        //      boolean flag = false;
309
310        //      final ProgramRun pr = new ProgramRun( queue.getSystemId(), queue.getYkno(), queue.getListId(), false );
311        //      flag = pr.execute();
312        //      if( !flag ) {
313        //              queue.addMsg( "取り込み後のPG起動に失敗しました" );
314        //              queue.addMsg( pr.getErrMsg() );
315        //              throw new HybsSystemException();
316        //      }
317        //      addDebugMsg( "[INFO]EXEC-TIME:PGEXEC=" + ( System.currentTimeMillis() - start ) );
318        // }
319
320        // /**
321        // * RFID出力処理を行います。
322        // *
323        // * @og.rev 4.3.0.0 (2008/07/15) debugの追加
324        // * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
325        // * @og.rev 5.4.3.9 (2012/01/25) 雛形ファイル名
326        // * @og.rev 7.4.4.0 (2021/06/30) openGionV8事前準備(RFID出力廃止)
327        // */
328        // 7.4.4.0 (2021/06/30) Delete
329        // private void rfid() {
330        //      boolean flag = false;
331
332        //      // RFIDPrintRequest rpr = new RFIDPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrinterName(), false );
333        //      // 4.3.3.0 (2008/10/01) 板金RFID対応。
334        //      // 5.4.3.9 (2012/01/25) 雛形ファイル名を渡す
335        //      final RFIDPrintRequest rpr = new RFIDPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrtId()
336        //                                                                                               ,queue.getPrgDir(), queue.getPrgFile(), queue.getOutputName(),queue.getTemplateName(), false );
337        //      flag = rpr.initialDataSet();
338        //      if( flag ) {
339        //              flag = rpr.execute();
340        //      }
341        //      if( !flag ) {
342        //              queue.addMsg( "RFID出力処理に失敗しました" );
343        //              queue.addMsg( rpr.getErrMsg() );
344        //              throw new HybsSystemException();
345        //      }
346        //      addDebugMsg( "[INFO]EXEC-TIME:RFID=" + ( System.currentTimeMillis() - start ) );
347        // }
348
349        /**
350         * CSV出力処理を行います。
351         *
352         * @og.rev 5.9.0.0 (2015/09/04)
353         * @og.rev 5.9.2.2 (2015/11/22) grpid,demgrp
354         * @og.rev 5.9.2.3 (2015/11/27) 件数対応
355         */
356        private void csv() {
357                boolean flag = false;
358
359                final CSVPrintRequest rpr = new CSVPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrtId()
360                                                                                                         ,queue.getPrgDir(), queue.getPrgFile(), queue.getOutputName(), queue.getTemplateName(), queue.getGrpId(), queue.getDmnGrp(), debug );
361                flag = rpr.initialDataSet();
362                if( flag ) {
363                        flag = rpr.execute();
364                }
365                if( !flag ) {
366                        queue.addMsg( "CSV出力処理に失敗しました" );
367                        queue.addMsg( rpr.getErrMsg() );
368                        throw new HybsSystemException();
369                }
370                queue.setExecRowCnt( rpr.getBodyCount() ); // 5.9.2.3 (2015/11/27)
371
372                addDebugMsg( "[INFO]EXEC-TIME:CSV=" + ( System.currentTimeMillis() - start ) );
373        }
374
375        /**
376         * デバッグ用のメッセージを出力します。
377         *
378         * @param       msg     メッセージ
379         */
380        private void addDebugMsg( final String msg ) {
381                if( debug ){
382                        queue.addMsg( msg + CR );
383                }
384        }
385}