KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > version > ContentTreeRevisionsVisitor


1 package org.jahia.services.version;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Arrays JavaDoc;
5 import java.util.Comparator JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Locale JavaDoc;
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 /**
21  *
22  * <p>Title: Concrete visitor for handling Content object revisions </p>
23  * <p>Description: </p>
24  * <p>Copyright: Copyright (c) 2002</p>
25  * <p>Company: </p>
26  * @author Khue Nguyen
27  * @version 1.0
28  */

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 JavaDoc revisionsList;
36     protected HashMap JavaDoc revisionsStates;
37
38     /** lower revision date limit **/
39     protected long fromRevisionDate;
40
41     /** upper revision date limit **/
42     protected long toRevisionDate;
43
44     /** ignore or not revisions of content currently deleted **/
45     protected boolean withDeletedContent;
46
47     /** ignore or not revisions with active status **/
48     protected boolean withActiveRevisions = true;
49
50     /** ignore or not revisions with deleted status **/
51     protected boolean withDeletedRevisions = true;
52
53     protected boolean hasAttributeChange = true;
54
55     protected String JavaDoc lastSortLanguageCode = "";
56     protected int lastSortAttribute = -1;
57     protected int lastSortOrder = -1;
58
59     /**
60      *
61      * @param rootContentObject
62      * @param user
63      * @param loadRequest
64      * @param operationMode
65      */

66     public ContentTreeRevisionsVisitor(ContentObject rootContentObject,
67                                         JahiaUser user,
68                                         EntryLoadRequest loadRequest,
69                                         String JavaDoc operationMode){
70         super(user,loadRequest,operationMode);
71         this.revisionsList = new ArrayList JavaDoc();
72         this.revisionsStates = new HashMap JavaDoc();
73         this.contentTree = new ContentTree(rootContentObject);
74     }
75
76     /**
77      * Set the internal user used to check rights access
78      * @param user
79      */

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     /**
88      * Set the EntryLoadRequest used to retrieve's Content Object Childs.
89      * @param loadRequest
90      */

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     /**
99      * Set the internal OperationMode used to retrieve's Content Object Childs.
100      * @param operationMode
101      */

102     public void setOperationMode(String JavaDoc operationMode){
103         if ( !getOperationMode().equals(operationMode) ){
104             this.hasAttributeChange = true;
105         }
106         super.setOperationMode(operationMode);
107     }
108
109     /**
110      * Set the descending page level.
111      */

112     public void setDescendingPageLevel(int level){
113         if ( getDescendingPageLevel() != level ){
114             this.hasAttributeChange = true;
115         }
116         super.setDescendingPageLevel(level);
117     }
118
119     /**
120      * Return the internal ContentTree used to traverse the Content Tree
121      * @return
122      */

123     public ContentTree getContentTree(){
124         return this.contentTree;
125     }
126
127     /**
128      * Return the internal list of revisions
129      * @return
130      */

131     public ArrayList JavaDoc getRevisions(){
132         return this.revisionsList;
133     }
134
135     /**
136      * process the current content object when traversing the tree
137      *
138      * @param contentObject
139      * @param currentPageLevel
140      * @throws JahiaException
141      */

142     public abstract void processContentObjectBeforeChilds(ContentObject contentObject,
143         int currentPageLevel)
144     throws JahiaException;
145
146     /**
147      * Returns a ContentTreeStatus implementation for a given ContentObject
148      *
149      * @param contentObject
150      * @return
151      */

152     public ContentTreeStatusInterface getContentTreeStatus(ContentObject contentObject,
153             int currentPageLevel)
154     throws JahiaException{
155         ContentTreeStatus contentTreeStatus = new ContentTreeStatus();
156         return (ContentTreeStatusInterface)contentTreeStatus;
157     }
158
159     /**
160      * Returns an array list of childs for a given ContentObject
161      *
162      * @param contentObject
163      * @param currentPageLevel
164      * @return
165      */

166     public ArrayList JavaDoc getChilds(ContentObject contentObject,
167                                         int currentPageLevel)
168     throws JahiaException{
169
170         ArrayList JavaDoc results = new ArrayList JavaDoc();
171         /*
172         if ( contentObject instanceof ContentPageField ){
173             if ( this.getDescendingPageLevel() >=0
174                  && this.getDescendingPageLevel()>=currentPageLevel ){
175                 return results;
176             }
177             return results;
178         }*/

179         return super.getChilds(contentObject,currentPageLevel);
180     }
181
182     /**
183      * Reload the revisions
184      *
185      * @param ignoreAttributeChange if true, ignore the state returned by hasChange
186      */

187     public void loadRevisions(boolean ignoreAttributeChange)
188     throws JahiaException{
189         if ( ignoreAttributeChange || hasAttributeChange() ){
190             // have to reload the content root object
191
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 JavaDoc();
197                 getContentTree().iterate(this);
198             } catch ( Throwable JavaDoc t ){
199                 logger.debug("Exception when trying to reload Revisions", t);
200             }
201
202             // filter out all revisions at which the rootObject is deleted
203
ArrayList JavaDoc result = new ArrayList JavaDoc();
204             ArrayList JavaDoc 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     /**
221      * Sort the revisions
222      *
223      * @param languageCode
224      * @param sortAttribute
225      * @param sortOrder
226      * @param ignoreSortAttributeChange
227      * @throws JahiaException
228      */

229     public void sortRevisions(String JavaDoc 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     /**
246      * Returns true if one of the attributes used to retrieve revisions has changed
247      * @return
248      */

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     /**
309      * Returns true if the entry state is inside the date range
310      *
311      * @param entryState
312      * @return
313      */

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     /**
322      *
323      * @param rev
324      * @return
325      */

326     protected void setRevisionTitle(RevisionEntrySet rev) {
327
328        String JavaDoc title = "";
329        try {
330            ContentObject contentObject
331                = (ContentObject) JahiaObject.getInstance(rev.getObjectKey());
332            String JavaDoc langCode = "";
333            Locale JavaDoc locale = this.getEntryLoadRequest().getFirstLocale(true);
334            if (locale != null ){
335                langCode = locale.toString();
336            }
337            ContentObjectEntryState entryState =
338                    //new ContentObjectEntryState(rev.getWorkflowState(),
339
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 JavaDoc 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     /**
360      *
361      * @param revisionEntry
362      * @param revisionEntrySet
363      * @param revisions
364      */

365     protected void addRevision(RevisionEntry revisionEntry,
366                              RevisionEntrySet revisionEntrySet,
367                              ArrayList JavaDoc revisions){
368
369         // ensure to remove the active entry if the one to add is versioned
370
/*
371         if ( revisionEntry.getWorkflowState() <
372              ContentObjectEntryState.WORKFLOW_STATE_ACTIVE ){
373             RevisionEntrySet res = (RevisionEntrySet) this.revisionsStates
374                                  .get(new Integer(revisionEntrySet.getVersionID()));
375
376             int revIndex = revisions.indexOf(res);
377             if ( revIndex != -1 ){
378                 revisions.remove(revIndex);
379             }
380             revIndex = revisionsList.indexOf(res);
381             if ( revIndex != -1 ){
382                 revisionsList.remove(revIndex);
383             }
384
385             if ( res != null ){
386                 new RevisionEntry(revisionEntry,revisionEntry.getObjectKey());
387             }
388         }
389         */

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 JavaDoc comparator){
410         Object JavaDoc[] array = this.getRevisions().toArray();
411         Arrays.sort(array,comparator);
412         this.revisionsList = new ArrayList JavaDoc(Arrays.asList(array));
413     }
414 }
415
Popular Tags