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    public class Selection_DBRADIO extends Selection_NULL {
049            private final long              DB_CACHE_TIME                   = (long)HybsSystem.sysInt( "DB_CACHE_TIME" ) ;
050    
051            private final boolean   isClass ;                       // クエリでクラス属æ?が指定されてã�?‚‹かどã�?�‹
052            private final boolean   isDisable ;                     // クエリで選択不可属æ?が指定されてã�?‚‹かどã�?�‹
053            private final long              createTime ;            // キャãƒ?‚·ュの破æ£?‚¿イミングを計るための作æ?時間
054    
055            private final Map<String,Integer> map  ;
056    
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                    label   = new String[count];
106                    cls             = new String[count];
107                    disabled= new String[count];
108                    map             = new LinkedHashMap<String,Integer>(count);
109    
110                    int len   = ( count > 0 ) ? cols[0].length : 0 ;
111                    isClass   = len > CLS ;                      // >= 3 と同意;
112                    isDisable = len > DISABLED ; // >= 4 と同意;
113    
114                    boolean useLabelData = false ;
115                    ResourceManager resource = null;
116                    if( len == 1 ) {
117                            useLabelData = true;
118                            resource = ResourceFactory.newInstance( lang );
119                    }
120    
121                    for( int i=0; i<count; i++ ) {
122                            String value = cols[i][VAL];
123                            if( useLabelData ) {
124                                    label[i] = resource.getShortLabel( value );
125                            }
126                            else {
127                                    label[i] = cols[i][LBL];
128                            }
129    
130                            if( isClass ) {
131                                    cls[i] = cols[i][CLS];
132                            }
133                            if( isDisable ) {
134                                    disabled[i] = cols[i][DISABLED];
135                            }
136    
137                            map.put( value, Integer.valueOf( i ) );
138                    }
139    
140                    createTime = System.currentTimeMillis() ;
141            }
142    
143            /**
144             * 初期値が選択済みの 選択肢(オプション)を返しますã?
145             * こã?オプションは、引数の値をå?期å?とするオプションタグを返しますã?
146             * ※ こã?クラスでは実è£?�•れてã�?�¾せんã€?
147             *
148             * @og.rev 5.7.7.1 (2014/06/13) Selection_NULL ã‚?継承するため、削除
149             *
150             * @param       selectValue     選択されてã�?‚‹値
151             * @param       seqFlag         シーケンスアクセス機è?のæŒ?®?
152             *
153             * @return  オプションタグ
154             */
155    //      public String getOption( final String selectValue,final boolean seqFlag ) {
156    //              String errMsg = "こã?クラスでは実è£?�•れてã�?�¾せんã€?;
157    //              throw new UnsupportedOperationException( errMsg );
158    //      }
159    
160            /**
161             * 初期値が選択済みの 選択肢(オプション)を返しますã?
162             * こã?オプションは、引数の値をå?期å?とするオプションタグを返しますã?
163             * ※ こã?クラスでは実è£?�•れてã�?�¾せんã€?
164             *
165             * @og.rev 5.1.3.0 (2010/02/01) 追åŠ?
166             * @og.rev 5.7.7.1 (2014/06/13) Selection_NULL ã‚?継承するため、削除
167             *
168             * @param   selectValue  選択されてã�?‚‹値
169             * @param       seqFlag         シーケンスアクセス機è?のæŒ?®?
170             * @param       useShortLabel   短ラベルのæŒ?®?
171             *
172             * @return  オプションタグ
173             */
174            @Override
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            @Override
191            public String getRadio( final String name,final String selectValue,final boolean useLabel ) {
192                    String inputTag = "<input type=\"radio\" name=\"" + name + "\" value=\"" ;
193                    StringBuilder buf = new StringBuilder( HybsSystem.BUFFER_LARGE );
194                    for( Map.Entry<String, Integer> e : map.entrySet() ) {
195                            String val = e.getKey();        // Ver 5.0.0.0 value �val
196                            int valIdx = e.getValue().intValue();
197                            if( useLabel ) {
198                                    buf.append( "<label" );
199                                    if( isClass && cls[valIdx] != null && cls[valIdx].length() > 0 ) {
200                                            buf.append( " class=\"" ).append( cls[valIdx] ).append( "\"" );
201                                    }
202                                    buf.append(  ">" );
203                            }
204                            buf.append( inputTag ).append( val ).append( "\"" );
205                            if( val.equals( selectValue ) ) {
206                                    buf.append( " checked=\"checked\"" );
207                            }
208    
209                            if( isDisable && disabled[valIdx] != null && disabled[valIdx].length() > 0 ) {
210                                    if( "false".equalsIgnoreCase( disabled[valIdx] ) || "0".equals( disabled[valIdx] ) ) {
211                                            buf.append( " disabled=\"disabled\"" );
212                                    }
213                            }
214    
215                            buf.append( "/>" );
216                            if( useLabel ) { buf.append( label[valIdx] ).append( "</label>" ); }
217                    }
218                    return buf.toString();
219            }
220    
221            /**
222             * 選択肢(value)に対するラベルを返しますã?
223             * 選択肢(value)がã?存在しなかったå?合ã?ã€?�¸択肢そã?もã?を返しますã?
224             * getValueLabel( XX ) は、getValueLabel( XX,false ) と同じですã?
225             *
226             * @og.rev 5.7.7.1 (2014/06/13) Selection_NULL ã‚?継承するため、削除
227             *
228             * @param   selectValue 選択肢の値
229             *
230             * @return  選択肢のラベル
231             * @see     #getValueLabel( String,boolean )
232             */
233    //      public String getValueLabel( final String selectValue ) {
234    //              return getValueLabel( selectValue,false );
235    //      }
236    
237            /**
238             * 選択肢(value)に対するラベルを返しますã?
239             * 選択肢(value)がã?存在しなかったå?合ã?ã€?�¸択肢そã?もã?を返しますã?
240             * こã?メソãƒ?ƒ‰では、短縮ラベルを返すかどã�?�‹を指定するフラグを指定しますã?
241             * getValueLabel( XX,false ) は、getValueLabel( XX ) と同じですã?
242             *
243             * @param       selectValue     選択肢の値
244             * @param       flag    短縮ラベルを使用する [true:使用する/false:しない]
245             *
246             * @return  選択肢のラベル
247             * @see     #getValueLabel( String )
248             */
249            @Override
250            public String getValueLabel( final String selectValue,final boolean flag ) {
251                    // マッチするアドレスを探すã?
252                    Integer sel = map.get( selectValue );
253    
254                    if( sel == null ) {
255                            return selectValue;
256                    }
257                    else {
258                            return label[sel.intValue()] ;
259                    }
260            }
261    
262            /**
263             * 選択肢(value)に対するラベルを返しますã?
264             * 選択肢(value)がã?存在しなかったå?合ã?ã€?�¸択肢そã?もã?を返しますã?
265             * getValueLabel( XX ) は、getValueLabel( XX,false ) と同じですã?
266             *
267             * @param   selectValue 選択肢の値
268             *
269             * @return  選択肢のラベル
270             * @see     #getValueLabel( String,boolean )
271             */
272            @Override
273            public String getRadioLabel( final String selectValue ) {
274    
275                    StringBuilder rtn = new StringBuilder( HybsSystem.BUFFER_MIDDLE );
276                    for( Map.Entry<String, Integer> e : map.entrySet() ) {
277                            String val = e.getKey();        // Ver 5.0.0.0 value �val
278                            if( val.equals( selectValue ) ) {
279                                    rtn.append( "â—? );
280                            }
281                            else {
282                                    rtn.append( "� );
283                            }
284                    }
285    
286                    return rtn.toString();
287            }
288    
289            /***
290             * マルチã?キーセレクトを使用するかどã�?�‹を返しますã?
291             * true?š使用する。false:使用しなã�?ですã?
292             * ただしã?実際に使用するかどã�?�‹は、HTML出力時に決めることがå?来ますã?
293             * ここでは、USE_MULTI_KEY_SELECT ã�?true で、USE_SIZE(=20)以上ã?場合に
294             * true を返しますã?
295             *
296             * @og.rev 5.7.7.1 (2014/06/13) Selection_NULL ã‚?継承するため、削除
297             *
298             * @return  選択リストで、ã?ルチã?キーセレクトを使用するかどã�?�‹(true:使用する)
299             */
300    //      public boolean useMultiSelect() {
301    //              return false;
302    //      }
303    
304            /**
305             * オブジェクトã?キャãƒ?‚·ュが時間å?れかどã�?�‹を返しますã?
306             * キャãƒ?‚·ュが時間å?ã‚?無効)であれば、true をã?有効であればã€?
307             * false を返しますã?
308             *
309             * @return  キャãƒ?‚·ュが時間å?れなã‚?true
310             */
311            @Override
312            public boolean isTimeOver() {
313                    return ( System.currentTimeMillis() - createTime ) > DB_CACHE_TIME ;
314            }
315    }