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             * @og.rev 5.7.3.0 (2014/02/07) 新規作?
146             *
147             * @param   type  Selectionオブジェクト?タイ?KEYVAL,HM,NUM,YMD)
148             * @param       param パラメータ
149             *
150             * @return  Selectionオブジェク?
151             * @throws  HybsSystemException
152             */
153            public static Selection newSelection( final String type,final String param ) {
154                    Selection select = null;
155                    if( "KEYVAL".equalsIgnoreCase( type ) ) {
156                            select = new Selection_KEYVAL( param );
157                    }
158                    else if( "HM".equalsIgnoreCase( type ) ) {
159                            select = new Selection_HM( param );
160                    }
161                    else if( "NUM".equalsIgnoreCase( type ) ) {
162                            select = new Selection_NUM( param );
163                    }
164                    else if( "YMD".equalsIgnoreCase( type ) ) {
165                            select = new Selection_YMD( param );
166                    }
167                    else if( "NULL".equalsIgnoreCase( type ) ) {
168                            select = new Selection_NULL( param );
169                    }
170                    else {
171                            select = new Selection_NULL( param );
172                            String errMsg = "??タイプ[" + type + "]が?存在しません。タイプ?=[KEYVAL,HM,NUM,YMD]" + HybsSystem.CR ;
173                            throw new HybsSystemException( errMsg );
174                    }
175    
176                    return select;
177            }
178    
179            /**
180             * ?Selectionオブジェクトを構築します?
181             * ここでは、Selectionオブジェクト?タイプが?MENU,RADIO)につ?作?されます?
182             * ここで作?されるオブジェクト?、この、SelectionFactoryではキャ?ュしません?
183             * 各RendererやEditorが?有されて?ので、そちらでキャ?ュされて?す?
184             * type が指定?キーワード以外?場合?、Exception が返されます?
185             * codeData オブジェクトが null の場合?、Selectionオブジェク?は null が返されます?
186             *
187             * @og.rev 5.7.3.0 (2014/02/07) 新規作?
188             *
189             * @param   type  Selectionオブジェクト?タイ?MENU,RADIO)
190             * @param       codeData CodeDataオブジェク?
191             *
192             * @return  Selectionオブジェク?
193             * @throws  HybsSystemException
194             */
195            public static Selection newSelection( final String type,final CodeData codeData ) {
196                    Selection select = null;
197                    if( codeData != null ) {
198                            if( "MENU".equalsIgnoreCase( type ) ) {
199                                    select = new Selection_CODE( codeData );
200                            }
201                            else if( "RADIO".equalsIgnoreCase( type ) ) {
202                                    select = new Selection_RADIO( codeData );
203                            }
204                            else {
205                                    String errMsg = "??タイプ[" + type + "]が?存在しません。タイプ?=[MENU,RADIO]" + HybsSystem.CR ;
206                                    throw new HybsSystemException( errMsg );
207                            }
208                    }
209    
210                    return select;
211            }
212    
213            /**
214             * Selectionオブジェクトをプ?ルからすべて削除します?
215             * シス?全体を初期化するときや、動作が不安定になったときに行います?
216             * プ?ルの方法?体が,?のキャ?ュ?使?たしかして??,
217             * 実行中でも??でも?ールを?期化できます?
218             *
219             * @og.rev 4.3.3.6 (2008/11/15) DBRadioMap追?
220             */
221            public static void clear() {
222                    // synchronized( codeMap ) { codeMap.clear(); }
223                    synchronized( dbMap   ) { dbMap.clear(); }
224                    synchronized( dbRadioMap ) { dbRadioMap.clear(); } // 4.3.3.6 (2008/11/15)
225            }
226    }