1 package com.dotmarketing.portlets.virtuallinks.action; 2 3 import javax.portlet.ActionRequest; 4 import javax.portlet.ActionResponse; 5 import javax.portlet.PortletConfig; 6 7 import org.apache.commons.beanutils.BeanUtils; 8 import org.apache.struts.action.ActionForm; 9 import org.apache.struts.action.ActionMapping; 10 11 import com.dotmarketing.beans.Host; 12 import com.dotmarketing.beans.Identifier; 13 import com.dotmarketing.cache.VirtualLinksCache; 14 import com.dotmarketing.factories.HostFactory; 15 import com.dotmarketing.factories.IdentifierFactory; 16 import com.dotmarketing.factories.InodeFactory; 17 import com.dotmarketing.portal.struts.DotPortletAction; 18 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 19 import com.dotmarketing.portlets.virtuallinks.factories.VirtualLinkFactory; 20 import com.dotmarketing.portlets.virtuallinks.model.VirtualLink; 21 import com.dotmarketing.portlets.virtuallinks.struts.VirtualLinkForm; 22 import com.dotmarketing.util.Logger; 23 import com.dotmarketing.util.UtilMethods; 24 import com.dotmarketing.util.Validator; 25 import com.dotmarketing.util.WebKeys; 26 import com.liferay.portal.model.User; 27 import com.liferay.portal.util.Constants; 28 import com.liferay.util.servlet.SessionMessages; 29 32 33 public class EditVirtualLinkAction extends DotPortletAction { 34 35 public void processAction( 36 ActionMapping mapping, ActionForm form, PortletConfig config, 37 ActionRequest req, ActionResponse res) 38 throws Exception { 39 40 String cmd = req.getParameter(Constants.CMD); 41 Logger.debug(this, "Inside EditVirtualLinkAction cmd=" + cmd); 42 43 User user = _getUser(req); 45 46 50 try { 51 Logger.debug(this, "I'm retrieving the list"); 52 _retrieveVirtualLink(req, res, config, form); 53 } 54 catch (Exception ae) { 55 _handleException(ae, req); 56 } 57 58 62 if ((cmd != null) && cmd.equals(Constants.ADD)) { 63 try { 64 65 if (Validator.validate(req,form,mapping)) { 66 Logger.debug(this, "I'm Saving the virtual link"); 67 _saveVirtualLink(req, res, config, form, user); 68 _sendToReferral(req, res, ""); 69 } 70 } 71 catch (Exception ae) { 72 if (!ae.getMessage().equals(WebKeys.UNIQUE_VIRTUAL_LINK_EXCEPTION)){ 73 _handleException(ae, req); 74 } 75 } 76 } 77 78 82 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 83 try { 84 Logger.debug(this, "I'm deleting the virtual link"); 85 _deleteVirtualLink(req, res, config, form,user); 86 87 } 88 catch (Exception ae) { 89 _handleException(ae, req); 90 } 91 _sendToReferral(req, res, ""); 92 } 93 94 98 if ((cmd != null) && cmd.equals(Constants.EDIT)) { 99 VirtualLink vl = (VirtualLink) req.getAttribute(WebKeys.VIRTUAL_LINK_EDIT); 100 BeanUtils.copyProperties(form, req.getAttribute(WebKeys.VIRTUAL_LINK_EDIT)); 101 String linkUrl = vl.getUrl(); 102 if (linkUrl != null) { 103 String [] splittedLink = linkUrl.split(":"); 104 if (splittedLink.length > 1) { 105 Host host = HostFactory.getHostByHostName(splittedLink[0]); 106 ((VirtualLinkForm)form).setUrl(splittedLink[1]); 107 ((VirtualLinkForm)form).setHostId(host.getInode()); 108 } 109 } 110 } 111 112 116 setForward(req, "portlet.ext.virtuallinks.edit_virtuallink"); 117 } 118 119 120 private void _retrieveVirtualLink(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form) 121 throws Exception { 122 123 VirtualLink ml = (VirtualLink) InodeFactory.getInode(req.getParameter("inode"),VirtualLink.class); 124 Logger.debug(this, "ML:" + ml.getInode()); 125 req.setAttribute(WebKeys.VIRTUAL_LINK_EDIT, ml); 126 127 } 128 129 private void _saveVirtualLink(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 130 throws Exception { 131 132 VirtualLinkForm mlForm = (VirtualLinkForm) form; 133 134 String completeUrl = null; 135 if (mlForm.getHostId() > 0) { 136 Host host = HostFactory.getHost(mlForm.getHostId()); 137 completeUrl = host.getHostname() + ":" + mlForm.getUrl(); 138 } else { 139 completeUrl = mlForm.getUrl(); 140 } 141 142 if (completeUrl.trim().endsWith("/")) { 143 completeUrl = completeUrl.trim().substring(0, completeUrl.trim().length() - 1); 144 } 145 146 VirtualLink vl = VirtualLinkFactory.getVirtualLinkByURL(completeUrl); 147 148 if (vl.getInode()>0 && mlForm.getInode() != vl.getInode()) { 149 SessionMessages.add(req,"message", "message.virtuallink.uniquelink.save"); 151 throw new Exception (WebKeys.UNIQUE_VIRTUAL_LINK_EXCEPTION); 152 153 } 154 else { 155 156 VirtualLink ml = (VirtualLink) req.getAttribute(WebKeys.VIRTUAL_LINK_EDIT); 157 if (UtilMethods.isSet(ml.getUrl())) { 159 VirtualLinksCache.removePathFromCache(ml.getUrl()); 160 } 161 162 BeanUtils.copyProperties(req.getAttribute(WebKeys.VIRTUAL_LINK_EDIT),form); 163 164 ml = (VirtualLink) req.getAttribute(WebKeys.VIRTUAL_LINK_EDIT); 165 166 ml.setUrl(completeUrl); 167 168 long htmlPageInode = mlForm.getHtmlInode(); 169 if (htmlPageInode>0) { 170 HTMLPage htmlPage = (HTMLPage) InodeFactory.getInode(htmlPageInode+"",HTMLPage.class); 172 Identifier identifier = IdentifierFactory.getParentIdentifier(htmlPage); 173 ml.setUri(identifier.getURI()); 174 } 175 176 InodeFactory.saveInode(ml); 177 SessionMessages.add(req,"message", "message.virtuallink.save"); 178 179 VirtualLinksCache.addPathToCache(ml); 181 182 } 184 BeanUtils.copyProperties(form,req.getAttribute(WebKeys.VIRTUAL_LINK_EDIT)); 186 } 187 188 private void _deleteVirtualLink(ActionRequest req, ActionResponse res,PortletConfig config, ActionForm form , User user) 189 throws Exception { 190 191 VirtualLink vl = (VirtualLink) req.getAttribute(WebKeys.VIRTUAL_LINK_EDIT); 192 193 if (UtilMethods.isSet(vl.getUrl())) { 195 VirtualLinksCache.removePathFromCache(vl.getUrl()); 196 } 197 198 InodeFactory.deleteInode(vl); 199 SessionMessages.add(req, "message", "message.virtuallink.delete"); 201 202 } 203 204 205 } 206 | Popular Tags |