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