1 package org.jahia.services.version; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import java.util.Comparator ; 6 import java.util.HashMap ; 7 import java.util.Locale ; 8 9 import org.jahia.content.AbstractContentTreeVisitor; 10 import org.jahia.content.ContentDefinition; 11 import org.jahia.content.ContentObject; 12 import org.jahia.content.ContentTree; 13 import org.jahia.content.ContentTreeStatus; 14 import org.jahia.content.ContentTreeStatusInterface; 15 import org.jahia.exceptions.JahiaException; 16 import org.jahia.services.pages.ContentPage; 17 import org.jahia.services.usermanager.JahiaUser; 18 import org.jahia.content.JahiaObject; 19 20 29 public abstract class ContentTreeRevisionsVisitor extends AbstractContentTreeVisitor { 30 31 private static org.apache.log4j.Logger logger = 32 org.apache.log4j.Logger.getLogger(ContentTreeRevisionsVisitor.class); 33 34 protected ContentTree contentTree; 35 protected ArrayList revisionsList; 36 protected HashMap revisionsStates; 37 38 39 protected long fromRevisionDate; 40 41 42 protected long toRevisionDate; 43 44 45 protected boolean withDeletedContent; 46 47 48 protected boolean withActiveRevisions = true; 49 50 51 protected boolean withDeletedRevisions = true; 52 53 protected boolean hasAttributeChange = true; 54 55 protected String lastSortLanguageCode = ""; 56 protected int lastSortAttribute = -1; 57 protected int lastSortOrder = -1; 58 59 66 public ContentTreeRevisionsVisitor(ContentObject rootContentObject, 67 JahiaUser user, 68 EntryLoadRequest loadRequest, 69 String operationMode){ 70 super(user,loadRequest,operationMode); 71 this.revisionsList = new ArrayList (); 72 this.revisionsStates = new HashMap (); 73 this.contentTree = new ContentTree(rootContentObject); 74 } 75 76 80 public void setUser(JahiaUser user){ 81 if ( !getUser().getUserKey().equals(user.getUserKey()) ){ 82 this.hasAttributeChange = true; 83 } 84 super.setUser(user); 85 } 86 87 91 public void setEntryLoadRequest(EntryLoadRequest loadRequest){ 92 if ( !getEntryLoadRequest().toString().equals(loadRequest.toString()) ){ 93 this.hasAttributeChange = true; 94 } 95 super.setEntryLoadRequest(loadRequest); 96 } 97 98 102 public void setOperationMode(String operationMode){ 103 if ( !getOperationMode().equals(operationMode) ){ 104 this.hasAttributeChange = true; 105 } 106 super.setOperationMode(operationMode); 107 } 108 109 112 public void setDescendingPageLevel(int level){ 113 if ( getDescendingPageLevel() != level ){ 114 this.hasAttributeChange = true; 115 } 116 super.setDescendingPageLevel(level); 117 } 118 119 123 public ContentTree getContentTree(){ 124 return this.contentTree; 125 } 126 127 131 public ArrayList getRevisions(){ 132 return this.revisionsList; 133 } 134 135 142 public abstract void processContentObjectBeforeChilds(ContentObject contentObject, 143 int currentPageLevel) 144 throws JahiaException; 145 146 152 public ContentTreeStatusInterface getContentTreeStatus(ContentObject contentObject, 153 int currentPageLevel) 154 throws JahiaException{ 155 ContentTreeStatus contentTreeStatus = new ContentTreeStatus(); 156 return (ContentTreeStatusInterface)contentTreeStatus; 157 } 158 159 166 public ArrayList getChilds(ContentObject contentObject, 167 int currentPageLevel) 168 throws JahiaException{ 169 170 ArrayList results = new ArrayList (); 171 179 return super.getChilds(contentObject,currentPageLevel); 180 } 181 182 187 public void loadRevisions(boolean ignoreAttributeChange) 188 throws JahiaException{ 189 if ( ignoreAttributeChange || hasAttributeChange() ){ 190 ContentObject rootObject = this.getContentTree() 192 .getRootContentObject(); 193 try { 194 rootObject = (ContentObject) JahiaObject.getInstance(rootObject.getObjectKey()); 195 this.contentTree = new ContentTree(rootObject); 196 this.revisionsList = new ArrayList (); 197 getContentTree().iterate(this); 198 } catch ( Throwable t ){ 199 logger.debug("Exception when trying to reload Revisions", t); 200 } 201 202 ArrayList result = new ArrayList (); 204 ArrayList revisions = this.getRevisions(); 205 int size = revisions.size(); 206 RevisionEntrySet rs = null; 207 rootObject = this.getContentTree().getRootContentObject(); 208 for ( int i=0; i<size; i++ ){ 209 rs = (RevisionEntrySet)revisions.get(i); 210 if ( !rootObject.isDeletedOrDoesNotExist(rs.getVersionID()) ){ 211 result.add(rs); 212 } 213 } 214 this.revisionsList = result; 215 216 } 217 this.hasAttributeChange = false; 218 } 219 220 229 public void sortRevisions(String languageCode, 230 int sortAttribute, 231 int sortOrder, 232 boolean ignoreSortAttributeChange) 233 throws JahiaException{ 234 235 if ( ignoreSortAttributeChange || 236 !lastSortLanguageCode.equals(languageCode) || 237 lastSortAttribute != sortAttribute || 238 lastSortOrder != sortOrder ){ 239 RevisionEntrySetComparator comparator = 240 new RevisionEntrySetComparator(languageCode, sortAttribute, sortOrder); 241 this.sort(comparator); 242 } 243 } 244 245 249 public boolean hasAttributeChange(){ 250 return this.hasAttributeChange; 251 } 252 253 public void setFromRevisionDate(long date){ 254 if ( date != this.fromRevisionDate){ 255 this.hasAttributeChange = true; 256 } 257 this.fromRevisionDate = date; 258 } 259 260 public long getFromRevisionDate(){ 261 return this.fromRevisionDate; 262 } 263 264 public long getToRevisionDate(){ 265 return this.toRevisionDate; 266 } 267 268 public void setToRevisionDate(long date){ 269 if ( date != this.toRevisionDate){ 270 this.hasAttributeChange = true; 271 } 272 this.toRevisionDate = date; 273 } 274 275 public boolean isWithDeletedContent(){ 276 return this.withDeletedContent; 277 } 278 279 public void setWithDeletedContent(boolean value){ 280 if ( value != this.withDeletedContent){ 281 this.hasAttributeChange = true; 282 } 283 this.withDeletedContent = value; 284 } 285 286 public boolean isWithActiveRevisions(){ 287 return this.withActiveRevisions; 288 } 289 290 public void setWithActiveRevisions(boolean value){ 291 if ( value != this.withActiveRevisions){ 292 this.hasAttributeChange = true; 293 } 294 this.withActiveRevisions = value; 295 } 296 297 public boolean isWithDeletedRevisions(){ 298 return this.withDeletedContent; 299 } 300 301 public void setWithDeletedRevisions(boolean value){ 302 if ( value != this.withDeletedRevisions){ 303 this.hasAttributeChange = true; 304 } 305 this.withDeletedRevisions = value; 306 } 307 308 314 protected boolean inDateRange(ContentObjectEntryState entryState){ 315 316 long entryStateDate = entryState.getVersionID()*1000L; 317 return ( this.fromRevisionDate<=entryStateDate 318 && this.toRevisionDate>=entryStateDate); 319 } 320 321 326 protected void setRevisionTitle(RevisionEntrySet rev) { 327 328 String title = ""; 329 try { 330 ContentObject contentObject 331 = (ContentObject) JahiaObject.getInstance(rev.getObjectKey()); 332 String langCode = ""; 333 Locale locale = this.getEntryLoadRequest().getFirstLocale(true); 334 if (locale != null ){ 335 langCode = locale.toString(); 336 } 337 ContentObjectEntryState entryState = 338 new ContentObjectEntryState(ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, 340 rev.getVersionID(),langCode); 341 EntryLoadRequest loadRequest = new EntryLoadRequest(entryState); 342 ContentDefinition definition = 343 ContentDefinition.getInstance( 344 contentObject.getDefinitionKey(loadRequest)); 345 if ( definition != null ){ 346 title = definition.getTitle(contentObject,entryState); 347 } else if ( contentObject instanceof ContentPage ){ 348 title = ((ContentPage)contentObject).getTitle(loadRequest); 349 } 350 } catch ( Throwable t ){ 351 logger.debug("Exception retrieving content object title",t); 352 } 353 if ( title == null ){ 354 title = rev.getObjectKey().toString(); 355 } 356 rev.setProperty(RevisionEntrySet.REVISION_TITLE,title); 357 } 358 359 365 protected void addRevision(RevisionEntry revisionEntry, 366 RevisionEntrySet revisionEntrySet, 367 ArrayList revisions){ 368 369 390 391 int revIndex = revisions.indexOf(revisionEntrySet); 392 RevisionEntrySet res = null; 393 if ( revIndex == -1 ){ 394 revIndex = revisionsList.indexOf(revisionEntrySet); 395 if ( revIndex != -1 ){ 396 res = (RevisionEntrySet)revisionsList.get(revIndex); 397 } 398 } else { 399 res = (RevisionEntrySet)revisions.get(revIndex); 400 } 401 if ( res != null ){ 402 res.addRevision(revisionEntry); 403 } else { 404 revisionEntrySet.addRevision(revisionEntry); 405 revisions.add(revisionEntrySet); 406 } 407 } 408 409 public void sort(Comparator comparator){ 410 Object [] array = this.getRevisions().toArray(); 411 Arrays.sort(array,comparator); 412 this.revisionsList = new ArrayList (Arrays.asList(array)); 413 } 414 } 415 | Popular Tags |