KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.services.version;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.SortedSet JavaDoc;
6
7 import org.jahia.content.ContentObject;
8 import org.jahia.content.ContentTreeStatusInterface;
9 import org.jahia.content.ObjectKey;
10 import org.jahia.content.PageReferenceableInterface;
11 import org.jahia.exceptions.JahiaException;
12 import org.jahia.services.fields.ContentField;
13 import org.jahia.services.fields.ContentPageField;
14 import org.jahia.services.pages.ContentPage;
15 import org.jahia.services.usermanager.JahiaUser;
16
17 /**
18  *
19  * <p>Title: Concrete visitor for handling Content object revisions at page level</p>
20  * <p>Description: </p>
21  * <p>Copyright: Copyright (c) 2002</p>
22  * <p>Company: </p>
23  * @author Khue Nguyen
24  * @version 1.0
25  */

26 public class PageRevisionsCompositor extends ContentTreeRevisionsVisitor {
27
28     private static org.apache.log4j.Logger logger =
29             org.apache.log4j.Logger.getLogger(PageRevisionsCompositor.class);
30
31     public static final int ALL_REVISIONS = 0;
32     public static final int CURRENT_PAGE_MODIFICATION_REVISIONS = 1;
33     public static final int PAGES_MODIFICATION_REVISIONS = 2;
34     public static final int CURRENT_PAGE_FIELDS_AND_CONTAINERS_REVISIONS = 3;
35     public static final int PAGES_FIELDS_AND_CONTAINERS_REVISIONS = 4;
36
37     private int typeOfRevisions = PAGES_MODIFICATION_REVISIONS;
38
39     /**
40      *
41      * @param rootContentObject
42      * @param user
43      * @param loadRequest
44      * @param operationMode
45      */

46     public PageRevisionsCompositor(ContentObject rootContentObject,
47                                         JahiaUser user,
48                                         EntryLoadRequest loadRequest,
49                                         String JavaDoc operationMode){
50         super(rootContentObject,user,loadRequest,operationMode);
51     }
52
53     /**
54      * process the current content object when traversing the tree
55      *
56      * @param contentObject
57      * @param currentPageLevel
58      * @throws JahiaException
59      */

60     public void processContentObjectBeforeChilds(ContentObject contentObject, int currentPageLevel)
61     throws JahiaException{
62
63         ContentTreeStatusInterface contentTreeStatus = (ContentTreeStatusInterface)
64             this.getContentTree().getContentTreeStatusStack().peek();
65
66         //check permission
67
if ( !contentObject.checkAdminAccess(this.getUser())
68              && !contentObject.checkWriteAccess(this.getUser()) ){
69             // stop processing childs too.
70
contentTreeStatus.setContinueWithChilds(false);
71             contentTreeStatus.setContinueAfterChilds(false);
72             return;
73         }
74
75         // check to skip revisions of deleted content or not
76
boolean hasActiveEntries
77                 = contentObject.hasActiveEntries();
78         if ( !this.isWithDeletedContent() && !hasActiveEntries ){
79             // stop processing childs too.
80
contentTreeStatus.setContinueWithChilds(false);
81             contentTreeStatus.setContinueAfterChilds(false);
82             return;
83         }
84
85         ArrayList JavaDoc revisions = new ArrayList JavaDoc();
86         SortedSet JavaDoc entryStates =
87                 contentObject.getEntryStates();
88
89         Iterator JavaDoc iterator = entryStates.iterator();
90         while ( iterator.hasNext() ){
91             try {
92                 ContentObjectEntryState entryState =
93                         (ContentObjectEntryState)iterator.next();
94
95                 // skip staging entry
96
if ( entryState.getWorkflowState()
97                      > ContentObjectEntryState.WORKFLOW_STATE_ACTIVE ){
98                     continue;
99                 }
100
101                 // check to skip active revisions or not
102
if ( !this.isWithActiveRevisions() && entryState.getWorkflowState()
103                      == ContentObjectEntryState.WORKFLOW_STATE_ACTIVE ){
104                     continue;
105                 }
106
107                 // check to skip deleted revisions or not
108
if ( !this.isWithDeletedRevisions() && entryState.getWorkflowState()
109                      == ContentObjectEntryState.WORKFLOW_STATE_VERSIONING_DELETED ){
110                     continue;
111                 }
112
113                 if ( inDateRange(entryState) ){
114
115                     // replacing low level revisions to a higher level of revisions
116
// that are more usefull and easier to understand by the end user.
117
// Group all revisions as Page revisions
118

119                     // skip Content Page Field revisions
120
if ( contentObject instanceof ContentPageField ){
121                         if ( ((ContentField)contentObject).getContainerID() == 0 ){
122                             continue;
123                         }
124                     }
125
126                     RevisionEntrySet revisionEntrySet = null;
127
128                     RevisionEntry revisionEntry = new RevisionEntry(
129                             new ContentObjectEntryState(
130                             entryState.getWorkflowState(),
131                             entryState.getVersionID(),
132                             entryState.getLanguageCode()),
133                             ObjectKey.getInstance(contentObject.getObjectKey().getType()
134                             + "_" + contentObject.getObjectKey().getIDInType()));
135
136                     if ( !(contentObject instanceof ContentPage) ){
137                         ContentPage objectPage =
138                                 ((PageReferenceableInterface)contentObject).getPage();
139
140                         revisionEntrySet = new PageRevisionEntrySet(
141                                 entryState.getVersionID(),
142                                 ObjectKey.getInstance(objectPage.getObjectKey().getType()
143                                 + "_" + objectPage.getObjectKey().getIDInType()));
144                         setRevisionTitle(revisionEntrySet);
145
146                         addRevision(revisionEntry, revisionEntrySet, revisions);
147                         continue;
148                     }
149
150                     /*
151                     // Replace ContentField by ContentContainer revision.
152                     try {
153                         int ctnID =
154                                 ((ContentField) contentObject).getContainerID();
155                         if ( ctnID != 0 ){
156                             ContentContainer parentContainer =
157                                     ContentContainer.getContainer(ctnID);
158
159                             revisionEntrySet = new RevisionEntrySet(
160                                     entryState.getWorkflowState(),
161                                     entryState.getVersionID(),
162                                     ObjectKey.getInstance(parentContainer.getObjectKey().getType()
163                                     + "_" + parentContainer.getObjectKey().getIDInType()));
164                             setRevisionTitle(revisionEntrySet);
165
166                             RevisionEntry revisionEntry = new RevisionEntry(
167                                     new ContentObjectEntryState(
168                                     entryState.getWorkflowState(),
169                                     entryState.getVersionID(),
170                                     entryState.getLanguageCode()),
171                                     ObjectKey.getInstance(parentContainer.getObjectKey().getType()
172                                     + "_" + parentContainer.getObjectKey().getIDInType()));
173
174                             addRevision(revisionEntry, revisionEntrySet, revisions);
175                             continue;
176                         }
177                     } catch ( Throwable t ){
178                         //t.printStackTrace();
179                     }
180                     */

181
182                     // default
183
revisionEntrySet = new PageRevisionEntrySet( entryState.getVersionID(),
184                             ObjectKey.getInstance(contentObject.getObjectKey().getType()
185                             + "_" + contentObject.getObjectKey().getIDInType()));
186                     setRevisionTitle(revisionEntrySet);
187                     addRevision(revisionEntry, revisionEntrySet, revisions);
188                 }
189             } catch ( Throwable JavaDoc t ){
190                 throw new JahiaException("Exception creating revision entry ",
191                         "Exception creating revision entry ",
192                         JahiaException.DATA_ERROR,
193                         JahiaException.DATA_ERROR,t);
194             }
195         }
196
197         this.getRevisions().addAll(revisions);
198     }
199
200     /**
201      * Return the type of revisions
202      * @return
203      */

204     public int getTypeOfRevisions(){
205         return this.typeOfRevisions;
206     }
207
208     /**
209      * Set the type of revisions
210      * @return
211      */

212     public void setTypeOfRevisions(int value){
213         if ( value != this.typeOfRevisions ){
214             this.hasAttributeChange = true;
215         }
216         this.typeOfRevisions = value;
217     }
218 }
219
Popular Tags