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