KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.dotmarketing.portlets.webevents.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.portal.struts.DotPortletAction;
26 import com.dotmarketing.portlets.categories.model.Category;
27 import com.dotmarketing.portlets.files.model.File;
28 import com.dotmarketing.util.Logger;
29 import com.dotmarketing.util.UtilMethods;
30 import com.dotmarketing.util.Validator;
31 import com.dotmarketing.util.WebKeys;
32 import com.liferay.portal.model.User;
33 import com.liferay.portal.struts.ActionException;
34 import com.liferay.portal.util.Constants;
35 import com.liferay.portlet.ActionRequestImpl;
36 import com.liferay.util.servlet.SessionMessages;
37 import com.dotmarketing.portlets.webevents.factories.WebEventFactory;
38 import com.dotmarketing.portlets.webevents.factories.WebEventLocationFactory;
39 import com.dotmarketing.portlets.webevents.model.WebEvent;
40 import com.dotmarketing.portlets.webevents.model.WebEventLocation;
41 import com.dotmarketing.portlets.webevents.struts.WebEventForm;
42
43
44 /**
45  * @author David Torres
46  *
47  */

48 public class EditEventAction extends DotPortletAction {
49     
50     public static boolean debug = false;
51     
52     public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig config,
54              ActionRequest req, ActionResponse res)
55          throws Exception JavaDoc {
56
57         String JavaDoc cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : Constants.EDIT;
58         String JavaDoc referer = req.getParameter("referer");
59
60         if ((referer!=null) && (referer.length()!=0)) {
61             referer = URLDecoder.decode(referer,"UTF-8");
62         }
63
64         DotHibernate.startTransaction();
65         User user = _getUser(req);
66         
67         try {
68             _retrieveEvent(req, res, config, form, user);
69
70         } catch (ActionException ae) {
71             _handleException(ae, req);
72         }
73
74         /*
75          * We are editing the recurance
76          */

77         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
78             try {
79                 //_reorderEvents(form,req,res);
80
_editEvent(req, res, config, form, user);
81                 setForward(req,"portlet.ext.webevents.edit_event");
82             } catch (ActionException ae) {
83                 _handleException(ae, req);
84             }
85         }
86         // Reordering categories
87
else if ((cmd != null) && cmd.equals("reorder")) {
88             Logger.debug(this, "Reordering Events");
89             
90             try {
91                 _reorderEvents(form,req,res);
92                 _sendToReferral(req,res,referer);
93             }
94             catch(Exception JavaDoc e) {
95                 _handleException(e, req);
96             }
97             setForward(req,"portlet.ext.webevents.edit_event");
98         }
99         
100         /*
101          * Save the event occurrence
102          */

103         else if ((cmd != null) && cmd.equals(Constants.SAVE)) {
104             try {
105
106                 if (Validator.validate(req,form,mapping)) {
107                     _saveEvent(req, res, config, form, user);
108                     _sendToReferral(req,res,referer);
109                 } else {
110                     WebEvent e = ( WebEvent ) req.getAttribute(WebKeys.WEBEVENTS_EDIT);
111                     //get Locations list
112
java.util.List JavaDoc locations = WebEventFactory.getEventLocations(e,"start_date desc");
113                     req.setAttribute(WebKeys.WEBEVENT_LOCATIONS, locations);
114                     setForward(req,"portlet.ext.webevents.edit_event");
115                 }
116
117             } catch (ActionException ae) {
118                 _handleException(ae, req);
119             }
120         }
121         /*
122          * If we are deleting the event,
123          * run the delete action and return to the list
124          *
125          */

126         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
127             try {
128                 _deleteEvent(req, res, config, form, user);
129
130             } catch (ActionException ae) {
131                 _handleException(ae, req);
132             }
133             _sendToReferral(req,res,referer);
134         }
135         DotHibernate.commitTransaction();
136
137     }
138
139
140     ///// ************** ALL METHODS HERE *************************** ////////
141
private void _retrieveEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
142     throws Exception JavaDoc {
143
144         String JavaDoc inode = (req.getParameter("inode")!=null) ? req.getParameter("inode") : "0";
145         WebEvent e = null;
146         if(inode.equals("0")){
147             e = WebEventFactory.newInstance();
148         } else {
149             e = WebEventFactory.getWebEvent(inode);
150         }
151         req.setAttribute(WebKeys.WEBEVENTS_FORM, form);
152         req.setAttribute(WebKeys.WEBEVENTS_EDIT, e);
153     }
154     
155     private void _editEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
156         throws Exception JavaDoc {
157
158         WebEventForm formBean = ( WebEventForm ) form;
159         WebEvent e = ( WebEvent ) req.getAttribute(WebKeys.WEBEVENTS_EDIT);
160         BeanUtils.copyProperties(formBean, e);
161
162         //Set the files
163
if (e.getInode() > 0) {
164             ArrayList JavaDoc<String JavaDoc> fileInodes = new ArrayList JavaDoc<String JavaDoc>();
165             List JavaDoc identifiers = InodeFactory.getChildrenClass(e, Identifier.class);
166             Iterator JavaDoc it = identifiers.iterator();
167             while (it.hasNext()) {
168                 Identifier identifier = (Identifier)it.next();
169                 File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier, File.class);
170                 fileInodes.add(Long.toString(file.getInode()));
171             }
172             formBean.setFilesInodes(fileInodes);
173         }
174         
175         //IMAGES
176
if (e.getEventImage1()>0) {
177             Identifier identifier = (Identifier) InodeFactory.getInode(e.getEventImage1(),Identifier.class);
178             File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier,File.class);
179             formBean.setEventImage1(file.getInode());
180         }
181         if (e.getEventImage2()>0) {
182             Identifier identifier = (Identifier) InodeFactory.getInode(e.getEventImage2(),Identifier.class);
183             File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier,File.class);
184             formBean.setEventImage2(file.getInode());
185         }
186         if (e.getEventImage3()>0) {
187             Identifier identifier = (Identifier) InodeFactory.getInode(e.getEventImage3(),Identifier.class);
188             File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier,File.class);
189             formBean.setEventImage3(file.getInode());
190         }
191         if (e.getEventImage4()>0) {
192             Identifier identifier = (Identifier) InodeFactory.getInode(e.getEventImage4(),Identifier.class);
193             File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier,File.class);
194             formBean.setEventImage4(file.getInode());
195         }
196
197         
198         
199         //set the list of selected categories for the event
200
java.util.List JavaDoc _cat = InodeFactory.getParentsOfClass(e, Category.class);
201         formBean.setCategories(_cat);
202
203         //get category list
204
java.util.List JavaDoc categories = InodeFactory.getInodesOfClass(Category.class, "sort_order");
205         req.setAttribute(WebKeys.WEBEVENT_CATEGORIES, categories);
206         
207         //get Locations list
208
java.util.List JavaDoc locations = WebEventFactory.getEventLocations(e,"start_date desc");
209         req.setAttribute(WebKeys.WEBEVENT_LOCATIONS, locations);
210         
211
212     }
213
214
215     private void _saveEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
216     throws Exception JavaDoc {
217         
218         WebEventForm formBean = ( WebEventForm ) form;
219         WebEvent e = ( WebEvent ) req.getAttribute(WebKeys.WEBEVENTS_EDIT);
220         BeanUtils.copyProperties(e, formBean);
221         InodeFactory.saveInode(e);
222         
223         //wipe out the old categories
224
//java.util.Set _parents = e.getParents();
225
java.util.List JavaDoc _parents = InodeFactory.getParentsOfClass(e, WebEvent.class);
226         java.util.List JavaDoc _cats = InodeFactory.getParentsOfClass(e, Category.class);
227         Iterator JavaDoc it = _cats.iterator();
228         while (it.hasNext()) {
229             Category cat = ( Category ) it.next();
230             cat.deleteChild(e);
231             _parents.remove(cat);
232         }
233         
234         //add the new categories
235
String JavaDoc[] arr = formBean.getCategories();
236         
237         if (arr != null) {
238             for (int i = 0; i < arr.length; i++) {
239                 Category node = ( Category ) InodeFactory.getInode(arr[i], Category.class);
240                 node.addChild(e);
241                 _parents.add(node);
242             }
243         }
244         e.setParents(_parents);
245         WebEventFactory.saveWebEvent(e);
246
247         //Save the event files
248
_saveFiles(e, formBean.getFilesInodesList());
249         
250         //Save the event images
251
_saveImages(e, formBean);
252         
253         req.setAttribute(WebKeys.WEBEVENTS_EDIT, e);
254         
255         //add message
256
//wraps request to get session object
257
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
258         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
259         
260         SessionMessages.add(httpReq, "message", "message.webevent.saved");
261         
262     }
263     //reorder categories
264
private void _reorderEvents(ActionForm form, ActionRequest req, ActionResponse res) throws Exception JavaDoc {
265         
266         int count = 0;
267         try {
268             count = Integer.parseInt(req.getParameter("count"));
269         }
270         catch (Exception JavaDoc e) {}
271         String JavaDoc[] order = new String JavaDoc[(count)];
272         for (int i = 0; i < order.length; i++) {
273             WebEvent event = (WebEvent) InodeFactory.getInode(req.getParameter("inode" + i), WebEvent.class);
274             String JavaDoc newOrderString = req.getParameter("newOrder" + i);
275             int newOrder = 0;
276             if(UtilMethods.isInt(newOrderString))
277             {
278                 newOrder = Integer.parseInt(newOrderString);
279             }
280             event.setSortOrder(newOrder);
281             InodeFactory.saveInode(event);
282         }
283         SessionMessages.add(req, "message", "message.webevent.reorder");
284
285     }
286
287     private void _saveImages(WebEvent e, WebEventForm form) {
288
289         //gets image inode and saves the identifier inode
290
long eventImage1 = form.getEventImage1();
291         if (eventImage1>0) {
292             File file = (File) InodeFactory.getInode(eventImage1,File.class);
293             Identifier identifier = IdentifierFactory.getIdentifierByInode(file);
294             e.setEventImage1(identifier.getInode());
295         }
296         
297         //gets image inode and saves the identifier inode
298
long eventImage2 = form.getEventImage2();
299         if (eventImage2>0) {
300             File file = (File) InodeFactory.getInode(eventImage2,File.class);
301             Identifier identifier = IdentifierFactory.getIdentifierByInode(file);
302             e.setEventImage2(identifier.getInode());
303         }
304
305         //gets image inode and saves the identifier inode
306
long eventImage3 = form.getEventImage3();
307         if (eventImage3>0) {
308             File file = (File) InodeFactory.getInode(eventImage3,File.class);
309             Identifier identifier = IdentifierFactory.getIdentifierByInode(file);
310             e.setEventImage3(identifier.getInode());
311         }
312
313         //gets image inode and saves the identifier inode
314
long eventImage4 = form.getEventImage4();
315         if (eventImage4>0) {
316             File file = (File) InodeFactory.getInode(eventImage4,File.class);
317             Identifier identifier = IdentifierFactory.getIdentifierByInode(file);
318             e.setEventImage4(identifier.getInode());
319         }
320         
321         //Save changes
322
WebEventFactory.saveWebEvent(e);
323     
324     }
325     
326     private void _saveFiles (WebEvent e, String JavaDoc[] fileInodes) {
327         
328         java.util.List JavaDoc _files = InodeFactory.getChildrenClass(e, Identifier.class);
329         Iterator JavaDoc it = _files.iterator();
330         while (it.hasNext()) {
331             Identifier iden = ( Identifier ) it.next();
332             e.deleteChild(iden);
333         }
334
335         for (int i = 0; i < fileInodes.length; i++) {
336             String JavaDoc inode = fileInodes[i];
337             File file = (File)InodeFactory.getInode(inode, File.class);
338             Identifier identifier = IdentifierFactory.getParentIdentifier(file);
339             e.addChild(identifier);
340         }
341     }
342     
343     private void _deleteEvent(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
344     throws Exception JavaDoc {
345
346         WebEvent e = ( WebEvent ) req.getAttribute(WebKeys.WEBEVENTS_EDIT);
347         
348         //delete all locations for this event
349
List JavaDoc locations = WebEventFactory.getEventLocations(e,"start_date");
350         Iterator JavaDoc locationsIter = locations.iterator();
351         while (locationsIter.hasNext()) {
352             WebEventLocation location = (WebEventLocation) locationsIter.next();
353             WebEventLocationFactory.deleteWebEventLocation(location);
354         }
355         //delete this event
356
WebEventFactory.deleteWebEvent(e);
357         SessionMessages.add(req, "message", "message.webevents.deleted");
358
359     }
360
361 }
362
363
Popular Tags