001    package biz.wedoit4u;
002    
003    import java.util.Collection;
004    import java.util.Date;
005    import java.util.Iterator;
006    
007    import javax.ejb.FinderException;
008    import javax.naming.NamingException;
009    import javax.servlet.http.HttpServletRequest;
010    import javax.servlet.http.HttpSession;
011    
012    import biz.wedoit4u.databeans.*;
013    
014    import biz.wedoit4u.ejb.entity.StyleAttributesPK;
015    import biz.wedoit4u.ejb.session.SessionBeanProvider;
016    import biz.wedoit4u.ejb.session.DateFormatter;
017    
018    /**
019     * A Java Bean that is used to store sessional information for a
020     * CMA user.  This class also contains utility methods that are used
021     * to abstract business logic away from the presentation layer.
022     *
023     * <p>Copyright 2003, Rakesh Vidyadharan and wedoit4u.biz</p>
024     *
025     * @author Rakesh Vidyadharan 01<sup><small>st</small></sup> October 2003
026     *
027     * @version $Id: AdminSession.java,v 1.2 2004/05/26 11:42:29 rakesh Exp $
028     */
029    public abstract class AdminSession extends Session
030    {
031      /**
032       * The default constructor.  Does nothing special.
033       */
034      public AdminSession() {}
035    
036      /**
037       * Return an <code>Array</code> of java bean instances of type
038       * {@link biz.wedoit4u.databeans.PartnerBean}.
039       *
040       * @see biz.wedoit4u.databeans.PartnerBean#findAll()
041       * @return PartnerBean[] - The array of java bean instances.
042       * @throws CMAException - If Naming or Finder exceptions are 
043       *   encoutered while fetching the collection of entity beans.
044       */
045      public PartnerBean[] getPartnerList() throws CMAException
046      {
047        Collection collection = PartnerBean.findAll();
048        PartnerBean[] partnerArray = 
049          new PartnerBean[ collection.size() ];
050    
051        Iterator iterator = collection.iterator();
052        for ( int i = 0; iterator.hasNext(); ++i )
053        {
054          partnerArray[i] = (PartnerBean) iterator.next();
055        }
056    
057        return partnerArray;
058      }
059    
060      /**
061       * Create/edit a <code>partners</code> record.  If the specified
062       * <code>partner_id</code> is <code>0</code> then a new record
063       * is created in the <code>partner</code> table.  If the
064       * specified <code>partner_id</code> exists in the database, then 
065       * the associated record is updated, otherwise, a new record is 
066       * created with the specified primary key value.
067       *
068       * @see biz.wedoit4u.databeans.PartnerBean#create( int, String, String, char, Date, String, String, String, String, String )
069       * @see biz.wedoit4u.databeans.SitePropertyBean#create( int )
070       * @param request - The request from the client that
071       *   contains the form elements with all the value for the database
072       *   fields.
073       * @return PartnerBean - The appropriate instance of the java
074       *   bean that represents a record in the table.
075       * @throws CMAException - If database errors are encountered while
076       *   creating/editing the database record.
077       */
078      public PartnerBean editPartner( HttpServletRequest request )
079          throws CMAException
080      {
081        // Fetch all the form parameters
082        String idString = request.getParameter( "partnerId" );
083        String username = request.getParameter( "username" );
084        String password = request.getParameter( "password" );
085        String activeStr = request.getParameter( "active" );
086        String expiration = request.getParameter( "expirationDate" );
087        String partnerName = request.getParameter( "partnerName" );
088        String description = request.getParameter( "description" );
089        if ( description.equals( "" ) ) description = null;
090        String email = request.getParameter( "email" );
091        if ( email.equals( "" ) ) email = null;
092        String telephone = request.getParameter( "telephone" );
093        if ( telephone.equals( "" ) ) telephone = null;
094        String mobilePhone = request.getParameter( "mobilePhone" );
095        if ( mobilePhone.equals( "" ) ) mobilePhone = null;
096    
097        int partnerId = 0;
098        char active = activeStr.charAt( 0 );
099        Date expirationDate = new Date();
100        PartnerBean pb = null;
101    
102        try
103        {
104          partnerId = Integer.parseInt( idString );
105        }
106        catch ( NumberFormatException nfex )
107        {
108          throw new CMAException( "Invalid partnerId " + idString + "specified.", nfex );
109        }
110    
111        try
112        {
113          DateFormatter dateFormatter = SessionBeanProvider.getDateFormatter();
114          expirationDate = dateFormatter.getSQLTimestamp( expiration );
115        }
116        catch ( Throwable t )
117        {
118          Logger.error( "Error using DateFormatter in CMASession.editPartner for partnerId " + partnerId + ".", t );
119        }
120    
121        try
122        {
123          if ( partnerId == 0 )
124          {
125            pb = PartnerBean.create( partnerId, username, password, active, expirationDate, partnerName, description, email, telephone, mobilePhone );
126          }
127          else
128          {
129            boolean newRecord = false;
130            try
131            {
132              pb = PartnerBean.getInstance( partnerId );
133            }
134            catch ( CMAException cex )
135            {
136              pb = PartnerBean.create( partnerId, username, password, active, expirationDate, partnerName, description, email, telephone, mobilePhone );
137              newRecord = true;
138            }
139    
140            if ( ! newRecord )
141            {
142              pb.username = username;
143              pb.password = password;
144              pb.active = active;
145              pb.expirationDate.setTime( expirationDate.getTime() );
146              pb.partnerName = partnerName;
147              pb.description = description;
148              pb.email = email;
149              pb.telephone = telephone;
150              pb.mobilePhone = mobilePhone;
151              pb.save();
152            }
153          }
154        }
155        catch ( Throwable t )
156        {
157          String error = "Error while creating/editing partner record in CMASession.editPartner for partner_id " + partnerId + " username " + username + " password " + password + " active " + active + " partnerName " + partnerName + ".";
158          throw new CMAException( error, t );
159        }
160    
161        return pb;
162      }
163    
164      /**
165       * Delete a <code>partners</code> record.  The database record
166       * as well as the associated {@link 
167       * biz.wedoit4u.databeans.PartnerBean} instance are deleted.
168       *
169       * @see biz.wedoit4u.databeans.PartnerBean#delete( PartnerBean )
170       * @param request - The request from the client that
171       *   contains the form elements with all the value for the database
172       *   fields.
173       * @throws CMAException - If database errors are encountered while
174       *   deleting the database record.
175       */
176      public void deletePartner( HttpServletRequest request ) 
177        throws CMAException
178      {
179        // Fetch all the form parameters
180        String idString = request.getParameter( "partnerId" );
181        int partnerId = 0;
182    
183        try
184        {
185          partnerId = Integer.parseInt( idString );
186        }
187        catch ( NumberFormatException nfex )
188        {
189          throw new CMAException( "Invalid partnerId " + idString + " specified.", nfex );
190        }
191    
192        try
193        {
194          PartnerBean pb = PartnerBean.getInstance( partnerId );
195          PartnerBean.delete( pb );
196        }
197        catch ( Throwable t )
198        {
199          String error = "Error while deleting partner record in CMASession.deletePartner for partner_id " + partnerId + ".";
200          throw new CMAException( error, t );
201        }
202      }
203    
204      /**
205       * Return an <code>Array</code> of java bean instances of
206       * type {@link biz.wedoit4u.databeans.CustomerTypeBean}.
207       *
208       * @see biz.wedoit4u.databeans.CustomerTypeBean#findAll()
209       * @return CustomerTypeBean[] - The array of java bean instances.
210       * @throws CMAException - If Naming or Finder exceptions are 
211       *   encountered while fetching the collection of java beans.
212       */
213      public CustomerTypeBean[] getCustomerTypeList() throws CMAException
214      {
215        Collection collection = CustomerTypeBean.findAll();
216        CustomerTypeBean[] customerTypeArray = 
217          new CustomerTypeBean[ collection.size() ];
218    
219        Iterator iterator = collection.iterator();
220        for ( int i = 0; iterator.hasNext(); ++i )
221        {
222          customerTypeArray[i] = (CustomerTypeBean) iterator.next();
223        }
224    
225        return customerTypeArray;
226      }
227    
228      /**
229       * Create/edit a <code>customer_types</code> record.  If the specified
230       * <code>customer_type_id</code> is <code>0</code> then a new record
231       * is created.  The the specified <code>customer_type_id</code> 
232       * exists in the database, then the associated record is updated,
233       * otherwise, a new record is created with the specified value.
234       *
235       * @param request - The request from the client that
236       *   contains the form elements with all the value for the database
237       *   fields.
238       * @return CustomerTypeBean - The appropriate instance of the java
239       *   bean that represents a record in the table.
240       * @throws CMAException - If database errors are encountered while
241       *   creating/editing the database record.
242       */
243      public CustomerTypeBean editCustomerType( HttpServletRequest request )
244          throws CMAException
245      {
246        // Fetch all the form parameters
247        String idString = request.getParameter( "customerTypeId" );
248        String type = request.getParameter( "type" );
249        String description = request.getParameter( "description" );
250        String partner = request.getParameter( "partnerId" );
251        if ( description.equals( "" ) ) description = null;
252        int customerTypeId = 0;
253        int partnerId = 0;
254        CustomerTypeBean ctb = null;
255    
256        try
257        {
258          customerTypeId = Integer.parseInt( idString );
259        }
260        catch ( NumberFormatException nfex )
261        {
262          throw new CMAException( "Invalid customerTypeId " + idString + " specified.", nfex );
263        }
264    
265        try
266        {
267          partnerId = Integer.parseInt( partner );
268        }
269        catch ( NumberFormatException nfex )
270        {
271          throw new CMAException( "Invalid partnerId " + partner + " specified.", nfex );
272        }
273    
274        try
275        {
276          if ( customerTypeId == 0 )
277          {
278            ctb = CustomerTypeBean.create( customerTypeId, type, description, partnerId );
279          }
280          else
281          {
282            boolean newRecord = false;
283            try
284            {
285              ctb = CustomerTypeBean.getInstance( customerTypeId );
286            }
287            catch ( CMAException cex )
288            {
289              ctb = CustomerTypeBean.create( customerTypeId, type, description, partnerId );
290              newRecord = true;
291            }
292    
293            if ( ! newRecord )
294            {
295              ctb.type = type;
296              ctb.description = description;
297              ctb.save();
298            }
299          }
300        }
301        catch ( Throwable t )
302        {
303          String error = "Error while creating/editing customer_type record in CMASession.editCustomerType for customer_type_id " + customerTypeId + ".";
304          throw new CMAException( error, t );
305        }
306    
307        return ctb;
308      }
309    
310      /**
311       * Delete a <code>customer_types</code> record.  The database record
312       * as well as the associated {@link 
313       * biz.wedoit4u.databeans.CustomerTypeBean} instance are deleted.
314       *
315       * @see biz.wedoit4u.databeans.CustomerTypeBean#delete( CustomerTypeBean )
316       * @param request - The request from the client that
317       *   contains the form elements with all the value for the database
318       *   fields.
319       * @throws CMAException - If database errors are encountered while
320       *   deleting the database record.
321       */
322      public void deleteCustomerType( HttpServletRequest request ) 
323        throws CMAException
324      {
325        // Fetch all the form parameters
326        String idString = request.getParameter( "customerTypeId" );
327        int customerTypeId = 0;
328    
329        try
330        {
331          customerTypeId = Integer.parseInt( idString );
332        }
333        catch ( NumberFormatException nfex )
334        {
335          throw new CMAException( "Invalid customerTypeId " + idString + " specified.", nfex );
336        }
337    
338        try
339        {
340          CustomerTypeBean ctb = CustomerTypeBean.getInstance( customerTypeId );
341          CustomerTypeBean.delete( ctb );
342        }
343        catch ( Throwable t )
344        {
345          String error = "Error while deleting customer_type record in CMASession.deleteCustomerType for customer_type_id " + customerTypeId + ".";
346          throw new CMAException( error, t );
347        }
348      }
349    
350      /**
351       * Return an <code>Array</code> of java bean instances of type
352       * {@link biz.wedoit4u.databeans.CustomerBean}.  If {@link
353       * #partnerId} has been set a valid value, then only customers
354       * belonging to that partner are retrieved.
355       *
356       * @see biz.wedoit4u.databeans.CustomerBean#findAll()
357       * @see biz.wedoit4u.databeans.CustomerBean#findByPartner()
358       * @see #getCustomerBeans( Collection )
359       * @return CustomerBean[] - The array of java bean instances.
360       * @throws CMAException - If Naming or Finder exceptions are 
361       *   encoutered while fetching the collection of entity beans.
362      public CustomerBean[] getCustomerList() throws CMAException
363      {
364        if ( partnerId != 0 )
365        {
366          Collection collection = CustomerBean.findByPartner( partnerId );
367          return getCustomerBeans( collection );
368        }
369        else
370        {
371          Collection collection = CustomerBean.findAll();
372          return getCustomerBeans( collection );
373        }
374      }
375       */
376    
377      /**
378       * Return an <code>Array</code> of java bean instances of type
379       * {@link biz.wedoit4u.databeans.CustomerBean} which belong to the
380       * <code>customer_type_id</code> specified.  If {@link #partnerId}
381       * is set to a valid value, then only customer who belong to that
382       * partner are retrieved.
383       *
384       * @see biz.wedoit4u.databeans.CustomerBean#findByPartnerAndCustomerType( int, int )
385       * @see biz.wedoit4u.databeans.CustomerBean#findByCustomerType( int )
386       * @see #getCustomerBeans( Collection )
387       * @param customerTypeId - The <code>customer_type_id</code>
388       *   primary key value that identifies the CustomerType entity
389       *   bean instance.
390       * @return CustomerBean[] - The array of entity bean instances.
391       * @throws CMAException - If Naming or Finder exceptions are 
392       *   encoutered while fetching the collection of entity beans.
393      public CustomerBean[] getCustomerList( int customerTypeId )
394        throws CMAException
395      {
396        if ( partnerId != 0 )
397        {
398          Collection collection = CustomerBean.findByPartnerAndCustomerType( partnerId, customerTypeId );
399          return getCustomerBeans( collection );
400        }
401        else
402        {
403          Collection collection = CustomerBean.findByCustomerType( customerTypeId );
404          return getCustomerBeans( collection );
405        }
406      }
407       */
408    
409      /**
410       * Iterate through the <code>Collection</code> of entity bean
411       * instances of type {@link biz.wedoit4u.ejb.entity.Customer}
412       * and create an array of java beans.
413       *
414       * @param collection - The collection of entity bean
415       *   instances.
416       * @return CustomerBean[] - The array of java bean.
417       * @throws CMAException - If errors are encountered while fetching
418       *   the java bean instances.
419       */
420      private CustomerBean[] getCustomerBeans( Collection collection )
421        throws CMAException
422      {
423        CustomerBean[] customerArray = 
424          new CustomerBean[ collection.size() ];
425    
426        Iterator iterator = collection.iterator();
427        for ( int i = 0; iterator.hasNext(); ++i )
428        {
429          customerArray[i] = (CustomerBean) iterator.next();
430        }
431    
432        return customerArray;
433      }
434    
435      /**
436       * Create/edit a <code>customers</code> record.  If the specified
437       * <code>customer_id</code> is <code>0</code> then a new record
438       * is created in the <code>customer</code> table and and associated
439       * record in the <code>site_properties</code> tables.  If the 
440       * specified <code>customer_id</code> exists in the database, then 
441       * the associated record is updated, otherwise, a new record is 
442       * created with the specified primary key value.
443       *
444       * @see biz.wedoit4u.databeans.CustomerBean#create( int, String, String, String, String, char, Date, Date, int )
445       * @see biz.wedoit4u.databeans.SitePropertyBean#create( int )
446       * @param request - The request from the client that
447       *   contains the form elements with all the value for the database
448       *   fields.
449       * @return CustomerBean - The appropriate instance of the java
450       *   bean that represents a record in the table.
451       * @throws CMAException - If database errors are encountered while
452       *   creating/editing the database record.
453       */
454      public CustomerBean editCustomer( HttpServletRequest request )
455          throws CMAException
456      {
457        // Fetch all the form parameters
458        String idString = request.getParameter( "customerId" );
459        String username = request.getParameter( "username" );
460        String password = request.getParameter( "password" );
461        String email = request.getParameter( "email" );
462        if ( email.equals( "" ) ) email = null;
463        String domain = request.getParameter( "domain" );
464        if ( domain.equals( "" ) ) domain = null;
465        String activeStr = request.getParameter( "active" );
466        String activation = request.getParameter( "activationDate" );
467        String expiration = request.getParameter( "expirationDate" );
468        String customerType = request.getParameter( "customerTypeId" );
469    
470        int customerId = 0;
471        int customerTypeId = 0;
472        char active = activeStr.charAt( 0 );
473        Date activationDate = new Date();
474        Date expirationDate = new Date();
475        CustomerBean cb = null;
476    
477        try
478        {
479          customerId = Integer.parseInt( idString );
480        }
481        catch ( NumberFormatException nfex )
482        {
483          throw new CMAException( "Invalid customerId " + idString + "specified.", nfex );
484        }
485    
486        try
487        {
488          customerTypeId = Integer.parseInt( customerType );
489        }
490        catch ( NumberFormatException nfex )
491        {
492          throw new CMAException( "Invalid customerTypeId " + customerType + "specified.", nfex );
493        }
494    
495        try
496        {
497          DateFormatter dateFormatter = SessionBeanProvider.getDateFormatter();
498          activationDate = dateFormatter.getSQLTimestamp( activation );
499          expirationDate = dateFormatter.getSQLTimestamp( expiration );
500        }
501        catch ( Throwable t )
502        {
503          Logger.error( "Error using DateFormatter in CMASession.editCustomer for customerId " + customerId + ".", t );
504        }
505    
506        try
507        {
508          if ( customerId == 0 )
509          {
510            cb = CustomerBean.create( customerId, username, password, email, domain, active, activationDate, expirationDate, customerTypeId );
511            SitePropertyBean.create( cb.getCustomerId() );
512          }
513          else
514          {
515            boolean newRecord = false;
516            try
517            {
518              cb = CustomerBean.getInstance( customerId );
519            }
520            catch ( CMAException cex )
521            {
522              cb = CustomerBean.create( customerId, username, password, email, domain, active, activationDate, expirationDate, customerTypeId );
523              newRecord = true;
524            }
525    
526            if ( ! newRecord )
527            {
528              cb.username = username;
529              cb.password = password;
530              cb.email = email;
531              cb.domain = domain;
532              cb.active = active;
533              cb.activationDate.setTime( activationDate.getTime() );
534              cb.expirationDate.setTime( expirationDate.getTime() );
535              cb.customerTypeId = customerTypeId;
536              cb.save();
537            }
538          }
539        }
540        catch ( Throwable t )
541        {
542          String error = "Error while creating/editing customer record in CMASession.editCustomer for customer_id " + customerId + ".";
543          throw new CMAException( error, t );
544        }
545    
546        return cb;
547      }
548    
549      /**
550       * Delete a <code>customers</code> record.  The database record
551       * as well as the associated {@link 
552       * biz.wedoit4u.databeans.CustomerBean} instance are deleted.
553       *
554       * @see biz.wedoit4u.databeans.CustomerBean#delete( CustomerBean )
555       * @param request - The request from the client that
556       *   contains the form elements with all the value for the database
557       *   fields.
558       * @throws CMAException - If database errors are encountered while
559       *   deleting the database record.
560       */
561      public void deleteCustomer( HttpServletRequest request ) 
562        throws CMAException
563      {
564        // Fetch all the form parameters
565        String idString = request.getParameter( "customerId" );
566        int customerId = 0;
567    
568        try
569        {
570          customerId = Integer.parseInt( idString );
571        }
572        catch ( NumberFormatException nfex )
573        {
574          throw new CMAException( "Invalid customerId " + idString + " specified.", nfex );
575        }
576    
577        try
578        {
579          CustomerBean cb = CustomerBean.getInstance( customerId );
580          CustomerBean.delete( cb );
581        }
582        catch ( Throwable t )
583        {
584          String error = "Error while deleting customer record in CMASession.deleteCustomer for customer_id " + customerId + ".";
585          throw new CMAException( error, t );
586        }
587      }
588    
589      /**
590       * Return an <code>Array</code> of java bean instances of type
591       * {@link biz.wedoit4u.databeans.CategoryBean} that represents all the
592       * records in the <code>categories</code> table.
593       *
594       * @see biz.wedoit4u.databeans.CategoryBean#findAll()
595       * @see #getCategoryBeans( Collection )
596       * @return CategoryBean[] - The array of java bean instances.
597       * @throws CMAException - If Naming or Finder exceptions are 
598       *   encoutered while fetching the collection of entity beans.
599       */
600      public CategoryBean[] getCategoryList() throws CMAException
601      {
602        Collection collection = CategoryBean.findAll();
603        return getCategoryBeans( collection );
604      }
605    
606      /**
607       * Create/edit a <code>categories</code> record.  If the specified
608       * <code>category_id</code> is <code>0</code> then a new record
609       * is created.  If the specified <code>category_id</code> 
610       * exists in the database, then the associated record is updated,
611       * otherwise, a new record is created with the specified value.
612       *
613       * @param request - The request from the client that
614       *   contains the form elements with all the value for the database
615       *   fields.
616       * @return CategoryBean - The appropriate instance of the java
617       *   bean that represents a record in the table.
618       * @throws CMAException - If database errors are encountered while
619       *   creating/editing the database record.
620       */
621      public CategoryBean editCategory( HttpServletRequest request )
622          throws CMAException
623      {
624        // Fetch all the form parameters
625        String id = request.getParameter( "categoryId" );
626        String customer = request.getParameter( "customerId" );
627        String menuName = request.getParameter( "menuName" );
628        String longName = request.getParameter( "longName" );
629        String brief = request.getParameter( "brief" );
630        if ( brief.equals( "" ) ) brief = null;
631        String picture = request.getParameter( "picture" );
632        if ( picture.equals( "" ) ) picture = null;
633        String description = request.getParameter( "description" );
634        if ( description.equals( "" ) ) description = null;
635        String sortOrderString = request.getParameter( "sortOrder" );
636    
637        CategoryBean cb = null;
638        int categoryId = 0;
639        int customerId = Integer.parseInt( customer );
640        int sortOrder = 1;
641    
642        try
643        {
644          categoryId = Integer.parseInt( id );
645        }
646        catch ( NumberFormatException nfex )
647        {
648          throw new CMAException( "Invalid categoryId " + id + " specified.", nfex );
649        }
650    
651        try
652        {
653          customerId = Integer.parseInt( customer ); 
654        }
655        catch ( NumberFormatException nfex )
656        {
657          throw new CMAException( "Invalid customerId " + customer + " specified.", nfex );
658        }
659    
660        try
661        {
662          sortOrder = Integer.parseInt( sortOrderString );
663        }
664        catch ( NumberFormatException nfex )
665        {
666          // Ignore it
667        }
668    
669        try
670        {
671          if ( categoryId == 0 )
672          {
673            cb = CategoryBean.create( categoryId, customerId, menuName, longName, brief, picture, description, sortOrder );
674          }
675          else
676          {
677            boolean newRecord = false;
678            try
679            {
680              cb = CategoryBean.getInstance( categoryId );
681            }
682            catch ( CMAException cex )
683            {
684              cb = CategoryBean.create( categoryId, customerId, menuName, longName, brief, picture, description, sortOrder );
685              newRecord = true;
686            }
687    
688            if ( ! newRecord )
689            {
690              cb.customerId = customerId;
691              cb.menuName = menuName;
692              cb.longName = longName;
693              cb.brief = brief;
694              cb.picture = picture;
695              cb.description = description;
696              cb.sortOrder = sortOrder;
697              cb.save();
698            }
699          }
700        }
701        catch ( Throwable t )
702        {
703          String error = "Error while creating/editing category record in CMASession.editCategory for category_id " + categoryId + ".";
704          throw new CMAException( error, t );
705        }
706    
707        return cb;
708      }
709    
710      /**
711       * Delete a <code>categories</code> record.  The database record
712       * as well as the associated {@link 
713       * biz.wedoit4u.databeans.CategoryBean} instance are deleted.
714       *
715       * @see biz.wedoit4u.databeans.CategoryBean#delete( CategoryBean )
716       * @param request - The request from the client that
717       *   contains the form elements with all the value for the database
718       *   fields.
719       * @throws CMAException - If database errors are encountered while
720       *   deleting the database record.
721       */
722      public void deleteCategory( HttpServletRequest request ) 
723        throws CMAException
724      {
725        // Fetch all the form parameters
726        String idString = request.getParameter( "categoryId" );
727        int categoryId = 0;
728    
729        try
730        {
731          categoryId = Integer.parseInt( idString );
732        }
733        catch ( NumberFormatException nfex )
734        {
735          throw new CMAException( "Invalid categoryId " + idString + " specified.", nfex );
736        }
737    
738        try
739        {
740          CategoryBean cb = CategoryBean.getInstance( categoryId );
741          CategoryBean.delete( cb );
742        }
743        catch ( Throwable t )
744        {
745          String error = "Error while deleting category record in CMASession.deleteCategory for category_id " + categoryId + ".";
746          throw new CMAException( error, t );
747        }
748      }
749    
750      /**
751       * Create/edit a <code>contents</code> record.  If the specified
752       * <code>content_id</code> is <code>0</code> then a new record
753       * is created.  If the specified <code>content_id</code> 
754       * exists in the database, then the associated record is updated,
755       * otherwise, a new record is created with the specified value.
756       *
757       * @param request - The request from the client that
758       *   contains the form elements with all the value for the database
759       *   fields.
760       * @return ContentBean - The appropriate instance of the java
761       *   bean that represents a record in the table.
762       * @throws CMAException - If database errors are encountered while
763       *   creating/editing the database record.
764       */
765      public ContentBean editContent( HttpServletRequest request )
766          throws CMAException
767      {
768        // Fetch all the form parameters
769        String id = request.getParameter( "contentId" );
770        String category = request.getParameter( "categoryId" );
771        String title = request.getParameter( "title" );
772        String brief = request.getParameter( "brief" );
773        if ( brief.equals( "" ) ) brief = null;
774        String picture = request.getParameter( "picture" );
775        if ( picture.equals( "" ) ) picture = null;
776        String body = request.getParameter( "body" );
777        if ( body == null || body.equals( "" ) ) body = " ";
778    
779        ContentBean cb = null;
780        int contentId = 0;
781        int categoryId = 0;
782        try
783        {
784          contentId = Integer.parseInt( id );
785        }
786        catch ( NumberFormatException nfex )
787        {
788          throw new CMAException( "Invalid contentId " + id + " specified.", nfex );
789        }
790    
791        try
792        {
793          categoryId = Integer.parseInt( category );
794        }
795        catch ( NumberFormatException nfex )
796        {
797          throw new CMAException( "Invalid categoryId " + category + " specified.", nfex );
798        }
799    
800        try
801        {
802          if ( contentId == 0 )
803          {
804            cb = ContentBean.create( contentId, categoryId, title, brief, picture, body );
805          }
806          else
807          {
808            boolean newRecord = false;
809            try
810            {
811              cb = ContentBean.getInstance( contentId );
812            }
813            catch ( CMAException cex )
814            {
815              cb = ContentBean.create( contentId, categoryId, title, brief, picture, body );
816              newRecord = true;
817            }
818    
819            if ( ! newRecord )
820            {
821              cb.categoryId = categoryId;
822              cb.title = title;
823              cb.brief = brief;
824              cb.picture = picture;
825              cb.body = body;
826              cb.save();
827            }
828          }
829        }
830        catch ( Throwable t )
831        {
832          String error = "Error while creating/editing content record in CMASession.editContent for content_id " + contentId + ".";
833          throw new CMAException( error, t );
834        }
835    
836        return cb;
837      }
838    
839      /**
840       * Delete a <code>contents</code> record.  The database record
841       * as well as the associated {@link 
842       * biz.wedoit4u.databeans.ContentBean} instance are deleted.
843       *
844       * @see biz.wedoit4u.databeans.ContentBean#delete( ContentBean )
845       * @param request - The request from the client that
846       *   contains the form elements with all the value for the database
847       *   fields.
848       * @throws CMAException - If database errors are encountered while
849       *   deleting the database record.
850       */
851      public void deleteContent( HttpServletRequest request ) 
852        throws CMAException
853      {
854        // Fetch all the form parameters
855        String idString = request.getParameter( "contentId" );
856        int contentId = 0;
857    
858        try
859        {
860          contentId = Integer.parseInt( idString );
861        }
862        catch ( NumberFormatException nfex )
863        {
864          throw new CMAException( "Invalid contentId " + idString + " specified.", nfex );
865        }
866    
867        try
868        {
869          ContentBean cb = ContentBean.getInstance( contentId );
870          ContentBean.delete( cb );
871        }
872        catch ( Throwable t )
873        {
874          String error = "Error while deleting content record in CMASession.deleteContent for content_id " + contentId + ".";
875          throw new CMAException( error, t );
876        }
877      }
878    
879      /**
880       * Return an <code>Array</code> of java bean instances of
881       * type {@link biz.wedoit4u.databeans.SitePropertyBean}.
882       *
883       * @see biz.wedoit4u.databeans.SitePropertyBean#findAll()
884       * @return SitePropertyBean[] - The array of java bean instances.
885       * @throws CMAException - If Naming or Finder exceptions are 
886       *   encountered while fetching the collection of java beans.
887       */
888      public SitePropertyBean[] getSitePropertyList() throws CMAException
889      {
890        Collection collection = SitePropertyBean.findAll();
891        SitePropertyBean[] sitePropertyArray = 
892          new SitePropertyBean[ collection.size() ];
893    
894        Iterator iterator = collection.iterator();
895        for ( int i = 0; iterator.hasNext(); ++i )
896        {
897          sitePropertyArray[i] = (SitePropertyBean) iterator.next();
898        }
899    
900        return sitePropertyArray;
901      }
902    
903      /**
904       * Create/edit a <code>site_properties</code> record.  If the specified
905       * <code>customer_type_id</code> is <code>0</code> then a new record
906       * is created.  The the specified <code>customer_type_id</code> 
907       * exists in the database, then the associated record is updated,
908       * otherwise, a new record is created with the specified value.
909       *
910       * @param request - The request from the client that
911       *   contains the form elements with all the value for the database
912       *   fields.
913       * @return SitePropertyBean - The appropriate instance of the java
914       *   bean that represents a record in the table.
915       * @throws CMAException - If database errors are encountered while
916       *   creating/editing the database record.
917       */
918      public SitePropertyBean editSiteProperty( HttpServletRequest request )
919          throws CMAException
920      {
921        // Fetch all the form parameters
922        String idString = request.getParameter( "customerId" );
923        String topRailStr = request.getParameter( "topRail" );
924        String leftRailStr = request.getParameter( "leftRail" );
925        String rightRailStr = request.getParameter( "rightRail" );
926        String bottomRailStr = request.getParameter( "bottomRail" );
927        String header = request.getParameter( "header" );
928        String footer = request.getParameter( "footer" );
929    
930        if ( header == null || header.equals( "" ) ) header = " ";
931        if ( footer == null || footer.equals( "" ) ) footer = " ";
932    
933        int customerId = 0;
934        int topRail = 0;
935        int leftRail = 0;
936        int rightRail = 0;
937        int bottomRail = 0;
938        SitePropertyBean spb = null;
939    
940        try
941        {
942          customerId = Integer.parseInt( idString );
943        }
944        catch ( NumberFormatException nfex )
945        {
946          throw new CMAException( "Invalid customerId " + idString + " specified.", nfex );
947        }
948    
949        try
950        {
951          topRail = Integer.parseInt( topRailStr );
952          leftRail = Integer.parseInt( leftRailStr );
953          rightRail = Integer.parseInt( rightRailStr );
954          bottomRail = Integer.parseInt( bottomRailStr );
955        }
956        catch ( NumberFormatException nfex )
957        {
958          // Ignore it
959        }
960    
961        try
962        {
963          boolean update = true;
964          try
965          {
966            spb = SitePropertyBean.getInstance( customerId );
967          }
968          catch ( CMAException cex )
969          {
970            spb = SitePropertyBean.create( customerId, topRail, leftRail, rightRail, bottomRail, header, footer );
971            update = false;
972          }
973    
974          if ( update )
975          {
976            spb.topRail = topRail;
977            spb.leftRail = leftRail;
978            spb.rightRail = rightRail;
979            spb.bottomRail = bottomRail;
980            spb.header = header;
981            spb.footer = footer;
982            spb.save();
983          }
984        }
985        catch ( Throwable t )
986        {
987          String error = "Error while creating/editing customer_type record in CMASession.editSiteProperty for customer_id " + customerId + ".";
988          throw new CMAException( error, t );
989        }
990    
991        return spb;
992      }
993    
994      /**
995       * Delete a <code>site_properties</code> record.  The database record
996       * as well as the associated {@link 
997       * biz.wedoit4u.databeans.SitePropertyBean} instance are deleted.
998       *
999       * @see biz.wedoit4u.databeans.SitePropertyBean#delete( SitePropertyBean )
1000       * @param request - The request from the client that
1001       *   contains the form elements with all the value for the database
1002       *   fields.
1003       * @throws CMAException - If database errors are encountered while
1004       *   deleting the database record.
1005       */
1006      public void deleteSiteProperty( HttpServletRequest request ) 
1007        throws CMAException
1008      {
1009        // Fetch all the form parameters
1010        String idString = request.getParameter( "customerId" );
1011        int customerId = 0;
1012    
1013        try
1014        {
1015          customerId = Integer.parseInt( idString );
1016        }
1017        catch ( NumberFormatException nfex )
1018        {
1019          throw new CMAException( "Invalid customerId " + idString + " specified.", nfex );
1020        }
1021    
1022        try
1023        {
1024          SitePropertyBean spb = SitePropertyBean.getInstance( customerId );
1025          SitePropertyBean.delete( spb );
1026        }
1027        catch ( Throwable t )
1028        {
1029          String error = "Error while deleting customer_type record in CMASession.deleteSiteProperty for customer_id " + customerId + ".";
1030          throw new CMAException( error, t );
1031        }
1032      }
1033    
1034      /**
1035       * Return an <code>Array</code> of java bean instances of
1036       * type {@link biz.wedoit4u.databeans.StyleClassBean}.
1037       *
1038       * @see biz.wedoit4u.databeans.StyleClassBean#findAll()
1039       * @return StyleClassBean[] - The array of java bean instances.
1040       * @throws CMAException - If Naming or Finder exceptions are 
1041       *   encountered while fetching the collection of java beans.
1042       */
1043      public StyleClassBean[] getStyleClassList() throws CMAException
1044      {
1045        Collection collection = StyleClassBean.findAll();
1046        StyleClassBean[] styleClassArray = 
1047          new StyleClassBean[ collection.size() ];
1048    
1049        Iterator iterator = collection.iterator();
1050        for ( int i = 0; iterator.hasNext(); ++i )
1051        {
1052          styleClassArray[i] = (StyleClassBean) iterator.next();
1053        }
1054    
1055        return styleClassArray;
1056      }
1057    
1058      /**
1059       * Create/edit a <code>style_classes</code> record.  If the specified
1060       * <code>style_class_id</code> is <code>0</code> then a new record
1061       * is created.  The the specified <code>style_class_id</code> 
1062       * exists in the database, then the associated record is updated,
1063       * otherwise, a new record is created with the specified value.
1064       *
1065       * @param request - The request from the client that
1066       *   contains the form elements with all the value for the database
1067       *   fields.
1068       * @return StyleClassBean - The appropriate instance of the java
1069       *   bean that represents a record in the table.
1070       * @throws CMAException - If database errors are encountered while
1071       *   creating/editing the database record.
1072       */
1073      public StyleClassBean editStyleClass( HttpServletRequest request )
1074          throws CMAException
1075      {
1076        // Fetch all the form parameters
1077        String idString = request.getParameter( "styleClassId" );
1078        String className = request.getParameter( "className" );
1079        String description = request.getParameter( "description" );
1080        if ( description.equals( "" ) ) description = null;
1081        int styleClassId = 0;
1082        StyleClassBean scb = null;
1083    
1084        try
1085        {
1086          styleClassId = Integer.parseInt( idString );
1087        }
1088        catch ( NumberFormatException nfex )
1089        {
1090          throw new CMAException( "Invalid styleClassId " + idString + " specified.", nfex );
1091        }
1092    
1093        try
1094        {
1095          if ( styleClassId == 0 )
1096          {
1097            scb = StyleClassBean.create( styleClassId, className, description );
1098          }
1099          else
1100          {
1101            boolean newRecord = false;
1102            try
1103            {
1104              scb = StyleClassBean.getInstance( styleClassId );
1105            }
1106            catch ( CMAException cex )
1107            {
1108              scb = StyleClassBean.create( styleClassId, className, description );
1109              newRecord = true;
1110            }
1111    
1112            if ( ! newRecord )
1113            {
1114              scb.className = className;
1115              scb.description = description;
1116              scb.save();
1117            }
1118          }
1119        }
1120        catch ( Throwable t )
1121        {
1122          String error = "Error while creating/editing style_class record in CMASession.editStyleClass for style_class_id " + styleClassId + ".";
1123          throw new CMAException( error, t );
1124        }
1125    
1126        return scb;
1127      }
1128    
1129      /**
1130       * Delete a <code>style_classes</code> record.  The database record
1131       * as well as the associated {@link 
1132       * biz.wedoit4u.databeans.StyleClassBean} instance are deleted.
1133       *
1134       * @see biz.wedoit4u.databeans.StyleClassBean#delete( StyleClassBean )
1135       * @param request - The request from the client that
1136       *   contains the form elements with all the value for the database
1137       *   fields.
1138       * @throws CMAException - If database errors are encountered while
1139       *   deleting the database record.
1140       */
1141      public void deleteStyleClass( HttpServletRequest request ) 
1142        throws CMAException
1143      {
1144        // Fetch all the form parameters
1145        String idString = request.getParameter( "styleClassId" );
1146        int styleClassId = 0;
1147    
1148        try
1149        {
1150          styleClassId = Integer.parseInt( idString );
1151        }
1152        catch ( NumberFormatException nfex )
1153        {
1154          throw new CMAException( "Invalid styleClassId " + idString + " specified.", nfex );
1155        }
1156    
1157        try
1158        {
1159          StyleClassBean scb = StyleClassBean.getInstance( styleClassId );
1160          StyleClassBean.delete( scb );
1161        }
1162        catch ( Throwable t )
1163        {
1164          String error = "Error while deleting style_class record in CMASession.deleteStyleClass for style_class_id " + styleClassId + ".";
1165          throw new CMAException( error, t );
1166        }
1167      }
1168    
1169      /**
1170       * Return a String representation of the specified <code>Date</code>
1171       * object.  The representation is in <code>SQL Timestamp</code>
1172       * format.  This method returns an empty String if errors are
1173       * encountered while using the {@link 
1174       * biz.wedoit4u.ejb.session.DateFormatter} session bean.
1175       *
1176       * @see biz.wedoit4u.ejb.session.DateFormatter#getSQLTimestamp( Date )
1177       * @param date - The date to format.
1178       * @return String - The formatted date, or an empty String if the Date
1179       *   is null.
1180       */
1181      public String getFormattedDate( Date date )
1182      {
1183        String value = "";
1184    
1185        if ( date == null )
1186        {
1187          value = "";
1188        }
1189        else
1190        {
1191          try
1192          {
1193            value = SessionBeanProvider.getDateFormatter().getSQLTimestamp( date );
1194          }
1195          catch ( Throwable t )
1196          {
1197            Logger.error( "Exception using DateFormatter in CMASession.getFormattedDate for date " + date + ".", t );
1198          }
1199        }
1200    
1201        return value;
1202      }
1203    }