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