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         * DB検索(SQL)文字列より、データベースSelectionオブジェクトを構築します。
065         * Selection_DB では、検索行毎のクエリーがあるため、name + query でキャッシュします。
066         *
067         * @og.rev 4.0.0.0 (2006/11/15) lang 属性を追加します。
068         *
069         * @param   query DB検索(SQL)文字列
070         * @param       dbid  データベース接続先ID
071         * @param       lang  リソースを使用する場合の言語
072         *
073         * @return  Selectionオブジェクト
074         */
075        public static Selection newDBSelection( final String query,final String dbid,final String lang ) {
076                String key = query+dbid+lang;
077                Selection select = dbMap.get( key );
078
079                if( select == null || select.isTimeOver() ) {
080                        synchronized( dbMap ) {
081                                select = new Selection_DB( query,dbid,lang );
082                                dbMap.put( key,select );
083                        }
084                }
085                return select;
086        }
087
088        /**
089         * DB検索(SQL)文字列より、データベースSelectionオブジェクトを構築します。
090         * Selection_DB では、検索行毎のクエリーがあるため、name + query でキャッシュします。
091         *
092         * @og.rev 4.3.3.6 (2008/11/15) 新規作成
093         *
094         * @param   query DB検索(SQL)文字列
095         * @param       dbid  データベース接続先ID
096         * @param       lang  リソースを使用する場合の言語
097         *
098         * @return  Selectionオブジェクト
099         */
100        public static Selection newDBRadioSelection( final String query,final String dbid,final String lang ) {
101                String key = query+dbid+lang;
102                Selection select = dbRadioMap.get( key );
103
104                if( select == null || select.isTimeOver() ) {
105                        synchronized( dbRadioMap ) {
106                                select = new Selection_DBRADIO( query,dbid,lang );
107                                dbRadioMap.put( key,select );
108                        }
109                }
110                return select;
111        }
112
113        /**
114         * 各種Selectionオブジェクトを構築します。
115         * ここでは、Selectionオブジェクトのタイプが、(KEYVAL,HM,NUM,YMD)について作成されます。
116         * ここで作成されるオブジェクトは、この、SelectionFactoryではキャッシュしません。
117         * 各RendererやEditorが共有されているので、そちらでキャッシュされています。
118         * type が指定のキーワード以外の場合は、Exception が返されます。
119         * ※ type="NULL" も使用可能です。これは、どんな場合でも、引数の param を返す Selection
120         * オブジェクトを返します。内部的に、CodeDataが存在しない場合など、エラーメッセージを
121         * 引数に与えて修正を促すようなケースで使用します。
122         *
123         * ※ 指定のタイプが存在しない場合、HybsSystemException が throw されます。
124         *
125         * @og.rev 5.7.3.0 (2014/02/07) 新規作成
126         *
127         * @param   type  Selectionオブジェクトのタイプ(KEYVAL,HM,NUM,YMD)
128         * @param       param パラメータ
129         *
130         * @return  Selectionオブジェクト
131         */
132        public static Selection newSelection( final String type,final String param ) {
133                Selection select = null;
134                if( "KEYVAL".equalsIgnoreCase( type ) ) {
135                        select = new Selection_KEYVAL( param );
136                }
137                else if( "HM".equalsIgnoreCase( type ) ) {
138                        select = new Selection_HM( param );
139                }
140                else if( "NUM".equalsIgnoreCase( type ) ) {
141                        select = new Selection_NUM( param );
142                }
143                else if( "YMD".equalsIgnoreCase( type ) ) {
144                        select = new Selection_YMD( param );
145                }
146                else if( "NULL".equalsIgnoreCase( type ) ) {
147                        select = new Selection_NULL( param );
148                }
149                else {
150                        select = new Selection_NULL( param );
151                        String errMsg = "指定のタイプ[" + type + "]が、存在しません。タイプ一覧=[KEYVAL,HM,NUM,YMD]" + HybsSystem.CR ;
152                        throw new HybsSystemException( errMsg );
153                }
154
155                return select;
156        }
157
158        /**
159         * 各種Selectionオブジェクトを構築します。
160         * ここでは、Selectionオブジェクトのタイプが、(MENU,RADIO)について作成されます。
161         * ここで作成されるオブジェクトは、この、SelectionFactoryではキャッシュしません。
162         * 各RendererやEditorが共有されているので、そちらでキャッシュされています。
163         * type が指定のキーワード以外の場合は、Exception が返されます。
164         * codeData オブジェクトが null の場合は、Selectionオブジェクト は null が返されます。
165         *
166         * ※ 指定のタイプが存在しない場合、HybsSystemException が throw されます。
167         *
168         * @og.rev 5.7.3.0 (2014/02/07) 新規作成
169         *
170         * @param   type  Selectionオブジェクトのタイプ(MENU,RADIO)
171         * @param       codeData CodeDataオブジェクト
172         *
173         * @return  Selectionオブジェクト
174         */
175        public static Selection newSelection( final String type,final CodeData codeData ) {
176                Selection select = null;
177                if( codeData != null ) {
178                        if( "MENU".equalsIgnoreCase( type ) ) {
179                                select = new Selection_CODE( codeData );
180                        }
181                        else if( "RADIO".equalsIgnoreCase( type ) ) {
182                                select = new Selection_RADIO( codeData );
183                        }
184                        else {
185                                String errMsg = "指定のタイプ[" + type + "]が、存在しません。タイプ一覧=[MENU,RADIO]" + HybsSystem.CR ;
186                                throw new HybsSystemException( errMsg );
187                        }
188                }
189
190                return select;
191        }
192
193        /**
194         * Selectionオブジェクトをプールからすべて削除します。
195         * システム全体を初期化するときや、動作が不安定になったときに行います。
196         * プールの方法自体が,一種のキャッシュ的な使いかたしかしていない為,
197         * 実行中でも、いつでもプールを初期化できます。
198         *
199         * @og.rev 4.3.3.6 (2008/11/15) DBRadioMap追加
200         */
201        public static void clear() {
202                // synchronized( codeMap ) { codeMap.clear(); }
203                synchronized( dbMap   ) { dbMap.clear(); }
204                synchronized( dbRadioMap ) { dbRadioMap.clear(); } // 4.3.3.6 (2008/11/15)
205        }
206}