1 13 package org.jahia.services.containers; 14 15 import org.apache.log4j.Logger; 16 import org.jahia.content.*; 17 import org.jahia.data.containers.JahiaContainerList; 18 import org.jahia.data.fields.LoadFlags; 19 import org.jahia.exceptions.JahiaException; 20 import org.jahia.params.ParamBean; 21 import org.jahia.registries.ServicesRegistry; 22 import org.jahia.services.containers.JahiaContainerUtilsDB; 23 import org.jahia.services.pages.ContentPage; 24 import org.jahia.services.usermanager.JahiaUser; 25 import org.jahia.services.version.*; 26 import org.jahia.utils.LanguageCodeConverters; 27 28 import java.util.*; 29 30 39 40 public class ContentContainerList extends ContentObject implements PageReferenceableInterface { 41 42 private static Logger logger = Logger.getLogger (ContentContainerList.class); 43 44 private ArrayList activeAndStagedEntryStates; 45 private ArrayList versionedEntryStates = null; 46 private int parentContainerID; 47 private int pageID; 48 private int aclID; 49 private int containerListDefinitionID; 50 51 static { 52 JahiaObject.registerType (ContentContainerListKey.CONTAINERLIST_TYPE, 53 ContentContainerList.class.getName ()); 54 } 55 56 public static ContentObject getChildInstance (String IDInType) { 57 try { 58 return getContainerList (Integer.parseInt (IDInType)); 59 } catch (JahiaException je) { 60 logger.debug ("Error retrieving container list instance for id : " + IDInType, je); 61 } 62 return null; 63 } 64 65 protected ContentContainerList (int ID, int aParentContainerID, int aPageID, int ctnDefID, 66 int anAclID, ArrayList activeAndStagedEntries) { 67 super (new ContentContainerListKey (ID)); 68 this.parentContainerID = aParentContainerID; 69 this.pageID = aPageID; 70 this.aclID = anAclID; 71 this.containerListDefinitionID = ctnDefID; 72 this.activeAndStagedEntryStates = activeAndStagedEntries; 73 } 74 75 82 public int getPageID () { 83 return this.pageID; 84 } 85 86 public int getSiteID() { 87 try { 88 return getParent(null,null,null).getSiteID(); 89 } catch (JahiaException e) { 90 e.printStackTrace(); } 92 return -1; 93 } 94 95 101 public ContentPage getPage () throws JahiaException { 102 return ContentPage.getPage (this.getPageID ()); 103 } 104 105 112 public int getDefinitionID (EntryLoadRequest loadRequest) { 113 return this.containerListDefinitionID; 114 } 115 116 124 public ObjectKey getDefinitionKey (EntryLoadRequest loadRequest) { 125 int defID = getDefinitionID (loadRequest); 126 ContainerDefinitionKey ctnDefKey = (ContainerDefinitionKey) 127 ContainerDefinitionKey.getChildInstance (String.valueOf (defID)); 128 return ctnDefKey; 129 } 130 131 public SortedSet getEntryStates () 132 throws JahiaException { 133 SortedSet resultSet = new TreeSet (); 134 if (versionedEntryStates == null) { 135 versionedEntryStates = 136 ContentContainerListDB.getInstance ().getVersionedEntryStates (getID (), 137 false); 138 } 139 resultSet.addAll (activeAndStagedEntryStates); 140 resultSet.addAll (versionedEntryStates); 141 return resultSet; 142 } 143 144 public ArrayList getChilds (JahiaUser user, 145 EntryLoadRequest loadRequest) 146 throws JahiaException { 147 150 ArrayList resultList = new ArrayList (); 151 Vector containerIDs = ServicesRegistry.getInstance ().getJahiaContainersService () 152 .getctnidsInList (getID (), loadRequest); 153 Enumeration containerIDEnum = containerIDs.elements (); 154 while (containerIDEnum.hasMoreElements ()) { 155 Integer curContainerID = (Integer ) containerIDEnum.nextElement (); 156 ContentContainer curContainer = ContentContainer.getContainer ( 157 curContainerID.intValue ()); 158 if (curContainer != null) { 159 resultList.add (curContainer); 160 } 161 } 162 return resultList; 163 } 164 165 public ContentObject getParent (JahiaUser user, 166 EntryLoadRequest loadRequest, 167 String operationMode) 168 throws JahiaException { 169 if (parentContainerID > 0) { 170 return ContentContainer.getContainer (parentContainerID); 171 } else { 172 return ContentPage.getPage (pageID); 173 } 174 } 175 176 public static synchronized ContentContainerList getContainerList (int containerListID) 177 throws JahiaException { 178 return ContentContainerListDB.getInstance ().getContainerList (containerListID); 179 } 180 181 public JahiaContainerList getJahiaContainerList (ParamBean jParams, 182 EntryLoadRequest loadRequest) 183 throws JahiaException { 184 return ServicesRegistry.getInstance ().getJahiaContainersService ().loadContainerList ( 185 getID (), LoadFlags.ALL, jParams, loadRequest, new Hashtable (), new Hashtable (), new Hashtable()); 186 } 187 188 public int getAclID () { 189 return aclID; 190 } 191 192 public RestoreVersionTestResults isValidForRestore (JahiaUser user, 193 String operationMode, 194 ContentObjectEntryState entryState, 195 boolean removeMoreRecentActive, 196 StateModificationContext stateModificationContext) 197 throws JahiaException { 198 RestoreVersionTestResults opResult = new RestoreVersionTestResults (); 201 opResult.merge ( 202 super.isValidForRestore (user, operationMode, entryState, 203 removeMoreRecentActive, stateModificationContext)); 204 if (opResult.getStatus () == RestoreVersionTestResults.FAILED_OPERATION_STATUS) { 205 return opResult; 206 } 207 208 ArrayList locales = new ArrayList (); 211 locales.add ( 212 LanguageCodeConverters.languageCodeToLocale (ContentObject.SHARED_LANGUAGE)); 213 locales.add (LanguageCodeConverters.languageCodeToLocale (entryState.getLanguageCode ())); 214 EntryLoadRequest loadRequest = new EntryLoadRequest (entryState.getWorkflowState (), 215 entryState.getVersionID (), locales); 216 ArrayList children = getChilds (user, loadRequest); 217 ListIterator childrenIter = children.listIterator (); 218 while (childrenIter.hasNext ()) { 219 ContentObject curChild = (ContentObject) childrenIter.next (); 220 RestoreVersionTestResults childResult = curChild.isValidForRestore (user, 221 operationMode, entryState, removeMoreRecentActive, 222 stateModificationContext); 223 if (childResult.getStatus () == RestoreVersionTestResults.FAILED_OPERATION_STATUS) { 226 childResult.setStatus (RestoreVersionTestResults.PARTIAL_OPERATION_STATUS); 227 childResult.moveErrorsToWarnings (); 228 } 229 opResult.merge (childResult); 230 } 231 return opResult; 232 } 233 234 private boolean hasEntry (EntryStateable entryState) 235 throws JahiaException { 236 getEntryStates (); ContentObjectEntryState entryStateObject = new ContentObjectEntryState (entryState); 239 if (entryStateObject.getWorkflowState () >= ContentObjectEntryState.WORKFLOW_STATE_ACTIVE) { 240 int objectPos = activeAndStagedEntryStates.indexOf (entryStateObject); 241 if (objectPos != -1) { 242 return true; 243 } 244 } else { 245 int objectPos = versionedEntryStates.indexOf (entryStateObject); 246 if (objectPos != -1) { 247 return true; 248 } 249 250 } 251 return false; 252 } 253 254 private void removeEntryFromCaches (EntryStateable entryState) 255 throws JahiaException { 256 getEntryStates (); ContentObjectEntryState entryStateObject = new ContentObjectEntryState (entryState); 259 if (entryStateObject.getWorkflowState () >= ContentObjectEntryState.WORKFLOW_STATE_ACTIVE) { 260 int objectPos = activeAndStagedEntryStates.indexOf (entryStateObject); 261 if (objectPos != -1) { 262 activeAndStagedEntryStates.remove (objectPos); 263 } 264 } else { 265 int objectPos = versionedEntryStates.indexOf (entryStateObject); 266 if (objectPos != -1) { 267 versionedEntryStates.remove (objectPos); 268 } 269 270 } 271 } 272 273 281 protected void copyEntry (EntryStateable fromEntryState, 282 EntryStateable toEntryState) 283 throws JahiaException { 284 285 ContentObjectEntryState fromE = new ContentObjectEntryState (fromEntryState); 286 ContentObjectEntryState toE = new ContentObjectEntryState (toEntryState); 287 if (this.isShared ()) { 288 fromE = new ContentObjectEntryState (fromEntryState.getWorkflowState (), 290 fromEntryState.getVersionID (), ContentObject.SHARED_LANGUAGE); 291 292 toE = new ContentObjectEntryState (toEntryState.getWorkflowState (), 293 toEntryState.getVersionID (), ContentObject.SHARED_LANGUAGE); 294 } 295 296 if (hasEntry (toE)) { 297 deleteEntry (toE); 298 } 299 300 ContentContainerListDB.getInstance ().copyEntry (getID (), fromE, toE); 301 if (toE.getWorkflowState () >= ContentObjectEntryState.WORKFLOW_STATE_ACTIVE) { 302 activeAndStagedEntryStates.add (toE); 303 } else { 304 versionedEntryStates.add (toE); 305 } 306 } 307 308 309 320 protected void deleteEntry (EntryStateable deleteEntryState) 321 throws JahiaException { 322 removeEntryFromCaches (deleteEntryState); 323 ContentContainerListDB.getInstance ().deleteEntry (getID (), deleteEntryState); 324 } 325 326 342 public void markLanguageForDeletion (JahiaUser user, 343 String languageCode, 344 StateModificationContext 345 stateModificationContext) 346 throws JahiaException { 347 ServicesRegistry.getInstance ().getJahiaContainersService () 348 .markContainerListLanguageForDeletion (getID (), user, languageCode, 349 stateModificationContext); 350 } 351 352 public RestoreVersionTestResults restoreVersion (JahiaUser user, 353 String operationMode, 354 ContentObjectEntryState entryState, 355 boolean removeMoreRecentActive, 356 RestoreVersionStateModificationContext stateModificationContext) 357 throws JahiaException { 358 359 RestoreVersionTestResults opResult = new RestoreVersionTestResults (); 360 361 372 373 getClosestVersionedEntryState(entryState); 375 376 ArrayList locales = new ArrayList(); 377 locales.add(LanguageCodeConverters.languageCodeToLocale(entryState.getLanguageCode())); 378 EntryLoadRequest loadRequest = null; 379 380 loadRequest = new EntryLoadRequest(entryState. 383 getWorkflowState(), entryState.getVersionID(), locales); 384 ArrayList children = getChilds(user, loadRequest); 385 ListIterator childrenIter = children.listIterator(); 386 387 ArrayList processedChilds = new ArrayList(); 389 while (childrenIter.hasNext()) { 390 ContentObject curChild = (ContentObject) childrenIter.next(); 391 opResult.merge(curChild.restoreVersion(user, 392 operationMode, entryState, removeMoreRecentActive, stateModificationContext)); 393 processedChilds.add(curChild.getObjectKey().toString()); 394 } 395 396 if ( removeMoreRecentActive ){ 398 loadRequest = 400 new EntryLoadRequest(EntryLoadRequest.STAGING_WORKFLOW_STATE, 401 0, locales); 402 children = getChilds(user, loadRequest); 403 childrenIter = children.listIterator(); 404 while (childrenIter.hasNext()) { 405 ContentObject curChild = (ContentObject) childrenIter.next(); 406 if ( !processedChilds.contains(curChild.getObjectKey().toString()) ){ 407 opResult.merge(curChild.restoreVersion(user, 408 operationMode, entryState, removeMoreRecentActive, 409 stateModificationContext)); 410 } 411 } 412 } 413 414 opResult.merge(super.restoreVersion(user, operationMode, entryState, 415 removeMoreRecentActive, 416 stateModificationContext)); 417 418 JahiaContainerUtilsDB.getInstance().invalidateSubCtnListIDsByCtnCache(this.getParentContainerID()); 419 JahiaContainerUtilsDB.getInstance().invalidateCtnIdsByCtnListCache(this.getID()); 420 421 ServicesRegistry.getInstance ().getJahiaContainersService () 422 .invalidateContainerListFromCache(this.getID()); 423 424 return opResult; 425 } 426 427 public int getParentContainerID () { 428 return parentContainerID; 429 } 430 431 public int getContainerListDefinitionID () { 432 return containerListDefinitionID; 433 } 434 435 443 public synchronized void undoStaging (ParamBean jParams) 444 throws JahiaException { 445 446 Vector stagedEntryStates = new Vector (); 448 for (int i = 0; i < activeAndStagedEntryStates.size (); i++) { 449 ContentObjectEntryState entryState = (ContentObjectEntryState) activeAndStagedEntryStates.get ( 450 i); 451 if (entryState.isStaging ()) { 453 stagedEntryStates.add (entryState); 454 } 455 } 456 457 for (int i = 0; i < stagedEntryStates.size (); i++) { 459 ContentObjectEntryState curEntryState = (ContentObjectEntryState) stagedEntryStates.get ( 460 i); 461 462 this.deleteEntry (curEntryState); 464 } 465 466 JahiaContainerUtilsDB.getInstance().invalidateSubCtnListIDsByCtnCache(this.getParentContainerID()); 467 JahiaContainerUtilsDB.getInstance().invalidateCtnIdsByCtnListCache(this.getID()); 468 469 ServicesRegistry.getInstance ().getJahiaContainersService () 470 .invalidateContainerListFromCache(this.getID()); 471 472 } 473 474 477 public boolean isShared () { 478 return true; 479 } 480 } 481 | Popular Tags |