KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > events > action > EditEventAction


1 /*
2  * Created on 19/10/2004
3  *
4  */

5 package com.dotmarketing.portlets.events.action;
6
7 import java.net.URLDecoder JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.List JavaDoc;
11
12 import javax.portlet.ActionRequest;
13 import javax.portlet.ActionResponse;
14 import javax.portlet.PortletConfig;
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16
17 import org.apache.commons.beanutils.BeanUtils;
18 import org.apache.struts.action.ActionForm;
19 import org.apache.struts.action.ActionMapping;
20
21 import com.dotmarketing.beans.Identifier;
22 import com.dotmarketing.db.DotHibernate;
23 import com.dotmarketing.factories.IdentifierFactory;
24 import com.dotmarketing.factories.InodeFactory;
25 import com.dotmarketing.factories.RoleFactory;
26 import com.dotmarketing.portal.struts.DotPortletAction;
27 import com.dotmarketing.portlets.categories.model.Category;
28 import com.dotmarketing.portlets.events.factories.EventFactory;
29 import com.dotmarketing.portlets.events.factories.RecuranceFactory;
30 import com.dotmarketing.portlets.events.model.Event;
31 import com.dotmarketing.portlets.events.model.EventRegistration;
32 import com.dotmarketing.portlets.events.model.Recurance;
33 import com.dotmarketing.portlets.events.struts.EventForm;
34 import com.dotmarketing.portlets.facilities.model.Facility;
35 import com.dotmarketing.portlets.files.model.File;
36 import com.dotmarketing.util.Config;
37 import com.dotmarketing.util.Validator;
38 import com.dotmarketing.util.WebKeys;
39 import com.liferay.portal.model.User;
40 import com.liferay.portal.struts.ActionException;
41 import com.liferay.portal.util.Constants;
42 import com.liferay.portlet.ActionRequestImpl;
43 import com.liferay.util.servlet.SessionErrors;
44 import com.liferay.util.servlet.SessionMessages;
45
46
47 /**
48  * @author David Torres
49  *
50  */

51 public class EditEventAction extends DotPortletAction {
52     
53     public static boolean debug = false;
54     
55     public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig config,
57              ActionRequest req, ActionResponse res)
58          throws Exception JavaDoc {
59
60         String JavaDoc cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : Constants.EDIT;
61         String JavaDoc referer = req.getParameter("referer");
62         
63         if ((referer!=null) && (referer.length()!=0)) {
64             referer = URLDecoder.decode(referer,"UTF-8");
65         }
66
67         DotHibernate dh = new DotHibernate();
68         dh.startTransaction();
69         User user = _getUser(req);
70         
71         boolean admin = EventFactory.isAnEventAdministrator(user);
72         req.setAttribute("isAdmin", new Boolean JavaDoc (admin));
73
74         try {
75             _retrieveEvent(req, res, config, form, user);
76
77         } catch (ActionException ae) {
78             _handleException(ae, req);
79         }
80
81         Event e = (Event) req.getAttribute(WebKeys.EVENT_EDIT);
82         
83         if (!EventFactory.hasPermissionsOverTheEvent(user, e)) {
84             //add message
85
//wraps request to get session object
86
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
87             HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
88             
89             SessionErrors.add(httpReq, "error", "error.event.no.permissions");
90             
91             _handleException(new Exception JavaDoc("User donīt have permissions to edit this event."), req);
92             return;
93         }
94          
95         req.setAttribute("isEventAdministrator", new Boolean JavaDoc(EventFactory.isAnEventAdministrator(user)));
96         req.setAttribute("isCMSAdministrator", new Boolean JavaDoc(RoleFactory.userHasRole(user, Config.getStringProperty("CMS_ADMINISTRATOR_ROLE"))));
97         
98         /*
99          * We are editing the recurance
100          */

101         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
102             try {
103                 _editEvent(req, res, config, form, user);
104                 setForward(req,"portlet.ext.events.edit_event");
105             } catch (ActionException ae) {
106                 _handleException(ae, req);
107             }
108         }
109         
110         /*
111          * Save the event occurrence
112          */

113         else if ((cmd != null) && cmd.equals(Constants.SAVE)) {
114             try {
115
116                 if (Validator.validate(req,form,mapping)) {
117                     if (!_saveEvent(req, res, config, form, user))
118                         setForward(req,"portlet.ext.events.edit_event");
119                     else {
120                         _sendToReferral(req,res,referer);
121                         return;
122                     }
123                 } else {
124                     setForward(req,"portlet.ext.events.edit_event");
125                 }
126
127             } catch (ActionException ae) {
128                 _handleException(ae, req);
129             }
130         }
131         /*
132          * Save the event recurance
133          */

134         else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.SAVE_SERIES)) {
135             try {
136
137                 if (Validator.validate(req,form,mapping)) {
138                     if (!_saveEventSeries(req, res, config, form, user))
139                         setForward(req,"portlet.ext.events.edit_event");
140                     else {
141                         _sendToReferral(req,res,referer);
142                         return;
143                     }
144                 }
145                 else {
146                     setForward(req,"portlet.ext.events.edit_event");
147                 }
148
149             } catch (ActionException ae) {
150                 _handleException(ae, req);
151             }
152         }
153         /*
154          * If we are deleting the event,
155          * run the delete action and return to the list
156          *
157          */

158         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
159             try {
160                 _deleteEvent(req, res, config, form, user);
161
162             } catch (ActionException ae) {
163                 _handleException(ae, req);
164             }
165             _sendToReferral(req,res,referer);
166             return;
167         }
168         /*
169          * If we are deleting the event and all the series,
170          * run the delete action and return to the list
171          *
172          */

173         else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.DELETE_SERIES)) {
174             try {
175                 _deleteEventSeries(req, res, config, form, user);
176
177             } catch (ActionException ae) {
178                 _handleException(ae, req);
179             }
180             _sendToReferral(req,res,referer);
181             return;
182         }
183         /*
184          * We are going to show the event registrations page.
185          */

186         else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.SHOW_REGISTRATIONS)) {
187             try {
188                 _showRegistrations(req, res, config, form, user);
189                 setForward(req,"portlet.ext.events.show_event_registrations");
190             } catch (ActionException ae) {
191                 _handleException(ae, req);
192             }
193         }
194         /*
195          * We are going to reset the event status
196          */

197         else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.RESET_STATUS)) {
198             try {
199                 _resetStatus(req, res, config, form, user);
200                 _sendToReferral(req,res,referer);
201                 return;
202             } catch (ActionException ae) {
203                 _handleException(ae, req);
204             }
205         }
206         DotHibernate.commitTransaction();
207
208     }
209
210
211     ///// ************** ALL METHODS HERE *************************** ////////
212

213     private void _retrieveEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
214     throws Exception JavaDoc {
215
216         
217         String JavaDoc inode = (req.getParameter("inode")!=null) ? req.getParameter("inode") : "0";
218         
219         Event e = null;
220         if(inode.equals("0")){
221             e = EventFactory.newInstance();
222             e.setContactName(user.getFullName());
223             e.setContactEmail(user.getEmailAddress());
224         } else {
225             e = EventFactory.getEvent(inode);
226         }
227         req.setAttribute("eventForm", form);
228         req.setAttribute(WebKeys.EVENT_EDIT, e);
229         Recurance r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
230         req.setAttribute(WebKeys.RECURANCE_EDIT, r);
231         
232     }
233     
234     private void _editEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
235         throws Exception JavaDoc {
236
237         
238         EventForm formBean = ( EventForm ) form;
239         Event e = ( Event ) req.getAttribute(WebKeys.EVENT_EDIT);
240         BeanUtils.copyProperties(formBean, e);
241
242         
243         //Set the files
244
if (e.getInode() > 0) {
245             ArrayList JavaDoc fileInodes = new ArrayList JavaDoc ();
246             List JavaDoc identifiers = InodeFactory.getChildrenClass(e, Identifier.class);
247             Iterator JavaDoc it = identifiers.iterator();
248             while (it.hasNext()) {
249                 Identifier identifier = (Identifier)it.next();
250                 File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier, File.class);
251                 fileInodes.add(Long.toString(file.getInode()));
252             }
253             formBean.setFilesInodes((String JavaDoc[])fileInodes.toArray(new String JavaDoc[0]));
254         }
255         
256         //set the list of selected categories for the event
257
java.util.List JavaDoc _cat = InodeFactory.getParentsOfClass(e, Category.class);
258         formBean.setCategories(_cat);
259
260         //get category list
261
java.util.List JavaDoc categories = InodeFactory.getInodesOfClass(Category.class, "sort_order");
262         req.setAttribute("categories", categories);
263         
264         //get the facility
265
Facility fac = (Facility) InodeFactory.getParentOfClass(e, Facility.class);
266         formBean.setFacilityInode(fac.getInode());
267     }
268
269
270     private boolean _saveEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
271     throws Exception JavaDoc {
272         
273         //wraps request
274
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
275         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
276
277         //Copying form information to the object
278
EventForm formBean = ( EventForm ) form;
279         Event e = ( Event ) req.getAttribute(WebKeys.EVENT_EDIT);
280         
281         boolean newEvent = (e.getInode() == 0)?true:false;
282         
283         
284         boolean eventAdmin = EventFactory.isAnEventAdministrator(user);
285         int actualApprovalStatus = e.getApprovalStatus();
286         
287         if(eventAdmin ){
288             actualApprovalStatus = formBean.getApprovalStatus();
289         }
290         
291
292         
293         
294         Facility fac = (Facility) InodeFactory.getParentOfClass(e, Facility.class);
295         boolean approvalChange = false;
296         
297         if (formBean.getStartDate().compareTo(e.getStartDate()) != 0
298             || formBean.getEndDate().compareTo(e.getEndDate()) != 0
299             || formBean.getSetupDate().compareTo(e.getSetupDate()) != 0
300             || formBean.getBreakDate().compareTo(e.getBreakDate()) != 0
301             || fac.getInode() != formBean.getFacilityInode()) {
302                 approvalChange = true;
303         }
304         
305         //Check event conflicts
306
long newFacInode = formBean.getFacilityInode();
307         Facility newFacility = (Facility) InodeFactory.getInode(newFacInode, Facility.class);
308         
309         Event eventCopy = new Event ();
310         BeanUtils.copyProperties(eventCopy, formBean);
311         if (!req.getParameter("continueWithConflicts").equals("true") && EventFactory.findConflicts(eventCopy, newFacility).size() > 0) {
312             SessionMessages.add(httpReq, "message", "message.event.has.conflicts");
313             req.setAttribute("conflict_found", "true");
314             return false;
315         }
316         
317         BeanUtils.copyProperties(e, formBean);
318         e.setUserId(user.getUserId());
319         
320         if (approvalChange && ! eventAdmin) {
321             e.setApprovalStatus(com.dotmarketing.util.Constants.EVENT_WAITING_APPROVAL_STATUS);
322         } else {
323             e.setApprovalStatus(actualApprovalStatus);
324         }
325
326         InodeFactory.saveInode(e);
327         
328         //wipe out the old categories
329
java.util.List JavaDoc _cats = InodeFactory.getParentsOfClass(e, Category.class);
330         Iterator JavaDoc it = _cats.iterator();
331         boolean delChild = true;
332         while (it.hasNext()) {
333             Category cat = ( Category ) it.next();
334             delChild = cat.deleteChild(e);
335         }
336         
337         //add the new categories
338
String JavaDoc[] arr = formBean.getCategories();
339         
340         if (arr != null) {
341             for (int i = 0; i < arr.length; i++) {
342                 Category node = ( Category ) InodeFactory.getInode(arr[i], Category.class);
343                 node.addChild(e);
344             }
345         }
346         
347         //wipe out the old facility
348
java.util.List JavaDoc facilities = InodeFactory.getParentsOfClass(e, Facility.class);
349         it = facilities.iterator();
350         while (it.hasNext()) {
351             fac = ( Facility ) it.next();
352             fac.deleteChild(e);
353         }
354         
355         //add the new facility
356
newFacInode = formBean.getFacilityInode();
357         newFacility = (Facility) InodeFactory.getInode(newFacInode, Facility.class);
358         newFacility.addChild(e);
359         
360         _saveFiles(e, formBean.getFilesInodes());
361         
362         req.setAttribute(WebKeys.EVENT_EDIT, e);
363         Recurance r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
364         req.setAttribute(WebKeys.RECURANCE_EDIT, r);
365         
366         //add message
367
SessionMessages.add(httpReq, "message", "message.event.saved");
368         
369         //Email notifications
370
if (newEvent)
371         {
372             EventFactory.sendEmailNotification(e, newFacility, user, false);
373         }
374         else if (approvalChange)
375         {
376             EventFactory.sendEmailNotification(e, newFacility, user, true);
377         }
378         return true;
379     }
380
381     private boolean _saveEventSeries(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
382         throws Exception JavaDoc {
383
384         EventForm formBean = ( EventForm ) form;
385         Event e = ( Event ) req.getAttribute(WebKeys.EVENT_EDIT);
386         Recurance r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
387         if (InodeFactory.countChildrenOfClass(e, Recurance.class) > 0) {
388             r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
389         }
390
391         long newFacInode = formBean.getFacilityInode();
392         Facility newFacility = (Facility) InodeFactory.getInode(newFacInode, Facility.class);
393         
394         Event eventCopy = new Event ();
395         BeanUtils.copyProperties(eventCopy, formBean);
396         if (!req.getParameter("continueWithConflicts").equals("true") && EventFactory.findConflicts(eventCopy, r, newFacility).size() > 0) {
397             SessionMessages.add(req, "message", "message.event.has.conflicts");
398             req.setAttribute("conflict_found", "true");
399             req.setAttribute("saving_series", "true");
400             return false;
401         }
402         
403         if (!_saveEvent(req, res, config, form, user))
404             return false;
405         
406         formBean = ( EventForm ) form;
407         e = ( Event ) req.getAttribute(WebKeys.EVENT_EDIT);
408         
409         if (InodeFactory.countChildrenOfClass(e, Recurance.class) > 0) {
410             r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
411             r.setStartTime(e.getStartDate());
412             r.setEndTime(e.getEndDate());
413             RecuranceFactory.buildRecurringEvents(r,e);
414         }
415         
416         _saveFiles(e, formBean.getFilesInodes());
417
418         req.setAttribute(WebKeys.EVENT_EDIT, e);
419         r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
420         req.setAttribute(WebKeys.RECURANCE_EDIT, r);
421
422         //add message
423
SessionMessages.add(req, "message", "message.event.saved");
424         
425         return true;
426
427     }
428     
429     private void _saveFiles (Event e, String JavaDoc[] fileInodes) {
430         
431         java.util.List JavaDoc _files = InodeFactory.getChildrenClass(e, Identifier.class);
432         Iterator JavaDoc it = _files.iterator();
433         while (it.hasNext()) {
434             Identifier iden = ( Identifier ) it.next();
435             e.deleteChild(iden);
436         }
437
438         for (int i = 0; i < fileInodes.length; i++) {
439             String JavaDoc inode = fileInodes[i];
440             File file = (File)InodeFactory.getInode(inode, File.class);
441             Identifier identifier = IdentifierFactory.getParentIdentifier(file);
442             e.addChild(identifier);
443         }
444     }
445     
446     private void _deleteEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
447     throws Exception JavaDoc {
448
449         Event e = (Event) req.getAttribute(WebKeys.EVENT_EDIT);
450         EventFactory.deleteEvent(e);
451         SessionMessages.add(req, "message", "message.events.deleted");
452
453     }
454
455     private void _deleteEventSeries(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
456     throws Exception JavaDoc {
457
458         Event e = (Event) req.getAttribute(WebKeys.EVENT_EDIT);
459         EventFactory.deleteEventSeries(e);
460         SessionMessages.add(req, "message", "message.events.seriesdeleted");
461
462     }
463
464     /**
465      * @param req
466      * @param res
467      * @param config
468      * @param form
469      * @param user
470      */

471     private void _showRegistrations(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user)
472     throws Exception JavaDoc {
473         Event e = (Event) req.getAttribute(WebKeys.EVENT_EDIT);
474         List JavaDoc registrations = InodeFactory.getChildrenClass(e, EventRegistration.class);
475         req.setAttribute(WebKeys.EVENT_REGISTRATIONS, registrations);
476     }
477     
478     private void _resetStatus(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form, User user)
479     throws Exception JavaDoc {
480         Event e = (Event) req.getAttribute(WebKeys.EVENT_EDIT);
481
482         Recurance r = (Recurance) InodeFactory.getChildOfClass(e, Recurance.class);
483         if (r.getInode() > 0) { //Recur over the children
484
List JavaDoc l = InodeFactory.getParentsOfClass(r, Event.class);
485             Iterator JavaDoc i = l.iterator();
486             while (i.hasNext()) {
487                 Event event = (Event) i.next();
488                 e.setReceivedAdminApproval(false);
489                 event.setApprovalStatus(com.dotmarketing.util.Constants.EVENT_WAITING_APPROVAL_STATUS);
490                 InodeFactory.saveInode(event);
491             }
492         } else { //Simple event without recurrance
493
e.setReceivedAdminApproval(false);
494             e.setApprovalStatus(com.dotmarketing.util.Constants.EVENT_WAITING_APPROVAL_STATUS);
495             InodeFactory.saveInode(e);
496         }
497     }
498     
499 }
500
501
Popular Tags