1 package org.jahia.services.version; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 import java.util.SortedSet ; 6 import java.util.Vector ; 7 8 import org.jahia.content.AbstractContentTreeVisitor; 9 import org.jahia.content.ContentObject; 10 import org.jahia.content.ContentTree; 11 import org.jahia.content.ContentTreeStatus; 12 import org.jahia.content.ContentTreeStatusInterface; 13 import org.jahia.exceptions.JahiaException; 14 import org.jahia.params.ParamBean; 15 import org.jahia.registries.ServicesRegistry; 16 import org.jahia.services.containers.ContentContainer; 17 import org.jahia.services.fields.ContentField; 18 import org.jahia.services.fields.ContentPageField; 19 import org.jahia.services.pages.ContentPage; 20 import org.jahia.services.usermanager.JahiaUser; 21 22 31 public class UndoStagingContentTreeVisitor extends AbstractContentTreeVisitor { 32 33 private static org.apache.log4j.Logger logger = 34 org.apache.log4j.Logger.getLogger(UndoStagingContentTreeVisitor.class); 35 36 protected ContentTree contentTree; 37 private ParamBean jParams = null; 38 39 private int startPageId = -1; 43 44 52 public UndoStagingContentTreeVisitor( ContentObject rootContentObject, 53 JahiaUser user, 54 EntryLoadRequest loadRequest, 55 String operationMode, 56 ParamBean jParams){ 57 super(user,loadRequest,operationMode); 58 contentTree = new ContentTree(rootContentObject); 59 this.setDescendingPageLevel(0); this.jParams = jParams; 61 if ( rootContentObject instanceof ContentPage ){ 62 this.startPageId = rootContentObject.getID(); 63 } 64 } 65 66 75 public UndoStagingContentTreeVisitor( ContentObject rootContentObject, 76 JahiaUser user, 77 EntryLoadRequest loadRequest, 78 String operationMode, 79 ParamBean jParams, 80 int startPageId){ 81 super(user,loadRequest,operationMode); 82 contentTree = new ContentTree(rootContentObject); 83 this.setDescendingPageLevel(0); this.jParams = jParams; 85 this.startPageId = startPageId; 86 } 87 88 92 public void undoStaging() 93 throws JahiaException{ 94 getContentTree().iterate(this); 95 } 96 97 102 public ContentTree getContentTree(){ 103 return this.contentTree; 104 } 105 106 112 public ContentTreeStatusInterface getContentTreeStatus(ContentObject contentObject, 113 int currentPageLevel) 114 throws JahiaException{ 115 ContentTreeStatus contentTreeStatus = new ContentTreeStatus(); 116 return (ContentTreeStatusInterface)contentTreeStatus; 117 } 118 119 126 public void processContentObjectBeforeChilds(ContentObject contentObject, 127 int currentPageLevel) 128 throws JahiaException{ 129 } 130 131 138 public void processContentObjectAfterChilds( 139 ContentObject contentObject, int currentPageLevel) 140 throws JahiaException{ 141 142 if ( contentObject instanceof ContentPage ){ 143 ContentPage page = (ContentPage)contentObject; 144 contentObject.undoStaging(this.jParams); 147 148 Vector ids = ServicesRegistry.getInstance().getJahiaPageService() 149 .getStagingAndActivePageFieldIDs(page.getID()); 150 Iterator iterator = ids.iterator(); 151 Integer id = null; 152 ContentObject pageField = null; 153 while ( iterator.hasNext() ){ 154 id = (Integer )iterator.next(); 155 pageField = ContentPageField.getField(id.intValue()); 156 try { 159 ContentObject parent = pageField 160 .getParent(getUser(), getEntryLoadRequest(), 161 getOperationMode()); 162 if (parent != null && parent instanceof ContentContainer) { 163 this.undoPageMove(page.getID(),parent); 164 } else { 165 this.undoPageMove(page.getID(),pageField); 166 } 167 } catch ( Throwable t ){ 168 logger.debug(t); 169 } 170 } 171 ids = ServicesRegistry.getInstance().getJahiaPageService() 173 .getStagingAndActivePageFieldIDs(page.getID()); 174 if ( ids == null || ids.size() == 0 ){ 175 int activeVersionId = page.getActiveVersionID(); 176 if ( activeVersionId > 0 ){ 177 EntryLoadRequest loadRequest = new EntryLoadRequest(0,activeVersionId, 178 jParams.getEntryLoadRequest().getLocales()); 179 pageField = page.getParent(loadRequest); 180 if ( pageField != null && !pageField.hasActiveEntries() ){ 181 int ctnId = ((ContentField)pageField).getContainerID(); 182 if ( ctnId > 0 ){ 183 ContentObjectEntryState contentObjectState = 184 new ContentObjectEntryState(0,activeVersionId,ContentObject.SHARED_LANGUAGE); 185 RestoreVersionStateModificationContext rvsmc = 186 new RestoreVersionStateModificationContext(page.getObjectKey(),null,false,contentObjectState); 187 page.restoreVersion(jParams.getUser(),jParams.getOperationMode(),contentObjectState,false,false,rvsmc); 188 } 189 } 190 } 191 192 } 193 } else { 194 contentObject.undoStaging(this.jParams); 195 } 196 } 197 198 206 public void processLastContentPageField( 207 ContentObject contentObject, 208 int currentPageLevel) 209 throws JahiaException{ 210 211 ContentPage page = null; 212 String value = ((ContentPageField)contentObject).getValue(this.jParams); 213 int pageId = -1; 214 if ( value != null ){ 215 try { 216 pageId = Integer.parseInt(value); 217 if ( pageId == -1 ){ 218 contentObject.undoStaging(this.jParams); 220 return; 221 } else { 222 page = ContentPage.getPage(pageId); 223 } 224 } catch ( Throwable t ){ 225 } 226 } 227 228 if ( page == null ){ 229 contentObject.undoStaging(this.jParams); 231 return; 232 } 233 234 if ( page.getPageType(jParams.getEntryLoadRequest()) 236 == ContentPage.TYPE_DIRECT ){ 237 238 if ( page.getID() == this.getStartPageId() ){ 239 contentObject.undoStaging(this.jParams); 240 } else { 241 int versionId = ServicesRegistry.getInstance() 242 .getJahiaVersionService().getCurrentVersionID(); 243 if ( !contentObject.hasArchiveEntryState(versionId) ){ 244 ContentPageField contentPageField = (ContentPageField) 247 contentObject; 248 if (contentPageField.getContainerID() > 0) { 249 ContentTreeStatusInterface contentTreeStatus = 251 (ContentTreeStatusInterface) 252 this.contentTree.getContentTreeStatusStack().pop(); 253 254 ContentTreeStatusInterface parentStatus = 255 (ContentTreeStatusInterface) 256 this.contentTree.getContentTreeStatusStack().peek(); 257 parentStatus.setContinueAfterChilds(false); 258 259 this.contentTree.getContentTreeStatusStack() 260 .push(contentTreeStatus); 261 } 262 } else { 263 if (this.getStartPageId() > 0) { 269 int fieldId = ServicesRegistry.getInstance() 270 .getJahiaPageService().getPageFieldID(this. 271 getStartPageId()); 272 if ( fieldId != -1 ){ 273 ContentField contentField = ContentField.getField( 274 fieldId); 275 if ( contentField != null ){ 276 try { 277 ContentObject parent = contentField 278 .getParent(getUser(), getEntryLoadRequest(), 279 getOperationMode()); 280 if (parent != null && parent instanceof ContentContainer) { 281 ContentObject objectParent = 282 contentObject.getParent(getUser(), 283 getEntryLoadRequest(), 284 getOperationMode()); 285 if ( objectParent != null 286 && parent.getObjectKey().equals(objectParent.getObjectKey()) ){ 287 return; 289 } 290 } 291 } catch ( Throwable t ){ 292 logger.debug(t); 293 } 294 295 } 296 } 297 298 } 299 ContentTreeStatusInterface contentTreeStatus = 301 (ContentTreeStatusInterface) 302 this.contentTree.getContentTreeStatusStack().pop(); 303 304 ContentTreeStatusInterface parentStatus = 305 (ContentTreeStatusInterface) 306 this.contentTree.getContentTreeStatusStack().peek(); 307 parentStatus.setContinueAfterChilds(false); 308 309 this.contentTree.getContentTreeStatusStack() 310 .push(contentTreeStatus); 311 } 312 } 313 } else { 314 contentObject.undoStaging(jParams); 317 page.undoStaging(jParams); 318 } 319 } 320 321 328 public ArrayList getChilds(ContentObject contentObject, 329 int currentPageLevel) 330 throws JahiaException { 331 332 return super.getChilds(contentObject,currentPageLevel); 333 } 334 335 340 private void undoStagingDeletedContainer(ContentObject contentObject) 341 throws JahiaException { 342 SortedSet entryStates = contentObject.getActiveAndStagingEntryStates(); 343 Iterator iterator = entryStates.iterator(); 344 ContentObjectEntryState entryState = null; 345 ContentObject parentContainer = null; 346 UndoStagingContentTreeVisitor undoStagingVisitor = null; 347 while ( iterator.hasNext() ){ 348 entryState = (ContentObjectEntryState)iterator.next(); 349 if ( entryState.getWorkflowState() >= 350 ContentObjectEntryState.WORKFLOW_STATE_START_STAGING 351 && entryState.getVersionID() == 352 ContentObjectEntryState.WORKFLOW_STATE_VERSIONING_DELETED ){ 353 parentContainer = contentObject 355 .getParent(this.getUser(),this.getEntryLoadRequest(), 356 this.getOperationMode()); 357 if ( parentContainer != null ){ 358 undoStagingVisitor = 359 new UndoStagingContentTreeVisitor(parentContainer, 360 this.getUser(),this.getEntryLoadRequest(), 361 this.getOperationMode(),this.jParams); 362 undoStagingVisitor.undoStaging(); 363 } 364 contentObject.undoStaging(jParams); 366 break; 367 } 368 } 369 } 370 371 private void setStartPageId(int value){ 372 this.startPageId = value; 373 } 374 375 private int getStartPageId(){ 376 return this.startPageId; 377 } 378 379 385 private void undoPageMove(int pageId, ContentObject rootContentObject) 386 throws JahiaException { 387 if ( rootContentObject == null ){ 388 return; 389 } 390 UndoStagingContentTreeVisitor undoSV = 391 new UndoStagingContentTreeVisitor(rootContentObject, 392 getUser(), getEntryLoadRequest(), getOperationMode(), 393 jParams, pageId); 394 undoSV.undoStaging(); 395 } 396 397 } 398 | Popular Tags |