1 package com.dotmarketing.portlets.templates.action; 2 3 import java.net.URLDecoder ; 4 import java.net.URLEncoder ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 8 import javax.portlet.ActionRequest; 9 import javax.portlet.ActionResponse; 10 import javax.portlet.PortletConfig; 11 import javax.portlet.WindowState; 12 import javax.servlet.http.HttpServletRequest ; 13 14 import org.apache.commons.beanutils.BeanUtils; 15 import org.apache.struts.action.ActionForm; 16 import org.apache.struts.action.ActionMapping; 17 18 import com.dotmarketing.beans.Host; 19 import com.dotmarketing.beans.Identifier; 20 import com.dotmarketing.beans.Inode; 21 import com.dotmarketing.db.DotHibernate; 22 import com.dotmarketing.factories.HostFactory; 23 import com.dotmarketing.factories.IdentifierFactory; 24 import com.dotmarketing.factories.InodeFactory; 25 import com.dotmarketing.factories.WebAssetFactory; 26 import com.dotmarketing.portal.struts.DotPortletAction; 27 import com.dotmarketing.portal.struts.DotPortletActionInterface; 28 import com.dotmarketing.portlets.files.model.File; 29 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 30 import com.dotmarketing.portlets.templates.factories.TemplateFactory; 31 import com.dotmarketing.portlets.templates.model.Template; 32 import com.dotmarketing.portlets.templates.struts.TemplateForm; 33 import com.dotmarketing.services.PageServices; 34 import com.dotmarketing.services.TemplateServices; 35 import com.dotmarketing.util.Config; 36 import com.dotmarketing.util.Logger; 37 import com.dotmarketing.util.PortletURLUtil; 38 import com.dotmarketing.util.Validator; 39 import com.dotmarketing.util.WebKeys; 40 import com.liferay.portal.model.User; 41 import com.liferay.portal.util.Constants; 42 import com.liferay.portlet.ActionRequestImpl; 43 import com.liferay.util.servlet.SessionMessages; 44 45 48 49 public class EditTemplateAction extends DotPortletAction implements 50 DotPortletActionInterface { 51 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, "EditTemplateAction cmd=" + cmd); 68 69 if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.TEMPLATE_ADD_CONTAINER)) { 70 Logger.debug(this, "I'm popping up the Template selector"); 71 setForward(req, "portlet.ext.templates.container_selector"); 72 return; 73 } 74 75 if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.TEMPLATE_ADD_FILE)) { 77 Logger.debug(this, "I'm popping up the file selector"); 78 setForward(req, "portlet.ext.templates.file_selector"); 79 return; 80 } 81 82 DotHibernate.startTransaction(); 83 84 User user = _getUser(req); 85 86 try { 87 Logger.debug(this, "Calling Retrieve method"); 88 _retrieveWebAsset(req, res, config, form, user, Template.class, 89 WebKeys.TEMPLATE_EDIT); 90 91 } catch (Exception ae) { 92 _handleException(ae, req); 93 return; 94 } 95 96 99 if ((cmd != null) && cmd.equals(Constants.EDIT)) { 100 try { 101 Logger.debug(this, "Calling Edit method"); 102 _editWebAsset(req, res, config, form, user); 103 104 } catch (Exception ae) { 105 if ((referer != null) && (referer.length() != 0)) { 106 if (ae.getMessage().equals(WebKeys.EDIT_ASSET_EXCEPTION)) { 107 java.util.Map <String ,String []> params = new java.util.HashMap <String ,String []>(); 110 params.put("struts_action",new String [] { "/ext/director/direct" }); 111 params.put("cmd", new String [] { "editTemplate" }); 112 params.put("template", new String [] { req.getParameter("inode") }); 113 params.put("referer", new String [] { URLEncoder.encode(referer, "UTF-8") }); 114 115 String directorURL = com.dotmarketing.util.PortletURLUtil.getActionURL(httpReq, WindowState.MAXIMIZED.toString(), params); 116 117 _sendToReferral(req, res, directorURL); 118 return; 119 } 120 } 121 _handleException(ae, req); 122 } 123 } 124 125 130 if ((cmd != null) && cmd.equals(Constants.ADD)) { 131 try { 132 133 if (Validator.validate(req, form, mapping)) { 134 135 Logger.debug(this, "Calling Save method"); 136 _saveWebAsset(req, res, config, form, user); 137 138 String subcmd = req.getParameter("subcmd"); 139 140 if ((subcmd != null) 141 && subcmd 142 .equals(com.dotmarketing.util.Constants.PUBLISH)) { 143 Logger.debug(this, "Calling Publish method"); 144 _publishWebAsset(req, res, config, form, user, 145 WebKeys.TEMPLATE_FORM_EDIT); 146 } 148 149 java.util.Map params = new java.util.HashMap (); 150 params.put("struts_action",new String [] {"/ext/templates/view_templates"}); 151 referer = PortletURLUtil.getActionURL(req,WindowState.MAXIMIZED.toString(),params); 152 _sendToReferral(req, res, referer); 153 return; 154 } 155 156 } catch (Exception ae) { 157 _handleException(ae, req); 158 } 159 160 } 161 166 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 167 try { 168 Logger.debug(this, "Calling Delete method"); 169 _deleteWebAsset(req, res, config, form, user, 170 WebKeys.TEMPLATE_EDIT); 171 172 } catch (Exception ae) { 173 _handleException(ae, req); 174 return; 175 } 176 _sendToReferral(req, res, referer); 177 } 178 183 else if ((cmd != null) 184 && cmd.equals(com.dotmarketing.util.Constants.UNDELETE)) { 185 try { 186 Logger.debug(this, "Calling UnDelete method"); 187 _undeleteWebAsset(req, res, config, form, user, 188 WebKeys.TEMPLATE_EDIT); 189 190 } catch (Exception ae) { 191 _handleException(ae, req); 192 return; 193 } 194 _sendToReferral(req, res, referer); 195 196 } 197 201 else if ((cmd != null) 202 && cmd.equals(com.dotmarketing.util.Constants.DELETEVERSION)) { 203 try { 204 Logger.debug(this, "Calling Delete Version Method"); 205 _deleteVersionWebAsset(req, res, config, form, user, 206 WebKeys.TEMPLATE_EDIT); 207 208 } catch (Exception ae) { 209 _handleException(ae, req); 210 return; 211 } 212 _sendToReferral(req, res, referer); 213 } 214 218 else if ((cmd != null) 219 && cmd.equals(com.dotmarketing.util.Constants.UNPUBLISH)) { 220 try { 221 Logger.debug(this, "Calling Unpublish Method"); 222 _unPublishWebAsset(req, res, config, form, user, 223 WebKeys.TEMPLATE_EDIT); 224 225 } catch (Exception ae) { 226 _handleException(ae, req); 227 return; 228 } 229 _sendToReferral(req, res, referer); 230 231 } 232 236 else if ((cmd != null) 237 && cmd.equals(com.dotmarketing.util.Constants.GETVERSIONBACK)) { 238 try { 239 Logger.debug(this, "Calling Get Version Back Method"); 240 _getVersionBackWebAsset(req, res, config, form, user); 241 242 } catch (Exception ae) { 243 _handleException(ae, req); 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.TEMPLATE_EDIT, WebKeys.TEMPLATE_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.TEMPLATE_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 } else 295 Logger.debug(this, "Unspecified Action"); 296 297 DotHibernate.commitTransaction(); 298 setForward(req, "portlet.ext.templates.edit_template"); 299 } 300 301 303 @SuppressWarnings ("unchecked") 304 public void _editWebAsset(ActionRequest req, ActionResponse res, 305 PortletConfig config, ActionForm form, User user) throws Exception { 306 307 super._editWebAsset(req, res, config, form, user, 309 WebKeys.TEMPLATE_EDIT); 310 311 Template template = (Template) req.getAttribute(WebKeys.TEMPLATE_EDIT); 313 314 Identifier imageIdentifier = (Identifier) InodeFactory.getInode(template.getImage(), Identifier.class); 316 File imageFile = (File) IdentifierFactory.getWorkingChildOfClass(imageIdentifier, File.class); 317 318 Host host = HostFactory.getParentHost(template); 320 321 TemplateForm cf = (TemplateForm) form; 323 cf.setImage(imageFile.getInode()); 324 cf.setSelectedimage(imageFile.getFileName()); 325 cf.setHostInode(host.getInode()); 326 327 } 328 329 public void _publishHTMLPages(ActionRequest req, ActionResponse res, 330 PortletConfig config, ActionForm form) throws Exception { 331 332 User user = _getUser(req); 333 334 Template template = (Template) req.getAttribute(WebKeys.TEMPLATE_EDIT); 336 337 String condition = "live=" + com.dotmarketing.db.DbConnectionFactory.getDBTrue() + " and deleted=" 338 + com.dotmarketing.db.DbConnectionFactory.getDBFalse(); 339 List pagesForThisTemplate = InodeFactory.getChildrenClassByCondition( 340 template, HTMLPage.class, condition); 341 342 Iterator iterPages = pagesForThisTemplate.iterator(); 343 while (iterPages.hasNext()) { 345 HTMLPage page = (HTMLPage) iterPages.next(); 346 try { 347 _checkUserPermissions(page, user, Config.getIntProperty("PERMISSION_PUBLISH")); 348 PageServices.publishPageToFile(page); 350 } catch (Exception e) { } 351 } 352 } 353 354 @SuppressWarnings ("unchecked") 355 public void _saveWebAsset(ActionRequest req, ActionResponse res, 356 PortletConfig config, ActionForm form, User user) throws Exception { 357 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 359 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 360 361 TemplateForm cf = (TemplateForm) form; 363 364 req.setAttribute(WebKeys.TEMPLATE_FORM_EDIT, new Template()); 366 BeanUtils.copyProperties(req.getAttribute(WebKeys.TEMPLATE_FORM_EDIT), 367 form); 368 369 Template template = (Template) req 371 .getAttribute(WebKeys.TEMPLATE_FORM_EDIT); 372 373 Template currentTemplate = (Template) req 375 .getAttribute(WebKeys.TEMPLATE_EDIT); 376 377 _checkPermissions(currentTemplate, user, httpReq); 379 380 java.util.List <HTMLPage> htmlPageChildren = InodeFactory.getChildrenClass( 382 currentTemplate, HTMLPage.class); 383 384 String userId = user.getUserId(); 386 387 Identifier identifier = IdentifierFactory.getIdentifierByInode(currentTemplate); 389 390 if (currentTemplate.getInode() > 0) { 392 WebAssetFactory.createAsset(template, userId, identifier, false); 393 template = (Template) WebAssetFactory.saveAsset(template, identifier); 394 } else { 395 WebAssetFactory.createAsset(template, userId); 396 } 397 398 req.setAttribute(WebKeys.TEMPLATE_FORM_EDIT, template); 399 400 File imageFile = (File) InodeFactory.getInode(cf.getImage(), File.class); 402 Identifier imageIdentifier = IdentifierFactory.getParentIdentifier(imageFile); 403 if (imageIdentifier!=null) { 404 template.setImage(imageIdentifier.getInode()); 405 } 406 407 java.util.Set <HTMLPage> templateChildrenSet = new java.util.HashSet <HTMLPage>(); 409 410 templateChildrenSet.addAll(htmlPageChildren); 412 for(HTMLPage html : templateChildrenSet ){ 414 template.addChild(html); 415 } 416 417 String containerTag = "#parse( $container"; 419 String body = template.getBody(); 420 int idx1 = 0; 421 int idx2 = 0; 422 String containerId = ""; 423 while ((idx2 = body.indexOf(containerTag, idx1)) != -1) { 424 idx1 = body.indexOf(")", idx2 + containerTag.length()); 425 containerId = body 426 .substring(idx2 + containerTag.length(), idx1 - 1); 427 Identifier containerIdentifier = (Identifier) InodeFactory.getInode( 428 containerId, Identifier.class); 429 template.addChild(containerIdentifier); 430 } 431 432 identifier = IdentifierFactory.getIdentifierByInode(template); 433 434 Host host = HostFactory.getHost(cf.getHostInode()); 436 template.addParent(host); 437 identifier.setHostInode(host.getInode()); 438 InodeFactory.saveInode(identifier); 439 440 TemplateServices.writeTemplateToFile(template, true); 441 SessionMessages.add(httpReq, "message", "message.template.save"); 442 443 _applyPermissions(req, template); 445 446 BeanUtils.copyProperties(form, req 448 .getAttribute(WebKeys.TEMPLATE_FORM_EDIT)); 449 BeanUtils.copyProperties(currentTemplate, req 450 .getAttribute(WebKeys.TEMPLATE_FORM_EDIT)); 451 DotHibernate.flush(); 452 } 453 454 public void _copyWebAsset(ActionRequest req, ActionResponse res, 455 PortletConfig config, ActionForm form, User user) throws Exception { 456 457 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 459 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 460 461 Logger.debug(this, "I'm copying the Template"); 462 463 Template currentTemplate = (Template) req 465 .getAttribute(WebKeys.TEMPLATE_EDIT); 466 467 _checkCopyAndMovePermissions(currentTemplate, user, httpReq,"copy"); 469 470 TemplateFactory.copyTemplate(currentTemplate); 472 473 SessionMessages.add(httpReq, "message", "message.template.copy"); 474 } 475 476 @SuppressWarnings ("unchecked") 477 public void _getVersionBackWebAsset(ActionRequest req, ActionResponse res, 478 PortletConfig config, ActionForm form, User user) throws Exception { 479 480 Template versionTemplate = (Template) InodeFactory.getInode(req 481 .getParameter("inode_version"), Template.class); 482 483 Identifier id = (Identifier)IdentifierFactory.getParentIdentifier(versionTemplate); 484 485 Template workingTemplate = (Template)IdentifierFactory.getWorkingChildOfClass(id, Template.class); 486 487 java.util.List <HTMLPage> htmlPageChildren = InodeFactory.getChildrenClass( 489 workingTemplate, HTMLPage.class); 490 491 java.util.List <Identifier> containersChildren = (java.util.List <Identifier>)InodeFactory.getChildrenClass( 493 workingTemplate, Identifier.class); 494 495 java.util.Set <Inode> templateChildrenSet = new java.util.HashSet <Inode>(); 497 498 templateChildrenSet.addAll(htmlPageChildren); 500 templateChildrenSet.addAll(containersChildren); 501 502 for(Inode inode : templateChildrenSet ){ 505 versionTemplate.addChild(inode); 506 } 507 508 509 510 Template newWorkingTemplate = (Template) super._getVersionBackWebAsset(req, res, config, form, user, Template.class, WebKeys.TEMPLATE_EDIT); 511 TemplateServices.writeTemplateToFile(newWorkingTemplate, true); 512 } 513 514 } | Popular Tags |