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