KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > event_registrations > action > EditRegistrationAction


1 package com.dotmarketing.portlets.event_registrations.action;
2
3 import java.net.URLDecoder JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.HashMap JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8
9 import javax.portlet.ActionRequest;
10 import javax.portlet.ActionResponse;
11 import javax.portlet.PortletConfig;
12 import javax.servlet.http.HttpServletRequest JavaDoc;
13
14 import org.apache.commons.beanutils.BeanUtils;
15 import org.apache.struts.action.ActionForm;
16 import org.apache.struts.action.ActionMapping;
17
18 import com.dotmarketing.beans.UserProxy;
19 import com.dotmarketing.db.DotHibernate;
20 import com.dotmarketing.factories.HostFactory;
21 import com.dotmarketing.factories.InodeFactory;
22 import com.dotmarketing.factories.UserProxyFactory;
23 import com.dotmarketing.portal.struts.DotPortletAction;
24 import com.dotmarketing.util.Config;
25 import com.dotmarketing.util.Mailer;
26 import com.dotmarketing.util.UtilMethods;
27 import com.dotmarketing.util.Validator;
28 import com.dotmarketing.util.WebKeys;
29 import com.liferay.portal.ejb.UserLocalManagerUtil;
30 import com.liferay.portal.model.User;
31 import com.liferay.portal.struts.ActionException;
32 import com.liferay.portal.util.Constants;
33 import com.liferay.portlet.ActionRequestImpl;
34 import com.liferay.util.servlet.SessionMessages;
35 import com.dotmarketing.portlets.event_registrations.factories.WebEventAttendeeFactory;
36 import com.dotmarketing.portlets.event_registrations.factories.WebEventRegistrationFactory;
37 import com.dotmarketing.portlets.event_registrations.model.WebEventAttendee;
38 import com.dotmarketing.portlets.event_registrations.model.WebEventRegistration;
39 import com.dotmarketing.portlets.event_registrations.struts.WebEventAttendeeForm;
40 import com.dotmarketing.portlets.event_registrations.struts.WebEventRegistrationForm;
41 import com.dotmarketing.portlets.webevents.factories.WebEventFactory;
42 import com.dotmarketing.portlets.webevents.model.WebEvent;
43 import com.dotmarketing.portlets.organization.factories.OrganizationFactory;
44 import com.dotmarketing.portlets.organization.model.Organization;
45
46
47 /**
48  * @author Maria Ahues
49  *
50  */

51 public class EditRegistrationAction extends DotPortletAction {
52     
53     public void processAction(
54              ActionMapping mapping, ActionForm form, PortletConfig config,
55              ActionRequest req, ActionResponse res)
56          throws Exception JavaDoc {
57
58         String JavaDoc cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : Constants.EDIT;
59         String JavaDoc referer = req.getParameter("referer");
60
61         //if ((referer!=null) && (referer.length()!=0)) {
62
// referer = URLDecoder.decode(referer,"UTF-8");
63
//}
64

65         DotHibernate.startTransaction();
66         User user = _getUser(req);
67         
68         try {
69             _retrieveEventRegistration(req, res, config, form, user);
70
71         } catch (ActionException ae) {
72             _handleException(ae, req);
73         }
74
75         /*
76          * We are editing the registration
77          */

78         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
79             try {
80                 _editEventRegistration(req, res, config, form);
81                 setForward(req,"portlet.ext.webevents_registration.edit_registration");
82             } catch (ActionException ae) {
83                 _handleException(ae, req);
84             }
85         }
86         /*
87          * We are saving an attendee
88          */

89         if ((cmd != null) && cmd.equals("add_attendee")) {
90             try {
91                 _addAttendee(req, res, config, form, mapping, user);
92                 _editEventRegistration(req, res, config, form);
93                 setForward(req,"portlet.ext.webevents_registration.edit_registration");
94             } catch (ActionException ae) {
95                 _handleException(ae, req);
96             }
97         }
98         /*
99          * We are deleting an attendee
100          */

101         if ((cmd != null) && cmd.equals("delete_attendee")) {
102             try {
103                 _deleteAttendee(req, res, config, form, user);
104                 _editEventRegistration(req, res, config, form);
105                 setForward(req,"portlet.ext.webevents_registration.edit_registration");
106             } catch (ActionException ae) {
107                 _handleException(ae, req);
108             }
109         }
110         
111         /*
112          * Save the event registration
113          */

114         else if ((cmd != null) && cmd.equals(Constants.SAVE)) {
115             try {
116
117                 if (Validator.validate(req,form,mapping)) {
118                     _saveEventRegistration(req, res, config, form, user);
119                     _editEventRegistration(req, res, config, form);
120                     //_sendToReferral(req,res,referer);
121
setForward(req,"portlet.ext.webevents_registration.edit_registration");
122                 } else {
123                     setForward(req,"portlet.ext.webevents_registration.edit_registration");
124                 }
125
126             } catch (ActionException ae) {
127                 _handleException(ae, req);
128             }
129         }
130         else if ((cmd != null) && cmd.equals("sendInvoice")) {
131             try {
132                 _sendInvoice(mapping, req, res, config, form);
133                 _editEventRegistration(req, res, config, form);
134             } catch (Exception JavaDoc ae) {
135                 _handleException(ae, req);
136             }
137             setForward(req,"portlet.ext.webevents_registration.edit_registration");
138         }
139         /*
140          * If we are deleting the registration,
141          * run the delete action and return to the list
142          *
143          */

144         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
145             try {
146                 _deleteEventRegistration(req, res, config, form, user);
147
148             } catch (ActionException ae) {
149                 _handleException(ae, req);
150             }
151             _sendToReferral(req,res,referer);
152         }
153         DotHibernate.commitTransaction();
154
155     }
156
157
158     ///// ************** ALL METHODS HERE *************************** ////////
159
private void _sendInvoice(ActionMapping mapping, ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form)
160     throws Exception JavaDoc {
161         WebEventRegistration registration = ( WebEventRegistration ) req.getAttribute(WebKeys.WEBEVENTS_REG_EDIT);
162         UserProxy proxy = UserProxyFactory.getUserProxy(registration.getUserInode());
163         User user = com.dotmarketing.cms.factories.PublicUserFactory.getUserByUserId(proxy.getUserId());
164         String JavaDoc path = "";
165         
166         WebEvent event = WebEventFactory.getWebEvent(registration.getEventInode());
167         
168         if (event.isInstitute()) {
169             if (registration.getPaymentType() == 1)
170                 path = mapping.findForward("portlet.ext.webevents_registration.add_registration.creditCardConfirmationEmail").getPath();
171             else
172                 path = mapping.findForward("portlet.ext.webevents_registration.add_registration.checkConfirmationEmail").getPath();
173         } else {
174             //if it's been paid we send the regular email
175
if (registration.getRegistrationStatus() == 1)
176                 path = mapping.findForward("portlet.ext.webevents_registration.add_registration.webinarConfirmationEmail").getPath();
177             else
178                 path = mapping.findForward("portlet.ext.webevents_registration.add_registration.webinarCheckConfirmationEmail").getPath();
179         }
180         
181         try {
182             StringBuffer JavaDoc writer = UtilMethods.getURL("http://"
183                     + HostFactory.getCurrentHost(req).getHostname() + path
184                     + "?registrationInode=" + registration.getInode());
185             
186             String JavaDoc[] reportEmails = Config.getStringArrayProperty("BCCEMAIL_REGISTRATION_ADDRESSES");
187             
188             StringBuffer JavaDoc bcc = new StringBuffer JavaDoc();
189             for (String JavaDoc email : reportEmails) {
190                 if (bcc.toString().length() > 0)
191                     bcc.append(", ");
192                 bcc.append(email);
193             }
194             
195             Mailer m = new Mailer();
196             m.setToEmail(user.getEmailAddress());
197             m.setSubject(Config.getStringProperty("WEB_EVENT_REGISTRATION_EMAIL_TITLE"));
198             m.setHTMLBody(writer.toString().trim());
199             m.setFromEmail(Config.getStringProperty("EMAIL_REGISTRATION_ADDRESS"));
200             m.setBcc(bcc.toString());
201             m.sendMessage();
202             
203             //wraps request to get session object
204
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
205             HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
206             //add message
207
SessionMessages.add(httpReq, "message", "message.webevent_registration.email_sent");
208         } catch (Exception JavaDoc e) {
209             e.printStackTrace(System.out);
210         }
211         
212     }
213     
214     private void _retrieveEventRegistration(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
215     throws Exception JavaDoc {
216
217         String JavaDoc inode = (req.getParameter("inode")!=null) ? req.getParameter("inode") : "0";
218         WebEventRegistration e = null;
219         if(inode.equals("0")){
220             e = WebEventRegistrationFactory.newInstance();
221         } else {
222             e = WebEventRegistrationFactory.getWebEventRegistration(inode);
223         }
224         req.setAttribute(WebKeys.WEBEVENTS_REG_FORM, form);
225         req.setAttribute(WebKeys.WEBEVENTS_REG_EDIT, e);
226     }
227     
228     private void _editEventRegistration(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form)
229         throws Exception JavaDoc {
230
231         WebEventRegistrationForm formBean = ( WebEventRegistrationForm ) form;
232         WebEventRegistration e = ( WebEventRegistration ) req.getAttribute(WebKeys.WEBEVENTS_REG_EDIT);
233         BeanUtils.copyProperties(formBean, e);
234
235         
236         //statuses list
237
List JavaDoc<HashMap JavaDoc> statuses = new ArrayList JavaDoc<HashMap JavaDoc>();
238         String JavaDoc[] statusesArray = Config.getStringArrayProperty("EREG_STATUSES");
239         for (int i=0;i<statusesArray.length;i++) {
240             HashMap JavaDoc<String JavaDoc, String JavaDoc> hs = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
241             String JavaDoc status = statusesArray[i];
242             hs.put("statusName",Config.getStringProperty(status + "_FN"));
243             hs.put("statusValue",Config.getStringProperty(status));
244             statuses.add(hs);
245         }
246         req.setAttribute("registrationStatuses",statuses.iterator());
247         
248         //paymentTypes list
249
List JavaDoc<HashMap JavaDoc> paymentTypes = new ArrayList JavaDoc<HashMap JavaDoc>();
250         String JavaDoc[] paymentTypesArray = Config.getStringArrayProperty("EREG_PAYMENT_TYPES");
251         for (int i=0;i<paymentTypesArray.length;i++) {
252             String JavaDoc pType = paymentTypesArray[i];
253             HashMap JavaDoc<String JavaDoc, String JavaDoc> hs = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
254             hs.put("paymentTypeName",Config.getStringProperty(pType + "_FN"));
255             hs.put("paymentTypeValue",Config.getStringProperty(pType));
256             paymentTypes.add(hs);
257         }
258         
259         req.setAttribute("paymentTypes",paymentTypes.iterator());
260
261         //get registrant contact info
262
if (e.getUserInode() > 0){
263         UserProxy registrantUser = UserProxyFactory.getUserProxy(e.getUserInode());
264         User user = com.dotmarketing.cms.factories.PublicUserFactory.getUserByUserId(registrantUser.getUserId());
265         
266         formBean.setRegistrantFirstName(user.getFirstName());
267         formBean.setRegistrantLastName(user.getLastName());
268         formBean.setRegistrantEmail(user.getEmailAddress());
269         formBean.setUserId(user.getUserId());
270         if (registrantUser.getInode()>0) {
271             Organization organization = (Organization) InodeFactory.getParentOfClass(registrantUser,Organization.class);
272             if (organization.getInode()>0) {
273                 formBean.setRegistrantFacility(organization.getTitle().trim());
274                 formBean.setRegistrantFacilityInode(organization.getInode());
275             
276                 Organization parentSystem = OrganizationFactory.getParentOrganization(organization);
277                 if (parentSystem.getInode()>0) {
278                     formBean.setRegistrantSystem(parentSystem.getTitle().trim());
279                     formBean.setRegistrantSystemInode(parentSystem.getInode());
280                 }
281             }
282         }
283         }
284         else{
285             formBean.setRegistrantFirstName("");
286             formBean.setRegistrantLastName("");
287             formBean.setRegistrantEmail("");
288         }
289         //get Attendee list
290
List JavaDoc<WebEventAttendee> attendees = WebEventRegistrationFactory.getEventAttendees(e);
291         req.setAttribute(WebKeys.WEBEVENT_REG_ATTENDEES, attendees);
292         
293         WebEventAttendee attendee = WebEventAttendeeFactory.getWebEventAttendee(formBean.getCurrentAttendeeInode());
294         WebEventAttendeeForm attendeeForm = new WebEventAttendeeForm();
295         
296         if (attendee.getInode() > 0) {
297             BeanUtils.copyProperties(attendeeForm,attendee);
298             req.setAttribute("WebEventAttendeeForm",attendeeForm);
299         }
300     }
301
302
303     private void _saveEventRegistration(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
304     throws Exception JavaDoc {
305         
306         WebEventRegistrationForm formBean = ( WebEventRegistrationForm ) form;
307         WebEventRegistration e = ( WebEventRegistration ) req.getAttribute(WebKeys.WEBEVENTS_REG_EDIT);
308         boolean runAutoStatus = ((e.getTotalPaid() != formBean.getTotalPaid()) ? true : false);
309         e.setTotalPaid(formBean.getTotalPaid());
310         e.setCheckBankName(formBean.getCheckBankName());
311         e.setCheckNumber(formBean.getCheckNumber());
312         e.setPoNumber(formBean.getPoNumber());
313         e.setBillingAddress1(formBean.getBillingAddress1());
314         e.setBillingAddress2(formBean.getBillingAddress2());
315         e.setBillingCity(formBean.getBillingCity());
316         e.setBillingContactEmail(formBean.getBillingContactEmail());
317         e.setBillingContactName(formBean.getBillingContactName());
318         e.setBillingContactPhone(formBean.getBillingContactPhone());
319         e.setBillingState(formBean.getBillingState());
320         e.setBillingZip(formBean.getBillingZip());
321         e.setRegistrationStatus(formBean.getRegistrationStatus());
322         e.setPaymentType(formBean.getPaymentType());
323         e.setInvoiceNumber(formBean.getInvoiceNumber());
324         //all attendees for this event registration
325
java.util.List JavaDoc attendees = WebEventRegistrationFactory.getEventAttendees(e);
326         int total = 0;
327         Iterator JavaDoc attendeesIter = attendees.iterator();
328         while (attendeesIter.hasNext()) {
329             WebEventAttendee attendee = (WebEventAttendee) attendeesIter.next();
330             total += attendee.getRegistrationPrice();
331         }
332         //sets new total due
333
e.setTotalDue(total - e.getTotalPaid());
334         e.setTotalRegistration(total);
335         formBean.setTotalDue(e.getTotalDue());
336         if(runAutoStatus)
337         {
338             _setRegistrationStatus(formBean);
339             e.setRegistrationStatus(formBean.getRegistrationStatus());
340         }
341         e.setModified_QB(true);
342         WebEventRegistrationFactory.saveWebEventRegistration(e);
343         req.setAttribute(WebKeys.WEBEVENTS_REG_EDIT, e);
344         
345         //wraps request to get session object
346
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
347         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
348         //add message
349
SessionMessages.add(httpReq, "message", "message.webevent_registration.saved");
350         
351     }
352
353     private void _deleteEventRegistration(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
354     throws Exception JavaDoc {
355
356         WebEventRegistration e = ( WebEventRegistration ) req.getAttribute(WebKeys.WEBEVENTS_REG_EDIT);
357         
358         //delete all attendees for this event registration
359
java.util.List JavaDoc attendees = WebEventRegistrationFactory.getEventAttendees(e);
360         Iterator JavaDoc attendeesIter = attendees.iterator();
361         while (attendeesIter.hasNext()) {
362             WebEventAttendee attendee = (WebEventAttendee) attendeesIter.next();
363             WebEventAttendeeFactory.deleteWebEventAttendee(attendee);
364         }
365         //delete this event
366
WebEventRegistrationFactory.deleteWebEventRegistration(e);
367         SessionMessages.add(req, "message", "message.webevent_registration.deleted");
368
369     }
370     private void _deleteAttendee(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
371     throws Exception JavaDoc {
372         
373         WebEventRegistrationForm registrationForm = (WebEventRegistrationForm) form;
374         //get attendee to delete
375
WebEventAttendee attendee = WebEventAttendeeFactory.getWebEventAttendee(registrationForm.getCurrentAttendeeInode());
376         WebEventAttendeeFactory.deleteWebEventAttendee(attendee);
377         registrationForm.setCurrentAttendeeInode(0);
378         
379         req.setAttribute("WebEventAttendeeForm",new WebEventAttendeeForm());
380         SessionMessages.add(req, "message", "message.webevent_registration.delete_attendee");
381     
382     }
383     
384     private void _setRegistrationStatus(WebEventRegistrationForm registrationForm) {
385         if (registrationForm.getTotalDue() > 0) {
386             registrationForm.setRegistrationStatus(Config.getIntProperty("EREG_WAITING"));
387         }
388         else if (registrationForm.getTotalDue() < 0) {
389             registrationForm.setRegistrationStatus(Config.getIntProperty("EREG_REIMBURSEMENT"));
390         }
391         else {
392             registrationForm.setRegistrationStatus(Config.getIntProperty("EREG_PAID"));
393         }
394     }
395     
396     private void _addAttendee(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, ActionMapping mapping, User user)
397     throws Exception JavaDoc {
398         
399         //this event registration
400
WebEventRegistration e = ( WebEventRegistration ) req.getAttribute(WebKeys.WEBEVENTS_REG_EDIT);
401         
402         //wraps request to get session object
403
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
404         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
405
406         WebEventRegistrationForm registrationForm = (WebEventRegistrationForm) form;
407         //String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId();
408
//String userId = registrationForm.getUserId();
409
//user = UserLocalManagerUtil.getUserById(companyId, userId);
410
//UserProxy userProxy = UserProxyFactory.getUserProxy(user);
411
WebEventAttendeeForm attendeeForm = new WebEventAttendeeForm();
412         attendeeForm.setFirstName(registrationForm.getCurrentAttendeeFirstName());
413         attendeeForm.setLastName(registrationForm.getCurrentAttendeeLastName());
414         attendeeForm.setTitle(registrationForm.getCurrentAttendeeTitle());
415         attendeeForm.setBadgeName(registrationForm.getCurrentAttendeeBadgeName());
416         attendeeForm.setEmail(registrationForm.getCurrentAttendeeEmail());
417         attendeeForm.setRegistrationPrice(registrationForm.getCurrentAttendeePrice());
418
419         //validates the form bean
420
if (Validator.validate(req, form, mapping)) {
421
422             //get all attendees for this event registration
423
WebEventAttendee attendee = WebEventAttendeeFactory.getWebEventAttendee(registrationForm.getCurrentAttendeeInode());
424             if (attendee.getInode()>0) {
425                 ///sets total due and new registration status
426
registrationForm.setTotalDue(registrationForm.getTotalDue() - attendee.getRegistrationPrice());
427                 registrationForm.setTotalDue(registrationForm.getTotalDue() + registrationForm.getCurrentAttendeePrice());
428                 _setRegistrationStatus(registrationForm);
429                 
430                 //updating existing one
431
attendee.setFirstName(registrationForm.getCurrentAttendeeFirstName());
432                 attendee.setLastName(registrationForm.getCurrentAttendeeLastName());
433                 attendee.setTitle(registrationForm.getCurrentAttendeeTitle());
434                 attendee.setBadgeName(registrationForm.getCurrentAttendeeBadgeName());
435                 attendee.setEmail(registrationForm.getCurrentAttendeeEmail());
436                 attendee.setRegistrationPrice(registrationForm.getCurrentAttendeePrice());
437                 WebEventAttendeeFactory.saveWebEventAttendee(attendee);
438                 req.setAttribute("WebEventAttendeeForm",new WebEventAttendeeForm());
439                 registrationForm.setCurrentAttendeeInode(0);
440                 SessionMessages.add(httpReq, "message", "message.webevent_registration.update_attendee");
441             }
442             else {
443                 //adding new one
444
java.util.List JavaDoc currentAttendees = WebEventRegistrationFactory.getEventAttendeesByEmail(e,registrationForm.getCurrentAttendeeEmail());
445                 /*if (currentAttendees.size()>0) {
446                     SessionMessages.add(httpReq, "message", "error.attendee.already.registered");
447                     req.setAttribute("WebEventAttendeeForm",attendeeForm);
448                 }
449                 else {*/

450                     ///sets total due and new registration status
451
registrationForm.setTotalDue(registrationForm.getTotalDue() + registrationForm.getCurrentAttendeePrice());
452                 _setRegistrationStatus(registrationForm);
453
454                 attendee.setFirstName(registrationForm.getCurrentAttendeeFirstName());
455                 attendee.setLastName(registrationForm.getCurrentAttendeeLastName());
456                 attendee.setTitle(registrationForm.getCurrentAttendeeTitle());
457                 attendee.setBadgeName(registrationForm.getCurrentAttendeeBadgeName());
458                 attendee.setEmail(registrationForm.getCurrentAttendeeEmail());
459                 attendee.setRegistrationPrice(registrationForm.getCurrentAttendeePrice());
460                 attendee.setEventRegistrationInode(e.getInode());
461                 WebEventAttendeeFactory.saveWebEventAttendee(attendee);
462                 req.setAttribute("WebEventAttendeeForm",new WebEventAttendeeForm());
463                 registrationForm.setCurrentAttendeeInode(0);
464                 SessionMessages.add(httpReq, "message", "message.webevent_registration.add_attendee");
465                 //}
466
}
467             //new------------------------------------------
468
java.util.List JavaDoc attendees = WebEventRegistrationFactory.getEventAttendees(e);
469             int total = 0;
470             Iterator JavaDoc attendeesIter = attendees.iterator();
471             while (attendeesIter.hasNext()) {
472                 WebEventAttendee attendee2 = (WebEventAttendee) attendeesIter.next();
473                 total += attendee2.getRegistrationPrice();
474             }
475             //sets new total due
476
e.setTotalDue(total - e.getTotalPaid());
477             e.setTotalRegistration(total);
478             e.setModified_QB(true);
479             registrationForm.setTotalDue(e.getTotalDue());
480             //new-----------------------------------------
481
} else {
482             req.setAttribute("WebEventAttendeeForm",attendeeForm);
483         }
484     }
485     
486
487 }
488
489
Popular Tags