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 */
016package org.opengion.hayabusa.db;
017
018import org.opengion.hayabusa.common.HybsSystem ;
019import org.opengion.hayabusa.common.HybsSystemException ;
020import org.opengion.hayabusa.common.SystemManager ;
021import org.opengion.hayabusa.resource.CodeData;
022import org.opengion.fukurou.util.Cleanable;
023
024import java.util.Map;
025import 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 */
040public 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}