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