KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > listOfValues > client > LovManager


1 package org.bsf.listOfValues.client;
2
3 import org.bsf.listOfValues.LOVService;
4 import org.bsf.listOfValues.exceptions.NoSuchLovException;
5 import org.bsf.listOfValues.exceptions.NoSuchLovValueException;
6 import org.bsf.listOfValues.lovValue.LovValue;
7
8 import java.rmi.RemoteException JavaDoc;
9 import java.util.*;
10
11 /**
12  * Enables access to the LOV using the given LovService. Is usaully used as a
13  * cache between the server and the client.
14  *
15  * @see LovValue
16  * @see LOVService
17  * @see LovListener
18  */

19 public class LovManager {
20     /**
21      * To specify that a listener is interested in all the lovs. Remember that
22      * a Lov OID shouldn't be negative (it's a rule :o).
23      */

24     public static final Long JavaDoc ALL_LOVS = new Long JavaDoc( -123321 );
25
26     private static LOVService _lovService = null;
27     private static final int DEFAULT_HASH_TABLE_SIZE = 30;
28
29     /**
30      * Holds the retrieved list of values (aLovOID, aList of LovValue).
31      */

32     private static Hashtable _listOfValues = new Hashtable( DEFAULT_HASH_TABLE_SIZE );
33
34     /**
35      * Holds the retrieved list of values (aLovOID, (aLovValuePK, aList of LovValue)).
36      */

37     private static Hashtable _listOfValuesPerPK = new Hashtable( DEFAULT_HASH_TABLE_SIZE );
38
39     /**
40      * Holds the listeners (LovOID, List of LovListeners).
41      */

42     private static Hashtable _lovListeners = new Hashtable( DEFAULT_HASH_TABLE_SIZE );
43
44     // *************************************************************************
45
// ************************* Static Accessors ******************************
46
// *************************************************************************
47

48     /**
49      * @return The lov service defined to be used. Can be null if not set.
50      *
51      * @see #setLovService
52      */

53     protected static LOVService getLovService() {
54         return _lovService;
55     }
56
57     /**
58      * Set the LovService to be used to access the LovServiceBean.
59      *
60      * @param p_lovService The LovService to use.
61      *
62      * @throws java.lang.IllegalArgumentException if the given LovService is null.
63      */

64     public static void setLovService( LOVService p_lovService ) {
65         _lovService = p_lovService;
66     }
67
68     // *************************************************************************
69
// ***************************** Static Methods ****************************
70
// *************************************************************************
71

72     /**
73      * Return the requested list of values, checking in its cache first. If not
74      * found, request it from the actually registered LovService, and store it
75      * in the cache before returning it.
76      *
77      * @param p_lovOID The OID of the desired list of values.
78      * @return The list of values corresponding to the given LovOID.
79      *
80      * @throws org.bsf.listOfValues.exceptions.NoSuchLovException If the LOV
81      * doesn't exist.
82      * @throws java.rmi.RemoteException If a communication problem occurs.
83      * @throws java.lang.IllegalArgumentException if the given LovOID is null.
84      * @throws java.lang.IllegalStateException if no LovService is registered
85      */

86     public static List getListOfValues( Long JavaDoc p_lovOID ) throws NoSuchLovException, RemoteException JavaDoc {
87         if ( getLovService() == null ) {
88             throw new IllegalStateException JavaDoc( "No LovService has been set..." );
89         }
90
91         if ( p_lovOID == null ) {
92             throw new IllegalArgumentException JavaDoc( "Can't return a LOV with a null lovOID..." );
93         }
94
95         // Do we have it in cache ?
96
List listOfValues = (List) _listOfValues.get( p_lovOID );
97
98         // Do we need to make a server call ?
99
if ( listOfValues == null ) {
100             // We retrieve it from the server
101
listOfValues = _lovService.getListOfValues( p_lovOID );
102
103             // We add it to the cache for the next call
104
_listOfValues.put( p_lovOID, listOfValues );
105         }
106
107         return listOfValues;
108     }
109
110     /**
111      * Return the requested list of values, checking in its cache first. If not
112      * found, request it from the actually registered LovService, and store it
113      * in the cache before returning it. It register the given LovListener
114      * as interested in notification of LOV modification.
115      *
116      * @param p_lovListener The lovListener to be notified if this requested
117      * lov changes. Can be null (no effect on the listener list).
118      * @param p_lovOID The OID of the desired list of values.
119      * @return The list of values corresponding to the given LovOID.
120      *
121      * @throws org.bsf.listOfValues.exceptions.NoSuchLovException If the LOV
122      * doesn't exist.
123      * @throws java.rmi.RemoteException If a communication problem occurs.
124      * @throws java.lang.IllegalArgumentException if the given LovOID is null.
125      * @throws java.lang.IllegalStateException if no LovService is registered
126      */

127     public static List getListOfValues( LovListener p_lovListener, Long JavaDoc p_lovOID )
128             throws NoSuchLovException, RemoteException JavaDoc {
129
130         List listOfValues = getListOfValues( p_lovOID );
131
132         // We add the given listener to the list of listeners
133
addLovManagerListener( p_lovListener, p_lovOID );
134
135         return listOfValues;
136     }
137
138     /**
139      * Return the requested LovValue that belongs to the specified LOV. The whole
140      * list of values is retrieved from the server if not yet in cache. It creates
141      * a map to store the LovValue per their oid to accelerate later data access.
142      * Can be really useful for example when the getLovValue method is used in an
143      * often called renderer (TableCellRenderer for example).
144      *
145      * @param p_lovOID The OID of the desired list of values.
146      * @param p_lovValuePK The PK of the LovValue (belonging to the LOV
147      * whose oid is p_lovOID) desired.
148      * @return The list of values corresponding to the given LovOID.
149      *
150      * @throws org.bsf.listOfValues.exceptions.NoSuchLovValueException if the
151      * requested lov value doesn't exist.
152      * @throws org.bsf.listOfValues.exceptions.NoSuchLovException if the LOV
153      * doesn't exist.
154      * @throws java.rmi.RemoteException if a communication problem occurs.
155      * @throws java.lang.IllegalStateException if no LovService is registered.
156      * @throws java.lang.ClassCastException if the LOV contains anything else
157      * than LovValue.
158      */

159     public static LovValue getLovValue( Long JavaDoc p_lovOID, Object JavaDoc p_lovValuePK )
160             throws NoSuchLovValueException, NoSuchLovException, RemoteException JavaDoc {
161
162         if ( getLovService() == null ) {
163             throw new IllegalStateException JavaDoc( "No LovService has been set..." );
164         }
165
166         if ( p_lovOID == null || p_lovValuePK == null ) {
167             throw new IllegalArgumentException JavaDoc( "Need a non null lovOID and lovValueOID..." );
168         }
169
170         // Is it already in cache ?
171
Hashtable lovValuePerPK = (Hashtable) _listOfValuesPerPK.get( p_lovOID );
172
173         // Do we need to retrieve the info from the server ?
174
if ( lovValuePerPK == null ) {
175             List listOfValues = getListOfValues( p_lovOID );
176
177             Iterator iterator = listOfValues.iterator();
178             lovValuePerPK = new Hashtable( listOfValues.size() );
179
180             // We add each LovValue to the map (lovValueOID, lovValue)
181
while ( iterator.hasNext() ) {
182                 LovValue lovValue = (LovValue) iterator.next();
183                 lovValuePerPK.put( lovValue.getPK(), lovValue );
184             }
185
186             _listOfValuesPerPK.put( p_lovOID, lovValuePerPK );
187         }
188
189         // We retrieve the requested LovValue from the hashTable
190
LovValue lovValue = (LovValue) lovValuePerPK.get( p_lovValuePK);
191
192         if ( lovValue == null ) {
193             throw new NoSuchLovValueException( "No LovValue " + p_lovValuePK + " in Lov " + p_lovOID );
194         }
195
196         return lovValue;
197     }
198
199     /**
200      * Return the requested LovValue that belongs to the specified LOV. The whole
201      * list of values is retrieved from the server if not yet in cache. It creates
202      * a map to store the LovValue per their oid to accelerate later data access.
203      * Can be really useful for example when the getLovValue method is used in an
204      * often called renderer (TableCellRenderer for example).
205      *
206      * @param p_lovListener The listener to be notify is the requested LOV changes.
207      * Can be null (no effect on the listeners list).
208      * @param p_lovOID The OID of the desired list of values.
209      * @param p_lovValuePK The OID of the LovValue (belonging to the LOV
210      * whose oid is p_lovOID) desired.
211      * @return The list of values corresponding to the given LovOID.
212      *
213      * @throws org.bsf.listOfValues.exceptions.NoSuchLovValueException if the
214      * requested lov value doesn't exist.
215      * @throws org.bsf.listOfValues.exceptions.NoSuchLovException if the LOV
216      * doesn't exist.
217      * @throws java.rmi.RemoteException if a communication problem occurs.
218      * @throws java.lang.IllegalStateException if no LovService is registered.
219      * @throws java.lang.ClassCastException if the LOV contains anything else
220      * than LovValue.
221      */

222     public static LovValue getLovValue( LovListener p_lovListener, Long JavaDoc p_lovOID, Object JavaDoc p_lovValuePK )
223             throws NoSuchLovValueException, NoSuchLovException, RemoteException JavaDoc {
224
225         LovValue lovValue = getLovValue( p_lovOID, p_lovValuePK );
226
227         // We add the listener to the list of listeners
228
addLovManagerListener( p_lovListener, p_lovOID );
229
230         return lovValue;
231     }
232
233     /**
234      * Clears the server and client cache and notifies the registered listeners that the
235      * LOV has changed. The LOV isn't reloaded through this call... It will notifies
236      * the listeners (if any). It will be reloaded the next time this lov is loaded.
237      *
238      * @param p_lovOID The OID of the Lov to synchronize with the server.
239      *
240      * @throws NoSuchLovException If no lov has the given OID.
241      * @throws RemoteException If a communication problem occurs.
242      * @throws IllegalArgumentException if the given oid is null.
243      * @throws IllegalStateException if no LovService has been set.
244      *
245      * @see LOVService
246      * @see #setLovService
247      */

248     public static void resynchronizeLov( Long JavaDoc p_lovOID ) throws NoSuchLovException, RemoteException JavaDoc {
249         if ( getLovService() == null ) {
250             throw new IllegalStateException JavaDoc( "No LovService has been set..." );
251         }
252
253         if ( p_lovOID == null ) {
254             throw new IllegalArgumentException JavaDoc( "Need a non null lovOID to refresh a Lov..." );
255         }
256
257         // We resynchronize the Lov
258
getLovService().resynchronizeListOfValues( p_lovOID );
259
260         // We clear the local cache
261
clearLocalCache( p_lovOID );
262
263         // We notify the registered listeners (if any)
264
notifyLovListeners( p_lovOID );
265     }
266
267     /**
268      * Removes the LOV, whose OID is the given oid, from the cache.
269      *
270      * @param p_lovOID The OID of the LOV to remove from the cache. Should not
271      * be null.
272      *
273      * @throws IllegalArgumentException if the given oid is null.
274      */

275     public static void clearLocalCache( Long JavaDoc p_lovOID ) {
276         if ( p_lovOID == null ) {
277             throw new IllegalArgumentException JavaDoc( "Need a non null lovOID to refresh a Lov..." );
278         }
279
280         // We clear the cache(s)
281
_listOfValues.remove( p_lovOID );
282         _listOfValuesPerPK.remove( p_lovOID );
283     }
284
285     // *************************************************************************
286
// *************************** Listener Methods ****************************
287
// *************************************************************************
288

289     /**
290      * Registers a LovListener for the given lovOID. If the lovOID equals
291      * ALL_LISTENERS the LovListener will be notified of the modification
292      * of every lov. Does nothing if the LovListener is null.
293      *
294      * @param p_lovListener The LovListener that we want to be notified.
295      * @param p_lovOID The OID of the LOV that the LovListener listens to, if
296      * equals to ALL_LOVS can be used to specify that a listener is interested
297      * by all the Lovs.
298      */

299     public static void addLovManagerListener( LovListener p_lovListener, Long JavaDoc p_lovOID ) {
300         if ( p_lovListener == null ) {
301             return;
302         }
303
304         List listeners = (List) _lovListeners.get( p_lovOID );
305
306         if ( listeners == null ) {
307             listeners = new Vector( 10 );
308             listeners.add( p_lovListener );
309
310             _lovListeners.put( p_lovOID, listeners );
311         } else {
312             if ( !listeners.contains( p_lovListener ) ) {
313                 listeners.add( p_lovListener );
314             }
315         }
316     }
317
318     /**
319      * Removes the given LovListener from the registered listeners.
320      *
321      * @param p_lovListener The LovListener that we want to remove.
322      *
323      * @throws IllegalArgumentException if the LovListener is null.
324      */

325     public static void removeLovManagerListener( LovListener p_lovListener ) {
326         if ( p_lovListener == null ) {
327             throw new IllegalArgumentException JavaDoc( "Can't remove a null listener..." );
328         }
329
330         Enumeration lovOIDs = _lovListeners.elements();
331
332         while ( lovOIDs.hasMoreElements() ) {
333             // We remove the listener from all the lists
334
( (List) lovOIDs.nextElement() ).remove( p_lovListener );
335         }
336     }
337
338     /**
339      * Notify all the listeners interested for the given lovOID.
340      *
341      * @param p_lovOID The OID of the LOV of which we want to notify the
342      * modificaiton.
343      *
344      * @throws IllegalArgumentException if the given lovOID is null.
345      */

346     public static void notifyLovListeners( Long JavaDoc p_lovOID ) {
347         if ( p_lovOID == null ) {
348             throw new IllegalArgumentException JavaDoc( "Can't notify for a null lovOID..." );
349         }
350
351         // First we take care of the listeners that listen to ALL_LOVS
352
Vector listenersList = new Vector();
353         Collection listeners = ( (Collection) _lovListeners.get( ALL_LOVS ) );
354
355         if ( listeners != null ) {
356             listenersList.addAll( listeners );
357         }
358
359         // And we add the one not present yet but that listen to the specified lov
360
listeners = (List) _lovListeners.get( p_lovOID );
361
362         if ( listeners != null ) {
363             Iterator iterator = listeners.iterator();
364
365             while ( iterator.hasNext() ) {
366                 Object JavaDoc object = iterator.next();
367
368                 if ( !listenersList.contains( object ) ) {
369                     listenersList.add( object );
370                 }
371             }
372         }
373
374         // And finally we notify each and every one of them
375
Iterator listenersIterator = listenersList.iterator();
376
377         while ( listenersIterator.hasNext() ) {
378             ( (LovListener) listenersIterator.next() ).lovHasChanged( p_lovOID );
379         }
380     }
381 }
Popular Tags