KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > bsf > listOfValues > LovServiceBean


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 package org.bsf.listOfValues;
52
53 import org.bsf.listOfValues.exceptions.NoSuchLovException;
54 import org.bsf.listOfValues.exceptions.NoSuchLovValueException;
55 import org.bsf.listOfValues.lovValue.LovValue;
56
57 import javax.ejb.CreateException JavaDoc;
58 import javax.ejb.FinderException JavaDoc;
59 import javax.ejb.ObjectNotFoundException JavaDoc;
60 import javax.ejb.RemoveException JavaDoc;
61 import javax.naming.InitialContext JavaDoc;
62 import javax.naming.NamingException JavaDoc;
63 import javax.rmi.PortableRemoteObject JavaDoc;
64 import java.sql.*;
65 import java.util.Collection JavaDoc;
66 import java.util.Iterator JavaDoc;
67 import java.util.List JavaDoc;
68 import java.util.Map JavaDoc;
69
70 /**
71  * This stateless ejb provides access to the list of values features. It
72  * provides a way to access and update a LOV.
73  *
74  * @see LovBean
75  * @see LovValue
76  * @see LovBusinessInterface
77  *
78  * @ejb:bean type="Stateless"
79  * name="LOVService"
80  * jndi-name="ejb/LOVService"
81  * local-jndi-name="ejb/LOVServiceLocal"
82  * view-type="both"
83  *
84  * @ejb:home extends="javax.ejb.EJBHome"
85  * local-extends="javax.ejb.EJBLocalHome"
86  *
87  * @ejb:interface extends="javax.ejb.EJBObject"
88  * local-extends="javax.ejb.EJBLocalObject"
89  *
90  * @ejb:ejb-ref ejb-name="LOV"
91  * view-type="local"
92  *
93  * @ejb:transaction type="Required"
94  *
95  * @ejb:resource-ref res-name="jdbc/DBPool"
96  * res-type="javax.sql.DataSource"
97  * res-auth="Container"
98  *
99  * @jboss:resource-ref res-ref-name="jdbc/DBPool"
100  * resource-name="FwkDataSourceManager"
101  *
102  * @jonas.bean
103  * ejb-name="LOVService"
104  * jndi-name="ejb/LOVService"
105  *
106  * @jonas.resource
107  * res-ref-name="jdbc/DBPool"
108  * jndi-name="jdbc_1"
109  *
110  */

111 public class LovServiceBean extends org.bsf.commons.ejb.SessionAdapterBean {
112     private static final String JavaDoc TABLE_NAME_FROM_LOV_OID_STATEMENT
113             = "SELECT TABLE_NAME FROM BSF_LOV WHERE OID = ?";
114
115     private LOVLocalHome _lovEntityLocalHome = null;
116
117     private final String JavaDoc JNDI_DATABASE_NAME = "DBPool";
118
119     /**
120      * Provides access to the metadata for a given list of values.
121      *
122      * @param p_lovOID The OID of the list of values for which we want the
123      * metadata.
124      *
125      * @return A list of LovMetaDataItems for the LOV corresponding to the
126      * given lovOID.
127      *
128      * @throws IllegalArgumentException if the given oid is null.
129      * @throws FinderException if a lov with the given oid isn't found.
130      *
131      * @see LovBean
132      * @see LovValue
133      * @see LovMetaDataItem
134      *
135      * @ejb:interface-method
136      */

137     public List JavaDoc getLOVMetaData( Long JavaDoc p_lovOID ) throws FinderException JavaDoc {
138         if ( p_lovOID == null ) {
139             throw new IllegalArgumentException JavaDoc( "Need a non null oid to retrun anything..." );
140         }
141
142         List JavaDoc metaDatas = null;
143
144         logGraphBegin( "getLOVMetaData for LOV " + p_lovOID );
145
146         // We retrieve the LOV, a FinderExecption is thrown if none found
147
LOVLocal lovLocal = _lovEntityLocalHome.findByPrimaryKey( p_lovOID );
148
149         // We retrieve the metadata from the found Lov
150
metaDatas = lovLocal.getMetaData();
151
152         return metaDatas;
153     }
154
155     /**
156      * Returns the LovValue having the oid p_lovValueOID in the LOV whose oid
157      * is p_lovOID. If the LOV doesn't exist a NoSuchLovException is thrown
158      * whereas a NoSuchLovValueException is thrown if, while existing, the
159      * LOV does not contain the desired LovValue.
160      *
161      * @param p_lovOID The oid of the LOV that we want to parse.
162      * @param p_lovValueOID The oid that we want to find in the selected LOV.
163      *
164      * @return The LovValue having the oid p_lovValueOID in the LOV whose oid is
165      * p_lovOID.
166      *
167      * @throws NoSuchLovException if no LOV is found for the given p_lovOID.
168      * @throws NoSuchLovValueException if no LovValue is found having the
169      * p_lovValueOID in the selected LOV.
170      * @throws IllegalArgumentException if the given p_lovOID or p_lovValueOID
171      * is (are) null.
172      *
173      * @see LovValue
174      *
175      * @ejb:interface-method
176      */

177     public LovValue getLovValue( Long JavaDoc p_lovOID, Long JavaDoc p_lovValueOID )
178             throws NoSuchLovException, NoSuchLovValueException {
179
180         LOVLocal lovLocal = null;
181         LovValue lovValue = null;
182
183         if ( ( p_lovOID == null ) || ( p_lovValueOID == null ) ) {
184             String JavaDoc msg = "Needs a non null lovOID (" + p_lovOID
185                     + ") and p_lovValueOID (" + p_lovValueOID
186                     + ") to return a LovValue...";
187
188             throw new IllegalArgumentException JavaDoc( msg );
189         }
190
191         logDebug( "getLovValue " + p_lovValueOID + " in LOV " + p_lovOID );
192
193         // A NoSuchLovException can be raised here...
194
lovLocal = getLov( p_lovOID );
195
196         // A NoSuchLovValueException can be raised here...
197
lovValue = lovLocal.getLovValue( p_lovValueOID );
198
199         return lovValue;
200     }
201
202     /**
203      * Returns, in the order of the executed SQL, the list of LovValues
204      * corresponding to the given p_lovOID.
205      *
206      * @param p_lovOID The oid of the LOV that we want to retrieve.
207      *
208      * @return A list of LovValues corresponding to the execution of the
209      * given lovOID. The LovValues are returned as they are retrieve from
210      * the execution of the SQL statement (cf. ORDER BY clause).
211      *
212      * @throws IllegalArgumentException if the given lovOID is null.
213      * @throws NoSuchLovException if no LOV is having the given oid.
214      *
215      * @see LovBean
216      * @see LovValue
217      *
218      * @ejb:interface-method
219      */

220     public List JavaDoc getListOfValues( Long JavaDoc p_lovOID ) throws NoSuchLovException {
221         if ( p_lovOID == null ) {
222             throw new IllegalArgumentException JavaDoc( "Need a non null oid to retrieve a LOV..." );
223         }
224
225         List JavaDoc listOfValues = null;
226         LOVLocal lovLocal = null;
227
228         logGraphBegin( "getSortedLOV for the lovOID " + p_lovOID );
229
230         // NoSuchLovException can be thrown from here
231
lovLocal = getLov( p_lovOID );
232
233         // RemoteException can be thrown from here
234
listOfValues = lovLocal.getListOfValues();
235
236         logGraphEnd( "getSortedLOV for the lovOID " + p_lovOID );
237
238         return listOfValues;
239     }
240
241     /**
242      * Returns a map (LovValueOID, LovValue) of LovValues corresponding to the
243      * given p_lovOID.
244      *
245      * @param p_lovOID The oid of the LOV that we want to retrieve.
246      *
247      * @return A list of LovValues corresponding to the execution of the
248      * given p_lovOID. The LovValues are returned as they are retrieve from
249      * the execution of the SQL statement (cf. ORDER BY clause).
250      *
251      * @throws IllegalArgumentException if the given p_lovOID is null.
252      * @throws NoSuchLovException if no LOV is having the given oid.
253      *
254      * @see LovBean
255      * @see LovValue
256      *
257      * @ejb:interface-method
258      */

259     public Map JavaDoc getLovValues( Long JavaDoc p_lovOID ) throws NoSuchLovException {
260         LOVLocal lovLocal = null;
261         Map JavaDoc mapLov = null;
262
263         logGraphBegin( "getLovValues as a map for lovOID " + p_lovOID );
264
265         lovLocal = getLov( p_lovOID );
266         mapLov = lovLocal.getLovValuesPerOID();
267
268         logGraphEnd( "getLovValues as a map for lovOID " + p_lovOID );
269
270         return mapLov;
271     }
272
273     /**
274      * Ensures that the LOVs using the same TABLE_NAME as the LOV identified
275      * by the given OID are reloaded (in general due to a data update). It
276      * includes, of course, the lov identified by the given lovOID.
277      *
278      * @param p_lovOID The oid of the LOV whose TABLE_NAME will be used to
279      * refresh the LOVs.
280      *
281      * @throws IllegalArgumentException if the given p_lovOID is null.
282      * @throws NoSuchLovException if there is no LOV having the given lovOID.
283      *
284      * @see LovBean
285      * @see #resynchronizeListOfValues(String p_tableName)
286      *
287      * @ejb:interface-method
288      */

289     public void resynchronizeListOfValues( Long JavaDoc p_lovOID ) throws NoSuchLovException {
290         if ( p_lovOID == null ) {
291             throw new IllegalArgumentException JavaDoc( "Can't resynchronize for a null lovOID" );
292         }
293
294         String JavaDoc physicalTable = null;
295         Connection connection = null;
296         PreparedStatement preparedStatement = null;
297         ResultSet rsTableName = null;
298
299         logGraphBegin( "resynchronizeLOV " + p_lovOID );
300
301         try {
302             // Getting the physical table name
303
connection = getConnection( JNDI_DATABASE_NAME );
304             preparedStatement = connection.prepareStatement( TABLE_NAME_FROM_LOV_OID_STATEMENT );
305             preparedStatement.setString( 1, p_lovOID.toString() );
306             rsTableName = preparedStatement.executeQuery();
307
308             // We should have a table name if the lov exist
309
if ( rsTableName.next() ) {
310                 physicalTable = rsTableName.getString( 1 );
311             } else {
312                 throw new NoSuchLovException( p_lovOID );
313             }
314         } catch( SQLException Se ) {
315             handleSQLException( Se );
316         } finally {
317             try {
318                 // We close the opened jdbc objects
319
if ( rsTableName != null ) {
320                     rsTableName.close();
321                 }
322
323                 if ( preparedStatement != null ) {
324                     preparedStatement.close();
325                 }
326
327                 if ( connection != null ) {
328                     connection.close();
329                 }
330             } catch( SQLException Se ) {
331                 logFatal( "Exception in finaly " + Se );
332             }
333         }
334
335         // If we reach here we should have something to resynchronize
336
resynchronizeListOfValues( physicalTable );
337     }
338
339     /**
340      * Ensures that the LOVs referencing the TABLE_NAME are reseted. Usually
341      * because the underlying data have changed.
342      *
343      * @param p_tableName The oid of the LOV whose TABLE_NAME will be used to
344      * refresh the LOVs. Does nothing if the name is invalid (null, no match).
345      *
346      * @see LovBean
347      * @see #resynchronizeListOfValues(Long p_lovOID)
348      *
349      * @ejb:interface-method
350      */

351     public void resynchronizeListOfValues( String JavaDoc p_tableName ) {
352         if ( p_tableName == null ) {
353             // Nothing to do...
354
return;
355         }
356
357         try {
358             Collection JavaDoc lovs = _lovEntityLocalHome.findByPhysicalTable( p_tableName );
359
360             Iterator JavaDoc lovIterator = lovs.iterator();
361
362             while ( lovIterator.hasNext() ) {
363                 Object JavaDoc aRef = lovIterator.next();
364
365                 // We need to narrow since it comes from thru collection from a finder
366
LOVLocal aLovLocal = (LOVLocal) PortableRemoteObject.narrow( aRef, LOVLocal.class );
367
368                 // We remove it (ie. we clear the cache)
369
aLovLocal.remove();
370             }
371         } catch( RemoveException JavaDoc removeException ) {
372             handleExceptionAsSystemException( removeException );
373         } catch( FinderException JavaDoc finderException ) {
374             handleFinderException( finderException );
375         }
376     }
377
378     /**
379      * Utility method to provide access to the desired LOV.
380      *
381      * @param p_lovOID The oid of the desired LOV.
382      *
383      * @return A LovLocal corresponding to the desired LOV.
384      *
385      * @throws NoSuchLovException if no LOV is found having the given oid.
386      */

387     private LOVLocal getLov( Long JavaDoc p_lovOID ) throws NoSuchLovException {
388         LOVLocal lovLocal = null;
389
390         try {
391             lovLocal = _lovEntityLocalHome.findByPrimaryKey( p_lovOID );
392         } catch( ObjectNotFoundException JavaDoc ONFe ) {
393             throw new NoSuchLovException( p_lovOID );
394         } catch( FinderException JavaDoc Fe ) {
395             handleFinderException( Fe );
396         }
397
398         return lovLocal;
399     }
400
401     // *************************************************************************
402
// ******************** SesssionAdapterBean Methods ************************
403
// *************************************************************************
404

405     public void ejbCreate() throws CreateException JavaDoc {
406         logDebug( "ejbCreate() -> Retrieving the home(s)..." );
407
408         // Retrieves the local home of the Lov Entity bean and keeps it for further use
409
try {
410             InitialContext JavaDoc ic = new InitialContext JavaDoc();
411             _lovEntityLocalHome = (LOVLocalHome) ic.lookup( "java:comp/env/ejb/LOV" );
412         } catch( NamingException JavaDoc e ) {
413             handleExceptionAsSystemException( e );
414         }
415     }
416 }
417
Popular Tags