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 org.opengion.hayabusa.common.HybsSystem ;
019    import org.opengion.hayabusa.common.HybsSystemException ;
020    import org.opengion.hayabusa.common.SystemManager ;
021    import org.opengion.hayabusa.resource.CodeData;
022    import org.opengion.fukurou.util.Cleanable;
023    
024    import java.util.Map;
025    import java.util.WeakHashMap ;
026    
027    /**
028     * Selectionオブジェクトを取得する為に使用するファクトリクラスですã?
029     *
030     * Selectionオブジェクãƒ?のキー(codeName)をå?に、オブジェクトをキャãƒ?‚·ュ管ç�?
031     * することがã?主な機è?ですã?
032     *
033     * @og.rev 3.5.5.7 (2004/05/10) 新規作æ?
034     * @og.group 選択データ制御
035     *
036     * @version  4.0
037     * @author   Kazuhiko Hasegawa
038     * @since    JDK5.0,
039     */
040    public final class SelectionFactory {
041            // private static final Map<String,Selection>  codeMap = new WeakHashMap<String,Selection>();
042            private static final Map<String,Selection>        dbMap           = new WeakHashMap<String,Selection>( HybsSystem.BUFFER_SMALL );
043            private static final Map<String,Selection>        dbRadioMap      = new WeakHashMap<String,Selection>( HybsSystem.BUFFER_SMALL ); // 4.3.3.6 (2008/11/15)
044    
045            // 4.0.0 (2005/01/31) Cleanable インターフェースによる初期化å?ç�?
046            static {
047                    Cleanable clr = new Cleanable() {
048                            public void clear() {
049                                    SelectionFactory.clear();
050                            }
051                    };
052    
053                    SystemManager.addCleanable( clr );
054            }
055    
056            /**
057             *  ãƒ?ƒ•ォルトコンストラクターをprivateにしてã€?
058             *  オブジェクトã?生æ?をさせなã�?‚ˆã�?�«するã€?
059             *
060             */
061            private SelectionFactory() {
062            }
063    
064            /**
065             * コードデータオブジェクトよりã?コードリソースSelectionオブジェクトを構築しますã?
066             *
067             * @og.rev 4.0.0.0 (2007/11/07) DBColumnにSelectionオブジェクトをキャãƒ?‚·ュするように変更
068             *
069             * @param   cdData CodeData コードデータ
070             *
071             * @return  Selectionオブジェクãƒ?
072             */
073    //      public static Selection newCodeSelection( final CodeData cdData ) {
074    //              String key = cdData.getColumn() ;
075    //              Selection select = codeMap.get( key );
076    //              if( select == null ) {
077    //                      synchronized( codeMap ) {
078    //                              select = new Selection_CODE( cdData );
079    //                              codeMap.put( key,select );
080    //                      }
081    //              }
082    //              return select;
083    //      }
084    
085            /**
086             * DB検索(SQL)æ–?­—å?より、データベã?スSelectionオブジェクトを構築しますã?
087             * Selection_DB では、検索行毎ã?クエリーがあるためã?name + query でキャãƒ?‚·ュしますã?
088             *
089             * @og.rev 4.0.0.0 (2006/11/15) lang 属æ?を追åŠ?�—ますã?
090             *
091             * @param   query DB検索(SQL)æ–?­—å?
092             * @param       dbid  ãƒ??タベã?ス接続å?ID
093             * @param       lang  リソースを使用する場合ã?è¨?ª?
094             *
095             * @return  Selectionオブジェクãƒ?
096             */
097            public static Selection newDBSelection( final String query,final String dbid,final String lang ) {
098                    String key = query+dbid+lang;
099                    Selection select = dbMap.get( key );
100    
101                    if( select == null || select.isTimeOver() ) {
102                            synchronized( dbMap ) {
103                                    select = new Selection_DB( query,dbid,lang );
104                                    dbMap.put( key,select );
105                            }
106                    }
107                    return select;
108            }
109    
110            /**
111             * DB検索(SQL)æ–?­—å?より、データベã?スSelectionオブジェクトを構築しますã?
112             * Selection_DB では、検索行毎ã?クエリーがあるためã?name + query でキャãƒ?‚·ュしますã?
113             *
114             * @og.rev 4.3.3.6 (2008/11/15) 新規作æ?
115             *
116             * @param   query DB検索(SQL)æ–?­—å?
117             * @param       dbid  ãƒ??タベã?ス接続å?ID
118             * @param       lang  リソースを使用する場合ã?è¨?ª?
119             *
120             * @return  Selectionオブジェクãƒ?
121             */
122            public static Selection newDBRadioSelection( final String query,final String dbid,final String lang ) {
123                    String key = query+dbid+lang;
124                    Selection select = dbRadioMap.get( key );
125    
126                    if( select == null || select.isTimeOver() ) {
127                            synchronized( dbRadioMap ) {
128                                    select = new Selection_DBRADIO( query,dbid,lang );
129                                    dbRadioMap.put( key,select );
130                            }
131                    }
132                    return select;
133            }
134    
135            /**
136             * å�?¨®Selectionオブジェクトを構築しますã?
137             * ここでは、Selectionオブジェクトã?タイプがã€?KEYVAL,HM,NUM,YMD)につã�?�¦作æ?されますã?
138             * ここで作æ?されるオブジェクトã?、この、SelectionFactoryではキャãƒ?‚·ュしませんã€?
139             * 各RendererやEditorがå?有されてã�?‚‹ので、そちらでキャãƒ?‚·ュされてã�?�¾すã?
140             * type が指定ã?キーワード以外ã?場合ã?、Exception が返されますã?
141             * ※ type="NULL" も使用可能ですã?これは、どんな場合でもã?引数の param を返す Selection
142             * オブジェクトを返しますã?å†?ƒ¨çš?�«、CodeDataが存在しなã�??合など、エラーメãƒ?‚»ージã‚?
143             * 引数に与えて修正をä¿?�™ようなケースで使用しますã?
144             *
145             * ※ æŒ?®šã?タイプが存在しなã�??合ã?HybsSystemException ã�?throw されますã?
146             *
147             * @og.rev 5.7.3.0 (2014/02/07) 新規作æ?
148             *
149             * @param   type  Selectionオブジェクトã?タイãƒ?KEYVAL,HM,NUM,YMD)
150             * @param       param パラメータ
151             *
152             * @return  Selectionオブジェクãƒ?
153             */
154            public static Selection newSelection( final String type,final String param ) {
155                    Selection select = null;
156                    if( "KEYVAL".equalsIgnoreCase( type ) ) {
157                            select = new Selection_KEYVAL( param );
158                    }
159                    else if( "HM".equalsIgnoreCase( type ) ) {
160                            select = new Selection_HM( param );
161                    }
162                    else if( "NUM".equalsIgnoreCase( type ) ) {
163                            select = new Selection_NUM( param );
164                    }
165                    else if( "YMD".equalsIgnoreCase( type ) ) {
166                            select = new Selection_YMD( param );
167                    }
168                    else if( "NULL".equalsIgnoreCase( type ) ) {
169                            select = new Selection_NULL( param );
170                    }
171                    else {
172                            select = new Selection_NULL( param );
173                            String errMsg = "æŒ?®šã?タイプ[" + type + "]がã?存在しません。タイプä¸?¦§=[KEYVAL,HM,NUM,YMD]" + HybsSystem.CR ;
174                            throw new HybsSystemException( errMsg );
175                    }
176    
177                    return select;
178            }
179    
180            /**
181             * å�?¨®Selectionオブジェクトを構築しますã?
182             * ここでは、Selectionオブジェクトã?タイプがã€?MENU,RADIO)につã�?�¦作æ?されますã?
183             * ここで作æ?されるオブジェクトã?、この、SelectionFactoryではキャãƒ?‚·ュしませんã€?
184             * 各RendererやEditorがå?有されてã�?‚‹ので、そちらでキャãƒ?‚·ュされてã�?�¾すã?
185             * type が指定ã?キーワード以外ã?場合ã?、Exception が返されますã?
186             * codeData オブジェクトが null の場合ã?、Selectionオブジェクãƒ?は null が返されますã?
187             *
188             * ※ æŒ?®šã?タイプが存在しなã�??合ã?HybsSystemException ã�?throw されますã?
189             *
190             * @og.rev 5.7.3.0 (2014/02/07) 新規作æ?
191             *
192             * @param   type  Selectionオブジェクトã?タイãƒ?MENU,RADIO)
193             * @param       codeData CodeDataオブジェクãƒ?
194             *
195             * @return  Selectionオブジェクãƒ?
196             */
197            public static Selection newSelection( final String type,final CodeData codeData ) {
198                    Selection select = null;
199                    if( codeData != null ) {
200                            if( "MENU".equalsIgnoreCase( type ) ) {
201                                    select = new Selection_CODE( codeData );
202                            }
203                            else if( "RADIO".equalsIgnoreCase( type ) ) {
204                                    select = new Selection_RADIO( codeData );
205                            }
206                            else {
207                                    String errMsg = "æŒ?®šã?タイプ[" + type + "]がã?存在しません。タイプä¸?¦§=[MENU,RADIO]" + HybsSystem.CR ;
208                                    throw new HybsSystemException( errMsg );
209                            }
210                    }
211    
212                    return select;
213            }
214    
215            /**
216             * Selectionオブジェクトをプã?ルからすべて削除しますã?
217             * シスãƒ?ƒ 全体を初期化するときや、動作が不安定になったときに行いますã?
218             * プã?ルの方法è?体が,ä¸?¨®のキャãƒ?‚·ュçš?�ª使ã�?�‹たしかしてã�?�ªã�?‚º,
219             * 実行中でもã?ã�?�¤でもã?ールをå?期化できますã?
220             *
221             * @og.rev 4.3.3.6 (2008/11/15) DBRadioMap追åŠ?
222             */
223            public static void clear() {
224                    // synchronized( codeMap ) { codeMap.clear(); }
225                    synchronized( dbMap   ) { dbMap.clear(); }
226                    synchronized( dbRadioMap ) { dbRadioMap.clear(); } // 4.3.3.6 (2008/11/15)
227            }
228    }