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 ; 9 import java.util.*; 10 11 19 public class LovManager { 20 24 public static final Long ALL_LOVS = new Long ( -123321 ); 25 26 private static LOVService _lovService = null; 27 private static final int DEFAULT_HASH_TABLE_SIZE = 30; 28 29 32 private static Hashtable _listOfValues = new Hashtable( DEFAULT_HASH_TABLE_SIZE ); 33 34 37 private static Hashtable _listOfValuesPerPK = new Hashtable( DEFAULT_HASH_TABLE_SIZE ); 38 39 42 private static Hashtable _lovListeners = new Hashtable( DEFAULT_HASH_TABLE_SIZE ); 43 44 48 53 protected static LOVService getLovService() { 54 return _lovService; 55 } 56 57 64 public static void setLovService( LOVService p_lovService ) { 65 _lovService = p_lovService; 66 } 67 68 72 86 public static List getListOfValues( Long p_lovOID ) throws NoSuchLovException, RemoteException { 87 if ( getLovService() == null ) { 88 throw new IllegalStateException ( "No LovService has been set..." ); 89 } 90 91 if ( p_lovOID == null ) { 92 throw new IllegalArgumentException ( "Can't return a LOV with a null lovOID..." ); 93 } 94 95 List listOfValues = (List) _listOfValues.get( p_lovOID ); 97 98 if ( listOfValues == null ) { 100 listOfValues = _lovService.getListOfValues( p_lovOID ); 102 103 _listOfValues.put( p_lovOID, listOfValues ); 105 } 106 107 return listOfValues; 108 } 109 110 127 public static List getListOfValues( LovListener p_lovListener, Long p_lovOID ) 128 throws NoSuchLovException, RemoteException { 129 130 List listOfValues = getListOfValues( p_lovOID ); 131 132 addLovManagerListener( p_lovListener, p_lovOID ); 134 135 return listOfValues; 136 } 137 138 159 public static LovValue getLovValue( Long p_lovOID, Object p_lovValuePK ) 160 throws NoSuchLovValueException, NoSuchLovException, RemoteException { 161 162 if ( getLovService() == null ) { 163 throw new IllegalStateException ( "No LovService has been set..." ); 164 } 165 166 if ( p_lovOID == null || p_lovValuePK == null ) { 167 throw new IllegalArgumentException ( "Need a non null lovOID and lovValueOID..." ); 168 } 169 170 Hashtable lovValuePerPK = (Hashtable) _listOfValuesPerPK.get( p_lovOID ); 172 173 if ( lovValuePerPK == null ) { 175 List listOfValues = getListOfValues( p_lovOID ); 176 177 Iterator iterator = listOfValues.iterator(); 178 lovValuePerPK = new Hashtable( listOfValues.size() ); 179 180 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 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 222 public static LovValue getLovValue( LovListener p_lovListener, Long p_lovOID, Object p_lovValuePK ) 223 throws NoSuchLovValueException, NoSuchLovException, RemoteException { 224 225 LovValue lovValue = getLovValue( p_lovOID, p_lovValuePK ); 226 227 addLovManagerListener( p_lovListener, p_lovOID ); 229 230 return lovValue; 231 } 232 233 248 public static void resynchronizeLov( Long p_lovOID ) throws NoSuchLovException, RemoteException { 249 if ( getLovService() == null ) { 250 throw new IllegalStateException ( "No LovService has been set..." ); 251 } 252 253 if ( p_lovOID == null ) { 254 throw new IllegalArgumentException ( "Need a non null lovOID to refresh a Lov..." ); 255 } 256 257 getLovService().resynchronizeListOfValues( p_lovOID ); 259 260 clearLocalCache( p_lovOID ); 262 263 notifyLovListeners( p_lovOID ); 265 } 266 267 275 public static void clearLocalCache( Long p_lovOID ) { 276 if ( p_lovOID == null ) { 277 throw new IllegalArgumentException ( "Need a non null lovOID to refresh a Lov..." ); 278 } 279 280 _listOfValues.remove( p_lovOID ); 282 _listOfValuesPerPK.remove( p_lovOID ); 283 } 284 285 289 299 public static void addLovManagerListener( LovListener p_lovListener, Long 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 325 public static void removeLovManagerListener( LovListener p_lovListener ) { 326 if ( p_lovListener == null ) { 327 throw new IllegalArgumentException ( "Can't remove a null listener..." ); 328 } 329 330 Enumeration lovOIDs = _lovListeners.elements(); 331 332 while ( lovOIDs.hasMoreElements() ) { 333 ( (List) lovOIDs.nextElement() ).remove( p_lovListener ); 335 } 336 } 337 338 346 public static void notifyLovListeners( Long p_lovOID ) { 347 if ( p_lovOID == null ) { 348 throw new IllegalArgumentException ( "Can't notify for a null lovOID..." ); 349 } 350 351 Vector listenersList = new Vector(); 353 Collection listeners = ( (Collection) _lovListeners.get( ALL_LOVS ) ); 354 355 if ( listeners != null ) { 356 listenersList.addAll( listeners ); 357 } 358 359 listeners = (List) _lovListeners.get( p_lovOID ); 361 362 if ( listeners != null ) { 363 Iterator iterator = listeners.iterator(); 364 365 while ( iterator.hasNext() ) { 366 Object object = iterator.next(); 367 368 if ( !listenersList.contains( object ) ) { 369 listenersList.add( object ); 370 } 371 } 372 } 373 374 Iterator listenersIterator = listenersList.iterator(); 376 377 while ( listenersIterator.hasNext() ) { 378 ( (LovListener) listenersIterator.next() ).lovHasChanged( p_lovOID ); 379 } 380 } 381 }
| Popular Tags
|