1 13 package org.jahia.data.containers; 14 15 import java.io.Serializable ; 16 import java.util.*; 17 18 import org.apache.log4j.Logger; 19 import org.jahia.data.fields.JahiaContentFieldFacade; 20 import org.jahia.data.fields.JahiaField; 21 import org.jahia.data.fields.JahiaFieldDefinition; 22 import org.jahia.exceptions.JahiaException; 23 import org.jahia.params.ParamBean; 24 import org.jahia.registries.ServicesRegistry; 25 import org.jahia.services.fields.PublicContentFieldEntryState; 26 import org.jahia.services.pages.JahiaPage; 27 import org.jahia.services.version.ContentObjectEntryState; 28 import org.jahia.services.version.EntryLoadRequest; 29 30 35 public class JahiaContentContainerFacade implements Serializable , ContainerFacadeInterface { 36 37 private static Logger logger = Logger.getLogger(JahiaContentContainerFacade.class); 38 39 private int ctnID = -1; 40 41 private int aclID = -1; 42 43 private boolean containerStructureChecked = false; 44 45 private Vector activeAndStagingEntryStates; 48 49 private Hashtable fields; 51 52 private Hashtable containers; 54 55 56 67 public JahiaContentContainerFacade( int aCtnID, 68 int loadFlag, 69 ParamBean jParams, 70 ArrayList locales, 71 boolean createMissingLanguages ) 72 throws JahiaException 73 { 74 this.ctnID = aCtnID; 75 this.containers = new Hashtable(); 76 this.fields = new Hashtable(); 77 this.activeAndStagingEntryStates = new Vector(); 78 instanceContainers(loadFlag, jParams, locales, createMissingLanguages); 79 } 80 81 87 public JahiaContentContainerFacade (int aCtnID, 88 int jahiaID, 89 int pageID, 90 int ctnListID, 91 int ctnDefID, 92 int anAclID, 93 ParamBean jParams, 94 ArrayList locales ) 95 throws JahiaException 96 { 97 98 this.ctnID = aCtnID; 99 this.aclID = anAclID; 100 this.containers = new Hashtable(); 101 this.fields = new Hashtable(); 102 this.activeAndStagingEntryStates = new Vector(); 103 createTempContainers( aCtnID, jahiaID, pageID, ctnListID, 104 ctnDefID, anAclID, jParams, locales ); 105 } 106 107 public int getAclID(){ 109 return this.aclID; 110 } 111 112 public int getContainerID(){ 114 return this.ctnID; 115 } 116 117 public Enumeration getContainers(){ 119 return containers.elements(); 120 } 121 122 public Enumeration getFields(){ 124 return fields.elements(); 125 } 126 127 public JahiaContentFieldFacade getContentFieldFacade(int fieldID){ 129 return (JahiaContentFieldFacade)this.fields.get(new Integer (fieldID)); 130 } 131 132 139 public JahiaContainer getContainer( EntryLoadRequest entryLoadRequest, 140 boolean activeIfStagingNotFound ){ 141 142 logger.debug("EntryLoadRequest :" + entryLoadRequest.toString()); 143 144 Locale locale = entryLoadRequest.getFirstLocale(true); 145 logger.debug("EntryLoadRequest locale :" + locale.toString()); 146 147 ContentObjectEntryState entryState = 148 (ContentObjectEntryState)ServicesRegistry.getInstance() 149 .getJahiaVersionService() 150 .resolveEntry(activeAndStagingEntryStates, 151 entryLoadRequest); 152 153 if ( entryState != null ){ 154 logger.debug("Resolved entryState :" + entryState.toString()); 155 } 156 if ( entryState == null && activeIfStagingNotFound ){ 157 158 EntryLoadRequest newEntryLoadRequest = new EntryLoadRequest( 159 ContentObjectEntryState.WORKFLOW_STATE_ACTIVE, 160 0, 161 entryLoadRequest.getLocales()); 162 163 entryState = (ContentObjectEntryState)ServicesRegistry.getInstance() 164 .getJahiaVersionService() 165 .resolveEntry(activeAndStagingEntryStates, 166 newEntryLoadRequest); 167 } else if ( entryState != null && entryState.isActive() && !activeIfStagingNotFound ){ 168 return null; 170 } 171 JahiaContainer container = null; 172 if ( entryState != null ){ 173 container = 174 (JahiaContainer)containers.get(new PublicContentFieldEntryState(entryState)); 175 logger.debug("Returned entryState :" + entryState.toString()); 176 } else { 177 logger.debug("Entry state is null"); 178 } 179 180 return container; 181 } 182 183 189 public void changeType(int fieldId, int type, 190 ParamBean jParams) throws JahiaException { 191 192 JahiaContentFieldFacade facade = this.getContentFieldFacade(fieldId); 193 facade = facade.changeType(type,jParams); 194 this.fields.put(new Integer (fieldId),facade); 195 196 Enumeration enumeration = this.activeAndStagingEntryStates.elements(); 197 while ( enumeration.hasMoreElements() ){ 198 PublicContentFieldEntryState entryState = 199 (PublicContentFieldEntryState)enumeration.nextElement(); 200 JahiaContainer container = 201 (JahiaContainer)this.containers.get(entryState); 202 EntryLoadRequest loadRequest = new EntryLoadRequest(entryState); 203 JahiaField field = facade.getField(loadRequest,true); 204 container.setField(field); 205 } 206 } 207 208 private void instanceContainers( int loadFlag, 210 ParamBean jParams, 211 ArrayList locales, 212 boolean createMissingLanguages ) 213 throws JahiaException 214 { 215 216 EntryLoadRequest loadVersion = 217 new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 218 0, locales); 219 220 Vector fieldIDs = ServicesRegistry.getInstance() 223 .getJahiaContainersService() 224 .getFieldIDsInContainer(this.ctnID,null); 225 226 int size = fieldIDs.size(); 228 for ( int i=0 ; i<size ; i++ ) 229 { 230 Integer I = (Integer )fieldIDs.get(i); 231 if ( this.fields.get(I) == null ){ 232 JahiaContentFieldFacade contentFieldFacade = 233 new JahiaContentFieldFacade(I.intValue(), loadFlag, 234 jParams,locales,createMissingLanguages); 235 this.fields.put(I,contentFieldFacade); 236 } 237 } 238 239 Iterator iterator = locales.iterator(); 240 Locale locale = null; 241 ArrayList loadLocales = null; 242 while ( iterator.hasNext() ) 243 { 244 locale = (Locale)iterator.next(); 245 loadLocales = new ArrayList(); 246 loadLocales.add(locale); 247 248 loadVersion = 250 new EntryLoadRequest(EntryLoadRequest.ACTIVE_WORKFLOW_STATE, 251 0, loadLocales); 252 253 JahiaContainer container = loadContainer(loadFlag,loadVersion,jParams,locales,createMissingLanguages); 254 if ( container != null ){ 255 container.setLanguageCode(locale.toString()); 256 container.fieldsStructureCheck(jParams); 257 PublicContentFieldEntryState entryStateKey = 258 new PublicContentFieldEntryState( 259 container.getWorkflowState(), 260 container.getVersionID(), locale.toString()); 261 if ( this.containers.get(entryStateKey)==null ){ 262 this.containers.put(entryStateKey,container); 263 this.activeAndStagingEntryStates.add(entryStateKey); 264 } 265 } 266 267 loadVersion = 269 new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 270 0, loadLocales); 271 272 JahiaContainer stagingContainer = loadContainer(loadFlag,loadVersion,jParams,locales,createMissingLanguages); 273 if ( (stagingContainer != null) && (stagingContainer.getWorkflowState()>EntryLoadRequest.ACTIVE_WORKFLOW_STATE) ){ 274 275 stagingContainer.setLanguageCode(locale.toString()); 276 stagingContainer.fieldsStructureCheck(jParams); 277 PublicContentFieldEntryState entryStateKey = 278 new PublicContentFieldEntryState( 279 stagingContainer.getWorkflowState(), 280 stagingContainer.getVersionID(), locale.toString()); 281 if ( this.containers.get(entryStateKey)==null ){ 282 this.containers.put(entryStateKey,stagingContainer); 283 this.activeAndStagingEntryStates.add(entryStateKey); 284 } 285 } else { 286 if ( stagingContainer != null) { 289 stagingContainer.setLanguageCode(locale.toString()); 290 if ( !this.containerStructureChecked ) 291 stagingContainer.fieldsStructureCheck(jParams); 292 PublicContentFieldEntryState entryStateKey = 293 new PublicContentFieldEntryState( 294 EntryLoadRequest.STAGING_WORKFLOW_STATE, 295 0, locale.toString()); 296 if ( this.containers.get(entryStateKey)==null ){ 297 this.containers.put(entryStateKey,stagingContainer); 298 this.activeAndStagingEntryStates.add(entryStateKey); 299 } 300 } 301 } 302 } 303 } 304 305 private void createTempContainers( int aCtnID, 307 int jahiaID, 308 int pageID, 309 int ctnListID, 310 int ctnDefID, 311 int anAclID, 312 ParamBean jParams, 313 ArrayList locales ) 314 throws JahiaException 315 { 316 317 EntryLoadRequest loadVersion = null; 318 319 321 Iterator iterator = locales.iterator(); 323 Locale locale = null; 324 while ( iterator.hasNext() ) 325 { 326 locale = (Locale)iterator.next(); 327 ArrayList entryLocales = new ArrayList(); 328 entryLocales.add(locale); 329 330 loadVersion = new EntryLoadRequest(ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, 331 0, entryLocales); 332 333 PublicContentFieldEntryState entryStateKey = 334 new PublicContentFieldEntryState( 335 ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, 336 0, 337 locale.toString()); 338 339 JahiaContainer container = 340 new JahiaContainer(aCtnID, jahiaID, pageID, ctnListID,0, 341 anAclID,ctnDefID,0,ContentObjectEntryState.WORKFLOW_STATE_START_STAGING); 342 343 container.setLanguageCode(locale.toString()); 344 if ( fields.size()==0 ){ 345 346 JahiaPage page = ServicesRegistry.getInstance() 348 .getJahiaPageService().lookupPage (pageID, jParams); 349 if (page == null) { 350 throw new JahiaException( "Page missing", 351 "Trying to add a container on a non-existing page (" + pageID + ")", 352 JahiaException.PAGE_ERROR, JahiaException.ERROR_SEVERITY ); 353 } 354 createContentFieldFacades(page,container,jParams,locales); 355 } 356 357 Enumeration enumeration = this.fields.elements(); 359 JahiaContentFieldFacade contentFieldFacade = null; 360 361 while ( enumeration.hasMoreElements() ){ 362 contentFieldFacade = (JahiaContentFieldFacade)enumeration.nextElement(); 363 JahiaField field = contentFieldFacade.getField(loadVersion,true); 364 container.addField(field); 365 } 366 367 if ( this.containers.get(entryStateKey) == null ){ 368 container.fieldsStructureCheck(jParams); 369 this.containers.put(entryStateKey,container); 370 this.activeAndStagingEntryStates.add(entryStateKey); 371 } 372 373 logger.debug( "Created new Container for entryState :" 374 + entryStateKey.toString() + " langCode=" 375 + locale.toString()); 376 } 377 } 378 379 private JahiaContainer loadContainer(int loadFlag, 381 EntryLoadRequest loadVersion, 382 ParamBean jParams, ArrayList locales, boolean createMissingLanguages) 383 throws JahiaException 384 { 385 JahiaContainer containerInfo = null; 386 try { 387 containerInfo = ServicesRegistry.getInstance() 388 .getJahiaContainersService() 389 .loadContainerInfo(this.ctnID,loadVersion); 390 } catch ( JahiaException je ) { 391 } 392 if ( containerInfo == null ){ 393 return null; 394 } 395 396 JahiaContainer container = 397 new JahiaContainer (this.ctnID, 398 containerInfo.getJahiaID(), 399 containerInfo.getPageID(), 400 containerInfo.getListID(), 401 containerInfo.getRank(), 402 containerInfo.getAclID(), 403 containerInfo.getctndefid(), 404 containerInfo.getVersionID(), 405 containerInfo.getWorkflowState()); 406 407 container.setLanguageCode(loadVersion.getFirstLocale(true).toString()); 408 409 Enumeration enumeration = this.fields.elements(); 411 JahiaContentFieldFacade contentFieldFacade = null; 412 while ( enumeration.hasMoreElements() ){ 413 contentFieldFacade = (JahiaContentFieldFacade)enumeration.nextElement(); 414 JahiaField field = contentFieldFacade.getField(loadVersion,true); 415 if ( loadVersion.isCurrent() && field == null ){ 416 EntryLoadRequest stagingVersion 418 = new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 419 loadVersion.getVersionID(), 420 loadVersion.getLocales()); 421 field = contentFieldFacade.getField(stagingVersion,true); 422 } 423 if ( field != null ){ 424 container.addField(field); 425 } 426 } 427 428 if ( !this.containerStructureChecked ){ 429 container.fieldsStructureCheck(jParams); 431 Enumeration fieldsEnum = container.getFields(); 433 JahiaField field = null; 434 boolean hasChanged = false; 435 while ( fieldsEnum.hasMoreElements() ){ 436 field = (JahiaField)fieldsEnum.nextElement(); 437 Integer I = new Integer (field.getID()); 438 if ( this.fields.get(I) == null ){ 439 hasChanged = true; 440 contentFieldFacade = 441 new JahiaContentFieldFacade(I.intValue(), loadFlag, 442 jParams,locales,createMissingLanguages); 443 this.fields.put(I,contentFieldFacade); 444 } 445 } 446 this.containerStructureChecked = true; 447 if ( hasChanged ){ 448 container = this.loadContainer(loadFlag,loadVersion, 450 jParams,locales,createMissingLanguages); 451 } 452 } 453 454 return container; 455 } 456 457 private void createContentFieldFacades( JahiaPage page, 459 JahiaContainer container, 460 ParamBean jParams, ArrayList locales ) 461 throws JahiaException 462 { 463 464 int fakeID = -1; 467 int pageDefID = page.getPageTemplateID(); 468 Enumeration structure = container.getDefinition().getStructure( 469 "", pageDefID, JahiaContainerStructure.JAHIA_FIELD ); 470 while (structure.hasMoreElements()) { 471 JahiaContainerStructure theStruct = 472 (JahiaContainerStructure) structure.nextElement(); 473 JahiaFieldDefinition theDef = 474 (JahiaFieldDefinition) theStruct.getObjectDef(); 475 JahiaContentFieldFacade contentFieldFacade 476 = createContentFieldFacade( theDef, container, page, fakeID, jParams, locales); 477 this.fields.put(new Integer (fakeID),contentFieldFacade); 478 fakeID--; 479 } 480 } 481 482 private JahiaContentFieldFacade createContentFieldFacade( JahiaFieldDefinition fieldDef, 484 JahiaContainer container, 485 JahiaPage thePage, 486 int fieldID, ParamBean jParams, 487 ArrayList locales ) 488 throws JahiaException 489 { 490 int connectType = 0; 491 String fieldValue = fieldDef.getDefaultValue( thePage.getPageTemplateID() ); 492 493 if (fieldValue != null 495 && fieldValue.toUpperCase().indexOf("JAHIA_MASKTYPE") != -1) { 496 fieldValue = ""; 497 } 498 499 int anAclID = 0; 500 JahiaContentFieldFacade contentFieldFacade = 501 new JahiaContentFieldFacade( fieldID, 502 thePage.getJahiaID(), 503 thePage.getID(), 504 container.getID(), 505 fieldDef.getID(), 506 fieldDef.getType(thePage.getPageTemplateID()), 507 connectType, 508 fieldValue, 509 anAclID, 510 jParams, 511 locales ); 512 return contentFieldFacade; 513 } 514 } 515 | Popular Tags |