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