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                 * ======================================================================
172                 */
173                if( ExecQueue.CSV_PRINT.equals( type ) || ExecQueue.CSV_PRINT_EXCEL.equals( type )
174                                || ExecQueue.CSV_PRINT_PDF.equals( type ) ) {
175                        csv();
176                }
177
178                addDebugMsg( "[INFO]EXEC-TIME:END=" + System.currentTimeMillis() );
179        }
180
181        /**
182         * 雛形ファイルを解析し、帳票データを挿入します。
183         *
184         */
185        private void parse() {
186                String template = queue.getTemplateName() + ".ods";
187
188                String tmp =
189                        HybsSystem.url2dir( StringUtil.nval( HybsSystem.sys( "REPORT_FILE_URL" ) ,HybsSystem.sys( "FILE_URL" ) + "REPORT/" ) )
190                        + queue.getSystemId() + File.separator + queue.getListId() + File.separator + queue.getYkno();
191                String tmpdir = tmp + File.separator;
192                String tmpods = tmp + ".ods";
193
194                // 一時ファイルを削除(エラー発生時の前のファイルを削除)
195                FileUtil.deleteFiles( new File( tmpdir ) );
196
197                // 雛形ODSをテンポラリフォルダに解凍
198                ZipFileUtil.unCompress( tmpdir, template );
199                addDebugMsg( "[INFO]EXEC-TIME:UNCOMP=" + ( System.currentTimeMillis() - start ) );
200
201                // DBTableModelのセット
202                queue.setData();
203                addDebugMsg( "[INFO]EXEC-TIME:DATA=" + ( System.currentTimeMillis() - start ) );
204
205                // content.xml,meta.xmlのパース
206                OdsContentParser contentParser = new OdsContentParser( queue, tmpdir );
207                contentParser.exec();
208                addDebugMsg( "[INFO]EXEC-TIME:PARSE=" + ( System.currentTimeMillis() - start ) );
209
210                // 雛形ODSを再圧縮
211                ZipFileUtil.compress( tmpdir, tmpods );
212                addDebugMsg( "[INFO]EXEC-TIME:COMP=" + ( System.currentTimeMillis() - start ) );
213
214                // 一時ファイルを削除
215                FileUtil.deleteFiles( new File( tmpdir ) );
216                addDebugMsg( "[INFO]EXEC-TIME:DELETE=" + ( System.currentTimeMillis() - start ) );
217        }
218
219        /**
220         * 出力処理を行います。
221         *
222         * @og.rev 4.2.3.1 (2008/06/04) 中間ファイルの存在チェックを追加
223         * @og.rev 4.3.0.0 (2008/07/18) プリント時のプリンタ名チェック追加
224         * @og.rev 4.3.0.0 (2008/07/18) 出力ファイル名を指定していない場合に要求番号にする
225         * @og.rev 4.3.3.4 (2008/11/01) ODS出力追加
226         * @og.rev 5.1.2.0 (2010/01/01) 例外発生時にCalcオブジェクトをCloseしていないバグを修正
227         * @og.rev 5.1.6.0 (2010/05/01) 変換クラスの大幅見直しによる修正(元のコードも削除します)
228         *
229         * @param String... types
230         */
231        private void output( final String... types ) {
232                String tmpods =
233                        HybsSystem.url2dir( StringUtil.nval( HybsSystem.sys( "REPORT_FILE_URL" ) ,HybsSystem.sys( "FILE_URL" ) + "REPORT/" ) )
234                        + queue.getSystemId() + File.separator + queue.getListId() + File.separator + queue.getYkno() + ".ods";
235
236                // 4.2.3.1 (2008/06/04) 中間ファイルの存在チェック
237                if( ! new File( tmpods ).exists() ){
238                        queue.addMsg( "中間ファイルが存在しません。" + tmpods + HybsSystem.CR );
239                        throw new HybsSystemException();
240                }
241
242                // 変換クラスの生成
243                DocConverter_OOO dc = new DocConverter_OOO( tmpods );
244                try {
245                        // 起動
246                        dc.open();
247                        addDebugMsg( "[INFO]EXEC-TIME:OPEN=" + ( System.currentTimeMillis() - start ) );
248
249                        for( int i=0; i<types.length; i++ ) {
250                                if( "PRINT".equals( types[i] ) ) {
251                                        // 4.3.0.0 (2008/07/18) プリント時のプリンタ名チェック
252                                        if( queue.getPrinterName() == null || queue.getPrinterName().length() ==0 ){
253                                                queue.addMsg( "出力先マスタが正しく設定されていません。" + HybsSystem.CR );
254                                                throw new Exception();
255                                        }
256                                        dc.print( queue.getPrinterName() );
257                                }
258                                else if( "PDF".equals( types[i] ) ) {
259                                        dc.pdf( queue.getOutputName(), queue.getPdfPasswd() );
260                                }
261                                else if( "EXCEL".equals( types[i] ) ) {
262                                        dc.xls( queue.getOutputName() );
263                                }
264                                // 4.3.3.4 (2008/11/01) 追加
265                                else if( "ODS".equals( types[i] ) ) {
266                                        dc.ods( queue.getOutputName() );
267                                }
268                                addDebugMsg( "[INFO]EXEC-TIME:EXEC["+types[i]+"]=" + ( System.currentTimeMillis() - start ) );
269                        }
270
271                        // Calcを閉じる
272                        dc.close();
273                        addDebugMsg( "[INFO]EXEC-TIME:RELEASE=" + ( System.currentTimeMillis() - start ) );
274                }
275                catch( Throwable th ) {
276                        // Calcを閉じる(エラー発生時)
277                        dc.close( true );
278                        queue.addMsg( "[INFO]EXEC-TIME:ERROR=" + ( System.currentTimeMillis() - start ) + HybsSystem.CR );
279                        throw new HybsSystemException( th );
280                }
281                // 一時ファイルの削除
282                FileUtil.deleteFiles( new File( tmpods ) );
283                addDebugMsg( "[INFO]EXEC-TIME:DELETE=" + ( System.currentTimeMillis() - start ) );
284        }
285
286        /**
287         * 取込処理を行います。
288         *
289         * @og.rev 4.3.0.0 (2008/07/15) debugの追加
290         */
291        private void input() {
292                boolean flag = false;
293
294                // エクセル入力の基底となるパス
295                String excelinUrl = HybsSystem.url2dir( StringUtil.nval( HybsSystem.sys( "EXCEL_IN_FILE_URL" ), HybsSystem.sys( "FILE_URL" ) + "EXCELIN/" ) );
296                String excelinDir = excelinUrl + queue.getSystemId() + HybsSystem.FS + queue.getListId();
297
298                ExcelInsert ei = new ExcelInsert( queue.getSystemId(), queue.getYkno(), queue.getListId(), excelinDir, false );
299                flag = ei.execute();
300                if( !flag ) {
301                        queue.addMsg( ei.getErrMsg() );
302                        queue.addMsg( "取り込み処理に失敗しました" );
303                        throw new HybsSystemException();
304                }
305                addDebugMsg( "[INFO]EXEC-TIME:INPUT=" + ( System.currentTimeMillis() - start ) );
306        }
307
308        /**
309         * Excel取込後のPG起動処理を行います。
310         *
311         * @og.rev 4.3.0.0 (2008/07/15) debugの追加
312         */
313        private void pgexec() {
314                boolean flag = false;
315
316                ProgramRun pr = new ProgramRun( queue.getSystemId(), queue.getYkno(), queue.getListId(), false );
317                flag = pr.execute();
318                if( !flag ) {
319                        queue.addMsg( "取り込み後のPG起動に失敗しました" );
320                        queue.addMsg( pr.getErrMsg() );
321                        throw new HybsSystemException();
322                }
323                addDebugMsg( "[INFO]EXEC-TIME:PGEXEC=" + ( System.currentTimeMillis() - start ) );
324        }
325
326        /**
327         * RFID出力処理を行います。
328         *
329         * @og.rev 4.3.0.0 (2008/07/15) debugの追加
330         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
331         * @og.rev 5.4.3.9 (2012/01/25) 雛形ファイル名
332         */
333        private void rfid() {
334                boolean flag = false;
335
336                // RFIDPrintRequest rpr = new RFIDPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrinterName(), false );
337                // 4.3.3.0 (2008/10/01) 板金RFID対応。
338                // 5.4.3.9 (2012/01/25) 雛形ファイル名を渡す
339                RFIDPrintRequest rpr = new RFIDPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrtId()
340                                                                                                         ,queue.getPrgDir(), queue.getPrgFile(), queue.getOutputName(),queue.getTemplateName(), false );
341                flag = rpr.initialDataSet();
342                if( flag ) {
343                        flag = rpr.execute();
344                }
345                if( !flag ) {
346                        queue.addMsg( "RFID出力処理に失敗しました" );
347                        queue.addMsg( rpr.getErrMsg() );
348                        throw new HybsSystemException();
349                }
350                addDebugMsg( "[INFO]EXEC-TIME:RFID=" + ( System.currentTimeMillis() - start ) );
351        }
352        
353        /**
354         * CSV出力処理を行います。
355         *
356         * @og.rev 5.9.0.0 (2015/09/04)
357         * @og.rev 5.9.2.2 (2015/11/22) grpid,demgrp
358         * @og.rev 5.9.2.3 (2015/11/27) 件数対応
359         */
360        private void csv() {
361                boolean flag = false;
362
363                CSVPrintRequest rpr = new CSVPrintRequest( queue.getSystemId(), queue.getYkno(), queue.getListId(), queue.getLang(), type, queue.getPrtId()
364                                                                                                         ,queue.getPrgDir(), queue.getPrgFile(), queue.getOutputName(), queue.getTemplateName()
365//                                                                                                       , false );
366                                                                                                         ,queue.getGrpId(), queue.getDmnGrp(), debug ); // 5.9.2.2
367                flag = rpr.initialDataSet();
368                if( flag ) {
369                        flag = rpr.execute();
370                }
371                if( !flag ) {
372                        queue.addMsg( "CSV出力処理に失敗しました" );
373                        queue.addMsg( rpr.getErrMsg() );
374                        throw new HybsSystemException();
375                }
376                
377                queue.setExecRowCnt( rpr.getBodyCount() ); // 5.9.2.3 (2015/11/27)
378                
379                addDebugMsg( "[INFO]EXEC-TIME:CSV=" + ( System.currentTimeMillis() - start ) );
380        }
381
382        /**
383         * デバッグ用のメッセージを出力します。
384         *
385         * @param       msg     メッセージ
386         */
387        private void addDebugMsg( final String msg ) {
388                if( debug ){
389                        queue.addMsg( msg + HybsSystem.CR );
390                }
391        }
392}