1 package org.jahia.services.homepages; 18 19 import org.jahia.data.JahiaDOMObject; 20 import org.jahia.exceptions.JahiaException; 21 import org.jahia.exceptions.JahiaInitializationException; 22 import org.jahia.services.acl.JahiaBaseACL; 23 import org.jahia.services.sites.JahiaSite; 24 import org.jahia.settings.SettingsBean; 25 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.InvocationTargetException ; 28 import java.util.Collections ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.Vector ; 32 33 34 39 public class JahiaHomepagesBaseService extends JahiaHomepagesService { 40 41 private static org.apache.log4j.Logger logger = 42 org.apache.log4j.Logger.getLogger (JahiaHomepagesBaseService.class); 43 44 private static final String CLASS_NAME = JahiaHomepagesBaseService.class.getName (); 45 46 private static JahiaHomepagesBaseService instance = null; 47 48 private JahiaHomepagesPersistance hppServ; 49 50 51 private Hashtable vCaches = null; 52 53 54 protected JahiaHomepagesBaseService () throws JahiaException { 55 logger.info (" ***** " + this.getClass ().getName () + " started *****"); 56 vCaches = new Hashtable (); 57 } 58 59 60 public static synchronized JahiaHomepagesBaseService getInstance () 61 throws JahiaException { 62 63 if (instance == null) { 64 instance = new JahiaHomepagesBaseService (); 65 } 66 return instance; 67 } 68 69 73 public void init (SettingsBean jSettings) 74 throws JahiaInitializationException { 75 if (!isInitialized ()) { 76 try { 77 loadHomepagesInCache (); 78 mIsServiceInitialized = true; 79 } catch (Throwable t) { 80 t.printStackTrace (); 81 throw new JahiaInitializationException (CLASS_NAME + ".init: Init error"); 82 } 83 } 84 } 85 86 92 public Enumeration getHomepages () throws JahiaException { 93 94 Vector v = new Vector (); 95 Enumeration keys = vCaches.keys (); 96 Hashtable hash = null; 97 while (keys.hasMoreElements ()) { 98 hash = (Hashtable ) vCaches.get (keys.nextElement ()); 99 v.addAll (hash.values ()); 100 } 101 102 JahiaHomepage hp = createHomepage ("", 103 "", 104 "", 105 HomepageTypes.HOMEPAGE_LINK, 106 null, 107 -1 108 ); 109 Collections.sort (v, hp); 110 111 return v.elements (); 112 } 113 114 120 public Enumeration getHomepages (JahiaSite site) throws JahiaException { 121 122 Vector v = new Vector (); 123 if (site == null) { 124 return v.elements (); 125 } 126 Hashtable hash = (Hashtable ) vCaches.get (site.getSiteKey ()); 127 128 if (hash == null) { 129 return v.elements (); 130 } 131 v.addAll (hash.values ()); 132 133 JahiaHomepage hp = createHomepage ("", 134 "", 135 "", 136 HomepageTypes.HOMEPAGE_LINK, 137 null, 138 -1 139 ); 140 Collections.sort (v, hp); 141 142 return v.elements (); 143 } 144 145 153 public JahiaHomepage getHomepage (int id) throws JahiaException { 154 155 157 Vector v = new Vector (); 158 Enumeration keys = vCaches.keys (); 159 Enumeration hpKeys = null; 160 Hashtable hash = null; 161 JahiaHomepage hp = null; 162 while (keys.hasMoreElements ()) { 163 hash = (Hashtable ) vCaches.get (keys.nextElement ()); 164 hp = (JahiaHomepage) hash.get (new Integer (id)); 165 if (hp != null) { 166 return hp; 167 } 168 } 169 hp = JahiaHomepagesPersistance.getInstance ().load (id); 171 if (hp != null) { 172 hash = (Hashtable ) vCaches.get (hp.getSiteKey ()); 173 if (hash == null) { 174 hash = new Hashtable (); 176 vCaches.put (hp.getSiteKey (), hash); 177 } 178 hash.put (new Integer (id), hp); 179 } 180 return hp; 181 } 182 183 196 public JahiaHomepage createHomepage (String name, 197 String descr, 198 String siteKey, 199 int type, 200 Hashtable props, 201 int aclID 202 ) throws JahiaException { 203 204 JahiaHomepage hp; 205 206 try { 207 Class theParams[] = {Class.forName ("java.lang.Integer"), 209 Class.forName ("java.lang.String"), 210 Class.forName ("java.lang.String"), 211 Class.forName ("java.lang.Integer"), 212 Class.forName ("java.lang.String"), 213 Class.forName ("java.util.Hashtable"), 214 Class.forName ("java.lang.Integer")}; 215 216 Constructor thisConstructor = 219 Class.forName ((String ) HomepageTypes.getInstance () 220 .getClassesNames ().get (new Integer (type))) 221 .getDeclaredConstructor (theParams); 222 223 224 Object args[] = {new Integer (-1), name, 227 descr, 228 new Integer (type), 229 siteKey, 230 props, 231 new Integer (aclID)}; 232 233 234 hp = (JahiaHomepage) thisConstructor.newInstance (args); 236 237 } catch (ClassNotFoundException ex) { 238 logger.error (ex); 239 throw new JahiaException (CLASS_NAME + ".createHomepage", 240 "Class not found!", 241 JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY); 242 243 } catch (NoSuchMethodException ex) { 244 logger.error (ex); 245 throw new JahiaException (CLASS_NAME + ".createHomepage", 246 "Method not found!", 247 JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY); 248 249 } catch (IllegalAccessException ex) { 250 logger.error (ex); 251 throw new JahiaException (CLASS_NAME + ".createHomepage", 252 "Illegal Access!", 253 JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY); 254 255 } catch (InvocationTargetException ex) { 256 logger.error (ex); 257 throw new JahiaException (CLASS_NAME + ".createHomepage", 258 "InvocationTarget exception", 259 JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY); 260 261 } catch (InstantiationException ex) { 262 logger.error (ex); 263 throw new JahiaException (CLASS_NAME + ".createHomepage", 264 "Instantiation exception", 265 JahiaException.ERROR_SEVERITY, JahiaException.ERROR_SEVERITY); 266 267 } 268 269 return hp; 271 272 } 273 274 280 public void deleteHomepage (int id) 281 throws JahiaException { 282 283 285 JahiaHomepage hp = getHomepage (id); 286 if (hp == null) 287 return; 288 289 Hashtable hash = null; 290 hash = (Hashtable ) vCaches.get (hp.getSiteKey ()); 291 if (hash != null) { 292 hash.remove (new Integer (hp.getID ())); 293 } 294 hp.delete (); 295 } 296 297 305 public void saveHomepage (JahiaHomepage hp, int parentAclID) 306 throws JahiaException { 307 308 310 if (hp == null) 311 return; 312 313 if (hp.getAclID () <= 0) { 315 JahiaBaseACL acl = new JahiaBaseACL (); 316 if (acl != null) { 317 if (!acl.create (parentAclID)) { 319 String message = "Could not create an ACL object for a new homepage."; 320 logger.debug (message); 321 throw new JahiaException (CLASS_NAME + ".saveHomepage", 322 message, 323 JahiaException.ACL_ERROR, 324 JahiaException.CRITICAL_SEVERITY); 325 } else { 326 logger.debug ("ACL [" + acl.getID () + "] has just been created!"); 327 } 328 } else { 329 throw new JahiaException (CLASS_NAME + ".saveHomepage", 330 "Could not instanciate the JahiaBaseACL class", 331 JahiaException.ACL_ERROR, JahiaException.CRITICAL_SEVERITY); 332 } 333 hp.setAclID (acl.getID ()); 335 } 336 337 hp.save (); 338 339 Hashtable hash = null; 340 hash = (Hashtable ) vCaches.get (hp.getSiteKey ()); 341 if (hash == null) { 342 hash = new Hashtable (); 344 vCaches.put (hp.getSiteKey (), hash); 345 } 346 hash.put (new Integer (hp.getID ()), hp); 347 } 348 349 353 private void loadHomepagesInCache () throws JahiaException { 354 355 if (vCaches == null) 356 vCaches = new Hashtable (); 357 358 Vector v = JahiaHomepagesPersistance.getInstance ().getHomepages (); 359 if (v == null) 360 return; 361 362 JahiaHomepage hp = null; 363 int size = v.size (); 364 Hashtable hash = null; 365 for (int i = 0; i < size; i++) { 366 hp = (JahiaHomepage) v.get (i); 367 hash = (Hashtable ) vCaches.get (hp.getSiteKey ()); 368 if (hash == null) { 369 hash = new Hashtable (); 371 vCaches.put (hp.getSiteKey (), hash); 372 } 373 hash.put (new Integer (hp.getID ()), hp); 374 } 375 } 376 377 383 public JahiaDOMObject getHomepageDefsAsDOM (String siteKey) 384 throws JahiaException { 385 386 return JahiaHomepagesPersistance.getInstance ().getHomepageDefsAsDOM (siteKey); 387 388 } 389 390 396 public JahiaDOMObject getHomepageDefPropsAsDOM (String siteKey) 397 throws JahiaException { 398 399 return JahiaHomepagesPersistance.getInstance ().getHomepageDefPropsAsDOM (siteKey); 400 401 } 402 403 410 public Vector getAclIDs (String siteKey) 411 throws JahiaException { 412 return JahiaHomepagesPersistance.getInstance ().db_get_all_acl_id (siteKey); 413 } 414 415 } 416 | Popular Tags |