1 package com.dotmarketing.portlets.communications.action; 2 3 import java.net.URLDecoder ; 4 5 import javax.portlet.ActionRequest; 6 import javax.portlet.ActionResponse; 7 import javax.portlet.PortletConfig; 8 import javax.servlet.http.HttpServletRequest ; 9 10 import org.apache.commons.beanutils.BeanUtils; 11 import org.apache.struts.Globals; 12 import org.apache.struts.action.ActionForm; 13 import org.apache.struts.action.ActionMapping; 14 import org.apache.struts.action.ActionMessages; 15 16 import com.dotmarketing.db.DotHibernate; 17 import com.dotmarketing.factories.InodeFactory; 18 import com.dotmarketing.portal.struts.DotPortletAction; 19 import com.dotmarketing.portlets.communications.factories.CommunicationsFactory; 20 import com.dotmarketing.portlets.communications.model.Communication; 21 import com.dotmarketing.portlets.communications.struts.CommunicationsForm; 22 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 23 import com.dotmarketing.portlets.virtuallinks.factories.VirtualLinkFactory; 24 import com.dotmarketing.portlets.virtuallinks.model.VirtualLink; 25 import com.dotmarketing.util.Config; 26 import com.dotmarketing.util.Logger; 27 import com.dotmarketing.util.UtilMethods; 28 import com.dotmarketing.util.Validator; 29 import com.dotmarketing.util.WebKeys; 30 import com.liferay.portal.model.User; 31 import com.liferay.portal.struts.ActionException; 32 import com.liferay.portal.util.Constants; 33 import com.liferay.portlet.ActionRequestImpl; 34 import com.liferay.util.servlet.SessionMessages; 35 36 41 public class EditCommunicationsAction extends DotPortletAction { 42 43 @SuppressWarnings ("unchecked") 44 public void processAction( 45 ActionMapping mapping, ActionForm form, PortletConfig config, 46 ActionRequest req, ActionResponse res) 47 throws Exception { 48 49 String cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : Constants.EDIT; 50 String referer = req.getParameter("referer"); 51 52 ActionRequestImpl reqImpl = (ActionRequestImpl)req; 54 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 55 56 if ((referer!=null) && (referer.length()!=0)) { 57 referer = URLDecoder.decode(referer,"UTF-8"); 58 } 59 60 Logger.debug(this, "EditCommunicationsAction cmd=" + cmd); 61 62 DotHibernate.startTransaction(); 63 64 User user = _getUser(req); 65 66 try { 67 _retrieveCommunication(req, res, config, form, user); 68 69 } catch (ActionException ae) { 70 _handleException(ae, req); 71 } 72 73 76 if ((cmd != null) && cmd.equals(Constants.EDIT)) { 77 try { 78 _editCommunication(req, res, config, form, user); 79 setForward(req,"portlet.ext.communications.edit_communication"); 80 81 } catch (ActionException ae) { 82 _handleException(ae, req); 83 } 84 } 85 86 91 else if ((cmd != null) && cmd.equals(Constants.ADD)) { 92 try { 93 94 if (Validator.validate(req,form,mapping)) { 95 _saveCommunication(req, res, config, form, user); 96 _sendToReferral(req,res,referer); 97 } 98 99 } catch (ActionException ae) { 100 _handleException(ae, req); 101 } 102 103 } 104 109 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 110 try { 111 _deleteCommunication(req, res, config, form, user); 112 } catch (ActionException ae) { 113 _handleException(ae, req); 114 } 115 _sendToReferral(req,res,referer); 116 } 117 118 DotHibernate.commitTransaction(); 119 if(req.getAttribute(com.dotmarketing.util.WebKeys.COMMUNICATION_EDIT_FORM) == null){ 120 CommunicationsForm cform = (CommunicationsForm) form; 121 if(!UtilMethods.isSet(cform.getModDate())){ 122 cform.setModDate(new java.util.Date ()); 123 cform.setModifiedBy(user.getUserId()); 124 } 125 req.setAttribute(com.dotmarketing.util.WebKeys.COMMUNICATION_EDIT_FORM, cform); 126 } 127 setForward(req,"portlet.ext.communications.edit_communication"); 128 129 130 } 131 132 134 public void _retrieveCommunication(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 135 throws Exception { 136 137 String inode = (req.getParameter("inode")!=null) ? req.getParameter("inode") : "0"; 138 139 Communication c = null; 140 c = CommunicationsFactory.getCommunication(inode); 141 142 if(c.getInode() ==0){ 143 c = CommunicationsFactory.newInstance(); 144 req.setAttribute(WebKeys.COMMUNICATION_EDIT_FORM_PERMISSION, true); 145 } 146 147 req.setAttribute(WebKeys.COMMUNICATION_EDIT, c); 148 req.setAttribute(WebKeys.PERMISSION_INODE_EDIT,c); 149 150 } 151 public void _editCommunication(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 152 throws Exception { 153 154 CommunicationsForm cfform = (CommunicationsForm) form; 155 Communication c = (Communication) req.getAttribute(WebKeys.COMMUNICATION_EDIT); 156 157 BeanUtils.copyProperties(cfform, c); 158 159 if(cfform.getModDate() == null) 160 cfform.setModDate(new java.util.Date ()); 161 162 HTMLPage page = (HTMLPage) InodeFactory.getChildOfClass(c, HTMLPage.class); 164 if (page.getInode() > 0) { 165 cfform.setHtmlPage(page.getInode()); 166 cfform.setSelectedhtmlPage(page.getTitle()); 167 } 168 169 VirtualLink vl = (VirtualLink) InodeFactory.getChildOfClass(c, VirtualLink.class); 170 if (vl.getInode() > 0) { 171 cfform.setTrackBackLinkInode(vl.getInode()); 172 } 173 req.setAttribute(WebKeys.COMMUNICATION_EDIT_FORM, cfform); 174 } 175 176 public void _saveCommunication(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 177 throws Exception { 178 179 Communication c = (Communication) req.getAttribute(WebKeys.COMMUNICATION_EDIT); 180 CommunicationsForm cfform = (CommunicationsForm) form; 181 182 BeanUtils.copyProperties(req.getAttribute(WebKeys.COMMUNICATION_EDIT), cfform); 183 184 if(cfform.getModDate() == null){ 185 c.setModDate(new java.util.Date ()); 186 } 187 188 c.setModifiedBy(user.getUserId()); 189 190 _checkUserPermissions(c, user, Config.getIntProperty("PERMISSION_WRITE")); 191 InodeFactory.saveInode(c); 192 193 HTMLPage page = (HTMLPage) InodeFactory.getChildOfClass(c, HTMLPage.class); 195 if (page.getInode() > 0) 196 c.deleteChild(page); 197 198 if (cfform.getHtmlPage() > 0) { 200 page = (HTMLPage) InodeFactory.getInode(String.valueOf(cfform.getHtmlPage()), HTMLPage.class); 201 if (page.getInode() > 0) { 202 c.addChild(page); 203 c.setHtmlPage(page.getInode()); 204 } 205 } 206 207 VirtualLink vl = (VirtualLink) InodeFactory.getChildOfClass(c, VirtualLink.class); 209 if (vl.getInode() > 0) 210 c.deleteChild(vl); 211 212 if (cfform.getTrackBackLinkInode() > 0) { 214 vl = VirtualLinkFactory.getVirtualLink(String.valueOf(cfform.getTrackBackLinkInode())); 215 if (vl.getInode() > 0) { 216 c.addChild(vl); 217 c.setTrackBackLinkInode(vl.getInode()); 218 } 219 } 220 221 _applyPermissions(req,c); 222 SessionMessages.add(req, "message", "message.communications.saved"); 224 225 } 226 227 228 public void _deleteCommunication(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 229 throws Exception { 230 231 Communication c = (Communication) req.getAttribute(WebKeys.COMMUNICATION_EDIT); 232 CommunicationsFactory.deleteCommunication(c, user.getUserId()); 233 SessionMessages.add(req, "message", "message.communications.deleted"); 234 235 } 236 237 } 238 | Popular Tags |