KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > facilities > action > EditFacilityAction


1 package com.dotmarketing.portlets.facilities.action;
2
3 import java.net.URLDecoder JavaDoc;
4 import javax.portlet.ActionRequest;
5 import javax.portlet.ActionResponse;
6 import javax.portlet.PortletConfig;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import org.apache.commons.beanutils.BeanUtils;
9 import org.apache.struts.action.ActionForm;
10 import org.apache.struts.action.ActionMapping;
11 import com.dotmarketing.db.DotHibernate;
12 import com.dotmarketing.factories.InodeFactory;
13 import com.dotmarketing.portal.struts.DotPortletAction;
14 import com.dotmarketing.portlets.facilities.factories.FacilityFactory;
15 import com.dotmarketing.portlets.facilities.model.Facility;
16 import com.dotmarketing.portlets.facilities.struts.FacilityForm;
17 import com.dotmarketing.util.Validator;
18 import com.dotmarketing.util.WebKeys;
19 import com.liferay.portal.model.User;
20 import com.liferay.portal.struts.ActionException;
21 import com.liferay.portal.util.Constants;
22 import com.liferay.portlet.ActionRequestImpl;
23 import com.liferay.util.servlet.SessionMessages;
24
25 /**
26  * @author Maria
27  */

28
29 public class EditFacilityAction extends DotPortletAction {
30     
31     public static boolean debug = false;
32     
33     public void processAction(
34              ActionMapping mapping, ActionForm form, PortletConfig config,
35              ActionRequest req, ActionResponse res)
36          throws Exception JavaDoc {
37
38         String JavaDoc cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : Constants.EDIT;
39         String JavaDoc referer = req.getParameter("referer");
40
41         //wraps request to get session object
42
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
43         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
44
45         if ((referer!=null) && (referer.length()!=0)) {
46             referer = URLDecoder.decode(referer,"UTF-8");
47         }
48
49         //System.out.println("\n\n\nEditFacilityAction :: cmd=" + cmd);
50

51         new DotHibernate().startTransaction();
52
53         User user = _getUser(req);
54         
55         try {
56             //System.out.println("Calling Retrieve method");
57
_retrieveFacility(req, res, config, form, user);
58
59         } catch (ActionException ae) {
60             _handleException(ae, req);
61         }
62
63         Facility c = (Facility) req.getAttribute(WebKeys.FACILITY_EDIT);
64          
65         /*
66          * We are editing the Facility
67          */

68         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
69             try {
70                 //System.out.println("Calling Edit Method");
71
_editFacility(req, res, config, form, user);
72
73                 //if we have a unrun Facility, show it. Else, redirect to the report page
74
setForward(req,"portlet.ext.facilities.edit_facility");
75
76             } catch (ActionException ae) {
77                 _handleException(ae, req);
78             }
79         }
80         
81         /*
82          * If we are updating the Facility, copy the information
83          * from the struts bean to the hbm inode and run the
84          * update action and return to the list
85          */

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

107         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
108             try {
109                 //System.out.println("Calling Delete Method");
110
_deleteFacility(req, res, config, form, user);
111
112             } catch (ActionException ae) {
113                 _handleException(ae, req);
114             }
115             _sendToReferral(req,res,referer);
116         }
117         /*
118          * If we are deleting the selected Facilities,
119          * run the delete action and return to the list
120          *
121          */

122         else if ((cmd != null) && cmd.equals("deleteselected")) {
123             try {
124                 //System.out.println("\n\nCalling Delete Selected Method");
125
_deleteSelectedFacilities(req, res, config, form, user);
126
127             } catch (ActionException ae) {
128                 _handleException(ae, req);
129             }
130             _sendToReferral(req,res,referer);
131         }
132         /*
133          * If we are deleting the selected Facilities,
134          * run the delete action and return to the list
135          *
136          */

137         else if ((cmd != null) && cmd.equals("reorder")) {
138             try {
139                 //System.out.println("Calling Reorder Method");
140
_orderSelectedFacilities(req, res, config, form, user);
141
142             } catch (ActionException ae) {
143                 _handleException(ae, req);
144             }
145             _sendToReferral(req,res,referer);
146         }
147     }
148
149     ///// ************** ALL METHODS HERE *************************** ////////
150

151     public void _retrieveFacility(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
152     throws Exception JavaDoc {
153
154         String JavaDoc inode = (req.getParameter("inode")!=null) ? req.getParameter("inode") : "0";
155         
156         Facility f = FacilityFactory.getFacility(inode);
157         
158         req.setAttribute(WebKeys.FACILITY_EDIT, f);
159
160     }
161     public void _editFacility(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
162         throws Exception JavaDoc {
163
164         FacilityForm ff = (FacilityForm) form;
165         Facility f = (Facility) req.getAttribute(WebKeys.FACILITY_EDIT);
166         
167         BeanUtils.copyProperties(form, f);
168
169     }
170
171     public void _saveFacility(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
172         throws Exception JavaDoc {
173
174         Facility c = (Facility) req.getAttribute(WebKeys.FACILITY_EDIT);
175         FacilityForm cfform = (FacilityForm) form;
176
177         BeanUtils.copyProperties(req.getAttribute(WebKeys.FACILITY_EDIT), form);
178
179         //System.out.println("Saving Facility:" + c.getInode());
180

181         InodeFactory.saveInode(c);
182
183         //no sure if this is needed
184
InodeFactory.saveInode(c);
185         
186         //add message
187
SessionMessages.add(req, "message", "message.facility.saved");
188
189     }
190     
191     public void _deleteFacility(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
192     throws Exception JavaDoc {
193
194         Facility c = (Facility) req.getAttribute(WebKeys.FACILITY_EDIT);
195         FacilityFactory.deleteFacility(c);
196         SessionMessages.add(req, "message", "message.facility.deleted");
197
198     }
199
200     public void _deleteSelectedFacilities(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
201     throws Exception JavaDoc {
202
203         String JavaDoc[] deleteInodes = req.getParameterValues("delInode");
204         
205         for (int i=0;i<deleteInodes.length;i++) {
206             String JavaDoc delInode = deleteInodes[i];
207             Facility f = FacilityFactory.getFacility(delInode);
208             FacilityFactory.deleteFacility(f);
209         }
210         
211         SessionMessages.add(req, "message", "message.facilities.deleted");
212
213     }
214
215     public void _orderSelectedFacilities(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
216     throws Exception JavaDoc {
217     
218         int count = 0;
219         try {
220             count = Integer.parseInt(req.getParameter("count"));
221             String JavaDoc[] order = new String JavaDoc[(count)];
222             int x = 9999;
223             for(int i = 0;i< order.length;i++){
224                 Facility f = FacilityFactory.getFacility(req.getParameter("sInode" + i));
225                 f.setSortOrder(Integer.parseInt(req.getParameter("sortOrder" + i) + x--));
226                 InodeFactory.saveInode(f);
227             }
228         }
229         catch (Exception JavaDoc e) {
230             //System.out.println("Error ordering");
231
}
232         SessionMessages.add(req, "message", "message.facilities.order");
233     }
234 }
235
Popular Tags