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.hayabusa.db.DBTableModel;
021
022/**
023 * 帳票処理要求を管理するキューオブジェクトです。
024 * このオブジェクトでは、帳票の定義及びデータと、処理中に発生したエラーメッセージを管理します。
025 * また、このキューを生成したオブジェクトもこのオブジェクトにセットされます。
026 *
027 * @og.group 帳票システム
028 *
029 * @version  4.0
030 * @author   Hiroki.Nakamura
031 * @since    JDK1.6
032 */
033public class ExecQueue {
034
035        /** 実行方法 {@value} */
036        protected static final String OUT_ODS_ONLY              = "1";
037        /** 実行方法 {@value} */
038        protected static final String OUT_PRINT_ONLY    = "2";
039        /** 実行方法 {@value} */
040        protected static final String OUT_ODS_PRINT             = "3";
041        /** 実行方法 {@value} */
042        protected static final String OUT_ODS_PDF               = "P";
043        /** 実行方法 {@value} */
044        protected static final String OUT_ODS_PRINT_PDF = "Q";
045        /** 実行方法 {@value} */
046        protected static final String OUT_ODS_EXCEL             = "E";
047        /** 実行方法 {@value} */
048        protected static final String OUT_ODS_ODS               = "S"; // 4.3.3.4 (2008/11/01) 追加
049        /** 実行方法 {@value} */
050        protected static final String IN_INPUT_ONLY             = "5";
051        /** 実行方法 {@value} */
052        protected static final String IN_EXEC_ONLY              = "6";
053        /** 実行方法 {@value} */
054        protected static final String IN_INPUT_EXEC             = "7";
055        /** 実行方法 {@value} */
056        protected static final String RFID_PRINT                = "A";
057        /** 実行方法 {@value} */
058        protected static final String RFID_ALLPRINT             = "B";
059        /** 実行方法 {@value} */
060        protected static final String RFID_ALLERASE             = "C";
061        /** 実行方法 {@value} */
062        protected static final String RFID_SEQERASE             = "D";
063
064        /** 最大シート数 {@value} */
065        protected static final int MAX_SHEETS_PER_FILE  = 256; // 5.1.2.0 (2010/01/01)
066
067        private String          ykno            = null;
068        private String          systemId        = null;
069        private DBTableModel body               = null;
070        private DBTableModel header             = null;
071        private DBTableModel footer             = null;
072        private String          listId          = null;
073        private String          pdfPasswd       = null;
074        private String          lang            = null;
075        private String          threadId        = null;
076        private String          templateName = null;
077        private String          outputType      = null;
078        private String          printerName     = null;
079        private String          outputName      = null;
080        private boolean         fglocal         = false;
081        private boolean         fgcut           = false;
082        private QueueManager manager    = null;
083        private String          prgdir          = null;         // 4.3.3.0 (2008/10/01) 板金RFID対応。
084        private String          prgfile         = null;         // 4.3.3.0 (2008/10/01)
085        private String          prtid           = null;         // 4.3.3.0 (2008/10/01)
086
087        private int                     pageCnt         = 0;            // 5.1.2.0 (2010/01/01) 処理したページ数
088        private int                     rowCnt          = 0;            // 5.1.2.0 (2010/01/01) 処理した行数
089        private boolean         isDataEnd       = false;        // 5.1.2.0 (2010/01/01) 全データが処理されたか (メソッド名と同じ変数名変更)
090
091        private boolean         useSheetName = false;   // 5.7.6.2 (2014/05/16) PAGEBREAKカラムの値を、シート名として使うかどうか。
092
093        private final StringBuilder errMsg = new StringBuilder();
094
095        /**
096         * 要求NOをセットします。
097         *
098         * @param ykno 要求NO
099         */
100        public void setYkno( final String ykno ) {
101                this.ykno = ykno;
102        }
103
104        /**
105         * 要求NOを取得します。
106         *
107         * @return      要求NO
108         */
109        public String getYkno() {
110                return ykno;
111        }
112
113        /**
114         * システムIDをセットします。
115         *
116         * @param systemId システムID
117         */
118        public void setSystemId( final String systemId ) {
119                this.systemId = systemId;
120        }
121
122        /**
123         * システムIDを取得します。
124         *
125         * @return  StringシステムID
126         */
127        public String getSystemId() {
128                return systemId;
129        }
130
131        /**
132         * ボディー部分のDBTableModelをセットします。
133         *
134         * @param body DBTableModelオブジェクト
135         */
136        public void setBody( final DBTableModel body ) {
137                this.body = body;
138        }
139
140        /**
141         * ボディー部分のDBTableModelを取得します。
142         *
143         * @return      ボディー部分のDBTableModelオブジェクト
144         */
145        public DBTableModel getBody() {
146                return body;
147        }
148
149        /**
150         * ヘッダー部分のDBTableModelをセットします。
151         *
152         * @param header DBTableModelオブジェクト
153         */
154        public void setHeader( final DBTableModel header ) {
155                this.header = header;
156        }
157
158        /**
159         * ヘッダー部分のDBTableModelを取得します。
160         *
161         * @return      ヘッダー部分のDBTableModelオブジェクト
162         */
163        public DBTableModel getHeader() {
164                return header;
165        }
166
167        /**
168         * フッター部分のDBTableModelをセットします。
169         *
170         * @param footer DBTableModelオブジェクト
171         */
172        public void setFooter( final DBTableModel footer ) {
173                this.footer = footer;
174        }
175
176        /**
177         * フッター部分のDBTableModelを取得します。
178         *
179         * @return      フッター部分のDBTableModelオブジェクト
180         */
181        public DBTableModel getFooter() {
182                return footer;
183        }
184
185        /**
186         * 帳票IDをセットします。
187         *
188         * @param listId 帳票ID
189         */
190        public void setListId( final String listId ) {
191                this.listId = listId;
192        }
193
194        /**
195         * 帳票IDを取得します。
196         *
197         * @return 帳票ID
198         */
199        public String getListId() {
200                return listId;
201        }
202
203        /**
204         * PDFパスワードをセットします。
205         *
206         * @param pdfPasswd PDFパスワード
207         */
208        public void setPdfPasswd( final String pdfPasswd ) {
209                this.pdfPasswd = pdfPasswd;
210        }
211
212        /**
213         * PDFパスワードを取得します。
214         *
215         * @return PDFパスワード
216         */
217        public String getPdfPasswd() {
218                return pdfPasswd;
219        }
220
221        /**
222         * 言語をセットします。
223         *
224         * @param lang 言語
225         */
226        public void setLang( final String lang ) {
227                this.lang = lang;
228        }
229
230        /**
231         * 言語を取得します。
232         *
233         * @return 言語
234         */
235        public String getLang() {
236                return lang;
237        }
238
239        /**
240         * 雛形ファイル名をセットします。
241         *
242         * @param templateName 雛形ファイル名
243         */
244        public void setTemplateName( final String templateName ) {
245                this.templateName = templateName;
246        }
247
248        /**
249         * 雛形ファイル名を取得します。
250         *
251         * @return 帳票雛形ファイル名
252         */
253        public String getTemplateName() {
254                return templateName;
255        }
256
257        /**
258         * 実行方法をセットします。
259         *
260         * @param outputType 実行方法
261         */
262        public void setOutputType( final String outputType ) {
263                this.outputType = outputType;
264        }
265
266        /**
267         * 出力タイプを取得します。
268         *
269         * @return 出力タイプ
270         */
271        public String getOutputType() {
272                return outputType;
273        }
274
275        /**
276         * プリンター名をセットします。
277         *
278         * @param  printerName プリンター名
279         */
280        public void setPrinterName( final String printerName ) {
281                this.printerName = printerName;
282        }
283
284        /**
285         * プリンター名を取得します。
286         *
287         * @return プリンタ名
288         */
289        public String getPrinterName() {
290                return printerName;
291        }
292
293        /**
294         * 処理要求を処理するスレッドIDをセットします。
295         *
296         * @param threadId スレッドID
297         */
298        public void setThreadId( final String threadId ) {
299                this.threadId = threadId;
300        }
301
302        /**
303         * 処理要求を処理するスレッドIDを取得します。
304         *
305         * @return スレッドID
306         */
307        public String getThreadId() {
308                return threadId;
309        }
310
311        /**
312         * 出力ファイル名をセットします。
313         *
314         * @param outputName 出力ファイル名
315         */
316        public void setOutputName( final String outputName ) {
317                this.outputName = outputName;
318        }
319
320        /**
321         * 出力ファイル名を設定します。
322         * GE50に設定されていない場合は第四引数(要求番号)を利用する。
323         * その場合、タイプに応じた拡張子が自動設定される。
324         *
325         * ".xls" : OUT_ODS_EXCEL
326         * ".pdf" : OUT_ODS_PDF , OUT_ODS_PRINT_PDF
327         * ".ods" : OUT_ODS_ODS
328         * ".xml" : RFID_PRINT , RFID_ALLPRINT , RFID_ALLERASE , RFID_SEQERASE
329         *
330         * @og.rev 4.3.3.4 (2008/11/01) ODS出力対応
331         * @og.rev 5.4.3.0 (2011/12/26) RFIDデフォルト対応
332         * @og.rev 5.4.4.1 (2012/02/03) RFID拡張子変更
333         *
334         * @param       outputDir       出力ディレクトリ名
335         * @param       outputFile      出力ファイル名
336         * @param       type            タイプ
337         * @param       yokyu           要求番号(ファイル名が指定されていない場合のファイル名)
338         *
339         */
340        public void setOutputName( final String outputDir, final String outputFile, final String type, final String yokyu ){
341                StringBuilder filePath = new StringBuilder();
342                filePath.append( outputDir + File.separator );
343
344                if( outputFile == null || outputFile.length() == 0 ){ // ファイル名が指定されていない場合は要求番号を利用する。
345                        if( OUT_ODS_EXCEL.equals( type ) ){
346                                filePath.append( yokyu );
347                                filePath.append( ".xls" );
348                        }
349                        else if( OUT_ODS_PDF.equals( type ) || OUT_ODS_PRINT_PDF.equals( type ) ){
350                                filePath.append( yokyu );
351                                filePath.append( ".pdf" );
352                        }
353                        // 4.3.3.4 (2008/11/01) 追加
354                        else if( OUT_ODS_ODS.equals ( type ) ){
355                                filePath.append( yokyu );
356                                filePath.append( ".ods" );
357                        }
358                        // 5.4.3.0 (2011/12/26) 追加
359                        // 5.4.4.2 (2012/02/03) .txtではなく.xml
360                        else if( ExecQueue.RFID_PRINT.equals( type ) || ExecQueue.RFID_ALLPRINT.equals( type )
361                                        || ExecQueue.RFID_ALLERASE.equals( type ) || ExecQueue.RFID_SEQERASE.equals( type ) ) {
362                                filePath.append( yokyu );
363                                filePath.append( ".xml" ); //txt-xml
364                        }
365                }
366                else {
367                        filePath.append( outputFile );
368                }
369
370                this.outputName = filePath.toString();
371        }
372
373        /**
374         * 出力ファイル名を取得します。
375         *
376         * @og.rev 5.1.2.0 (2010/01/01) 256シートを超える場合に対応。2ファイル目以降は、_1、_2・・・をファイル名の後ろにつける
377         *
378         * @return 出力先ファイル名
379         */
380        public String getOutputName() {
381                if( pageCnt <= MAX_SHEETS_PER_FILE ) {
382                        return outputName;
383                }
384                else {
385                        StringBuilder fileName = new StringBuilder();
386
387                        int idx = outputName.lastIndexOf( '.' );
388                        String name = outputName.substring( 0, idx );
389                        String suffix = outputName.substring( idx );
390                        int addNo = (int)Math.ceil( (double)pageCnt/(double)MAX_SHEETS_PER_FILE ) - 1;
391
392                        fileName.append( name ).append( "_" ).append( addNo ).append( suffix );
393
394                        return fileName.toString();
395                }
396        }
397
398        /**
399         * 実行ファイルディレクトリを指定します。
400         *
401         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
402         *
403         * @param dir ディレクトリ
404         */
405        public void setPrgDir( final String dir ) {
406                this.prgdir = dir;
407        }
408
409        /**
410         * 実行ファイルディレクトリを取得します。
411         *
412         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
413         *
414         * @return プログラムディレクトリ
415         */
416        public String getPrgDir() {
417                return prgdir;
418        }
419
420        /**
421         * 実行ファイル名をセットします。
422         *
423         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
424         * @param       file    ファイル名
425         */
426        public void setPrgFile( final String file ) {
427                this.prgfile = file;
428        }
429
430        /**
431         * 実行ファイル名を取得します。
432         *
433         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
434         *
435         * @return プログラムファイル名
436         */
437        public String getPrgFile() {
438                return prgfile;
439        }
440
441        /**
442         * プリンタIDをセットします。
443         *
444         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
445         * @param       id      プリンタID
446         */
447        public void setPrtId( final String id ) {
448                this.prtid = id;
449        }
450
451        /**
452         * プリンタIDを取得します。
453         *
454         * @og.rev 4.3.3.0 (2008/10/01) 板金RFID対応
455         *
456         * @return プリンタID
457         */
458        public String getPrtId() {
459                return prtid;
460        }
461
462        /**
463         * ローカルリソース使用フラグをセットします(初期値:false)。
464         *
465         * @param       fglocal ローカルリソース使用フラグ[true:使用する/false:使用しない]
466         */
467        public void setFglocal( final boolean fglocal ) {
468                this.fglocal = fglocal;
469        }
470
471        /**
472         * ローカルリソース使用フラグを取得します。
473         *
474         * @return ロールリソース使用フラグ[true:使用する/false:使用しない]
475         */
476        public boolean isFglocal() {
477                return fglocal;
478        }
479
480        /**
481         * ページエンドカットフラグをセットします(初期値:false)。
482         *
483         * @param fgcut ページエンドカットの使用可否[true:使用/false:通常]
484         */
485        public void setFgcut( final boolean fgcut ) {
486                this.fgcut = fgcut;
487        }
488
489        /**
490         * ページエンドカットフラグを取得します。
491         *
492         * @return ページエンドカットフラグ
493         */
494        public boolean isFgcut() {
495                return fgcut;
496        }
497
498        /**
499         * PAGEBREAKカラムの値を、シート名として使うかどうかをセットします(初期値:false)。
500         *
501         * @og.rev 5.7.6.2 (2014/05/16) 新規追加
502         *
503         * @param useSheetName PAGEBREAKカラムのシート名使用可否[true:使用/false:使用しない]
504         */
505        public void setUseSheetName( final boolean useSheetName ) {
506                this.useSheetName = useSheetName;
507        }
508
509        /**
510         * PAGEBREAKカラムの値を、シート名として使うかどうかを取得します。
511         *
512         * @og.rev 5.7.6.2 (2014/05/16) 新規追加
513         *
514         * @return PAGEBREAKカラムのシート名使用可否[true:使用/false:使用しない]
515         */
516        public boolean isUseSheetName() {
517                return useSheetName;
518        }
519
520        /**
521         * キューマネージャーをセットします。
522         *
523         * @param manager キューマネージャー
524         */
525        public void setManager( final QueueManager manager ) {
526                this.manager = manager;
527        }
528
529        /**
530         * 帳票処理データをセットします。
531         * 既にテーブルモデルがセットされている場合は、再セットしません。
532         *
533         */
534        public void setData() {
535                if( body == null && manager != null ) {
536                        manager.set( this );
537                }
538        }
539
540        /**
541         * キューを実行中の状態に更新します。
542         *
543         */
544        public void setExecute() {
545                if( manager != null ) {
546                        manager.execute( this );
547                }
548        }
549
550        /**
551         * キューを完了済の状態に更新します。
552         *
553         */
554        public void setComplete() {
555                if( manager != null ) {
556                        manager.complete( this );
557                }
558        }
559
560        /**
561         * キューをエラーの状態に更新します。
562         */
563        public void setError() {
564                if( manager != null ) {
565                        manager.error( this );
566                }
567        }
568
569        /**
570         * エラーメッセージをセットします。
571         *
572         * @param msg エラーメッセージ
573         */
574        public void addMsg( final String msg ) {
575                errMsg.append( msg );
576        }
577
578        /**
579         * エラーメッセージを取得します。
580         *
581         * @return エラーメッセージ
582         */
583        public String getMsg() {
584                return errMsg.toString();
585        }
586
587        /**
588         * 処理したページ数を引数の分だけカウントアップします。
589         *
590         * @og.rev 5.1.2.0 (2010/01/01) 新規追加
591         *
592         * @param pgs カウントアップするページ数
593         */
594        public void addExecPageCnt( final int pgs ) {
595                pageCnt += pgs;
596        }
597
598        /**
599         * 処理したページ数を返します。
600         *
601         * @og.rev 5.1.2.0 (2010/01/01) 新規追加
602         *
603         * @return 処理したページ数
604         */
605        public int getExecPagesCnt() {
606                return pageCnt;
607        }
608
609        /**
610         * 処理した行数をセットします。
611         *
612         * @og.rev 5.1.2.0 (2010/01/01) 新規追加
613         *
614         * @param rws 処理した行数
615         */
616        public void setExecRowCnt( final int rws ) {
617                rowCnt = rws;
618        }
619
620        /**
621         * 処理した行数を返します。
622         *
623         * @og.rev 5.1.2.0 (2010/01/01) 新規追加
624         *
625         * @return 処理した行数
626         */
627        public int getExecRowCnt() {
628                return rowCnt;
629        }
630
631        /**
632         * 全ての行が処理されたかをセットします(初期値:false)。
633         *
634         * これは、処理結果が、256シートを超えていた場合、再度残りのデータについて
635         * 処理を行うかどうかの判定するために、利用します。
636         *
637         * @og.rev 5.1.2.0 (2010/01/01) 新規追加
638         *
639         * @param flag 全ての行が処理されたか
640         */
641        public void setEnd( final boolean flag ) {
642                isDataEnd = flag;
643        }
644
645        /**
646         * 全ての行が処理されているかを返します。
647         *
648         * これは、処理結果が、256シートを超えていた場合、再度残りのデータについて
649         * 処理を行うかどうかの判定するために、利用します。
650         *
651         * @og.rev 5.1.2.0 (2010/01/01) 新規追加
652         *
653         * @return 全ての行が処理されたか
654         */
655        public boolean isEnd() {
656                return isDataEnd;
657        }
658}