1 51 52 package org.bsf.listOfValues; 53 54 import org.bsf.commons.ejb.EntityAdapterBean; 55 import org.bsf.listOfValues.exceptions.NoSuchLovValueException; 56 import org.bsf.listOfValues.lovValue.DefaultLovValue; 57 import org.bsf.listOfValues.lovValue.LovValue; 58 import org.bsf.listOfValues.util.LovDefinitionCache; 59 import org.bsf.listOfValues.util.LovTypeManager; 60 61 import javax.ejb.EJBException ; 62 import javax.ejb.FinderException ; 63 import javax.ejb.ObjectNotFoundException ; 64 import java.sql.Connection ; 65 import java.sql.PreparedStatement ; 66 import java.sql.ResultSet ; 67 import java.sql.SQLException ; 68 import java.sql.Statement ; 69 import java.util.Collection ; 70 import java.util.HashMap ; 71 import java.util.List ; 72 import java.util.Vector ; 73 74 143 public class LovBean extends EntityAdapterBean implements LovBusinessInterface { 144 private static final String NAME_AND_REQUEST_FOR_OID_STATEMENT 146 = "SELECT LOV_NAME, LOV_REQUEST FROM BSF_LOV WHERE OID = ?"; 147 148 private static final String METADATA_FOR_OID_STATEMENT 149 = "SELECT FIELD_NAME, FIELD_TYPE FROM BSF_LOV_DEFINITION " + 150 "WHERE LOV_OID = ? ORDER BY FIELD_POSITION"; 151 152 public static final String LOV_OID_FOR_TABLE_NAME_STATEMENT 153 = "SELECT OID FROM BSF_LOV WHERE TABLE_NAME = ?"; 154 155 158 private final String JNDI_DATABASE_NAME = "DBPool"; 159 160 164 private Vector _sortedLov; 165 166 170 private HashMap _lov; 171 172 175 private List _metaData; 176 177 183 public void ejbLoad() { 184 String lovRequest = null; 185 String lovTableName = null; 186 ResultSet resultSet = null; 187 Statement statement = null; 188 Connection connection = null; 189 ResultSet resultSetCache = null; 190 ResultSet resultSetMetaData = null; 191 PreparedStatement preparedStatement = null; 192 193 Long lovRequestID = (Long ) ejbContext.getPrimaryKey(); 195 196 logGraphBegin( "ejbLoad() with OID " + lovRequestID.toString() ); 197 198 try { 199 connection = getConnection( JNDI_DATABASE_NAME ); 201 202 lovRequest = LovDefinitionCache.getInstance().getRequestForLovOID( lovRequestID ); 206 207 if ( lovRequest == null ) { 210 preparedStatement = connection.prepareStatement( NAME_AND_REQUEST_FOR_OID_STATEMENT ); 212 preparedStatement.setString( 1, lovRequestID.toString() ); 213 resultSetCache = preparedStatement.executeQuery(); 214 215 if ( resultSetCache.next() == false ) { 216 String msg = "No existing LOV for OID " + lovRequestID; 217 218 logError( msg ); 220 221 throw new EJBException ( msg ); 222 } 223 224 lovTableName = resultSetCache.getString( 1 ); 226 lovRequest = resultSetCache.getString( 2 ); 227 228 preparedStatement.close(); 229 } 230 231 _metaData = LovDefinitionCache.getInstance().getTypesForLovOID( lovRequestID ); 232 233 if ( _metaData == null ) { 235 preparedStatement = connection.prepareStatement( METADATA_FOR_OID_STATEMENT ); 236 preparedStatement.setString( 1, lovRequestID.toString() ); 237 resultSetMetaData = preparedStatement.executeQuery(); 238 239 _metaData = new Vector (); 240 241 while ( resultSetMetaData.next() ) { 243 String fieldName = resultSetMetaData.getString( 1 ); 244 String fieldType = resultSetMetaData.getString( 2 ); 245 LovMetaDataItem metaDataItem = new LovMetaDataItem( fieldName, fieldType ); 246 247 _metaData.add( metaDataItem ); 249 } 250 } 251 252 if ( !LovDefinitionCache.getInstance().isOIDInCache( lovRequestID ) ) { 253 LovDefinitionCache.getInstance().addToCache( lovRequestID, lovTableName, lovRequest, _metaData ); 255 } 256 257 logDebug( "Executing : " + lovRequest ); 259 260 statement = connection.createStatement(); 262 resultSet = statement.executeQuery( lovRequest ); 263 264 int numberOfColumns = resultSet.getMetaData().getColumnCount(); 267 268 if ( numberOfColumns != _metaData.size() ) { 269 String msg = "The lov " + lovRequestID + " is badly parametered (check fields definition... PK must be defined in first position...)"; 272 273 logFatal( msg ); 274 275 throw new EJBException ( msg ); 276 } 277 278 _lov = new HashMap (); 279 _sortedLov = new Vector (); 280 281 while ( resultSet.next() ) { 282 DefaultLovValue lovValue = null; 284 285 for ( int index = 0; index < _metaData.size(); index++ ) { 287 LovMetaDataItem metaDataItem = (LovMetaDataItem) _metaData.get( index ); 288 String lovValueFieldType = metaDataItem.getFieldType(); 289 String lovValueFieldName = metaDataItem.getFieldName(); 290 291 Object lovValueFieldObject = null; 292 293 try { 294 lovValueFieldObject = LovTypeManager.getObjectOfType( lovValueFieldType, resultSet, (int) index + 1 ); 296 297 if ( index == 0 ) { 298 lovValue = new DefaultLovValue( lovValueFieldObject ); 300 } else { 301 lovValue.addField( lovValueFieldName, lovValueFieldObject ); 303 } 304 } catch ( IllegalArgumentException e ) { 305 String msg = "Unknown type " + lovValueFieldType + " for Lov " + lovRequestID; 306 307 logFatal( msg ); 308 309 throw new EJBException ( msg ); 310 } 311 } 312 313 _lov.put( lovValue.getPK(), lovValue ); 314 _sortedLov.add( lovValue ); 315 } 316 } catch ( SQLException Se ) { 317 logError( "SQL Exception... ", Se ); 318 319 handleSQLException( Se ); 320 } catch ( NullPointerException NPe ) { 321 String msg = "The first LOV_DEFINITION_ORDER in table LOV_DEFINITION must be 2... Check lov " 322 + lovRequestID; 323 324 logFatal( msg ); 325 326 throw new EJBException ( msg, NPe ); 327 } finally { 328 try { 330 if ( resultSetCache != null ) { 331 resultSetCache.close(); 332 } 333 334 if ( resultSetMetaData != null ) { 335 resultSetMetaData.close(); 336 } 337 338 if ( resultSet != null ) { 339 resultSet.close(); 340 } 341 } catch ( Exception Se ) { 342 } 344 345 try { 346 if ( preparedStatement != null ) { 347 preparedStatement.close(); 348 } 349 } catch ( Exception Se ) { 350 } 352 353 try { 354 if ( statement != null ) { 355 statement.close(); 356 } 357 } catch ( Exception Se ) { 358 } 360 361 try { 362 if ( connection != null ) { 363 connection.close(); 364 } 365 } catch ( Exception Se ) { 366 } 368 } 369 370 logGraphEnd( "ejbLoad " + lovRequestID.toString() ); 371 } 372 373 382 public Long ejbFindByPrimaryKey( Long p_OID ) throws ObjectNotFoundException { 383 if ( p_OID == null ) { 384 throw new IllegalArgumentException (); 385 } 386 387 PreparedStatement preparedStatement = null; 388 ResultSet resultSet = null; 389 Connection connection = null; 390 391 logGraphBegin( "ejbFindByPrimaryKey " + p_OID ); 392 393 if ( !LovDefinitionCache.getInstance().isOIDInCache( p_OID ) ) { 395 try { 396 connection = getConnection( JNDI_DATABASE_NAME ); 397 preparedStatement = connection.prepareStatement( NAME_AND_REQUEST_FOR_OID_STATEMENT ); 398 preparedStatement.setString( 1, p_OID.toString() ); 399 resultSet = preparedStatement.executeQuery(); 400 401 if ( resultSet.next() == false ) { 403 String msg = "The LOV " + p_OID + " does not exist..."; 404 405 logError( msg ); 406 407 throw new ObjectNotFoundException ( msg ); 408 } 409 } catch ( SQLException Se ) { 410 handleSQLException( Se ); 411 } finally { 412 try { 413 if ( resultSet != null ) { 415 resultSet.close(); 416 } 417 418 if ( preparedStatement != null ) { 419 preparedStatement.close(); 420 } 421 422 if ( connection != null ) { 423 connection.close(); 424 } 425 } catch ( SQLException Se ) { 426 handleSQLException( Se ); 427 } 428 } 429 } 430 431 logGraphEnd( "ejbFindByPrimaryKey " + p_OID.toString() ); 432 433 return p_OID; 435 } 436 437 460 public Collection ejbFindByPhysicalTable( String p_tableName ) throws FinderException { 461 if ( p_tableName == null ) { 462 throw new IllegalArgumentException (); 463 } 464 465 Vector lovs = new Vector (); 466 PreparedStatement preparedStatement = null; 467 ResultSet resultSet = null; 468 Connection connection = null; 469 470 logGraphBegin( "ejbFindByPhysicalTable " + p_tableName ); 471 472 try { 473 connection = getConnection( JNDI_DATABASE_NAME ); 474 475 preparedStatement = connection.prepareStatement( LOV_OID_FOR_TABLE_NAME_STATEMENT ); 476 preparedStatement.setString( 1, p_tableName ); 477 resultSet = preparedStatement.executeQuery(); 478 479 while ( resultSet.next() ) { 480 Long lovOID = new Long ( resultSet.getLong( 1 ) ); 482 lovs.add( lovOID ); 483 } 484 485 if ( lovs.size() == 0 ) { 486 String message = "No LOVs refering to the table name " + p_tableName; 487 488 throw new ObjectNotFoundException ( message ); 489 } 490 } catch ( SQLException Se ) { 491 handleSQLException( Se ); 492 } finally { 493 try { 494 if ( resultSet != null ) { 496 resultSet.close(); 497 } 498 499 if ( preparedStatement != null ) { 500 preparedStatement.close(); 501 } 502 503 if ( connection != null ) { 504 connection.close(); 505 } 506 } catch ( SQLException Se ) { 507 handleSQLException( Se ); 508 } 509 } 510 511 return lovs; 512 } 513 514 518 533 public LovValue getLovValue( Long p_lovValueOID ) throws NoSuchLovValueException { 534 Long lovOID = (Long ) ejbContext.getPrimaryKey(); 535 536 logDebug( "getLovValue " + p_lovValueOID.toString() + " in LOV " + lovOID ); 537 538 if ( !_lov.containsKey( p_lovValueOID ) ) { 539 throw new NoSuchLovValueException( lovOID, p_lovValueOID ); 540 } 541 542 return (LovValue) _lov.get( p_lovValueOID ); 543 } 544 545 552 public HashMap getLovValuesPerOID() { 553 logDebug( "getLovValues " + ejbContext.getPrimaryKey().toString() ); 554 555 return _lov; 556 } 557 558 567 public List getListOfValues() { 568 logDebug( "getSortedLovValues for the LOV " + ejbContext.getPrimaryKey().toString() ); 569 570 return _sortedLov; 571 } 572 573 577 582 public List getMetaData() { 583 logDebug( "getMetaData for the LOV " + ejbContext.getPrimaryKey().toString() ); 584 585 return _metaData; 586 } 587 } 588 | Popular Tags |