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
058        private DBTableModel body;
059        private DBTableModel header;
060        private DBTableModel footer;
061
062        private boolean isEnd = false;
063        private String errMsg = null;
064
065        /**
066         * 帳票処理キューを作成します。
067         *
068         * @og.rev 5.1.6.0 (2010/05/01) 要求単位にスレッドを生成するようにします。
069         * @og.rev 5.7.6.2 (2014/05/16) PAGEBREAKカラムの値を、シート名として使うかどうかをセットします
070         */
071        public void create() {
072                ExecQueue queue = new ExecQueue();
073                queue.setSystemId( SYSTEM_ID );
074                queue.setYkno( "DIRECT_" + Long.toString( RANDOM.nextLong() & 0x7fffffffffffffffL ) );
075                queue.setListId( listId );
076                queue.setLang( lang );
077                queue.setOutputName( outputName );
078                queue.setOutputType( outputType );
079                // 5.1.6.0 (2010/05/01)
080                queue.setThreadId( "DIRECT_" + Long.toString( RANDOM.nextLong() & 0x7fffffffffffffffL ) );
081                queue.setTemplateName( templateName );
082                queue.setPrinterName( printerName );
083                queue.setFglocal( fglocal );
084                queue.setFgcut( fgcut );
085                queue.setUseSheetName( useSheetName );          // 5.7.6.2 (2014/05/16) PAGEBREAKカラムの値を、シート名として使うかどうかをセットします
086
087                queue.setBody( body );
088                queue.setHeader( header );
089                queue.setFooter( footer );
090
091                queue.setManager( this );
092
093                // 5.1.6.0 (2010/05/01)
094                ExecThreadManager.insertQueueOnNewThread( queue );
095        }
096
097        /**
098         * 帳票処理データをキューにセットします。
099         * 画面から発行する場合は、テーブルモデルを直接セットするので、
100         * ここでは何もしません。
101         *
102         * @param       queue   ExecQueueオブジェクト
103         */
104        public void set( final ExecQueue queue ) {
105                // 何もありません。(PMD エラー回避)
106        }
107
108        /**
109         * キューを実行中の状態に更新します。
110         * 画面から発行する場合は、実行中であることを知る必要がないため、
111         * ここでは何もしません。
112         *
113         * @param       queue   ExecQueueオブジェクト
114         */
115        public void execute( final ExecQueue queue ) {
116                // 何もありません。(PMD エラー回避)
117        }
118
119        /**
120         * キューを完了済の状態に更新します。
121         *
122         * @param       queue   ExecQueueオブジェクト
123         */
124        public void complete( final ExecQueue queue ) {
125                isEnd = true;
126        }
127
128        /**
129         * キューをエラーの状態に更新します。
130         *
131         * @param       queue   ExecQueueオブジェクト
132         */
133        public void error( final ExecQueue queue ) {
134                isEnd = true;
135                errMsg = queue.getMsg();
136        }
137
138        /**
139         * 処理が完了してするまでスレッドを待ち状態にします。
140         * エラーが発生した場合は、例外が発生します。
141         * また、REPORT_DAEMON_TIMEOUTで指定された期間処理が終了しない場合は、
142         * タイムアウトエラーとなります。
143         *
144         */
145        public void waitExec() {
146                long start = System.currentTimeMillis();
147                while( true ) {
148                        if( isEnd ) {
149                                if( errMsg == null ) {
150                                        break;
151                                }
152                                else {
153                                        throw new HybsSystemException( errMsg );
154                                }
155                        }
156
157                        if( (int)(System.currentTimeMillis() - start) > timeout * 1000 ) {
158                                throw new HybsSystemException( "帳票処理でタイムアウトになりました" );
159                        }
160                        try {
161                                Thread.sleep( 100 );
162                        }
163                        catch( InterruptedException ex ) { }
164                }
165        }
166
167        /**
168         * 帳票IDを設定します。
169         *
170         * @param listId 帳票ID
171         */
172        public final void setListId( final String listId ) {
173                this.listId = listId;
174        }
175
176        /**
177         * 言語を設定します。
178         *
179         * @param lang 言語
180         */
181        public void setLang( final String lang ) {
182                this.lang = lang;
183        }
184
185        /**
186         * 出力ファイル名を設定します。
187         *
188         * @param outputName 出力ファイル名
189         */
190        public final void setOutputName( final String outputName ) {
191                this.outputName = outputName;
192        }
193
194        /**
195         * 実行方法を設定します。
196         *
197         * @param outputType 実行方法
198         */
199        public final void setOutputType( final String outputType ) {
200                this.outputType = outputType;
201        }
202
203        /**
204         * 雛形ファイル名を設定します。
205         *
206         * @param templateName 雛形ファイル名
207         */
208        public final void setTemplateName( final String templateName ) {
209                this.templateName = templateName;
210        }
211
212        /**
213         * 出力先のプリンタ名を設定します。
214         *
215         * @param       printerName     出力先のプリンタ名
216         */
217        public final void setPrinterName( final String printerName ) {
218                this.printerName = printerName;
219        }
220
221        /**
222         * ローカルリソースの使用可否を設定します。
223         *
224         * @param fglocal 使用可否[true/false]
225         */
226        public void setFglocal( final boolean fglocal ) {
227                this.fglocal = fglocal;
228        }
229
230        /**
231         * ページエンドカットを行うかを設定します。
232         *
233         * @param fgcut ページエンドカットの使用可否[true:使用/false:通常]
234         */
235        public void setFgcut( final boolean fgcut ) {
236                this.fgcut = fgcut;
237        }
238
239        /**
240         * PAGEBREAKカラムの値を、シート名として使うかどうかをセットします(初期値:false)。
241         *
242         * @og.rev 5.7.6.2 (2014/05/16) 新規追加
243         *
244         * @param useSheetName PAGEBREAKカラムのシート名使用可否[true:使用/false:使用しない]
245         */
246        public void setUseSheetName( final boolean useSheetName ) {
247                this.useSheetName = useSheetName;
248        }
249
250        /**
251         * ボディーのテーブルモデルを設定します。
252         *
253         * @param body DBTableModelオブジェクト
254         */
255        public void setBody( final DBTableModel body ) {
256                this.body = body;
257        }
258
259        /**
260         * ヘッダーのテーブルモデルを設定します。
261         *
262         * @param header DBTableModelオブジェクト
263         */
264        public void setHeader( final DBTableModel header ) {
265                this.header = header;
266        }
267
268        /**
269         * フッターのテーブルモデルを設定します。
270         *
271         * @param footer DBTableModelオブジェクト
272         */
273        public void setFooter( final DBTableModel footer ) {
274                this.footer = footer;
275        }
276}