1 40 41 42 package org.jahia.services.pages; 43 44 import java.util.Enumeration ; 45 import java.util.TreeMap ; 46 import java.util.Vector ; 47 48 import org.apache.log4j.Logger; 49 import org.jahia.data.JahiaDOMObject; 50 import org.jahia.exceptions.JahiaException; 51 import org.jahia.exceptions.JahiaInitializationException; 52 import org.jahia.exceptions.JahiaTemplateNotFoundException; 53 import org.jahia.registries.ServicesRegistry; 54 import org.jahia.services.acl.ACLNotFoundException; 55 import org.jahia.services.acl.ACLResource; 56 import org.jahia.services.acl.JahiaACLEntry; 57 import org.jahia.services.acl.JahiaBaseACL; 58 import org.jahia.services.cache.Cache; 59 import org.jahia.services.cache.CacheFactory; 60 import org.jahia.services.cache.CacheListener; 61 import org.jahia.services.sites.JahiaSite; 62 import org.jahia.services.usermanager.JahiaGroup; 63 import org.jahia.services.usermanager.JahiaGroupManagerService; 64 import org.jahia.services.usermanager.JahiaUser; 65 import org.jahia.settings.SettingsBean; 66 67 68 72 public class JahiaPageTemplateBaseService extends JahiaPageTemplateService 73 implements CacheListener { 74 75 76 private static final Logger logger = Logger.getLogger (JahiaPageTemplateBaseService.class); 77 78 79 private static JahiaPageTemplateBaseService instance = null; 80 81 public static final String PAGE_TEMPLATE_CACHE = "PageTemplateCache"; 83 84 private static Cache templateCache = null; 85 86 private JahiaPageTemplateDB mTemplateDB = null; 87 88 89 92 protected JahiaPageTemplateBaseService () { 93 } 94 95 96 112 public JahiaPageDefinition createPageTemplate ( 113 int siteID, 114 String name, 115 String sourcePath, 116 boolean isAvailable, 117 String image, 118 int parentAclID) throws JahiaException { 119 122 JahiaGroupManagerService grpService; 123 if (ServicesRegistry.getInstance () != null) { 124 grpService = ServicesRegistry.getInstance ().getJahiaGroupManagerService (); 125 126 if (grpService == null) { 127 logger.warn ( 128 "Could not access to the Group Manager Service instance. Stopping page template creation."); 129 throw new JahiaException ("Could not access the Group Manager Service", 130 "JahiaGroupManagerService.getInstance() returned null!!!", 131 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 132 } 133 134 } else { 135 logger.warn ( 136 "Could not access to the Service Registry instance. Stopping page template creation."); 137 throw new JahiaException ("Could not access the Service Registry", 138 "ServicesRegistry.getInstance() returned null!!!", 139 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 140 141 } 142 143 checkService (); 145 146 150 int newID = mTemplateDB.getNextID(); 152 153 JahiaBaseACL acl = new JahiaBaseACL (); 155 if (acl != null) { 156 try { 157 acl.create (parentAclID); 158 } catch (ACLNotFoundException ex) { 159 throw new JahiaException ("Could not create the page def.", 160 "The parent ACL ID [" + parentAclID + "] could not be found," + 161 " while trying to create a new page def.", 162 JahiaException.TEMPLATE_ERROR, JahiaException.ERROR_SEVERITY); 163 } 164 } else { 165 throw new JahiaException ("Could not create page def.", 166 "Could not instanciate a new ACL object while trying to create a new page def.", 167 JahiaException.TEMPLATE_ERROR, JahiaException.CRITICAL_SEVERITY); 168 } 169 170 JahiaPageDefinition template = new JahiaPageDefinition (newID, siteID, 172 name, sourcePath, isAvailable, image); 173 174 if (template == null) { 175 throw new JahiaException ("Could not create page template", 176 "Could not instanciate a new JahiaPageDefinition object.", 177 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 178 } 179 180 template.setACL (acl.getID ()); 181 182 if (!mTemplateDB.insertPageTemplate (template)) { 184 throw new JahiaException ("Could not create page template", 185 "Could not insert the page template into the database.", 186 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 187 } 188 189 190 JahiaACLEntry aclEntry = new JahiaACLEntry (1, 0); 193 JahiaGroup guestGroup = grpService.getGuestGroup (siteID); 194 acl.setGroupEntry (guestGroup, aclEntry); 195 196 templateCache.put (new Integer (template.getID ()), template); 198 return template; 199 } 200 201 202 210 public synchronized void deletePageTemplate (int defID) 211 throws JahiaException { 212 checkService (); 214 215 JahiaPageDefinition pageTemplate = lookupPageTemplate (defID); 216 217 JahiaBaseACL pageTemplateACL = pageTemplate.getACL (); 218 pageTemplateACL.delete (); 219 220 mTemplateDB.deletePageTemplate (defID); 221 222 templateCache.remove (new Integer (defID)); 224 225 } 226 227 228 235 public Vector getAllPageTemplateIDs () 236 throws JahiaException { 237 checkService (); 238 return mTemplateDB.getAllPageTemplateIDs (); 239 } 240 241 242 245 public static synchronized JahiaPageTemplateBaseService getInstance () { 246 if (instance == null) { 247 instance = new JahiaPageTemplateBaseService (); 248 } 249 return instance; 250 } 251 252 253 public JahiaPageDefinition getPageTemplateBySourcePath (int siteID, String path) 254 throws JahiaException, JahiaTemplateNotFoundException { 255 checkService (); 257 258 JahiaPageDefinition template = null; 259 int templateID = mTemplateDB.getPageTemplateIDMatchingSourcePath (siteID, path); 260 261 if (templateID != -1) { 262 template = lookupPageTemplate (templateID); 263 } 264 return template; 265 } 266 267 268 282 public Enumeration getPageTemplates (JahiaUser user, int siteID, boolean availableOnly) 283 throws JahiaException { 284 checkService (); 286 287 288 TreeMap tm = new TreeMap (); 289 Object [] templates = templateCache.keys (); 290 for (int i = 0; i < templates.length; i++) { 291 JahiaPageDefinition template = (JahiaPageDefinition) templateCache.get ( 292 templates[i]); 293 if (template.getJahiaID () == siteID && 294 (!availableOnly || template.isAvailable ()) && 295 ACLResource.checkReadAccess (template, user)) { 296 tm.put (template.getName (), template); 297 } 298 } 299 Vector theList = new Vector (tm.values ()); 300 return theList.elements (); 301 } 302 303 304 317 public Enumeration getPageTemplates (int siteID, boolean availableOnly) 318 throws JahiaException { 319 checkService (); 321 322 TreeMap tm = new TreeMap (); 323 Object [] templates = templateCache.keys (); 324 for (int i = 0; i < templates.length; i++) { 325 JahiaPageDefinition template = (JahiaPageDefinition) templateCache.get ( 326 templates[i]); 327 328 if (template.getJahiaID () == siteID) { 329 if (!availableOnly || template.isAvailable ()) { 330 tm.put (template.getName (), template); 331 } 332 } 333 } 334 335 Vector theList = new Vector (tm.values ()); 336 return theList.elements (); 337 } 338 339 340 345 public synchronized void init (SettingsBean settings) 346 throws JahiaInitializationException { 347 logger.debug ("** Initializing the Page Template Service ..."); 348 if (!isInitialized ()) { 350 351 logger.debug (" - Instanciate the page template cache ..."); 353 templateCache = CacheFactory.createCache (PAGE_TEMPLATE_CACHE); 354 if (templateCache != null) 355 templateCache.registerListener (this); 356 357 358 logger.debug (" - Instanciate the database page template tools ..."); 360 mTemplateDB = JahiaPageTemplateDB.getInstance (); 361 362 363 if ((templateCache != null) && (mTemplateDB != null)) { 365 366 mIsServiceInitialized = true; 367 logger.debug ("** Page Template Service successfully initialized!"); 368 369 } else { 370 mTemplateDB = null; 372 templateCache = null; 373 374 throw new JahiaInitializationException ( 376 "Page Template Service could not be initialized successfully."); 377 } 378 379 try { 380 loadAllPageTemplates (); 381 } catch (JahiaException je) { 382 throw new JahiaInitializationException ( 383 "Error while loading all page templates from database", je); 384 } 385 } 386 } 387 388 389 private synchronized void loadAllPageTemplates () 390 throws JahiaException { 391 Vector templateIDs = mTemplateDB.getAllPageTemplateIDs (); 392 for (int i = 0; i < templateIDs.size (); i++) { 393 int templateID = ((Integer ) templateIDs.elementAt (i)).intValue (); 394 395 try { 398 JahiaPageDefinition template = lookupPageTemplate (templateID); 399 if (template != null) { 400 templateCache.put (new Integer (template.getID ()), template); 401 } 402 403 } catch (JahiaTemplateNotFoundException ex) { 404 logger.warn(ex); 405 408 } 411 } 412 } 413 414 415 public JahiaPageDefinition lookupPageTemplate (int templateID) 416 throws JahiaException, JahiaTemplateNotFoundException { 417 checkService (); 419 420 JahiaPageDefinition template = 422 (JahiaPageDefinition) templateCache.get (new Integer (templateID)); 423 424 if (template == null) { 427 template = mTemplateDB.loadPageTemplate (templateID); 428 429 if (template != null) { 432 templateCache.put (new Integer (template.getID ()), template); 433 434 } else { 435 throw new JahiaTemplateNotFoundException (templateID); 436 } 437 } 438 return template; 439 } 440 441 442 public JahiaPageDefinition lookupPageTemplateByName (String name, int siteID) 443 throws JahiaException, 444 JahiaTemplateNotFoundException { 445 checkService (); 447 448 Object [] templates = templateCache.keys (); 449 for (int i = 0; i < templates.length; i++) { 450 451 JahiaPageDefinition template = 452 (JahiaPageDefinition) templateCache.get (templates[i]); 453 454 if ((template.getName ().equals (name)) && 455 (template.getJahiaID () == siteID)) { 456 return template; 457 } 458 } 459 throw new JahiaTemplateNotFoundException (name); 460 } 461 462 463 public synchronized void shutdown () { 464 473 if (isInitialized ()) { 474 templateCache.flush (); 475 476 mIsServiceInitialized = false; 478 } 479 } 480 481 482 public int getNbPageTemplates () 486 throws JahiaException { 487 return mTemplateDB.getNbPageTemplates (-1); 488 } 489 490 491 public int getNbPageTemplates (int siteID) 495 throws JahiaException { 496 return mTemplateDB.getNbPageTemplates (siteID); 497 } 498 499 500 505 public JahiaDOMObject getPageDefsAsDOM (int siteID) throws JahiaException { 506 507 return JahiaPageTemplateDB.getInstance ().getPageDefsAsDOM (siteID); 508 509 } 510 511 516 public JahiaDOMObject getPageDefPropsAsDOM (int siteID) throws JahiaException { 517 518 return JahiaPageDefinitionPropDB.getPageDefPropsAsDOM (siteID); 519 520 } 521 522 528 public Vector getAclIDs (int siteID) 529 throws JahiaException { 530 return JahiaPageDefinitionPropDB.db_get_all_acl_id (siteID); 531 } 532 533 public final void patchTemplateWithoutACL () throws JahiaException { 537 538 JahiaSite site = null; 539 JahiaPageDefinition template = null; 540 541 Object [] ids = templateCache.keys (); 542 Integer I = null; 543 for (int i = 0; i < ids.length; i++) { 544 I = (Integer ) ids[i]; 545 template = (JahiaPageDefinition) templateCache.get (I); 546 547 if (template.getAclID () == -1) { 548 site = 549 ServicesRegistry.getInstance ().getJahiaSitesService ().getSite ( 550 template.getJahiaID ()); 551 if (site != null) { 552 JahiaBaseACL acl = new JahiaBaseACL (); 554 if (acl != null) { 555 try { 556 acl.create (site.getAclID ()); 557 558 template.setACL (acl.getID ()); 559 560 JahiaACLEntry aclEntry = new JahiaACLEntry (1, 0); 563 JahiaGroup guestGroup = ServicesRegistry.getInstance () 564 .getJahiaGroupManagerService () 565 .getGuestGroup (site.getID ()); 566 acl.setGroupEntry (guestGroup, aclEntry); 567 568 template.commitChanges (); 569 logger.debug ("Patch : ACL [" + template.getAclID () + 570 "] has been created for the template :" + 571 template.getName ()); 572 573 } catch (ACLNotFoundException ex) { 574 logger.warn (ex); 575 throw new JahiaException ("Could not patch ACL for the page def.", 576 "The parent ACL ID [" + site.getAclID () + 577 "] could not be found," + 578 " while trying to patch ACL ( create a new one ) for page def [" + 579 template.getID () + "]", 580 JahiaException.TEMPLATE_ERROR, 581 JahiaException.ERROR_SEVERITY); 582 } 583 } else { 584 logger.debug ("Could not patch ACL for page def."); 585 } 586 } 587 } 588 } 589 } 590 591 593 594 599 public void onCacheFlush(String cacheName) { 600 try { 601 if (PAGE_TEMPLATE_CACHE.equals(cacheName)) { 602 logger.debug("Page template cache has been flushed, reload the page templates!"); 603 templateCache.flush(false); 604 loadAllPageTemplates(); 605 } 606 607 } catch (JahiaException ex) { 608 logger.warn (ex); 609 } 610 } 611 612 public void onCachePut(String cacheName, Object entryKey) { 613 } 615 616 621 public void updatePageTemplate(JahiaPageDefinition thePageTemplate) 622 throws JahiaException { 623 mTemplateDB.updatePageTemplate(thePageTemplate); 624 templateCache.put(new Integer (thePageTemplate.getID()), thePageTemplate); 625 } 626 627 } 628 629 630 631 632 | Popular Tags |