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.util.Random;
019
020import org.opengion.hayabusa.common.HybsSystem;
021import org.opengion.hayabusa.common.HybsSystemException;
022import org.opengion.hayabusa.db.DBTableModel;
023
024/**
025 * 画面から直接キューを作成するためのクラスです。
026 * 各種設定値を直接指定することでDBのマスタ設定を行うことなく帳票出力を行います。
027 * 現時点では、出力系の処理しか対応していません。
028 *
029 * ここで登録されたキューは、別スレッドで処理されるため、#create()メソッドを呼び出した後は、
030 * #waitExec()メソッドを呼び出し、処理の終了に同期させる必要があります。
031 * エラーが発生した場合は、HybsSystemExceptionを発生します。
032 *
033 * また、処理のタイムアウトは、システムリソースのREPORT_DAEMON_TIMEOUTで設定します。
034 *
035 * @og.group 帳票システム
036 *
037 * @version  4.0
038 * @author   Hiroki.Nakamura
039 * @since    JDK1.6
040 */
041public class QueueManager_DIRECT implements QueueManager {
042
043        private final int timeout = HybsSystem.sysInt( "REPORT_DAEMON_TIMEOUT" );
044
045        private static final String SYSTEM_ID = HybsSystem.sys( "SYSTEM_ID" );
046        private static final Random RANDOM = new Random();
047
048        private String listId;
049        private String outputName;
050        private String lang;
051        private String outputType;
052        private String templateName;
053        private String printerName;
054        private boolean fglocal;
055        private boolean fgcut;
056        private boolean useSheetName;   // 5.7.6.2 (2014/05/16) PAGEBREAKカラムの値を、シート名として使うかどうか。
057        private String storageType;             // 5.10.9.0 (2019/03/01) ADD
058        private String bucketName;              // 5.10.9.0 (2019/03/01) ADD
059        
060        private DBTableModel body;
061        private DBTableModel header;
062        private DBTableModel footer;
063
064        private boolean isEnd = false;
065        private String errMsg = null;
066
067        /**
068         * 帳票処理キューを作成します。
069         *
070         * @og.rev 5.1.6.0 (2010/05/01) 要求単位にスレッドを生成するようにします。
071         * @og.rev 5.7.6.2 (2014/05/16) PAGEBREAKカラムの値を、シート名として使うかどうかをセットします
072         * @og.rev 5.10.9.0 (2019/03/01) クラウドストレージ対応の追加。
073         */
074        public void create() {
075                ExecQueue queue = new ExecQueue();
076                queue.setSystemId( SYSTEM_ID );
077                queue.setYkno( "DIRECT_" + Long.toString( RANDOM.nextLong() & 0x7fffffffffffffffL ) );
078                queue.setListId( listId );
079                queue.setLang( lang );
080                queue.setOutputName( outputName );
081                queue.setOutputType( outputType );
082                // 5.1.6.0 (2010/05/01)
083                queue.setThreadId( "DIRECT_" + Long.toString( RANDOM.nextLong() & 0x7fffffffffffffffL ) );
084                queue.setTemplateName( templateName );
085                queue.setPrinterName( printerName );
086                queue.setFglocal( fglocal );
087                queue.setFgcut( fgcut );
088                queue.setUseSheetName( useSheetName );          // 5.7.6.2 (2014/05/16) PAGEBREAKカラムの値を、シート名として使うかどうかをセットします
089                queue.setStorageType( storageType );            // 5.10.9.0 (2019/03/01) ADD
090                queue.setBuketName( bucketName );                       // 5.10.9.0 (2019/03/01) ADD
091                
092                queue.setBody( body );
093                queue.setHeader( header );
094                queue.setFooter( footer );
095
096                queue.setManager( this );
097
098                // 5.1.6.0 (2010/05/01)
099                ExecThreadManager.insertQueueOnNewThread( queue );
100        }
101
102        /**
103         * 帳票処理データをキューにセットします。
104         * 画面から発行する場合は、テーブルモデルを直接セットするので、
105         * ここでは何もしません。
106         *
107         * @param       queue   ExecQueueオブジェクト
108         */
109        public void set( final ExecQueue queue ) {
110                // 何もありません。(PMD エラー回避)
111        }
112
113        /**
114         * キューを実行中の状態に更新します。
115         * 画面から発行する場合は、実行中であることを知る必要がないため、
116         * ここでは何もしません。
117         *
118         * @param       queue   ExecQueueオブジェクト
119         */
120        public void execute( final ExecQueue queue ) {
121                // 何もありません。(PMD エラー回避)
122        }
123
124        /**
125         * キューを完了済の状態に更新します。
126         *
127         * @param       queue   ExecQueueオブジェクト
128         */
129        public void complete( final ExecQueue queue ) {
130                isEnd = true;
131        }
132
133        /**
134         * キューをエラーの状態に更新します。
135         *
136         * @param       queue   ExecQueueオブジェクト
137         */
138        public void error( final ExecQueue queue ) {
139                isEnd = true;
140                errMsg = queue.getMsg();
141        }
142
143        /**
144         * 処理が完了してするまでスレッドを待ち状態にします。
145         * エラーが発生した場合は、例外が発生します。
146         * また、REPORT_DAEMON_TIMEOUTで指定された期間処理が終了しない場合は、
147         * タイムアウトエラーとなります。
148         *
149         */
150        public void waitExec() {
151                long start = System.currentTimeMillis();
152                while( true ) {
153                        if( isEnd ) {
154                                if( errMsg == null ) {
155                                        break;
156                                }
157                                else {
158                                        throw new HybsSystemException( errMsg );
159                                }
160                        }
161
162                        if( (int)(System.currentTimeMillis() - start) > timeout * 1000 ) {
163                                throw new HybsSystemException( "帳票処理でタイムアウトになりました" );
164                        }
165                        try {
166                                Thread.sleep( 100 );
167                        }
168                        catch( InterruptedException ex ) { }
169                }
170        }
171
172        /**
173         * 帳票IDを設定します。
174         *
175         * @param listId 帳票ID
176         */
177        public final void setListId( final String listId ) {
178                this.listId = listId;
179        }
180
181        /**
182         * 言語を設定します。
183         *
184         * @param lang 言語
185         */
186        public void setLang( final String lang ) {
187                this.lang = lang;
188        }
189
190        /**
191         * 出力ファイル名を設定します。
192         *
193         * @param outputName 出力ファイル名
194         */
195        public final void setOutputName( final String outputName ) {
196                this.outputName = outputName;
197        }
198
199        /**
200         * 実行方法を設定します。
201         *
202         * @param outputType 実行方法
203         */
204        public final void setOutputType( final String outputType ) {
205                this.outputType = outputType;
206        }
207
208        /**
209         * 雛形ファイル名を設定します。
210         *
211         * @param templateName 雛形ファイル名
212         */
213        public final void setTemplateName( final String templateName ) {
214                this.templateName = templateName;
215        }
216
217        /**
218         * 出力先のプリンタ名を設定します。
219         *
220         * @param       printerName     出力先のプリンタ名
221         */
222        public final void setPrinterName( final String printerName ) {
223                this.printerName = printerName;
224        }
225
226        /**
227         * ローカルリソースの使用可否を設定します。
228         *
229         * @param fglocal 使用可否[true/false]
230         */
231        public void setFglocal( final boolean fglocal ) {
232                this.fglocal = fglocal;
233        }
234
235        /**
236         * ページエンドカットを行うかを設定します。
237         *
238         * @param fgcut ページエンドカットの使用可否[true:使用/false:通常]
239         */
240        public void setFgcut( final boolean fgcut ) {
241                this.fgcut = fgcut;
242        }
243
244        /**
245         * PAGEBREAKカラムの値を、シート名として使うかどうかをセットします(初期値:false)。
246         *
247         * @og.rev 5.7.6.2 (2014/05/16) 新規追加
248         *
249         * @param useSheetName PAGEBREAKカラムのシート名使用可否[true:使用/false:使用しない]
250         */
251        public void setUseSheetName( final boolean useSheetName ) {
252                this.useSheetName = useSheetName;
253        }
254
255        /**
256         * ボディーのテーブルモデルを設定します。
257         *
258         * @param body DBTableModelオブジェクト
259         */
260        public void setBody( final DBTableModel body ) {
261                this.body = body;
262        }
263
264        /**
265         * ヘッダーのテーブルモデルを設定します。
266         *
267         * @param header DBTableModelオブジェクト
268         */
269        public void setHeader( final DBTableModel header ) {
270                this.header = header;
271        }
272
273        /**
274         * フッターのテーブルモデルを設定します。
275         *
276         * @param footer DBTableModelオブジェクト
277         */
278        public void setFooter( final DBTableModel footer ) {
279                this.footer = footer;
280        }
281        
282        /**
283         * 保存先のストレージタイプを設定します。
284         * 
285         * @og.rev 5.10.9.0 (2019/03/01) 新規追加
286         * 
287         * @param storage 保存先ストレージタイプ
288         */
289        public void setStorageType( final String storage ) {
290                this.storageType = storage;
291        }
292        
293        /**
294         * 保存先のバケット名を指定します。
295         * 
296         * @og.rev 5.10.9.0 (2019/03/01) 新規追加
297         * 
298         * @param bucket 保存先バケット名
299         */
300        public void setBucketName( final String bucket ) {
301                this.bucketName = bucket;
302        }
303}