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    }