1 package com.dotmarketing.portlets.containers.action; 2 3 import java.net.URLDecoder ; 4 import java.net.URLEncoder ; 5 import java.util.List ; 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 ; 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.beans.Host; 18 import com.dotmarketing.beans.Identifier; 19 import com.dotmarketing.db.DotHibernate; 20 import com.dotmarketing.factories.HostFactory; 21 import com.dotmarketing.factories.IdentifierFactory; 22 import com.dotmarketing.factories.InodeFactory; 23 import com.dotmarketing.factories.WebAssetFactory; 24 import com.dotmarketing.portal.struts.DotPortletAction; 25 import com.dotmarketing.portal.struts.DotPortletActionInterface; 26 import com.dotmarketing.portlets.containers.factories.ContainerFactory; 27 import com.dotmarketing.portlets.containers.model.Container; 28 import com.dotmarketing.portlets.containers.struts.ContainerForm; 29 import com.dotmarketing.portlets.structure.factories.StructureFactory; 30 import com.dotmarketing.portlets.structure.model.Structure; 31 import com.dotmarketing.portlets.templates.model.Template; 32 import com.dotmarketing.services.ContainerServices; 33 import com.dotmarketing.util.Logger; 34 import com.dotmarketing.util.UtilMethods; 35 import com.dotmarketing.util.Validator; 36 import com.dotmarketing.util.WebKeys; 37 import com.liferay.portal.model.User; 38 import com.liferay.portal.util.Constants; 39 import com.liferay.portlet.ActionRequestImpl; 40 import com.liferay.util.servlet.SessionMessages; 41 42 46 public class EditContainerAction extends DotPortletAction implements 47 DotPortletActionInterface { 48 49 52 public void processAction(ActionMapping mapping, ActionForm form, 53 PortletConfig config, ActionRequest req, ActionResponse res) 54 throws Exception { 55 56 String cmd = req.getParameter(Constants.CMD); 57 String referer = req.getParameter("referer"); 58 59 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 61 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 62 63 if ((referer != null) && (referer.length() != 0)) { 64 referer = URLDecoder.decode(referer, "UTF-8"); 65 } 66 67 Logger.debug(this, "EditContainerAction cmd=" + cmd); 68 69 if (com.dotmarketing.util.Constants.CONTAINER_ADD_VARIABLE.equals(cmd)) { 71 setForward(req, "portlet.ext.containers.add_variables"); 72 return; 73 } 74 75 DotHibernate.startTransaction(); 76 77 User user = _getUser(req); 78 79 try { 80 Logger.debug(this, "Calling Retrieve method"); 81 _retrieveWebAsset(req, res, config, form, user, Container.class, 82 WebKeys.CONTAINER_EDIT); 83 84 } catch (Exception ae) { 85 _handleException(ae, req); 86 return; 87 } 88 89 92 if ((cmd != null) && cmd.equals(Constants.EDIT)) { 93 try { 94 Logger.debug(this, "Calling Edit Method"); 95 _editWebAsset(req, res, config, form, user); 96 97 } catch (Exception ae) { 98 if ((referer != null) && (referer.length() != 0)) { 99 if (ae.getMessage().equals(WebKeys.EDIT_ASSET_EXCEPTION)) { 100 java.util.Map <String , String []> params = new java.util.HashMap <String , String []>(); 103 params.put("struts_action", 104 new String [] { "/ext/director/direct" }); 105 params.put("cmd", new String [] { "editContainer" }); 106 params.put("container", new String [] { req 107 .getParameter("inode") }); 108 params.put("referer", new String [] { URLEncoder.encode( 109 referer, "UTF-8") }); 110 111 String directorURL = com.dotmarketing.util.PortletURLUtil 112 .getActionURL(httpReq, WindowState.MAXIMIZED 113 .toString(), params); 114 115 _sendToReferral(req, res, directorURL); 116 return; 117 } 118 } 119 _handleException(ae, req); 120 return; 121 } 122 } 123 124 129 if ((cmd != null) && cmd.equals(Constants.ADD)) { 130 try { 131 132 if (Validator.validate(req, form, mapping)) { 133 134 Logger.debug(this, "Calling Save Method"); 135 _saveWebAsset(req, res, config, form, user); 136 137 String subcmd = req.getParameter("subcmd"); 138 139 if ((subcmd != null) 140 && subcmd 141 .equals(com.dotmarketing.util.Constants.PUBLISH)) { 142 try { 143 Logger.debug(this, "Calling Publish Method"); 144 _publishWebAsset(req, res, config, form, user, 145 WebKeys.CONTAINER_FORM_EDIT); 146 147 } catch (Exception ae) { 148 _handleException(ae, req); 149 return; 150 } 151 } 152 _sendToReferral(req, res, referer); 153 } 154 155 } catch (Exception ae) { 156 _handleException(ae, req); 157 } 158 159 } 160 165 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 166 try { 167 Logger.debug(this, "Calling Delete Method"); 168 _deleteWebAsset(req, res, config, form, user, 169 WebKeys.CONTAINER_EDIT); 170 171 } catch (Exception ae) { 172 _handleException(ae, req); 173 return; 174 } 175 _sendToReferral(req, res, referer); 176 } 177 182 else if ((cmd != null) 183 && cmd.equals(com.dotmarketing.util.Constants.UNDELETE)) { 184 try { 185 Logger.debug(this, "Calling UnDelete Method"); 186 _undeleteWebAsset(req, res, config, form, user, 187 WebKeys.CONTAINER_EDIT); 188 189 } catch (Exception ae) { 190 _handleException(ae, req); 191 return; 192 } 193 _sendToReferral(req, res, referer); 194 195 } 196 200 else if ((cmd != null) 201 && cmd.equals(com.dotmarketing.util.Constants.DELETEVERSION)) { 202 try { 203 Logger.debug(this, "Calling Delete Version Method"); 204 _deleteVersionWebAsset(req, res, config, form, user, 205 WebKeys.CONTAINER_EDIT); 206 207 } catch (Exception ae) { 208 _handleException(ae, req); 209 return; 210 } 211 _sendToReferral(req, res, referer); 212 } 213 217 else if ((cmd != null) 218 && cmd.equals(com.dotmarketing.util.Constants.UNPUBLISH)) { 219 try { 220 Logger.debug(this, "Calling Unpublish Method"); 221 _unPublishWebAsset(req, res, config, form, user, 222 WebKeys.CONTAINER_EDIT); 223 224 } catch (Exception ae) { 225 _handleException(ae, req); 226 return; 227 } 228 _sendToReferral(req, res, referer); 229 230 } 231 235 else if ((cmd != null) 236 && cmd.equals(com.dotmarketing.util.Constants.GETVERSIONBACK)) { 237 try { 238 Logger.debug(this, "Calling Get Version Back Method"); 239 _getVersionBackWebAsset(req, res, config, form, user); 240 241 } catch (Exception ae) { 242 _handleException(ae, req); 243 return; 244 } 245 _sendToReferral(req, res, referer); 246 } 247 251 else if ((cmd != null) 252 && cmd.equals(com.dotmarketing.util.Constants.ASSETVERSIONS)) { 253 try { 254 Logger.debug(this, "Calling Get Versions Method"); 255 _getVersionsWebAsset(req, res, config, form, user, 256 WebKeys.CONTAINER_EDIT, WebKeys.CONTAINER_VERSIONS); 257 258 } catch (Exception ae) { 259 _handleException(ae, req); 260 return; 261 } 262 } 263 267 else if ((cmd != null) 268 && cmd.equals(com.dotmarketing.util.Constants.UNLOCK)) { 269 try { 270 Logger.debug(this, "Calling Unlock Method"); 271 _unLockWebAsset(req, res, config, form, user, 272 WebKeys.CONTAINER_EDIT); 273 274 } catch (Exception ae) { 275 _handleException(ae, req); 276 return; 277 } 278 _sendToReferral(req, res, referer); 279 280 } 281 285 else if ((cmd != null) 286 && cmd.equals(com.dotmarketing.util.Constants.COPY)) { 287 try { 288 Logger.debug(this, "Calling Copy Method"); 289 _copyWebAsset(req, res, config, form, user); 290 } catch (Exception ae) { 291 _handleException(ae, req); 292 } 293 _sendToReferral(req, res, referer); 294 } 295 299 else if ((cmd != null) 300 && cmd.equals(com.dotmarketing.util.Constants.MOVE)) { 301 try { 302 Logger.debug(this, "Calling Move Method"); 303 _moveWebAsset(req, res, config, form, user, Container.class, 304 WebKeys.CONTAINER_EDIT); 305 } catch (Exception ae) { 306 _handleException(ae, req); 307 } 308 _sendToReferral(req, res, referer); 309 } else 310 Logger.debug(this, "Unspecified Action"); 311 312 DotHibernate.commitTransaction(); 313 setForward(req, "portlet.ext.containers.edit_container"); 314 } 315 316 318 321 @SuppressWarnings ("unchecked") 322 public void _editWebAsset(ActionRequest req, ActionResponse res, 323 PortletConfig config, ActionForm form, User user) throws Exception { 324 325 super._editWebAsset(req, res, config, form, user, WebKeys.CONTAINER_EDIT); 327 328 ContainerForm cf = (ContainerForm) form; 330 331 Container container = (Container) req 333 .getAttribute(WebKeys.CONTAINER_EDIT); 334 335 if (UtilMethods.isSet(container.getLuceneQuery())) { 336 cf.setDynamic(true); 337 } 338 339 req.setAttribute(WebKeys.PERMISSION_INODE_EDIT, container); 341 342 if (cf.getStructureInode() == 0) { 344 Structure currentStructure; 345 if (container.getInode() == 0) { 346 currentStructure = StructureFactory.getDefaultStructure(); 347 } else { 348 currentStructure = (Structure) InodeFactory.getParentOfClass( 349 container, Structure.class); 350 if (currentStructure.getInode() == 0) 351 currentStructure = StructureFactory.getDefaultStructure(); 352 } 353 cf.setStructureInode(currentStructure.getInode()); 354 } 355 356 Host host = HostFactory.getParentHost(container); 358 cf.setHostInode(host.getInode()); 359 360 req.setAttribute(WebKeys.VERSIONS_INODE_EDIT, container); 362 363 } 364 365 368 public void _saveWebAsset(ActionRequest req, ActionResponse res, 369 PortletConfig config, ActionForm form, User user) throws Exception { 370 371 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 373 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 374 375 ContainerForm fm = (ContainerForm) form; 376 377 req.setAttribute(WebKeys.CONTAINER_FORM_EDIT, 379 new com.dotmarketing.portlets.containers.model.Container()); 380 BeanUtils.copyProperties(req.getAttribute(WebKeys.CONTAINER_FORM_EDIT), 381 form); 382 383 Container container = (Container) req 385 .getAttribute(WebKeys.CONTAINER_FORM_EDIT); 386 Container currentContainer = (Container) req 388 .getAttribute(WebKeys.CONTAINER_EDIT); 389 390 _checkPermissions(currentContainer, user, httpReq); 392 393 List currentTemplates = InodeFactory.getChildrenClass(currentContainer, 395 Template.class); 396 397 String userId = user.getUserId(); 399 400 Identifier identifier = IdentifierFactory.getIdentifierByInode(currentContainer); 402 403 if (currentContainer.getInode() > 0) { 405 WebAssetFactory.createAsset(container, userId, identifier, false); 406 container = (Container) WebAssetFactory.saveAsset(container, 407 identifier); 408 } else { 409 WebAssetFactory.createAsset(container, userId); 410 } 411 412 req.setAttribute(WebKeys.CONTAINER_FORM_EDIT, container); 413 414 java.util.Iterator it = currentTemplates.iterator(); 417 418 while (it.hasNext()) { 420 Template parentInode = (Template) it.next(); 421 parentInode.addChild(container); 422 } 423 424 Structure currentStructure = null; 426 if (fm.getStructureInode() == 0) { 427 currentStructure = StructureFactory.getDefaultStructure(); 428 } else { 429 currentStructure = (Structure) InodeFactory.getInode(fm 430 .getStructureInode(), Structure.class); 431 } 432 container.addParent(currentStructure); 433 434 Host host = HostFactory.getHost(fm.getHostInode()); 436 container.addParent(host); 437 438 SessionMessages.add(httpReq, "message", "message.containers.save"); 439 ContainerServices.writeContainerToFile(container, true); 441 442 _applyPermissions(req, container); 444 445 BeanUtils.copyProperties(form, req 447 .getAttribute(WebKeys.CONTAINER_FORM_EDIT)); 448 } 449 450 public void _copyWebAsset(ActionRequest req, ActionResponse res, 451 PortletConfig config, ActionForm form, User user) throws Exception { 452 453 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 455 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 456 457 Logger.debug(this, "I'm copying the Container"); 458 459 Container currentContainer = (Container) req 461 .getAttribute(WebKeys.CONTAINER_EDIT); 462 463 _checkCopyAndMovePermissions(currentContainer, user, httpReq, 465 "copy"); 466 467 ContainerFactory.copyContainer(currentContainer); 469 470 SessionMessages.add(httpReq, "message", "message.containers.copy"); 471 } 472 473 public void _getVersionBackWebAsset(ActionRequest req, ActionResponse res, 474 PortletConfig config, ActionForm form, User user) throws Exception { 475 Container workingContainer = (Container) super._getVersionBackWebAsset( 476 req, res, config, form, user, Container.class, 477 WebKeys.CONTAINER_EDIT); 478 ContainerServices.writeContainerToFile(workingContainer, true); 479 } 480 481 } 482 | Popular Tags |