001    package biz.wedoit4u.ejb.entity;
002    
003    import java.util.Collection;
004    
005    import javax.ejb.FinderException;
006    import javax.naming.InitialContext;
007    import javax.naming.NamingException;
008    
009    /**
010     * This class provides <code>static accessor methods</code> that 
011     * provide a convenient means of getting <code>references</code> to
012     * the various EJB's developed for the TMS Webservices initiative.
013     * The methods defined in this <code>class</code> simply invoke the
014     * appropriate <code>finder</code> methods for accessing entity
015     * beans.
016     *
017     * @see EntityBeanJNDILocations
018     *
019     * <p>Copyright 2003, Rakesh Vidyadharan and wedoit4u.biz</p>
020     *
021     * @author Rakesh Vidyadharan 10<sup><small>th</small></sup> September 2003
022     *
023     * @version $Id: EntityBeanProvider.java,v 1.4 2004/05/26 11:42:35 rakesh Exp $
024     */
025    public final class EntityBeanProvider extends Object
026    {
027      /**
028       * A reference to the application servers <code>InitialContext</code>.
029       * This reference is fetched only once for the sake of efficiency.
030       */
031      private static InitialContext initialContext = null;
032    
033      /**
034       * Static initialiser for the {@link #initialContext}
035       */
036      static
037      {
038        try
039        {
040          initialContext = new InitialContext();
041        }
042        catch ( NamingException nex )
043        {
044          // Nothing can be done here.  Log the error.
045        }
046      }
047    
048      /**
049       * A reference to the {@link CustomerTypes} home object for the
050       * {@link CustomerType} entity bean.  This reference is fetched only
051       * once for the sake of efficiency.
052       */
053      private static CustomerTypes customerTypes = null;
054    
055      /**
056       * A reference to the {@link Customers} home object for the
057       * {@link Customer} entity bean.  This reference is fetched only
058       * once for the sake of efficiency.
059       */
060      private static Customers customers = null;
061    
062      /**
063       * A reference to the {@link Contents} home object for the
064       * {@link Category} entity bean.  This reference is fetched only
065       * once for the sake of efficiency.
066       */
067      private static Categories categories = null;
068    
069      /**
070       * A reference to the {@link Categories} home object for the
071       * {@link Content} entity bean.  This reference is fetched only
072       * once for the sake of efficiency.
073       */
074      private static Contents contents = null;
075    
076      /**
077       * A reference to the {@link SiteProperties} home object for the
078       * {@link SiteProperty} entity bean.  This reference is fetched only
079       * once for the sake of efficiency.
080       */
081      private static SiteProperties siteProperties = null;
082    
083      /**
084       * Fetch the <code>home object</code> for the {@link CustomerType}
085       * entity bean.  The reference is fetched only if the {@link
086       * #customerTypes} reference is <code>null</code>.
087       *
088       * @return CustomerTypes - The home object.
089       * @throws NamingException - Any errors encountered while getting a
090       *   reference to the application servers <code>InitialContext</code>.
091       */
092      public static final CustomerTypes getCustomerTypes() 
093        throws NamingException
094      {
095        if ( customerTypes == null )
096        {
097          customerTypes = (CustomerTypes) initialContext.lookup( EntityBeanJNDILocations.CUSTOMER_TYPES_LOCATION );
098        }
099    
100        return customerTypes;
101      }
102    
103      /**
104       * Return a reference to the {@link CustomerType} entity bean.
105       *
106       * @param customerTypeId - The <code>customerType_id</code> 
107       *   column value by which the appropriate bean instance is to be 
108       *   fetched.
109       * @return CustomerType - The reference to the appropriate instance of
110       *   the CustomerType bean.
111       * @throws FinderException - Any errors encountered while looking up
112       *   the associated bean instance specified by the userId value.
113       * @throws NamingException - Any errors encountered while getting a
114       *   reference to the {@link #customerTypes} home object.
115       */
116      public static final CustomerType getCustomerType( int customerTypeId )
117        throws FinderException, NamingException
118      {
119        return ( getCustomerTypes().findByPrimaryKey( customerTypeId ) );
120      }
121    
122      /**
123       * Return a reference to the {@link CustomerType} entity bean 
124       * identified by the <code>type</code> value.
125       *
126       * @param type - The <code>type</code> column 
127       *   value by which the appropriate bean instance is to be fetched.
128       * @return CustomerType - The reference to the appropriate instance of
129       *   the CustomerType bean.
130       * @throws FinderException - Any errors encountered while looking up
131       *   the associated bean instance specified by the username value.
132       * @throws NamingException - Any errors encountered while getting a
133       *   reference to the {@link #customerTypes} home object.
134       */
135      public static final CustomerType getCustomerType( String type ) 
136        throws FinderException, NamingException
137      {
138        return ( getCustomerTypes().findByType( type ) );
139      }
140    
141      /**
142       * Return a collection of all the {@link CustomerType} entity bean
143       * instances that represent all the rows in the <code>customer_type
144       * </code> table.
145       *
146       * @return Collection - The collection of references to the entity
147       *   bean instances.
148       * @throws FinderException - Any errors encountered while looking up
149       *   the associated bean instance specified by the username value.
150       * @throws NamingException - Any errors encountered while getting a
151       *   reference to the {@link #customerTypes} home object.
152       */
153      public static final Collection getAllCustomerTypes() 
154        throws FinderException, NamingException
155      {
156        return ( getCustomerTypes().findAll() );
157      }
158    
159      /**
160       * Fetch the <code>home object</code> for the {@link Customer}
161       * entity bean.  The reference is fetched only if the {@link
162       * #customers} reference is <code>null</code>.
163       *
164       * @return Customers - The home object.
165       * @throws NamingException - Any errors encountered while getting a
166       *   reference to the application servers <code>InitialContext</code>.
167       */
168      public static final Customers getCustomers() throws NamingException
169      {
170        if ( customers == null )
171        {
172          customers = (Customers) initialContext.lookup( EntityBeanJNDILocations.CUSTOMERS_LOCATION );
173        }
174    
175        return customers;
176      }
177    
178      /**
179       * Return a reference to the {@link Customer} entity bean.
180       *
181       * @param customerId - The <code>customer_id</code> column 
182       *   value by which the appropriate bean instance is to be fetched.
183       * @return Customer - The reference to the appropriate instance of
184       *   the Customer bean.
185       * @throws FinderException - Any errors encountered while looking up
186       *   the associated bean instance specified by the userId value.
187       * @throws NamingException - Any errors encountered while getting a
188       *   reference to the {@link #customers} home object.
189       */
190      public static final Customer getCustomer( int customerId ) 
191        throws FinderException, NamingException
192      {
193        return ( getCustomers().findByPrimaryKey( customerId ) );
194      }
195    
196      /**
197       * Return a reference to the {@link Customer} entity bean.
198       *
199       * @param username - The <code>username</code> column 
200       *   value by which the appropriate bean instance is to be fetched.
201       * @return Customer - The reference to the appropriate instance of
202       *   the Customer bean.
203       * @throws FinderException - Any errors encountered while looking up
204       *   the associated bean instance specified by the username value.
205       * @throws NamingException - Any errors encountered while getting a
206       *   reference to the {@link #customers} home object.
207       */
208      public static final Customer getCustomer( String username ) 
209        throws FinderException, NamingException
210      {
211        return ( getCustomers().findByUsername( username ) );
212      }
213    
214      /**
215       * Return a <code>Collection</code> of entity bean instances that
216       * represent all the records in the <code>customers</code> table,
217       * that belong to the specified {@link CustomerType}.
218       *
219       * @param customerType - The customer type for which
220       *   the collection of customers is to be retrieved.
221       * @return Collection - The collection of bean instances.
222       * @throws NamingException - Any errors encountered while getting a
223       *   reference to the {@link #customers} home object.
224       * @throws FinderException - If errors are encountered while fetching
225       *   the appropriate instances.
226       */
227      public static final Collection getCustomerByCustomerType( CustomerType customerType ) throws NamingException, FinderException
228      {
229        return ( getCustomers().findByCustomerType( customerType ) );
230      }
231    
232      /**
233       * Return a <code>Collection</code> of references to all the entity
234       * bean instances that represent all the records in the 
235       * <code>customers</code> table.
236       *
237       * @return Collection - The references to the entity bean instances.
238       * @throws FinderException - Any errors encountered while looking up
239       *   the associated bean instance specified by the username value.
240       * @throws NamingException - Any errors encountered while getting a
241       *   reference to the {@link #customers} home object.
242       */
243      public static final Collection getAllCustomers()
244        throws FinderException, NamingException
245      {
246        return ( getCustomers().findAll() );
247      }
248    
249      /**
250       * Fetch the <code>home object</code> for the {@link Category} entity
251       * bean.  The reference is fetched only if the {@link #categories}
252       * reference is <code>null</code>.
253       *
254       * @return Categorys - The home object.
255       * @throws NamingException - Any errors encountered while getting a
256       *   reference to the home object from the application servers 
257       *   <code>InitialContext</code>.
258       */
259      public static final Categories getCategories() throws NamingException
260      {
261        if ( categories == null )
262        {
263          categories = (Categories) initialContext.lookup( EntityBeanJNDILocations.CATEGORIES_LOCATION );
264        }
265    
266        return categories;
267      }
268    
269      /**
270       * Return a reference to the {@link Category} entity bean identified
271       * by the <code>primary key</code> value specified.
272       *
273       * @param categoryId - The <code>category_id</code> column value 
274       *   by which the appropriate bean instance is to be fetched.
275       * @return Category - The reference to the appropriate instance of
276       *   the Category bean.
277       * @throws FinderException - Any errors encountered while looking up
278       *   the associated bean instance specified by the categoryId value.
279       * @throws NamingException - Any errors encountered while getting a
280       *   reference to the home object from the application servers 
281       *   <code>InitialContext</code>.
282       */
283      public static final Category getCategory( int categoryId ) 
284        throws FinderException, NamingException
285      {
286        return ( getCategories().findByPrimaryKey( categoryId ) );
287      }
288    
289      /**
290       * Return a <code>Collection</code> of reference to the {@link 
291       * Category} entity beans that belong to the specified 
292       * <code>customer_id</code>.
293       *
294       * @param customer - The <code>customer_id</code> column 
295       *   value by which the appropriate bean instances are to be fetched.
296       *   The customer_id value is represented by its entity bean 
297       *   representation.
298       * @return Collection - The references to the appropriate instances of
299       *   the Category bean.
300       * @throws FinderException - Any errors encountered while looking up
301       *   the associated bean instance specified by the userId value.
302       * @throws NamingException - Any errors encountered while getting a
303       *   reference to the home object from the application servers 
304       *   <code>InitialContext</code>.
305       */
306      public static final Collection getCategoryByCustomer( Customer customer )
307        throws FinderException, NamingException
308      {
309        return ( getCategories().findByCustomer( customer ) );
310      }
311    
312      /**
313       * Return a <code>Collection</code> of reference to the {@link 
314       * Category} entity beans that represent all the records in the
315       * <code>customer_id</code> table.
316       *
317       * @return Collection - The references to the appropriate instances of
318       *   the Category bean.
319       * @throws FinderException - Any errors encountered while looking up
320       *   the associated bean instance specified by the userId value.
321       * @throws NamingException - Any errors encountered while getting a
322       *   reference to the home object from the application servers 
323       *   <code>InitialContext</code>.
324       */
325      public static final Collection getAllCategories()
326        throws FinderException, NamingException
327      {
328        return ( getCategories().findAll() );
329      }
330    
331      /**
332       * Fetch the <code>home object</code> for the {@link Contents}
333       * entity bean.  This reference is fetched only if the {@link
334       * #contents} is <code>null</code>.
335       *
336       * @return Contents - The home object.
337       * @throws NamingException - Any errors encountered while getting a
338       *   reference to the home object from the application servers 
339       *   <code>InitialContext</code>.
340       */
341      public static final Contents getContents() throws NamingException
342      {
343        if ( contents == null )
344        {
345          contents =  (Contents) initialContext.lookup( EntityBeanJNDILocations.CONTENTS_LOCATION );
346        }
347    
348        return contents;
349      }
350    
351      /**
352       * Return a reference to the {@link Content} entity bean identified
353       * by the <code>primary key</code> value specified.
354       *
355       * @param contentId - The <code>content_id</code> column 
356       *   value by which the appropriate bean instance is to be fetched.
357       * @return Content - The reference to the appropriate instance of
358       *   the Content bean.
359       * @throws FinderException - Any errors encountered while looking up
360       *   the associated bean instance specified by the deviceTypeId value.
361       * @throws NamingException - Any errors encountered while getting a
362       *   reference to the home object from the application servers 
363       *   <code>InitialContext</code>.
364       */
365      public static final Content getContent( int contentId ) 
366        throws FinderException, NamingException
367      {
368        return ( getContents().findByPrimaryKey( contentId ) );
369      }
370    
371      /**
372       * Return a <code>Collection</code> of {@link Content} entity
373       * bean instances that represent all the records in the
374       * <code>contents</code> table.
375       *
376       * @return Collection - A collection of references to valid entity
377       *   beans representing the content records.
378       * @throws FinderException - If errors are encountered while finding
379       *   devices for the specified customer.
380       * @throws NamingException - Any errors encountered while getting a
381       *   reference to the home object from the application servers 
382       *   <code>InitialContext</code>.
383       */
384      public static final Collection getAllContents() 
385          throws FinderException, NamingException
386      {
387        return ( getContents().findAll() );
388      }
389    
390      /**
391       * Return a <code>Collection</code> of {@link Content} entity
392       * bean instances that are associated with the specified 
393       * {@link Category}.
394       *
395       * @param category - The <code>category_id</code> for which
396       *   all the {@link Content} instances are to be retrieved using the
397       *   related {@link Category} entity bean instance.
398       * @return Collection - A collection of references to valid entity
399       *   beans representing the contents associated with the category.
400       * @throws FinderException - If errors are encountered while finding
401       *   devices for the specified customer.
402       * @throws NamingException - Any errors encountered while getting a
403       *   reference to the home object from the application servers 
404       *   <code>InitialContext</code>.
405       */
406      public static final Collection getContentByCategory( 
407          Category category ) throws FinderException, NamingException
408      {
409        return ( getContents().findByCategory( category ) );
410      }
411    
412      /**
413       * Fetch the <code>home object</code> for the {@link SiteProperty}
414       * entity bean.  This reference is fetched only if the {@link
415       * #siteProperties} is <code>null</code>.
416       *
417       * @return SiteProperties - The home object.
418       * @throws NamingException - Any errors encountered while getting a
419       *   reference to the home object from the application servers 
420       *   <code>InitialContext</code>.
421       */
422      public static final SiteProperties getSiteProperties() 
423        throws NamingException
424      {
425        if ( siteProperties == null )
426        {
427          siteProperties =  (SiteProperties) initialContext.lookup( EntityBeanJNDILocations.SITE_PROPERTIES_LOCATION );
428        }
429    
430        return siteProperties;
431      }
432    
433      /**
434       * Return a reference to the {@link SiteProperty} entity bean.
435       *
436       * @param customerId - The <code>customer_id</code> 
437       *   column value by which the appropriate bean instance is to be 
438       *   fetched.
439       * @return SiteProperty - The reference to the appropriate instance of
440       *   the SiteProperty bean.
441       * @throws FinderException - Any errors encountered while looking up
442       *   the associated bean instance specified by the userId value.
443       * @throws NamingException - Any errors encountered while getting a
444       *   reference to the {@link #customers} home object.
445       */
446      public static final SiteProperty getSiteProperty( int customerId )
447        throws FinderException, NamingException
448      {
449        return ( getSiteProperties().findByPrimaryKey( customerId ) );
450      }
451    
452      /**
453       * Return a <code>Collection</code> of entity beans of type {@link 
454       * SiteProperty} that represent all the records in the 
455       * <code>site_properties</code> table.
456       *
457       * @return Collection - The collection of entity bean instances.
458       * @throws FinderException - Any errors encountered while looking up
459       *   the associated bean instance specified by the userId value.
460       * @throws NamingException - Any errors encountered while getting a
461       *   reference to the {@link #customers} home object.
462       */
463      public static final Collection getAllSiteProperties()
464        throws FinderException, NamingException
465      {
466        return ( getSiteProperties().findAll() );
467      }
468    
469      /**
470       * Default constructor.  Cannot be instantiated.
471       */
472      private EntityBeanProvider() {}
473    }