1 20 package org.jahia.admin.templates; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.List ; 27 import java.util.Vector ; 28 29 import javax.servlet.ServletContext ; 30 import javax.servlet.ServletException ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 import javax.servlet.http.HttpSession ; 34 35 import org.jahia.bin.Jahia; 36 import org.jahia.bin.JahiaAdministration; 37 import org.jahia.data.JahiaData; 38 import org.jahia.data.templates.JahiaTemplatesPackage; 39 import org.jahia.exceptions.JahiaException; 40 import org.jahia.params.ParamBean; 41 import org.jahia.registries.ServicesRegistry; 42 import org.jahia.resourcebundle.JahiaResourceBundle; 43 import org.jahia.services.pages.ContentPage; 44 import org.jahia.services.pages.JahiaPage; 45 import org.jahia.services.pages.JahiaPageBaseService; 46 import org.jahia.services.pages.JahiaPageDefinition; 47 import org.jahia.services.pages.JahiaPageService; 48 import org.jahia.services.pages.JahiaPageTemplateService; 49 import org.jahia.services.sites.JahiaSite; 50 import org.jahia.services.sites.JahiaSiteTools; 51 import org.jahia.services.sites.JahiaSitesService; 52 import org.jahia.services.templates_deployer.JahiaTemplatesDeployerService; 53 import org.jahia.services.usermanager.JahiaUser; 54 import org.jahia.utils.JahiaTools; 55 import org.jahia.utils.properties.PropertiesManager; 56 import org.jahia.security.license.License; 57 58 59 72 public class ManageTemplates 73 { 74 private static org.apache.log4j.Logger logger = 75 org.apache.log4j.Logger.getLogger(ManageTemplates.class); 76 77 private static final String CLASS_NAME = JahiaAdministration.CLASS_NAME; 78 private static final String JSP_PATH = JahiaAdministration.JSP_PATH; 79 80 private static ServletContext context; 81 82 private JahiaSite site; 83 private JahiaUser user; 84 private ServicesRegistry sReg; 85 86 private License coreLicense; 87 88 89 98 public ManageTemplates( HttpServletRequest request, 99 HttpServletResponse response, 100 HttpSession session, 101 ServletContext context ) 102 throws Throwable 103 { 104 ManageTemplates.context = context; 105 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 106 ParamBean jParams = null; 107 if (jData != null) { 108 jParams = jData.params(); 109 } 110 111 coreLicense = Jahia.getCoreLicense(); 112 if ( coreLicense == null ){ 113 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.invalidLicenseKey.label", 115 jParams, jParams.getLocale()); 116 request.setAttribute("jahiaDisplayMessage", dspMsg); 117 JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "menu.jsp" ); 119 return; 120 } 121 122 userRequestDispatcher( request, response, session ); 123 } 125 126 127 135 private void userRequestDispatcher( HttpServletRequest request, 136 HttpServletResponse response, 137 HttpSession session ) 138 throws Throwable 139 { 140 String operation = request.getParameter("sub"); 141 142 sReg = ServicesRegistry.getInstance(); 143 144 user = (JahiaUser) session.getAttribute( ParamBean.SESSION_USER ); 146 site = (JahiaSite) session.getAttribute( ParamBean.SESSION_SITE ); 147 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 148 ParamBean jParams = null; 149 if (jData != null) { 150 jParams = jData.params(); 151 } 152 153 if ( site != null && user != null && sReg != null ){ 154 155 if ( JahiaSiteTools.getAdminGroup(site).isMember(user) ){ 156 157 request.setAttribute( "site", site ); 159 160 if(operation.equals("display")) { 161 displayTemplateList( request, response, session ); 162 } else if(operation.equals("displaynewlist")) { 163 displayNewTemplateList( request, response, session ); 164 } else if(operation.equals("details")) { 165 displayNewTemplateDetail( request, response, session ); 166 } else if(operation.equals("edit")) { 167 editTemplate( request, response, session ); 168 } else if(operation.equals("options")) { 169 editTemplateOption( request, response, session ); 170 } else if(operation.equals("add")) { 171 addTemplate( request, response, session ); 172 } else if(operation.equals("swap")) { 173 processSwap( request, response, session ); 174 } 175 } else { 176 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 178 jParams, jParams.getLocale()); 179 request.setAttribute("jahiaDisplayMessage", dspMsg); 180 JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "menu.jsp" ); 181 } 182 } else { 183 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 184 jParams, jParams.getLocale()); 185 request.setAttribute("jahiaDisplayMessage", dspMsg); 186 JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "menu.jsp" ); 187 } 188 189 } 191 192 193 202 private void displayTemplateList( HttpServletRequest request, 203 HttpServletResponse response, 204 HttpSession session ) 205 throws Throwable 206 207 { 208 209 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 210 ParamBean jParams = null; 211 if (jData != null) { 212 jParams = jData.params(); 213 } 214 try { 215 216 JahiaPageTemplateService pageTempServ = 217 sReg.getJahiaPageTemplateService(); 218 219 if ( pageTempServ == null ){ 220 throw new JahiaException( "Unavailable Services", 221 "Unavailable Services", 222 JahiaException.SERVICE_ERROR, 223 JahiaException.ERROR_SEVERITY ); 224 } 225 226 Enumeration enumeration = pageTempServ.getPageTemplates (site.getID(), false); 227 request.setAttribute("templList", enumeration); 228 229 JahiaAdministration.doRedirect( request, 230 response, 231 session, 232 JSP_PATH + "manage_templates.jsp" ); 233 234 } catch ( JahiaException je ){ 235 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 236 jParams, jParams.getLocale()); 237 request.setAttribute("jahiaDisplayMessage", dspMsg); 238 JahiaAdministration.doRedirect( request, 239 response, 240 session, 241 JSP_PATH + "menu.jsp" ); 242 } 243 244 245 } 247 248 257 private void editTemplate( HttpServletRequest request, 258 HttpServletResponse response, 259 HttpSession session ) 260 throws Throwable 261 { 262 263 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 264 ParamBean jParams = null; 265 if (jData != null) { 266 jParams = jData.params(); 267 } 268 try { 269 270 JahiaPageTemplateService pageTempServ = 271 sReg.getJahiaPageTemplateService(); 272 273 JahiaTemplatesDeployerService tempDepServ = 274 sReg.getJahiaTemplatesDeployerService(); 275 276 JahiaSitesService sitesServ = 277 sReg.getJahiaSitesService(); 278 279 JahiaPageService pageServ = 280 sReg.getJahiaPageService(); 281 282 if ( pageTempServ == null 283 || tempDepServ == null 284 || sitesServ == null 285 || pageServ == null ){ 286 throw new JahiaException( "Unavailable Services", 287 "Unavailable Services", 288 JahiaException.SERVICE_ERROR, 289 JahiaException.ERROR_SEVERITY ); 290 } 291 292 String strVal = request.getParameter("templid"); 294 if ( strVal != null ){ 295 int id = Integer.parseInt(strVal); 296 JahiaPageDefinition templ = pageTempServ.lookupPageTemplate (id); 297 298 request.setAttribute("canDelete", Boolean.TRUE ); 300 301 if ( templ != null ){ 302 303 String subAction = (String ) request.getParameter("subaction"); 304 305 Integer processedSwap = (Integer )request.getAttribute("processedSwap"); 308 if ( processedSwap != null && processedSwap.intValue() == 1 ) { 309 subAction = "confirmdelete"; 310 } 311 312 if ( subAction == null ){ 313 subAction = ""; 314 } 315 316 if ( subAction.equals("delete") ) { 317 String undeploy = (String )request.getParameter("undeploy"); 318 319 if ( undeploy != null ){ 320 tempDepServ.undeploy(templ); 321 } 322 323 pageTempServ.deletePageTemplate(templ.getID()); 324 displayTemplateList(request,response,session); 325 return; 326 327 } else if ( subAction.equals("confirmdelete") ) { 328 329 List criticalPages = new ArrayList (); 330 int mTemplID = templ.getID(); 331 Vector pageIDs = pageServ.getPageIDsWithTemplate(mTemplID); 332 for (int i=0; i<pageIDs.size(); i++) { 333 334 int pageID = ((Integer )pageIDs.get(i)).intValue(); 335 336 try { 337 ContentPage contentPage = ContentPage.getPage(pageID,false); 338 criticalPages.add(contentPage); 339 } catch (Throwable t) { 340 logger.debug("Exception when loading pages using template[" + mTemplID + "]",t); 341 } 342 } 343 if (!criticalPages.isEmpty()) { 344 request.setAttribute("processedSwap", null); 345 displaySwap (request, response, session, criticalPages, templ); 346 return; 347 } 348 349 request.setAttribute("currAction", "confirmdelete"); 350 351 } else if ( subAction.equals("swap") ) { 352 353 processSwap (request, response, session); 355 356 } else if ( subAction.equals("save") ) { 357 358 String templName = (String )request.getParameter("templName"); 359 String visible_status = (String )request.getParameter("visible_status"); 360 361 if (templName != null && (templName.trim().length()>0) ){ 362 templ.setName(templName); 363 } 364 if ( visible_status != null ){ 365 templ.setAvailable(true); 366 String isDefault = (String )request.getParameter("isDefault"); 367 if ( isDefault != null ){ 368 site.setDefaultTemplateID(templ.getID()); 369 } else { 370 site.setDefaultTemplateID(-1); 371 } 372 } else { 373 templ.setAvailable(false); 374 } 375 376 templ.commitChanges(); 377 378 sitesServ.updateSite(site); 379 380 templ = pageTempServ.lookupPageTemplate (id); 381 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.templateUpdated.label", 382 jParams, jParams.getLocale()); 383 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 384 } 385 386 } else { 387 String errMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.errMsg.noTemplateDefinition.label", 388 jParams, jParams.getLocale()); 389 request.setAttribute("errMsg", errMsg); 390 } 391 392 int pageid = site.getHomePageID(); 393 JahiaPage page = pageServ.lookupPage(pageid); 394 395 if ( page != null && (page.getPageTemplateID() == templ.getID()) ){ 396 request.setAttribute("canDelete", Boolean.FALSE ); 397 } 398 399 request.setAttribute("templ", templ); 400 401 } 402 403 request.setAttribute("templatesContext",getTemplatesContext(session)); 404 405 JahiaAdministration.doRedirect( request, 406 response, 407 session, 408 JSP_PATH + "template_edit.jsp" ); 409 410 } catch ( JahiaException je ){ 411 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 412 jParams, jParams.getLocale()); 413 request.setAttribute("jahiaDisplayMessage", dspMsg); 414 JahiaAdministration.doRedirect( request, 415 response, 416 session, 417 JSP_PATH + "menu.jsp" ); 418 } 419 420 } 421 422 431 private void editTemplateOption( HttpServletRequest request, 432 HttpServletResponse response, 433 HttpSession session ) 434 throws IOException , ServletException 435 { 436 437 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 438 ParamBean jParams = null; 439 if (jData != null) { 440 jParams = jData.params(); 441 } 442 try { 443 444 JahiaSitesService sitesServ = 445 sReg.getJahiaSitesService(); 446 447 448 if ( sitesServ == null ){ 449 throw new JahiaException( "Unavailable Services", 450 "Unavailable Services", 451 JahiaException.SERVICE_ERROR, 452 JahiaException.ERROR_SEVERITY ); 453 } 454 455 int autoDeploy = 0; 456 457 if ( site.getTemplatesAutoDeployMode() ){ 458 autoDeploy = 1; 459 } 460 461 request.setAttribute("autoDeploy", new Integer (autoDeploy)); 462 463 String subAction =(String )request.getParameter("subaction"); 465 if ( subAction != null && subAction.equals("save") ) { 466 467 String strVal = (String )request.getParameter("autoDeploy"); 468 int intVal = 0; 469 470 if ( strVal != null ){ 471 intVal = 1; 472 } 473 474 if ( intVal != autoDeploy ){ 475 try { 476 site.setTemplatesAutoDeployMode(intVal==1); 477 sitesServ.updateSite(site); 478 479 session.setAttribute( ParamBean.SESSION_SITE , site ); 480 481 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.changeUpdated.label", 482 jParams, jParams.getLocale()); 483 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 484 485 } catch ( JahiaException je ) { 486 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.changeNotUpdated.label", 487 jParams, jParams.getLocale()); 488 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 489 } 490 491 request.setAttribute("autoDeploy", new Integer (intVal)); 492 } 493 } 494 495 JahiaAdministration.doRedirect( request, 496 response, 497 session, 498 JSP_PATH + "template_option.jsp" ); 499 } catch ( JahiaException je ){ 500 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 501 jParams, jParams.getLocale()); 502 request.setAttribute("jahiaDisplayMessage", dspMsg); 503 JahiaAdministration.doRedirect( request, 504 response, 505 session, 506 JSP_PATH + "menu.jsp" ); 507 } 508 509 510 } 512 513 522 private void displayNewTemplateList( HttpServletRequest request, 523 HttpServletResponse response, 524 HttpSession session ) 525 throws IOException , ServletException 526 { 527 528 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 529 ParamBean jParams = null; 530 if (jData != null) { 531 jParams = jData.params(); 532 } 533 try { 534 535 JahiaTemplatesDeployerService tempDepServ = 536 sReg.getJahiaTemplatesDeployerService(); 537 538 539 if ( tempDepServ == null ){ 540 throw new JahiaException( "Unavailable Services", 541 "Unavailable Services", 542 JahiaException.SERVICE_ERROR, 543 JahiaException.ERROR_SEVERITY ); 544 } 545 546 Enumeration newTemplatesKeys = tempDepServ.getTemplatesPackageKeys(site.getSiteKey()); 548 Enumeration enumeration = tempDepServ.getTemplatesPackages(site.getSiteKey()); 549 Vector vec = new Vector (); 550 551 JahiaTemplatesPackage aPackage = null; 552 553 while (enumeration.hasMoreElements()){ 554 555 aPackage = (JahiaTemplatesPackage)enumeration.nextElement(); 556 557 if ( aPackage != null ){ 558 vec.add(aPackage); 559 } else { 560 } 562 } 563 564 request.setAttribute("packagesList", vec.elements()); 565 JahiaAdministration.doRedirect( request, 566 response, 567 session, 568 JSP_PATH + "new_templates.jsp" ); 569 } catch ( JahiaException je ){ 570 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 571 jParams, jParams.getLocale()); 572 request.setAttribute("jahiaDisplayMessage", dspMsg); 573 JahiaAdministration.doRedirect( request, 574 response, 575 session, 576 JSP_PATH + "menu.jsp" ); 577 } 578 579 } 580 581 582 583 592 private void displayNewTemplateDetail( HttpServletRequest request, 593 HttpServletResponse response, 594 HttpSession session ) 595 throws IOException , ServletException 596 { 597 598 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 599 ParamBean jParams = null; 600 if (jData != null) { 601 jParams = jData.params(); 602 } 603 try { 604 605 JahiaTemplatesDeployerService tempDepServ = 606 sReg.getJahiaTemplatesDeployerService(); 607 608 JahiaPageTemplateService pageTempServ = 609 sReg.getJahiaPageTemplateService(); 610 611 612 if ( tempDepServ == null || pageTempServ == null ){ 613 throw new JahiaException( "Unavailable Services", 614 "Unavailable Services", 615 JahiaException.SERVICE_ERROR, 616 JahiaException.ERROR_SEVERITY ); 617 } 618 619 String packageName = (String )request.getParameter("package_name"); 621 JahiaTemplatesPackage aPackage = 622 (JahiaTemplatesPackage)tempDepServ.getTemplatesPackage(site.getSiteKey() 623 + "_" 624 + packageName); 625 626 int nbTemplates = pageTempServ.getNbPageTemplates(site.getID()); 628 629 boolean canDeploy = ( ( Jahia.getTemplateLimit() == -1 ) 630 || !(nbTemplates + aPackage.getTemplates().size() > Jahia.getTemplateLimit()) ); 631 632 request.setAttribute("templateLimit", new Integer (Jahia.getTemplateLimit()) ); 633 request.setAttribute("canDeploy", new Boolean (canDeploy) ); 634 635 636 String subAction =(String )request.getParameter("subaction"); 637 if ( subAction == null ){ 638 request.setAttribute("aPackage", aPackage); 639 } else if ( subAction.equals("deploy") && canDeploy){ 640 try { 641 if (tempDepServ.deploy( site, 642 aPackage.getRootFolder(), 643 aPackage.getFilePath(),true)){ 644 645 tempDepServ.registerTemplates(site, aPackage); 647 648 tempDepServ.deletePackage(site,aPackage.getFilePath()); 650 651 displayNewTemplateList(request,response,session); 652 return; 653 } else { 654 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.deployingPackageError.label", 655 jParams, jParams.getLocale()); 656 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 657 request.setAttribute("aPackage", aPackage); 658 } 659 } catch ( JahiaException je ){ 660 request.setAttribute("aPackage", aPackage); 661 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.deployingPackageError.label", 662 jParams, jParams.getLocale()); 663 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 664 } 665 } else if ( subAction.equals("delete") ){ 666 667 try { 668 if (tempDepServ.deletePackage(site,aPackage.getFilePath())){ 669 displayNewTemplateList(request,response,session); 670 return; 671 } else { 672 request.setAttribute("aPackage", aPackage); 673 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.deletingPackageError.label", 674 jParams, jParams.getLocale()); 675 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 676 } 677 } catch ( IOException ioe ){ 678 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.deletingPackageError.label", 679 jParams, jParams.getLocale()); 680 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 681 } 682 } 683 684 JahiaAdministration.doRedirect( request, 685 response, 686 session, 687 JSP_PATH + "new_template_detail.jsp" ); 688 } catch ( JahiaException je ){ 689 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 690 jParams, jParams.getLocale()); 691 request.setAttribute("jahiaDisplayMessage", dspMsg); 692 JahiaAdministration.doRedirect( request, 693 response, 694 session, 695 JSP_PATH + "menu.jsp" ); 696 } 697 698 } 699 700 701 710 private void addTemplate( HttpServletRequest request, 711 HttpServletResponse response, 712 HttpSession session ) 713 714 throws IOException , ServletException 715 { 716 717 718 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 719 ParamBean jParams = null; 720 if (jData != null) { 721 jParams = jData.params(); 722 } 723 try { 724 725 JahiaTemplatesDeployerService tempDepServ = 726 sReg.getJahiaTemplatesDeployerService(); 727 728 JahiaPageTemplateService pageTempServ = 729 sReg.getJahiaPageTemplateService(); 730 731 if ( tempDepServ == null || pageTempServ == null ){ 732 throw new JahiaException( "Unavailable Services", 733 "Unavailable Services", 734 JahiaException.SERVICE_ERROR, 735 JahiaException.ERROR_SEVERITY ); 736 } 737 738 int nbTemplates = pageTempServ.getNbPageTemplates(site.getID()); 740 741 boolean canAddNew = ( (Jahia.getTemplateLimit() == -1) 742 || (nbTemplates < Jahia.getTemplateLimit()) ); 743 744 request.setAttribute("templateLimit", new Integer (Jahia.getTemplateLimit()) ); 745 request.setAttribute("canAddNew", new Boolean (canAddNew) ); 746 747 748 PropertiesManager properties = new PropertiesManager( Jahia.getJahiaPropertiesFileName() ); 749 750 request.setAttribute("warningMsg",""); 751 752 JahiaSite site = (JahiaSite)request.getAttribute("site"); 753 754 String subAction = (String )request.getParameter("subaction"); 755 String templName = (String )request.getParameter("templName"); 756 String rootFolder = (String )request.getParameter("rootFolder"); 757 if ( rootFolder == null ){ 758 rootFolder = File.separator; 759 } 760 761 String fileName = (String )request.getParameter("fileName"); 762 String isAvailable = (String )request.getParameter("isAvailable"); 763 764 request.setAttribute("templName",JahiaTools.replaceNullString(templName,"")); 765 request.setAttribute("rootFolder",JahiaTools.replaceNullString(rootFolder,"")); 766 request.setAttribute("fileName",JahiaTools.replaceNullString(fileName,"")); 767 768 if ( isAvailable != null ){ 769 request.setAttribute("isAvailable",new Integer (1)); 770 } else { 771 request.setAttribute("isAvailable",new Integer (0)); 772 } 773 774 if ( subAction != null && subAction.equals("save") && canAddNew ){ 775 if ( templName == null || (templName.length()<=0) 776 || fileName == null || (fileName.length()<=0) ) { 777 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.warningMsg.completeRequestInfo.label", 778 jParams, jParams.getLocale()); 779 request.setAttribute("warningMsg", dspMsg); 780 } else { 781 782 String path = ManageTemplates.context.getRealPath(properties.getProperty("jahiaTemplatesDiskPath") ); 784 785 while ( rootFolder.startsWith("/") 786 || rootFolder.startsWith("\\") 787 || rootFolder.startsWith(".") ){ 788 rootFolder = rootFolder.substring(1,rootFolder.length()); 789 } 790 791 while ( rootFolder.endsWith("/") 792 || rootFolder.endsWith("\\") ){ 793 rootFolder = rootFolder.substring(0,rootFolder.length()-1); 794 } 795 796 StringBuffer tempFullPath = new StringBuffer (1024); 798 tempFullPath.append(path); 799 tempFullPath.append(File.separator); 800 tempFullPath.append(site.getSiteKey()); 801 tempFullPath.append(File.separator); 802 tempFullPath.append(rootFolder); 803 tempFullPath.append(File.separator) ; 804 tempFullPath.append(fileName) ; 805 806 if ( !JahiaTools.checkFileNameCaseSensitive(tempFullPath.toString()) ){ 807 String warningMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.warningMsg.noFile.label", 808 jParams, jParams.getLocale()); 809 warningMsg += " " + fileName + " "; 810 warningMsg += JahiaResourceBundle.getAdminResource("org.jahia.admin.warningMsg.inFolder.label", 811 jParams, jParams.getLocale()); 812 warningMsg += " " + rootFolder; 813 request.setAttribute("warningMsg", warningMsg); 814 } else if ( rootFolder.startsWith(".") ){ 815 String warningMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.warningMsg.invalidTemplateFolder.label", 816 jParams, jParams.getLocale()); 817 request.setAttribute("warningMsg",warningMsg); 818 } else { 819 StringBuffer buff = new StringBuffer (1024); 820 buff.append(properties.getProperty("jahiaTemplatesDiskPath")); 821 buff.append(site.getSiteKey()); 822 buff.append("/") ; 823 if ( !rootFolder.equals("") ){ 824 buff.append(rootFolder); 825 buff.append("/") ; 826 } 827 buff.append(fileName) ; 828 String sourcePath = JahiaTools.replacePattern(buff.toString(),"\\","/"); 829 830 JahiaPageDefinition def = pageTempServ.getPageTemplateBySourcePath(site.getID(),sourcePath); 831 if ( def == null || !def.getSourcePath().equals(sourcePath) ){ 832 833 pageTempServ.createPageTemplate ( 834 835 site.getID(), 836 templName, 837 sourcePath, 838 (isAvailable != null), 839 "", site.getAclID() ); 841 842 String warningMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.warningMsg.template.label", 843 jParams, jParams.getLocale()); 844 warningMsg += " " + fileName + " "; 845 warningMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.warningMsg.templateAdded.label", 846 jParams, jParams.getLocale()); 847 request.setAttribute("warningMsg",warningMsg); 848 } else { 849 request.setAttribute("warningMsg", 850 "This template is already registered in Jahia"); 851 } 852 } 853 } 854 855 } 856 857 JahiaAdministration.doRedirect(request,response,session,JSP_PATH + "template_add.jsp"); 858 859 } catch ( JahiaException je ){ 860 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label", 861 jParams, jParams.getLocale()); 862 request.setAttribute("jahiaDisplayMessage", dspMsg); 863 JahiaAdministration.doRedirect( request, 864 response, 865 session, 866 JSP_PATH + "menu.jsp" ); 867 } 868 869 870 } 872 873 874 875 876 protected String getTemplatesContext(HttpSession session){ 877 878 String templatesContext = null; 879 880 templatesContext = (String )session.getAttribute(CLASS_NAME + "jahiaTemplatesDiskPath"); 882 if ( templatesContext == null ){ 883 884 PropertiesManager props = new PropertiesManager( Jahia.getJahiaPropertiesFileName() ); 885 886 if ( props!= null ){ 887 888 templatesContext = (String )props.getProperty("jahiaTemplatesDiskPath"); 889 890 if ( templatesContext != null ){ 891 session.setAttribute(CLASS_NAME + "jahiaTemplatesDiskPath", templatesContext); 892 } 893 } 894 } 895 896 return templatesContext; 897 } 898 899 900 914 private void displaySwap( HttpServletRequest request, 915 HttpServletResponse response, 916 HttpSession session, 917 List criticalPages, 918 JahiaPageDefinition templ ) 919 throws IOException , ServletException 920 { 921 try { 922 923 JahiaPageBaseService pageService = JahiaPageBaseService.getInstance(); 925 boolean canDeleteTemplate = true; 926 int currentVersion = ServicesRegistry.getInstance() 927 .getJahiaVersionService().getCurrentVersionID(); 928 929 Vector allPagesInfosVector = new Vector (); 931 Vector allTemplatesVector = new Vector (); 932 for (int i=0; i<criticalPages.size(); i++) { 933 ContentPage contentPage = (ContentPage) criticalPages.get(i); 934 if ( canDeleteTemplate 935 && (contentPage.hasActiveEntries() || 936 contentPage.hasArchiveEntryState(currentVersion) ) ){ 937 canDeleteTemplate = false; 938 } 939 allPagesInfosVector.add (contentPage); 940 } 941 942 Integer homePageID = new Integer ( site.getHomePageID() ); 944 945 Integer basePageID = (Integer ) request.getAttribute(CLASS_NAME + "basePageID"); 947 Integer baseTemplateID = (Integer ) request.getAttribute(CLASS_NAME + "baseTemplateID"); 948 949 Enumeration completeTemplatesEnumeration = ServicesRegistry.getInstance().getJahiaPageTemplateService().getPageTemplates (site.getID(), true); 951 952 while (completeTemplatesEnumeration.hasMoreElements()) { 954 JahiaPageDefinition template = (JahiaPageDefinition) completeTemplatesEnumeration.nextElement(); 955 if(template.getID() != templ.getID()) { 956 allTemplatesVector.add (template); 957 } 958 } 959 Enumeration allTemplatesEnumeration = allTemplatesVector.elements(); 960 961 if(basePageID == null) { basePageID = new Integer (0); } 963 if(baseTemplateID == null) { baseTemplateID = new Integer (0); } 964 if(homePageID == null) { homePageID = new Integer (0); } 965 966 request.setAttribute("canDeleteTemplate", new Boolean (canDeleteTemplate)); 968 request.setAttribute("homePageID", homePageID); 969 request.setAttribute("basePageID", basePageID); 970 request.setAttribute("baseTemplateID", baseTemplateID); 971 request.setAttribute("allTemplatesEnumeration", allTemplatesEnumeration); 972 request.setAttribute("allPagesInfosEnumeration", allPagesInfosVector.elements()); 973 request.setAttribute("totalCriticalPages", new Integer (criticalPages.size())); 974 request.setAttribute("templ", templ); 975 976 JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "template_cleanpages.jsp" ); 977 978 JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData"); 980 ParamBean jParams = null; 981 if (jData != null) { 982 jParams = jData.params (); 983 } 984 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", 985 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright", 986 jParams, jParams.getLocale())); 987 } catch (JahiaException je) { 988 } 989 } 991 992 1002 protected void processSwap( HttpServletRequest request, 1003 HttpServletResponse response, 1004 HttpSession session ) 1005 throws Throwable 1006 { 1007 int idPage = 0; 1008 int idTemplate = 0; 1009 String idPageString = ""; 1010 String idTemplateString = (String ) request.getParameter("templateid").trim(); 1011 String [] pageIDs = request.getParameterValues( "pageids" ); 1012 List errors = new ArrayList (); 1013 JahiaPage page = null; 1014 JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData"); 1015 ParamBean jParams = null; 1016 if (jData != null) { 1017 jParams = jData.params(); 1018 } 1019 1020 boolean valid = true; 1021 1022 1023 if ((pageIDs == null) || (pageIDs.length < 1)) { 1025 valid = false; 1026 errors.add("Please choose at least one page."); 1027 } 1028 1029 if (idTemplateString.equals("0")) { 1030 valid = false; 1031 errors.add("Please choose a template."); 1032 } 1033 1034 if (!valid) { 1035 request.setAttribute("manageTemplatesErrors", errors); 1036 idTemplate = 0; 1037 1038 } else { 1039 1040 try { 1041 1042 idTemplate = Integer.parseInt(idTemplateString); 1043 1044 } catch (NumberFormatException nfe) { 1045 String dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.invalidTemplateReference.label", 1047 jParams, jParams.getLocale()); 1048 session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg); 1049 } 1050 1051 } 1052 1053 if (pageIDs != null) { 1055 1056 String titlePage = ""; 1057 1058 for (int i = 0; i < pageIDs.length; i++) { 1059 idPageString = pageIDs[i]; 1060 1061 try { 1062 idPage = Integer.parseInt(idPageString); 1063 1064 if (!swapTemplate(idPage, idTemplate)) { 1065 1066 titlePage = sReg.getJahiaPageService().lookupPage(idPage).getTitle(); 1067 errors.add("Couldn't change templates for page "+ titlePage); 1068 } 1069 } catch (Exception e) { 1070 } 1072 } 1073 } 1074 request.setAttribute("processedSwap", new Integer (1)); 1075 editTemplate (request, response, session); 1076 1077 1078 } 1080 1081 1082 1090 private boolean swapTemplate( int idPage, int idTemplate ) 1091 { 1092 boolean out = false; 1093 JahiaPage page = null; 1094 try { 1095 1096 JahiaPageService pageServ = ServicesRegistry.getInstance() 1097 .getJahiaPageService(); 1098 1099 if ( idPage != 0 && idTemplate != 0 ){ 1100 page = pageServ.lookupPage(idPage); 1101 1102 if ( page != null ){ 1103 page.setPageTemplateID (idTemplate); 1105 page.commitChanges (true); 1106 out = true; 1108 } 1109 1110 } 1111 1112 } catch ( JahiaException je ){ 1113 } 1115 1116 return out; 1117 1118 } 1120 1121 1122} | Popular Tags |