KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > listOfValues > util > LovDefinitionCache


1 /**
2  * Copyright (c) 2002 Bright Side Factory. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * 3. The end-user documentation included with the redistribution,
17  * if any, must include the following acknowledgment:
18  * "This product includes software developed by the
19  * Bright Side Factory (http://www.bs-factory.org/)."
20  * Alternately, this acknowledgment may appear in the software itself,
21  * if and wherever such third-party acknowledgments normally appear.
22  *
23  * 4. The names "Bright Side", "BS Factory" and "Bright Side Factory" must
24  * not be used to endorse or promote products derived from this
25  * software without prior written permission. For written
26  * permission, please contact info@bs-factory.org.
27  *
28  * 5. Products derived from this software may not be called "Bright Side",
29  * nor may "Bright Side" appear in their name, without prior written
30  * permission of the Apache Software Foundation.
31  *
32  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
36  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
39  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
42  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43  * SUCH DAMAGE.
44  * ====================================================================
45  *
46  * This software consists of voluntary contributions made by many
47  * individuals on behalf of the Bright Side Factory. For more
48  * information on the Bright Side Factory, please see
49  * <http://www.bs-factory.org/>.
50  */

51
52 package org.bsf.listOfValues.util;
53
54 import java.util.HashMap JavaDoc;
55 import java.util.Iterator JavaDoc;
56 import java.util.List JavaDoc;
57
58 /**
59  * This class is used to store the "lov definitions" (in a LovEntityCacheItem)
60  * to prevent the reexecution of a select statement to obtain the to be executed
61  * SQL statement defined in the corresponding LOV.<p>
62  *
63  * Note that this Class is used as a singleton and that the LovEntityCacheItem
64  * is never used outside of this cache.
65  *
66  * @see org.bsf.listOfValues.util.LovDefinitionCache.LovEntityCacheItem (in this file as well)
67  */

68 public class LovDefinitionCache {
69     // *************************************************************************
70
// ********************* LovEntityCacheItem definition *********************
71
// *************************************************************************
72

73     /**
74      * Stores the needed informations related to the LovBean (mainly the
75      * SQL statement).
76      */

77     class LovEntityCacheItem {
78         private List JavaDoc _types;
79         private String JavaDoc _lovName;
80         private String JavaDoc _lovRequest;
81
82         public LovEntityCacheItem( String JavaDoc p_lovName, String JavaDoc p_lovRequest, List JavaDoc p_metaData ) {
83             _types = p_metaData;
84             _lovName = p_lovName;
85             _lovRequest = p_lovRequest;
86         }
87
88         public String JavaDoc getRequest() {
89             return _lovRequest;
90         }
91
92         public String JavaDoc getLovName() {
93             return _lovName;
94         }
95
96         public List JavaDoc getTypes() {
97             return _types;
98         }
99     }
100
101     // *************************************************************************
102
// ********************* Static part (field && method) *********************
103
// *************************************************************************
104

105     private static LovDefinitionCache _lovCacheInstance = null;
106
107     /**
108      * Gives access to the singleton.
109      *
110      * @return the singleton
111      */

112     public static LovDefinitionCache getInstance() {
113         if ( _lovCacheInstance == null ) {
114             synchronized( LovDefinitionCache.class ) {
115                 if ( _lovCacheInstance == null )
116                     _lovCacheInstance = new LovDefinitionCache();
117             }
118         }
119
120         return _lovCacheInstance;
121     }
122
123     // *************************************************************************
124
// *********************** LovDefinitionCache definition *****************************
125
// *************************************************************************
126

127     /**
128      * Cache that holds the LovEntityCacheItems. Informations are stored as
129      * (aLovOID, aLovEntityCacheItem). Initial cache size is 50 and shuold
130      * be enough for most applications (otherwise the map extends itself,
131      * minor performance cost).
132      */

133     private HashMap JavaDoc _cache = new HashMap JavaDoc( 50 );
134
135     private LovDefinitionCache() {
136         // We don't want this Class to be instantiated from outside
137
}
138
139     /**
140      * Returns the sql statement (or null if no match is found) stored in the
141      * LovDefinitionCache and corresponding to the Lov with the given OID.
142      *
143      * @param p_lovOID The OID of the LOV whose SQL statement is desired.
144      * @return the sql statement stored in the LovDefinitionCache corresponding to
145      * the given OID or null if the given oid is not found.
146      *
147      * @see #isOIDInCache
148      */

149     public String JavaDoc getRequestForLovOID( Long JavaDoc p_lovOID ) {
150         String JavaDoc request = null;
151         LovEntityCacheItem item = (LovEntityCacheItem) _cache.get( p_lovOID );
152
153         if ( item != null ) {
154             // We have a request (ie. SQL statement), we will return it
155
request = item.getRequest();
156         }
157
158         return request;
159     }
160
161     /**
162      * Returns a collection of type corresponding to the the metadata(or null if
163      * no match is found) stored in the LovDefinitionCache and corresponding to the Lov
164      * with the given OID.
165      *
166      * @param p_lovOID The OID of the LOV whose types are desired.
167      * @return the List of types stored in the LovDefinitionCache corresponding to
168      * the given OID or null if the given oid is not found.
169      *
170      * @see #isOIDInCache
171      */

172     public List JavaDoc getTypesForLovOID( Long JavaDoc p_lovOID ) {
173         List JavaDoc types = null;
174         LovEntityCacheItem item = (LovEntityCacheItem) _cache.get( p_lovOID );
175
176         if ( item != null ) {
177             // We have a types (ie. SQL statement), we will return it
178
types = item.getTypes();
179         }
180
181         return types;
182     }
183
184     /**
185      * Returns whether or not informations, for a given LOV, are stored in the
186      * LovDefinitionCache.
187      *
188      * @param p_lovOID The OID of the LOV for which we want to know if it is
189      * in cache.
190      * @return true if we have the informations for the given LovOID in the
191      * cache and false otherwise.
192      */

193     public boolean isOIDInCache( Long JavaDoc p_lovOID ) {
194         return _cache.containsKey( p_lovOID );
195     }
196
197     /**
198      * Returns whether or not informations, for a given LOV, are stored in the
199      * LovDefinitionCache.
200      *
201      * @param p_lovName The name of the LOV for which we want to know if it is
202      * in cache.
203      * @return true if we have the informations for the given LovName in the
204      * cache and false otherwise (or if the name is null).
205      */

206     public boolean isNameInCache( String JavaDoc p_lovName ) {
207         boolean isNameInCache = true;
208
209         if ( p_lovName == null || getLovOID( p_lovName ) == null ) {
210             isNameInCache = false;
211         }
212
213         return isNameInCache;
214     }
215
216     /**
217      * Adds a new item to the cache (will use a LovEntityCacheItem to do so).
218      *
219      * @param p_lovOID The LovOID for which we want to store the informations.
220      * @param p_lovName The LovName corresponding to the LovOID.
221      * @param p_lovRequest The LovRequest corresponding to the LovOID.
222      *
223      * @throws java.lang.IllegalArgumentException if the OID or the NAME or the REQUEST
224      * is (are) null.
225      */

226     public void addToCache( Long JavaDoc p_lovOID, String JavaDoc p_lovName, String JavaDoc p_lovRequest, List JavaDoc p_metaData ) {
227         if ( p_lovOID == null || p_lovName == null || p_lovRequest == null ) {
228             String JavaDoc msg = "Need non null (LovOID,LovName,LovRequest) to store them in cache";
229
230             throw new IllegalArgumentException JavaDoc( msg );
231         }
232
233         _cache.put( p_lovOID, new LovEntityCacheItem( p_lovName, p_lovRequest, p_metaData ) );
234     }
235
236     /**
237      * Returns the LovOID corresponding to a given LovName or null if no match is
238      * found in the cache. Note that it only checks in the cache and not anywhere
239      * else.
240      *
241      * @param p_lovName The name for which we want to obtain the LovOID.
242      * @return The LovOID corresponding to the given LovName, null if no match
243      * is found in the cache.
244      */

245     public Long JavaDoc getLovOID( String JavaDoc p_lovName ) {
246         Iterator JavaDoc iter = _cache.keySet().iterator();
247
248         while ( iter.hasNext() ) {
249             Long JavaDoc OID = (Long JavaDoc) iter.next();
250
251             LovEntityCacheItem item = (LovEntityCacheItem) _cache.get( OID );
252
253             if ( item.getLovName().equals( p_lovName ) ) {
254                 return OID;
255             }
256         }
257
258         return null;
259     }
260 }
261
Popular Tags