KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > portlets > campaigns > action > EditCampaignAction


1 package com.dotmarketing.portlets.campaigns.action;
2
3 import java.net.URLDecoder JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import javax.portlet.ActionRequest;
8 import javax.portlet.ActionResponse;
9 import javax.portlet.PortletConfig;
10 import javax.portlet.WindowState;
11 import javax.servlet.http.HttpServletRequest JavaDoc;
12
13 import org.apache.commons.beanutils.BeanUtils;
14 import org.apache.struts.action.ActionForm;
15 import org.apache.struts.action.ActionMapping;
16
17 import com.dotmarketing.db.DotHibernate;
18 import com.dotmarketing.factories.InodeFactory;
19 import com.dotmarketing.portal.struts.DotPortletAction;
20 import com.dotmarketing.portlets.campaigns.factories.CampaignFactory;
21 import com.dotmarketing.portlets.campaigns.model.Campaign;
22 import com.dotmarketing.portlets.campaigns.struts.CampaignForm;
23 import com.dotmarketing.portlets.communications.factories.CommunicationsFactory;
24 import com.dotmarketing.portlets.communications.model.Communication;
25 import com.dotmarketing.portlets.htmlpages.model.HTMLPage;
26 import com.dotmarketing.portlets.mailinglists.factories.MailingListFactory;
27 import com.dotmarketing.portlets.mailinglists.model.MailingList;
28 import com.dotmarketing.util.Config;
29 import com.dotmarketing.util.Logger;
30 import com.dotmarketing.util.Validator;
31 import com.dotmarketing.util.WebKeys;
32 import com.liferay.portal.ejb.RoleLocalManagerUtil;
33 import com.liferay.portal.ejb.UserLocalManagerUtil;
34 import com.liferay.portal.model.Role;
35 import com.liferay.portal.model.User;
36 import com.liferay.portal.struts.ActionException;
37 import com.liferay.portal.util.Constants;
38 import com.liferay.portlet.ActionRequestImpl;
39 import com.liferay.util.servlet.SessionMessages;
40
41 /**
42  * @author Maria
43  */

44
45 public class EditCampaignAction extends DotPortletAction {
46     
47     @SuppressWarnings JavaDoc("unchecked")
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         //wraps request to get session object
57
ActionRequestImpl reqImpl = (ActionRequestImpl)req;
58         HttpServletRequest JavaDoc httpReq = reqImpl.getHttpServletRequest();
59
60         if ((referer!=null) && (referer.length()!=0)) {
61             referer = URLDecoder.decode(referer,"UTF-8");
62         }
63
64         Logger.debug(this, "EditCampaignAction cmd=" + cmd);
65         
66         DotHibernate.startTransaction();
67
68         User user = _getUser(req);
69         
70         try {
71             _retrieveCampaign(req, res, config, form, user);
72
73         } catch (ActionException ae) {
74             _handleException(ae, req);
75         }
76
77         Campaign c = (Campaign) req.getAttribute(WebKeys.CAMPAIGN_EDIT);
78         if(c.isLocked()){
79             //add message
80
SessionMessages.add(req, "message", "message.campaign.locked");
81             setForward(req,"portlet.ext.campaigns.view_campaigns");
82         }
83          
84         //getting the user roles
85
boolean isCampaignManagerAdmin = false;
86         String JavaDoc campaignManagerAdminRoleId = "";
87         try {
88             Role campaignManagerAdminRole = RoleLocalManagerUtil.getRoleByName(user.getCompanyId(),Config.getStringProperty("CAMPAIGN_MANAGER_ADMIN"));
89             campaignManagerAdminRoleId = campaignManagerAdminRole.getRoleId();
90         }
91         catch (Exception JavaDoc e) {}
92
93         boolean isCampaignManagerEditor = false;
94         String JavaDoc campaignManagerEditorRoleId = "";
95         try {
96             Role campaignManagerEditorRole = RoleLocalManagerUtil.getRoleByName(user.getCompanyId(),Config.getStringProperty("CAMPAIGN_MANAGER_EDITOR"));
97             campaignManagerEditorRoleId = campaignManagerEditorRole.getRoleId();
98         }
99         catch (Exception JavaDoc e) {}
100
101         Role[] userRoles = (Role[])UserLocalManagerUtil.getRoles(user.getUserId()).toArray(new Role[0]);
102         for (int i = 0; i < userRoles.length; i++) {
103             Role userRole = (Role) userRoles[i];
104             if (userRole.getRoleId().equals(campaignManagerAdminRoleId)) {
105                 isCampaignManagerAdmin = true;
106             }
107             if (userRole.getRoleId().equals(campaignManagerEditorRoleId)) {
108                 isCampaignManagerEditor = true;
109             }
110         }
111         
112         /*
113          * We are editing the campaign
114          */

115         if ((cmd != null) && cmd.equals(Constants.EDIT)) {
116             try {
117                 _editCampaign(req, res, config, form, user);
118
119                 //if we have a unrun campaign, show it. Else, redirect to the report page
120
if((c.getCompletedDate() == null) &&
121                         (isCampaignManagerAdmin || (c.getUserId().equalsIgnoreCase(user.getUserId()) && isCampaignManagerEditor))){
122                     setForward(req,"portlet.ext.campaigns.edit_campaign");
123                 }
124                 else{
125                     //the campaign has run, it redirects to the reports page
126
java.util.Map JavaDoc params = new java.util.HashMap JavaDoc();
127                     params.put("struts_action",new String JavaDoc[] {"/ext/campaigns/view_report"});
128                     params.put("inode",new String JavaDoc[] { c.getInode() + "" });
129     
130                     String JavaDoc reportURL = com.dotmarketing.util.PortletURLUtil.getActionURL(httpReq,WindowState.MAXIMIZED.toString(),params);
131                     _sendToReferral(req,res,reportURL);
132                 }
133
134             } catch (ActionException ae) {
135                 _handleException(ae, req);
136             }
137         }
138         
139         /*
140          * If we are updating the campaign, copy the information
141          * from the struts bean to the hbm inode and run the
142          * update action and return to the list
143          */

144         else if ((cmd != null) && cmd.equals(Constants.ADD)) {
145             try {
146
147                 if (Validator.validate(req,form,mapping)) {
148                     _saveCampaign(req, res, config, form, user);
149                 }
150                 else {
151                     setForward(req,"portlet.ext.campaigns.edit_campaign");
152                 }
153
154             } catch (ActionException ae) {
155                 _handleException(ae, req);
156             }
157             _sendToReferral(req,res,referer);
158         }
159         /*
160          * If we are deleting the campaign,
161          * run the delete action and return to the list
162          *
163          */

164         else if ((cmd != null) && cmd.equals(Constants.DELETE)) {
165             try {
166                 _deleteCampaign(req, res, config, form, user);
167             } catch (ActionException ae) {
168                 _handleException(ae, req);
169             }
170             _sendToReferral(req,res,referer);
171         }
172         /*
173          * If we are copying the campaign,
174          * run the copy action and return to the list
175          *
176          */

177         else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.COPY)) {
178             try {
179                 _copyCampaign(req, res, config, form, user);
180             } catch (ActionException ae) {
181                 _handleException(ae, req);
182             }
183             _sendToReferral(req,res,referer);
184         }
185     }
186
187     ///// ************** ALL METHODS HERE *************************** ////////
188

189     public void _retrieveCampaign(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
190     throws Exception JavaDoc {
191
192         String JavaDoc inode = (req.getParameter("inode")!=null) ? req.getParameter("inode") : "0";
193         
194         Campaign c = null;
195         c = CampaignFactory.getCampaign(inode);
196         
197         if(c.getInode() ==0){
198             c = CampaignFactory.newInstance();
199             c.setUserId(user.getUserId());
200         }
201
202         req.setAttribute(WebKeys.CAMPAIGN_EDIT, c);
203
204         List JavaDoc<Communication> list = CommunicationsFactory.getCommunications("","");
205         List JavaDoc<Communication> permitted = new ArrayList JavaDoc();
206         for(Communication com : list){
207             try {
208                 _checkUserPermissions(com, user, Config.getIntProperty("PERMISSION_READ"));
209                 permitted.add(com);
210             }catch(Exception JavaDoc e){
211                 e.printStackTrace();
212             }
213         }
214         req.setAttribute(WebKeys.COMMUNICATION_LIST_VIEW, permitted);
215     }
216     public void _editCampaign(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
217         throws Exception JavaDoc {
218
219         CampaignForm cfform = (CampaignForm) form;
220         Campaign c = (Campaign) req.getAttribute(WebKeys.CAMPAIGN_EDIT);
221         
222         BeanUtils.copyProperties(form, c);
223
224         //add the campaigns mailing list to the form
225
MailingList ml = (MailingList) InodeFactory.getChildOfClass(c, MailingList.class);
226         cfform.setMailingList(ml.getInode());
227
228         //add the html page to the campaign
229
HTMLPage page = (HTMLPage) InodeFactory.getChildOfClass(c, HTMLPage.class);
230         if (page.getInode() > 0) {
231             cfform.setHtmlPage(page.getInode());
232             cfform.setSelectedHtmlPage(page.getTitle());
233         }
234
235         Communication comm = (Communication) InodeFactory.getChildOfClass(c, Communication.class);
236         cfform.setCommunicationInode(comm.getInode());
237     }
238
239     public void _saveCampaign(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
240         throws Exception JavaDoc {
241
242         Campaign c = (Campaign) req.getAttribute(WebKeys.CAMPAIGN_EDIT);
243         CampaignForm cfform = (CampaignForm) form;
244
245         BeanUtils.copyProperties(req.getAttribute(WebKeys.CAMPAIGN_EDIT), form);
246
247         InodeFactory.saveInode(c);
248
249         // wipe the old mailing list that was the child
250
MailingList ml = (MailingList) InodeFactory.getChildOfClass(c, MailingList.class);
251         c.deleteChild(ml);
252
253         //try to get the campaign's new mailing list
254
ml = (MailingList) InodeFactory.getInode(String.valueOf(cfform.getMailingList()), MailingList.class);
255         if (ml.getInode() > 0) {
256             c.addChild(ml);
257         }
258
259         // wipe the old communication that was the child
260
Communication comm = (Communication) InodeFactory.getChildOfClass(c, Communication.class);
261         c.deleteChild(comm);
262
263         //try to get the campaign's new communication
264
comm = (Communication) InodeFactory.getInode(String.valueOf(cfform.getCommunicationInode()), Communication.class);
265         if (comm.getInode() > 0) {
266             c.addChild(comm);
267         }
268
269         /*
270         // wipe the old HTML page entries
271         HTMLPage page = (HTMLPage) InodeFactory.getChildOfClass(c, HTMLPage.class);
272         if (page.getInode() > 0)
273             c.deleteChild(page);
274
275         //try to get the campaign's page
276         if (cfform.getHtmlPage() > 0) {
277             page = (HTMLPage) InodeFactory.getInode(String.valueOf(cfform.getHtmlPage()), HTMLPage.class);
278             if (page.getInode() > 0) {
279                 c.addChild(page);
280             }
281         }
282         */

283
284         c.setUserId(user.getUserId());
285         
286         //no sure if this is needed
287
InodeFactory.saveInode(c);
288         
289         //add message
290
SessionMessages.add(req, "message", "message.campaign.saved");
291
292     }
293     
294     public void _copyCampaign(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
295         throws Exception JavaDoc {
296
297         Campaign c = (Campaign) req.getAttribute(WebKeys.CAMPAIGN_EDIT);
298         MailingList ml = (MailingList) InodeFactory.getChildOfClass(c, MailingList.class);
299         HTMLPage page = (HTMLPage) InodeFactory.getChildOfClass(c, HTMLPage.class);
300         
301         Campaign copy = CampaignFactory.newInstance();
302
303         copy.setTitle( c.getTitle() + " (copy)");
304         copy.setFromEmail(c.getFromEmail());
305         copy.setFromName(c.getFromName());
306         copy.setStartDate(c.getStartDate());
307         copy.setSubject(c.getSubject());
308         copy.setMessage(c.getMessage());
309         copy.setOwner(c.getOwner());
310         copy.setUserId(c.getUserId());
311
312         //no sure if this is needed
313
InodeFactory.saveInode(copy);
314         
315         if(ml.getInode() > 0){
316             copy.addChild(ml);
317         }
318         if(page.getInode() > 0){
319             copy.addChild(page);
320         }
321         InodeFactory.saveInode(copy);
322         
323         //add message
324
SessionMessages.add(req, "message", "message.campaign.copied");
325     }
326
327     public void _deleteCampaign(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user)
328     throws Exception JavaDoc {
329
330         Campaign c = (Campaign) req.getAttribute(WebKeys.CAMPAIGN_EDIT);
331         CampaignFactory.deleteCampaign(c, user.getUserId());
332         SessionMessages.add(req, "message", "message.campaigns.deleted");
333
334     }
335 }
336
Popular Tags