1 13 29 package org.jahia.data.containers; 30 31 import java.io.Serializable ; 32 import java.util.Enumeration ; 33 import java.util.Hashtable ; 34 import java.util.Locale ; 35 import java.util.Properties ; 36 import java.util.Vector ; 37 38 import org.apache.log4j.Logger; 39 import org.jahia.content.ContainerDefinitionKey; 40 import org.jahia.content.ContentDefinition; 41 import org.jahia.content.ContentObject; 42 import org.jahia.data.fields.JahiaFieldDefinition; 43 import org.jahia.exceptions.JahiaException; 44 import org.jahia.resourcebundle.ResourceBundleMarker; 45 import org.jahia.services.containers.ContentContainer; 46 import org.jahia.services.containers.ContentContainerList; 47 import org.jahia.services.pages.ContentPage; 48 import org.jahia.services.version.ContentObjectEntryState; 49 import org.jahia.services.version.EntryLoadRequest; 50 import org.jahia.utils.LanguageCodeConverters; 51 52 64 65 public class JahiaContainerDefinition extends ContentDefinition implements Serializable { 66 67 private static Logger logger = Logger.getLogger(JahiaFieldDefinition.class); 68 69 static { 70 ContentDefinition.registerType(ContainerDefinitionKey.CONTAINER_TYPE, 71 JahiaContainerDefinition.class.getName()); 72 } 73 74 private int ID; 75 private int jahiaID; 76 private String name; 77 78 private ContainerEditView editView; 80 private Properties ctnDefProperties = null; 81 82 85 private Hashtable subDefs; 86 87 88 89 94 public JahiaContainerDefinition( int anID, 95 int aJahiaID, 96 String aName, 97 Hashtable subDefinitions ) 98 { 99 super(new ContainerDefinitionKey(anID)); 100 this.ID = anID; 101 this.jahiaID = aJahiaID; 102 this.name = aName; 103 this.subDefs = subDefinitions; 104 } 106 109 protected JahiaContainerDefinition () { 110 111 } 112 113 118 public int getID() { return ID; } 119 public int getJahiaID() { return jahiaID; } 120 public String getName() { return name; } 121 public Hashtable getSubDefs() { return subDefs; } 122 123 public void setID( int anID ) { 124 this.ID = anID; 125 setObjectKey(new ContainerDefinitionKey(anID)); 126 } 127 128 public void setName( String aName ) { this.name = aName; } 129 130 public static ContentDefinition getChildInstance(String IDInType) { 131 132 try { 133 return org.jahia.registries.JahiaContainerDefinitionsRegistry 134 .getInstance().getDefinition(Integer.parseInt(IDInType)); 135 136 } catch (JahiaException je) { 137 logger.debug("Error retrieving ContentDefinition instance for id : " + IDInType, je); 138 } 139 return null; 140 } 141 142 149 public String getTitle(ContentObject contentObject, 150 ContentObjectEntryState entryState) { 151 String value = ""; 152 int pageID = -1; 153 154 if ( contentObject instanceof ContentContainer ){ 155 pageID = ((ContentContainer)contentObject).getPageID(); 156 } else { 157 pageID = ((ContentContainerList)contentObject).getPageID(); 158 } 159 160 try { 161 ContentPage contentPage = ContentPage.getPage(pageID); 162 EntryLoadRequest loadRequest = new EntryLoadRequest(entryState); 163 int pageDefID = contentPage.getPageTemplateID(loadRequest); 164 return getTitle(pageDefID, 165 LanguageCodeConverters 166 .languageCodeToLocale(entryState.getLanguageCode())); 167 } catch ( Throwable t ){ 168 t.printStackTrace(); 169 } 170 return value; 171 } 172 173 174 175 public String getTitle( int pageDefID ) { 176 JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID ); 177 if (theSubDef != null) { 178 return theSubDef.getTitle(); 179 } else { 180 return ""; 181 } 182 } 183 184 public String getTitle( int pageDefID , Locale locale) { 185 String title = ""; 186 187 title = getTitle(pageDefID); 188 ResourceBundleMarker resMarker = ResourceBundleMarker.parseMarkerValue(title); 189 if ( resMarker == null ){ 190 return title; 191 } 192 try { 193 title = resMarker.getValue(locale); 194 } catch ( Throwable t ){ 195 title = this.getName(); 196 } 197 return title; 198 } 199 200 201 public Enumeration getStructure( String dummy, int pageDefID ) { 202 return getStructure( "", pageDefID, JahiaContainerStructure.ALL_TYPES ); 203 } 204 205 206 207 public Enumeration getStructure( String dummy, int pageDefID, int typeFlag ) { 208 Vector structure; 209 JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID ); 210 if (theSubDef != null) { 211 structure = theSubDef.getStructure(); 212 } else { 213 return (new Vector ()).elements(); 214 } 215 Vector out = new Vector (); 216 for (int i=0; i < structure.size(); i++) { 217 JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.elementAt(i); 218 if ((theStructure.getObjectType() & typeFlag) != 0) { 219 out.add( theStructure ); 220 } 221 } 222 return out.elements(); 223 } 224 225 226 public void setTitle( String title, int pageDefID ) 227 throws JahiaException 228 { 229 logger.debug("Setting title : " + title + " for pagedef " + pageDefID ); 230 JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID ); 231 if (theSubDef == null) { 232 logger.debug("Creating a new list for pagedef " + pageDefID ); 233 theSubDef = createSubDef( pageDefID ); 234 } else { 235 logger.debug("Using pagedef " + theSubDef.getPageDefID() + " 's list" ); 236 } 237 theSubDef.setTitle( title ); 238 } 239 240 241 public void setStructure( Vector structure, int pageDefID ) 242 throws JahiaException 243 { 244 JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID ); 245 if (theSubDef == null) { 246 theSubDef = createSubDef( pageDefID ); 247 } 248 theSubDef.setStructure( structure ); 249 } 250 251 252 public void composeStructure( Vector structure, int pageDefID ) 253 throws JahiaException 254 { 255 JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID ); 256 if (theSubDef == null) { 257 theSubDef = createSubDef( pageDefID ); 258 } 259 theSubDef.composeStructure( structure ); 260 } 261 262 263 264 private JahiaContainerSubDefinition getSubDef( int pageDefID ) 265 { 266 return (JahiaContainerSubDefinition) subDefs.get( new Integer (pageDefID) ); 267 } 269 270 271 272 private JahiaContainerSubDefinition createSubDef( int pageDefID ) 273 throws JahiaException 274 { 275 JahiaContainerSubDefinition theSubDef = new JahiaContainerSubDefinition( 0, pageDefID, "", null ); 276 if (subDefs == null) { 277 subDefs = new Hashtable (); 278 } 279 subDefs.put( new Integer (theSubDef.getPageDefID()), theSubDef ); 280 return theSubDef; 281 } 283 284 285 286 public JahiaFieldDefinition findFieldInStructure( String fieldName, int pageDefID ) 287 { 288 Enumeration structure = getStructure( "", pageDefID ); 289 if (fieldName != null) { 290 while (structure.hasMoreElements()) { 291 JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.nextElement(); 292 if ((theStructure.getObjectType() & JahiaContainerStructure.JAHIA_FIELD) != 0) { 293 JahiaFieldDefinition theFieldDef = (JahiaFieldDefinition) theStructure.getObjectDef(); 294 if (fieldName.equals(theFieldDef.getName())) { 295 return theFieldDef; 296 } 297 } 298 } 299 } 300 return null; 301 } 302 303 304 305 public JahiaContainerDefinition findContainerInStructure( String containerName, int pageDefID ) 306 { 307 Enumeration structure = getStructure( "", pageDefID ); 308 if (containerName != null) { 309 while (structure.hasMoreElements()) { 310 JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.nextElement(); 311 if ((theStructure.getObjectType() & JahiaContainerStructure.JAHIA_CONTAINER) != 0) { 312 JahiaContainerDefinition theContainerDef = (JahiaContainerDefinition) theStructure.getObjectDef(); 313 if (containerName.equals(theContainerDef.getName())) { 314 return theContainerDef; 315 } 316 } 317 } 318 } 319 return null; 320 } 321 322 323 324 public synchronized boolean structureChanged( Vector struct, int pageDefID ) 325 throws JahiaException 326 { 327 Vector structure; 329 JahiaContainerSubDefinition theSubDef = getSubDef( pageDefID ); 330 if (theSubDef != null) { 331 structure = theSubDef.getStructure(); 332 } else { 333 structure = null; 334 } 335 336 if ((struct == null) || (structure == null)) { 337 logger.warn(name + ":Structure non-existant in memory, must load from database"); 339 return true; 340 } else if (struct.size() != structure.size()) { 341 logger.warn(name + ":Size are not equal (cur=" + 343 Integer.toString(structure.size()) + ", new=" + Integer.toString(struct.size()) + ")"); 344 return true; 345 } else { 346 for (int i=0; i < structure.size(); i++) 348 { 349 JahiaContainerStructure theStructure = (JahiaContainerStructure) structure.elementAt(i); 350 JahiaContainerStructure aStructure = new JahiaContainerStructure( 351 (String )struct.elementAt(i), theSubDef.getID(), i, pageDefID ); 352 if (!theStructure.equals( aStructure )) { 353 logger.warn(name + ":Size equal, fields not equal."); 354 return true; 355 } 356 } 357 return false; 359 } 360 } 362 public void setProperties(Properties newProperties) { 363 this.ctnDefProperties = newProperties; 364 } 365 366 public Properties getProperties() { 367 return this.ctnDefProperties; 368 } 369 370 public String getProperty(String propertyName) { 371 if (this.ctnDefProperties != null) { 372 return this.ctnDefProperties.getProperty(propertyName); 373 } else { 374 return null; 375 } 376 } 377 378 public void setProperty(String propertyName, String propertyValue) { 379 if (this.ctnDefProperties != null) { 380 this.ctnDefProperties.setProperty(propertyName, propertyValue); 381 } else { 382 logger.error("ERROR: Properties object is not defined, ignoring property insertion."); 383 } 384 } 385 386 394 public boolean propertiesChanged(Properties newProperties) { 395 boolean mustSave = false; 396 Enumeration newPropKeys = newProperties.keys(); 397 while (newPropKeys.hasMoreElements()) { 398 String curNewPropName = (String ) newPropKeys.nextElement(); 399 String curNewPropValue = newProperties.getProperty(curNewPropName); 400 if (this.ctnDefProperties.containsKey(curNewPropName)) { 401 String internalPropValue = this.ctnDefProperties.getProperty(curNewPropName); 402 if (!internalPropValue.equals(curNewPropValue)) { 403 mustSave = true; 405 } 406 } else { 407 mustSave = true; 409 } 410 } 411 return mustSave; 412 } 413 414 420 public void setContainerEditView(ContainerEditView anEditView) 421 { 422 this.editView = anEditView; 423 } 424 425 430 public ContainerEditView getContainerEditView() 431 { 432 return this.editView; 433 } 434 435 440 453 454 455 } | Popular Tags |