1 40 41 package org.jahia.services.fields; 42 43 import java.lang.reflect.Constructor ; 44 import java.lang.reflect.InvocationTargetException ; 45 import java.util.Hashtable ; 46 import java.util.Vector ; 47 48 import org.apache.log4j.Logger; 49 import org.jahia.exceptions.JahiaException; 50 import org.jahia.exceptions.JahiaInitializationException; 51 import org.jahia.registries.ServicesRegistry; 52 import org.jahia.services.acl.JahiaBaseACL; 53 import org.jahia.services.cache.Cache; 54 import org.jahia.services.cache.CacheFactory; 55 import org.jahia.services.usermanager.JahiaUser; 56 import org.jahia.services.version.ContentObjectEntryState; 57 import org.jahia.services.version.StateModificationContext; 58 59 class ContentFieldTools { 60 61 62 final private Logger logger = Logger.getLogger (ContentFieldTools.class); 63 64 65 private static ContentFieldTools instance; 66 67 public static final String CONTENT_FIELD_CACHE = "ContentFieldCache"; 69 70 private static Cache cacheFields; 71 72 private Hashtable fieldClassNames; 73 private Hashtable fieldClassConstructor = new Hashtable (); 74 75 76 79 private ContentFieldTools () { 80 logger.info ("***** Starting up the ContentFieldTools singleton *****"); 81 82 try { 83 cacheFields = CacheFactory.createCache (CONTENT_FIELD_CACHE); 84 85 } catch (JahiaInitializationException e) { 86 logger.warn (e); 87 } 88 89 fieldClassNames = ContentFieldTypes.getInstance ().getFieldClassNames (); 92 } 93 94 99 public static synchronized ContentFieldTools getInstance () { 100 if (instance == null) { 101 instance = new ContentFieldTools (); 102 } 103 104 return instance; 105 } 106 107 116 protected ContentField createField (int siteID, int pageID, int containerID, int fieldDefID, int typeField, 117 int connectType, int parentAclID, int aclID) 118 throws JahiaException { 119 int theFieldID = ServicesRegistry.getInstance ().getJahiaIncrementorsDBService () 121 .autoIncrement ("jahia_fields_data"); 122 123 if (aclID == 0) { 125 JahiaBaseACL acl = new JahiaBaseACL (); 126 if (acl != null) { 127 if (!acl.create (parentAclID)) { 129 String message = "Could not create an ACL object for a new field in container."; 130 logger.debug (message + " -> Stop container creation!"); 131 throw new JahiaException ("AddContainer_Engine", message, 132 JahiaException.ACL_ERROR, JahiaException.CRITICAL_SEVERITY); 133 } else { 134 logger.debug ("ACL [" + acl.getID () + "] has just been created!"); 135 } 136 } else { 137 throw new JahiaException ("ContentFieldBaseService.saveField", "Could not instanciate the JahiaBaseACL class", 138 JahiaException.ACL_ERROR, JahiaException.CRITICAL_SEVERITY); 139 } 140 aclID = acl.getID (); } 143 144 return createContentFieldInstance (theFieldID, siteID, pageID, containerID, 145 fieldDefID, typeField, connectType, 146 aclID, new Vector (), new Hashtable ()); 147 } 148 149 158 protected void preloadActiveOrStagedFieldsByPageID (int pageID) 159 throws JahiaException { 160 ContentFieldDB.getInstance ().preloadActiveOrStagedFieldsByPageID (pageID, cacheFields); 161 } 162 163 171 protected void preloadActiveOrStagedFieldsByContainerID (int containerID) 172 throws JahiaException { 173 ContentFieldDB.getInstance ().preloadActiveOrStagedFieldsByContainerID(containerID, 174 cacheFields); 175 } 176 177 184 public ContentField getField (int fieldID) 185 throws JahiaException { 186 return getField (fieldID, false); 187 } 188 189 195 public void updateCache(ContentField contentField) { 196 cacheFields.put(new Integer (contentField.getID()), contentField); 197 } 198 199 207 public ContentField getField(int fieldID, boolean forceLoadFromDB) throws JahiaException { 208 ContentField cachedField = null; 209 210 if (!forceLoadFromDB) { 211 cachedField = (ContentField) cacheFields.get(new Integer (fieldID)); 212 if (cachedField != null) 213 return cachedField; 214 } 215 cachedField = ContentFieldDB.getInstance().loadContentFieldDB(fieldID); 216 if (cachedField != null) { 217 cacheFields.put(new Integer (fieldID), cachedField); 218 } 219 return cachedField; 220 } 221 222 public ContentField getContentFieldFromCacheOnly (int fieldId) { 223 return (ContentField)cacheFields.get(new Integer (fieldId)); 224 } 225 226 237 public void markFieldForDeletion (ContentField theField, 238 JahiaUser user, 239 Vector activeAndStagingEntryStates, 240 StateModificationContext stateModifContext) 241 throws JahiaException { 242 for (int i = 0; i < activeAndStagingEntryStates.size (); i++) { 243 ContentObjectEntryState entryState = (ContentObjectEntryState) activeAndStagingEntryStates.elementAt ( 244 i); 245 theField.markLanguageForDeletion (user, entryState.getLanguageCode (), 247 stateModifContext); 248 } 249 } 250 251 259 public void purgeFieldData (ContentField theField) 260 throws JahiaException { 261 theField.getACL ().delete (); 263 } 264 265 276 358 359 364 379 380 383 protected ContentField cloneField (int fieldID) 384 throws JahiaException { 385 return null; 386 } 387 388 394 protected ContentField createContentFieldInstance (int ID, 395 int jahiaID, 396 int pageID, 397 int ctnid, 398 int fieldDefID, 399 int fieldType, 400 int connectType, 401 int aclID, 402 Vector activeAndStagingEntryStates, 403 Hashtable activeAndStagedDBValues) 404 throws JahiaException { 405 ContentField theField; 406 407 try { 408 409 Integer fieldTypeInt = new Integer (fieldType); 412 Constructor thisConstructor = null; 413 if (fieldClassConstructor.containsKey (fieldTypeInt)) { 414 thisConstructor = (Constructor ) fieldClassConstructor.get (fieldTypeInt); 415 } else { 416 Class theParams[] = {Integer .class, 418 Integer .class, 419 Integer .class, 420 Integer .class, 421 Integer .class, 422 Integer .class, 423 Integer .class, 424 Integer .class, 425 Vector .class, 426 Hashtable .class}; 427 428 thisConstructor = Class.forName ((String ) fieldClassNames. 429 get (new Integer (fieldType))). 430 getDeclaredConstructor (theParams); 431 fieldClassConstructor.put (fieldTypeInt, thisConstructor); 432 } 433 434 Object args[] = {new Integer (ID), new Integer (jahiaID), 436 new Integer (pageID), new Integer (ctnid), 437 new Integer (fieldDefID), 438 new Integer (fieldType), 439 new Integer (connectType), 440 new Integer (aclID), 441 activeAndStagingEntryStates, 442 activeAndStagedDBValues}; 443 444 theField = (ContentField) thisConstructor.newInstance (args); 446 447 } catch (ClassNotFoundException cnfe) { 448 cnfe.printStackTrace (); 449 throw new JahiaException ("ContentFieldTools.createContentFieldInstance", 450 "Class not found!", 451 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, 452 cnfe.getException ()); 453 454 } catch (NoSuchMethodException nsme) { 455 throw new JahiaException ("JahiaContentBaseService:createContentFieldInstance", 456 "Method not found!", 457 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, nsme); 458 459 } catch (IllegalAccessException iae) { 460 throw new JahiaException ("JahiaContentBaseService:createContentFieldInstance", 461 "Illegal access", 462 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, iae); 463 464 } catch (InvocationTargetException ite) { 465 throw new JahiaException ("JahiaContentBaseService:createContentFieldInstance", 466 "InvocationTarget exception", 467 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, 468 ite.getTargetException ()); 469 470 } catch (InstantiationException ie) { 471 throw new JahiaException ("JahiaContentBaseService:createContentFieldInstance", 472 "Instantiation exception", 473 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, ie); 474 } 475 return theField; 477 } 478 479 486 public void removeFromCache (int fieldID) { 487 cacheFields.remove (new Integer (fieldID)); 488 } 489 490 496 public synchronized void allowFieldsPreloadingForPage(int pageId){ 497 ContentFieldDB.getInstance().allowFieldsPreloadingForPage(pageId); 498 } 499 500 } 501 | Popular Tags |