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            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    }