KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
9
10 import javax.portlet.ActionRequest;
11 import javax.portlet.ActionResponse;
12 import javax.portlet.PortletConfig;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14
15 import org.apache.commons.beanutils.BeanUtils;
16 import org.apache.struts.action.ActionForm;
17 import org.apache.struts.action.ActionMapping;
18
19 import com.dotmarketing.beans.Identifier;
20 import com.dotmarketing.db.DotHibernate;
21 import com.dotmarketing.factories.IdentifierFactory;
22 import com.dotmarketing.factories.InodeFactory;
23 import com.dotmarketing.portal.struts.DotPortletAction;
24 import com.dotmarketing.portlets.links.model.Link;
25 import com.dotmarketing.util.UtilMethods;
26 import com.dotmarketing.util.Validator;
27 import com.dotmarketing.util.WebKeys;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.struts.ActionException;
30 import com.liferay.portal.util.Constants;
31 import com.liferay.portlet.ActionRequestImpl;
32 import com.liferay.util.servlet.SessionMessages;
33 import com.dotmarketing.portlets.webevents.factories.WebEventFactory;
34 import com.dotmarketing.portlets.webevents.factories.WebEventLocationFactory;
35 import com.dotmarketing.portlets.webevents.model.WebEvent;
36 import com.dotmarketing.portlets.webevents.model.WebEventLocation;
37 import com.dotmarketing.portlets.webevents.struts.WebEventLocationForm;
38
39
40 /**
41  * @author David Torres
42  *
43  */

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

74         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
75             try {
76                 _editEventLocation(req, res, config, form, user);
77                 setForward(req,"portlet.ext.webevents.edit_location");
78             } catch (ActionException ae) {
79                 _handleException(ae, req);
80             }
81         }
82         
83         /*
84          * Save the EventLocation occurrence
85          */

86         else if ((cmd != null) && cmd.equals(Constants.SAVE)) {
87             try {
88
89                 if (Validator.validate(req,form,mapping)) {
90                     if (_saveEventLocation(req, res, config, form, user)) {
91                         _sendToReferral(req,res,referer);
92                     }
93                     else {
94                         setForward(req,"portlet.ext.webevents.edit_location");
95                     }
96                 } else setForward(req,"portlet.ext.webevents.edit_location");
97
98             } catch (ActionException ae) {
99                 _handleException(ae, req);
100             }
101         }
102         /*
103          * If we are deleting the EventLocation,
104          * run the delete action and return to the list
105          *
106          */

107         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
108             try {
109                 _deleteEventLocation(req, res, config, form, user);
110
111             } catch (ActionException ae) {
112                 _handleException(ae, req);
113             }
114             _sendToReferral(req,res,referer);
115         }
116         DotHibernate.commitTransaction();
117
118     }
119
120
121     ///// ************** ALL METHODS HERE *************************** ////////
122
private void _retrieveEventLocation(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
123     throws Exception JavaDoc {
124
125         String JavaDoc inode = (req.getParameter("inode")!=null) ? req.getParameter("inode") : "0";
126         WebEventLocation e = null;
127         if(inode.equals("0")){
128             e = WebEventLocationFactory.newInstance();
129         } else {
130             e = WebEventLocationFactory.getWebEventLocation(inode);
131         }
132         req.setAttribute(WebKeys.WEBEVENTS_LOCATION_FORM, form);
133         req.setAttribute(WebKeys.WEBEVENTS_LOCATION_EDIT, e);
134     }
135     
136     private void _editEventLocation(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
137         throws Exception JavaDoc {
138
139         WebEventLocationForm formBean = ( WebEventLocationForm ) form;
140
141         if (!UtilMethods.isSet(formBean.getTitle())) {
142             WebEvent event = WebEventFactory.getWebEvent(formBean.getWebEventInode());
143             formBean.setTitle(event.getTitle());
144         }
145
146         WebEventLocation e = ( WebEventLocation ) req.getAttribute(WebKeys.WEBEVENTS_LOCATION_EDIT);
147         BeanUtils.copyProperties(formBean, e);
148         
149         //set links on the form bean
150
if (e.getHotelLink()>0) {
151             Identifier identifier = (Identifier) InodeFactory.getInode(e.getHotelLink(),Identifier.class);
152             Link link = (Link) IdentifierFactory.getWorkingChildOfClass(identifier,Link.class);
153             formBean.setHotelLink(link.getInode());
154             formBean.setSelectedhotelLink(link.getProtocal() + link.getUrl());
155         }
156         if (e.getPastEventLink()>0) {
157             Identifier identifier = (Identifier) InodeFactory.getInode(e.getPastEventLink(),Identifier.class);
158             Link link = (Link) IdentifierFactory.getWorkingChildOfClass(identifier,Link.class);
159             formBean.setPastEventLink(link.getInode());
160             formBean.setSelectedpastEventLink(link.getProtocal() + link.getUrl());
161         }
162
163     }
164
165
166     private boolean _saveEventLocation(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
167     throws Exception JavaDoc {
168         
169         //wraps request to get session object
170
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
171         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
172
173         WebEventLocationForm formBean = ( WebEventLocationForm ) form;
174         WebEventLocation e = ( WebEventLocation ) req.getAttribute(WebKeys.WEBEVENTS_LOCATION_EDIT);
175         
176         Date JavaDoc startDate = formBean.getStartDate();
177         Date JavaDoc endDate = formBean.getEndDate();
178         if (startDate.after(endDate)) {
179             //add message
180
SessionMessages.add(httpReq, "message", "message.webeventlocation.startDateAfterendDate");
181             return false;
182         }
183         BeanUtils.copyProperties(e, formBean);
184         InodeFactory.saveInode(e);
185         
186         req.setAttribute(WebKeys.WEBEVENTS_LOCATION_EDIT, e);
187         
188         //save link relationships
189
//gets link inode and saves the identifier inode
190
long hotelLink = formBean.getHotelLink();
191         if (hotelLink>0) {
192             Link link = (Link) InodeFactory.getInode(hotelLink,Link.class);
193             Identifier identifier = IdentifierFactory.getIdentifierByInode(link);
194             e.setHotelLink(identifier.getInode());
195         }
196         //gets link inode and saves the identifier inode
197
long pastEventLink = formBean.getPastEventLink();
198         if (pastEventLink>0) {
199             Link link = (Link) InodeFactory.getInode(pastEventLink,Link.class);
200             Identifier identifier = IdentifierFactory.getIdentifierByInode(link);
201             e.setPastEventLink(identifier.getInode());
202         }
203         
204         
205         //add message
206
SessionMessages.add(httpReq, "message", "message.webeventlocation.saved");
207         return true;
208     }
209
210     private void _deleteEventLocation(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
211     throws Exception JavaDoc {
212
213         WebEventLocation e = ( WebEventLocation ) req.getAttribute(WebKeys.WEBEVENTS_LOCATION_EDIT);
214         WebEventLocationFactory.deleteWebEventLocation(e);
215         SessionMessages.add(req, "message", "message.webeventlocation.deleted");
216
217     }
218     
219 // reorder events
220
private void _reorderEvents(ActionForm form, ActionRequest req, ActionResponse res) throws Exception JavaDoc {
221         
222         int count = 0;
223         try {
224             count = Integer.parseInt(req.getParameter("count"));
225         }
226         catch (Exception JavaDoc e) {}
227         String JavaDoc[] order = new String JavaDoc[(count)];
228         for (int i = 0; i < order.length; i++) {
229             WebEvent event = (WebEvent) InodeFactory.getInode(req.getParameter("inode" + i), WebEvent.class);
230             event.setSortOrder(Integer.parseInt(req.getParameter("newOrder" + i)));
231             InodeFactory.saveInode(event);
232         }
233         //SessionMessages.add(req, "message", "message.webevent.reorder");
234

235     }
236
237 }
238
239
Popular Tags