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.db;
017    
018    import java.util.LinkedHashMap;
019    import java.util.Map;
020    
021    import org.opengion.fukurou.db.DBUtil;
022    import org.opengion.fukurou.util.ApplicationInfo;
023    import org.opengion.hayabusa.common.HybsSystem;
024    import org.opengion.hayabusa.resource.ResourceFactory;
025    import org.opengion.hayabusa.resource.ResourceManager;
026    
027    /**
028     * ãƒ??タのコード情報を取り扱ã�?‚¯ラスですã?
029     *
030     * コードã?キーとラベルのæƒ??から、HTMLのメニューã‚?ƒªストを作æ?するための オプション
031     * タグを作æ?したりã?与えられたキーをもとに、チェãƒ?‚¯済みのオプションタグを作æ?したã‚?
032     * しますã?
033     * QUERYのそれぞれのカラãƒ??、以下ã?意味を持ちますã?
034     *  第?‘カラãƒ?å¿??) : ラジオボタンのキー(値)
035     *  第?’カラãƒ?      : ラベル(æŒ?®šされなã�??合ã?、ラベルリソースの短縮ラベルを使用しまã�?
036     *  第?“カラãƒ?      : クラス そã?オプションに色づけなどを行う為のæŒ?®šしますã?
037     *                     NULL(またã?、ゼロæ–?­—å?)の場合ã?ã€?�©用されませんã€?
038     *  第?”カラãƒ?      : こã?値は'false'又ã?'0'である場合にそã?ラジオボタンを選択不可にしますã?
039     *                     NULL(またã?、ゼロæ–?­—å?)の場合ã?ã€?�¸択可能になりますã?
040     *
041     * @og.group 選択データ制御
042     *
043     * @version  4.0
044     * @author   Hiroki Nakamura
045     * @since    JDK5.0,
046     */
047    public class Selection_DBRADIO implements Selection {
048            private final long              DB_CACHE_TIME                   = (long)HybsSystem.sysInt( "DB_CACHE_TIME" ) ;
049    
050            private final boolean   isClass ;                       // クエリでクラス属æ?が指定されてã�?‚‹かどã�?�‹
051            private final boolean   isDisable ;                     // クエリで選択不可属æ?が指定されてã�?‚‹かどã�?�‹
052            private final long              createTime ;            // キャãƒ?‚·ュの破æ£?‚¿イミングを計るための作æ?時間
053    
054            private final Map<String,Integer> map  ;
055    
056    //      private final String[]  value   ;               // 値           // 5.1.9.0 (2010/08/01) å»?­¢
057            private final String[]  label   ;               // ラベル
058            private final String[]  cls     ;               // クラス
059            private final String[]  disabled ;              // ラジオ単位ã?disable
060    
061            private static final int VAL            = 0;
062            private static final int LBL            = 1;
063            private static final int CLS            = 2;
064            private static final int DISABLED       = 3;
065    
066            private static final ApplicationInfo appInfo;
067            static {
068                    /** コネクションにアプリケーションæƒ??を追記するかどã�?�‹æŒ?®?*/
069                    boolean USE_DB_APPLICATION_INFO  = HybsSystem.sysBool( "USE_DB_APPLICATION_INFO" ) ;
070    
071                    // 3.8.7.0 (2006/12/15) アクセスログ取得ã?為,ApplicationInfoオブジェクトを設å®?
072                    if( USE_DB_APPLICATION_INFO ) {
073                            String SYSTEM_ID = HybsSystem.sys( "SYSTEM_ID" );
074                            appInfo = new ApplicationInfo();
075                            // ユーザーID,IPアドレス,ホスト名
076                            appInfo.setClientInfo( SYSTEM_ID,HybsSystem.HOST_ADRS,HybsSystem.HOST_NAME );
077                            // 画面ID,操ä½?プログラãƒ?D
078                            appInfo.setModuleInfo( "Selection_DBRADIO",null,null );
079                    }
080                    else {
081                            appInfo = null;
082                    }
083            }
084    
085            /**
086             * コンストラクター
087             *
088             * ?¤?¢検索用の?³?±?¬æ–?‚’与えて、å?期化しますã?
089             * SQLæ–??、KEY,SNAMEで、SNAMEが設定されてなã�??合ã?、LNAMEを使用しますã?
090             * LNAME はã€??常の値を返す場合に、SNAME は、ä¸?¦§表示の値を返す場合に使用しますã?
091             * 特別に、KEY のみの場合ã?、lang に基づã�?ResourceManager から?—ベルを取得しますã?
092             * ただしã?そã?場合ã?、オーナã?(SYSTEM_ID)は選べませんã€?
093             *
094             * @og.rev 5.1.9.0 (2010/08/01) String[] value å»?­¢
095             *
096             * @param       newQuery        DB検索(SQL)æ–?­—å?
097             * @param dbid  ãƒ??タベã?ス接続å?ID
098             * @param lang  リソースを使用する場合ã?è¨?ª?
099             */
100            public Selection_DBRADIO( final String newQuery,final String dbid,final String lang ) {
101    
102                    String[][] cols = DBUtil.dbExecute( newQuery,null,appInfo,dbid );
103                    int count = cols.length;
104    
105    //              value   = new String[count];                    // 5.1.9.0 (2010/08/01) K.H
106                    label   = new String[count];
107                    cls             = new String[count];
108                    disabled= new String[count];
109                    map             = new LinkedHashMap<String,Integer>(count);
110    
111                    int len = ( count > 0 ) ? cols[0].length : 0 ;
112                    isClass = ( len > CLS );             // >= 3 と同意;
113                    isDisable = ( len > DISABLED );      // >= 4 と同意;
114    
115                    boolean useLabelData = false ;
116                    ResourceManager resource = null;
117                    if( len == 1 ) {
118                            useLabelData = true;
119                            resource = ResourceFactory.newInstance( lang );
120                    }
121    
122                    for( int i=0; i<count; i++ ) {
123    //                      value[i] = cols[i][VAL];
124                            String value = cols[i][VAL];
125                            if( useLabelData ) {
126    //                              label[i] = resource.getShortLabel( value[i] );
127                                    label[i] = resource.getShortLabel( value );
128                            }
129                            else {
130                                    label[i] = cols[i][LBL];
131                            }
132    
133                            if( isClass ) {
134                                    cls[i] = cols[i][CLS];
135                            }
136                            if( isDisable ) {
137                                    disabled[i] = cols[i][DISABLED];
138                            }
139    
140    //                      map.put( value[i], Integer.valueOf( i ) );
141                            map.put( value, Integer.valueOf( i ) );
142                    }
143    
144                    createTime = System.currentTimeMillis() ;
145            }
146    
147            /**
148             * 初期値が選択済みの 選択肢(オプション)を返しますã?
149             * こã?オプションは、引数の値をå?期å?とするオプションタグを返しますã?
150             * ※ こã?クラスでは実è£?�•れてã�?�¾せんã€?
151             *
152             * @param       selectValue     選択されてã�?‚‹値
153             * @param       seqFlag         シーケンスアクセス機è?のæŒ?®?
154             *
155             * @return  オプションタグ
156             */
157            public String getOption( final String selectValue,final boolean seqFlag ) {
158                    String errMsg = "こã?クラスでは実è£?�•れてã�?�¾せんã€?;
159                    throw new UnsupportedOperationException( errMsg );
160            }
161    
162            /**
163             * 初期値が選択済みの 選択肢(オプション)を返しますã?
164             * こã?オプションは、引数の値をå?期å?とするオプションタグを返しますã?
165             * ※ こã?クラスでは実è£?�•れてã�?�¾せんã€?
166             *
167             * @og.rev 5.1.3.0 (2010/02/01) 追åŠ?
168             *
169             * @param   selectValue  選択されてã�?‚‹値
170             * @param       seqFlag         シーケンスアクセス機è?のæŒ?®?
171             * @param       useShortLabel   短ラベルのæŒ?®?
172             *
173             * @return  オプションタグ
174             */
175            public String getOption( final String selectValue,final boolean seqFlag, final boolean useShortLabel ) {
176                    String errMsg = "こã?クラスでは実è£?�•れてã�?�¾せんã€?;
177                    throw new UnsupportedOperationException( errMsg );
178            }
179    
180            /**
181             * 初期値が選択済みの 選択肢(オプション)を返しますã?
182             * こã?オプションは、引数の値をå?期å?とするオプションタグを返しますã?
183             *
184             * @param   name         ラジオの name
185             * @param   selectValue  選択されてã�?‚‹値
186             * @param   useLabel     ラベル表示の有無 [true:æœ?false:無]
187             *
188             * @return  オプションタグ
189             */
190            public String getRadio( final String name,final String selectValue,final boolean useLabel ) {
191                    String inputTag = "<input type=\"radio\" name=\"" + name + "\" value=\"" ;
192                    StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_LARGE );
193                    for( Map.Entry<String, Integer> e : map.entrySet() ) {
194                            String val = e.getKey();        // Ver 5.0.0.0 value �val
195                            int valIdx = e.getValue().intValue();
196                            if( useLabel ) {
197                                    buf.append( "<label" );
198                                    if( isClass && cls[valIdx] != null && cls[valIdx].length() > 0 ) {
199                                            buf.append( " class=\"" ).append( cls[valIdx] ).append( "\"" );
200                                    }
201                                    buf.append(  ">" );
202                            }
203                            buf.append( inputTag ).append( val ).append( "\"" );
204                            if( val.equals( selectValue ) ) {
205                                    buf.append( " checked=\"checked\"" );
206                            }
207    
208                            if( isDisable && disabled[valIdx] != null && disabled[valIdx].length() > 0 ) {
209                                    if( "false".equalsIgnoreCase( disabled[valIdx] ) || "0".equals( disabled[valIdx] ) ) {
210                                            buf.append( " disabled=\"disabled\"" );
211                                    }
212                            }
213    
214                            buf.append( "/>" );
215                            if( useLabel ) { buf.append( label[valIdx] ).append( "</label>" ); }
216                    }
217                    return buf.toString();
218            }
219    
220            /**
221             * 選択肢(value)に対するラベルを返しますã?
222             * 選択肢(value)がã?存在しなかったå?合ã?ã€?�¸択肢そã?もã?を返しますã?
223             * getValueLabel( XX ) は、getValueLabel( XX,false ) と同じですã?
224             *
225             * @param   selectValue 選択肢の値
226             *
227             * @return  選択肢のラベル
228             * @see     #getValueLabel( String,boolean )
229             */
230            public String getValueLabel( final String selectValue ) {
231                    return getValueLabel( selectValue,false );
232            }
233    
234            /**
235             * 選択肢(value)に対するラベルを返しますã?
236             * 選択肢(value)がã?存在しなかったå?合ã?ã€?�¸択肢そã?もã?を返しますã?
237             * こã?メソãƒ?ƒ‰では、短縮ラベルを返すかどã�?�‹を指定するフラグを指定しますã?
238             * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じですã?
239             *
240             * @param       selectValue     選択肢の値
241             * @param       flag    短縮ラベルを使用する [true:使用する/false:しない]
242             *
243             * @return  選択肢のラベル
244             * @see     #getValueLabel( String )
245             */
246            public String getValueLabel( final String selectValue,final boolean flag ) {
247                    // マッチするアドレスを探すã?
248                    Integer sel = map.get( selectValue );
249    
250                    if( sel == null ) {
251                            return selectValue;
252                    }
253                    else {
254                            return label[sel.intValue()] ;
255                    }
256            }
257    
258            /**
259             * 選択肢(value)に対するラベルを返しますã?
260             * 選択肢(value)がã?存在しなかったå?合ã?ã€?�¸択肢そã?もã?を返しますã?
261             * getValueLabel( XX ) は、getValueLabel( XX,false ) と同じですã?
262             *
263             * @param   selectValue 選択肢の値
264             *
265             * @return  選択肢のラベル
266             * @see     #getValueLabel( String,boolean )
267             */
268            public String getRadioLabel( final String selectValue ) {
269    
270                    StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
271                    for( Map.Entry<String, Integer> e : map.entrySet() ) {
272                            String val = e.getKey();        // Ver 5.0.0.0 value �val
273                            if( val.equals( selectValue ) ) {
274                                    rtn.append( "â—? );
275                            }
276                            else {
277                                    rtn.append( "� );
278                            }
279                    }
280    
281                    return rtn.toString();
282            }
283    
284            /***
285             * マルチã?キーセレクトを使用するかどã�?�‹を返しますã?
286             * true?š使用する。false:使用しなã�?ですã?
287             * ただしã?実際に使用するかどã�?�‹は、HTML出力時に決めることがå?来ますã?
288             * ここでは、USE_MULTI_KEY_SELECT ã�?true で、USE_SIZE(=20)以上ã?場合に
289             * true を返しますã?
290             *
291             * @return  選択リストで、ã?ルチã?キーセレクトを使用するかどã�?�‹(true:使用する)
292             */
293            public boolean useMultiSelect() {
294                    return false;
295            }
296    
297            /**
298             * オブジェクトã?キャãƒ?‚·ュが時間å?れかどã�?�‹を返しますã?
299             * キャãƒ?‚·ュが時間å?ã‚?無効)であれば、true をã?有効であればã€?
300             * false を返しますã?
301             *
302             * @return  キャãƒ?‚·ュが時間å?れなã‚?true
303             */
304            public boolean isTimeOver() {
305                    return ( System.currentTimeMillis() - createTime > DB_CACHE_TIME ) ;
306            }
307    }