KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
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 /**
23  *
24  * <p>Title: Undo Staging Content Tree Visitor </p>
25  * <p>Description: </p>
26  * <p>Copyright: Copyright (c) 2002</p>
27  * <p>Company: </p>
28  * @author Khue Nguyen
29  * @version 1.0
30  */

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     // The id of the start page ( which is not the root content object, but the
40
// page for which, we can apply undo staging on referred parent page fields.
41
// We need this information to handle page move issue.
42
private int startPageId = -1;
43
44     /**
45      *
46      * @param rootContentObject ContentObject
47      * @param user JahiaUser
48      * @param loadRequest EntryLoadRequest
49      * @param operationMode String
50      * @param jParams ParamBean
51      */

52     public UndoStagingContentTreeVisitor( ContentObject rootContentObject,
53                                           JahiaUser user,
54                                           EntryLoadRequest loadRequest,
55                                           String JavaDoc operationMode,
56                                           ParamBean jParams){
57         super(user,loadRequest,operationMode);
58         contentTree = new ContentTree(rootContentObject);
59         this.setDescendingPageLevel(0); // never descending in pages childs.
60
this.jParams = jParams;
61         if ( rootContentObject instanceof ContentPage ){
62             this.startPageId = rootContentObject.getID();
63         }
64     }
65
66     /**
67      *
68      * @param rootContentObject ContentObject
69      * @param user JahiaUser
70      * @param loadRequest EntryLoadRequest
71      * @param operationMode String
72      * @param jParams ParamBean
73      * @param startPageId int, precise the page for which we allow undo page move
74      */

75     public UndoStagingContentTreeVisitor( ContentObject rootContentObject,
76                                           JahiaUser user,
77                                           EntryLoadRequest loadRequest,
78                                           String JavaDoc operationMode,
79                                           ParamBean jParams,
80                                           int startPageId){
81         super(user,loadRequest,operationMode);
82         contentTree = new ContentTree(rootContentObject);
83         this.setDescendingPageLevel(0); // never descending in pages childs.
84
this.jParams = jParams;
85         this.startPageId = startPageId;
86     }
87
88     /**
89      * Start undo staging
90      *
91      */

92     public void undoStaging()
93     throws JahiaException{
94         getContentTree().iterate(this);
95     }
96
97     /**
98      * Return the internal ContentTree used to traverse the Content Tree
99      *
100      * @return
101      */

102     public ContentTree getContentTree(){
103         return this.contentTree;
104     }
105
106     /**
107      * Returns a ContentTreeStatus implementation for a given ContentObject
108      *
109      * @param contentObject
110      * @return
111      */

112     public ContentTreeStatusInterface getContentTreeStatus(ContentObject contentObject,
113             int currentPageLevel)
114     throws JahiaException{
115         ContentTreeStatus contentTreeStatus = new ContentTreeStatus();
116         return (ContentTreeStatusInterface)contentTreeStatus;
117     }
118
119     /**
120      * process the current content object when traversing the tree
121      *
122      * @param contentObject
123      * @param currentPageLevel
124      * @throws JahiaException
125      */

126     public void processContentObjectBeforeChilds(ContentObject contentObject,
127                                                  int currentPageLevel)
128     throws JahiaException{
129     }
130
131     /**
132      * Called after processing the current object's childs when traversing the tree
133      *
134      * @param contentObject
135      * @param currentPageLevel
136      * @throws JahiaException
137      */

138     public void processContentObjectAfterChilds(
139             ContentObject contentObject, int currentPageLevel)
140     throws JahiaException{
141
142         if ( contentObject instanceof ContentPage ){
143             ContentPage page = (ContentPage)contentObject;
144             // we must undo all staging page field linked with this page
145
// ( case of moved page ).
146
contentObject.undoStaging(this.jParams);
147
148             Vector JavaDoc ids = ServicesRegistry.getInstance().getJahiaPageService()
149                 .getStagingAndActivePageFieldIDs(page.getID());
150             Iterator JavaDoc iterator = ids.iterator();
151             Integer JavaDoc id = null;
152             ContentObject pageField = null;
153             while ( iterator.hasNext() ){
154                 id = (Integer JavaDoc)iterator.next();
155                 pageField = ContentPageField.getField(id.intValue());
156                 // undoStagingDeletedContainer(pageField);
157
// undo the parent container too
158
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 JavaDoc t ){
168                     logger.debug(t);
169                 }
170             }
171             // restore the old parent container for the page too
172
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     /**
199      * Process the Last Content page Field on which, we reached the page level limit when
200      * descending in subtree.
201      *
202      * @param contentObject the last content page field on which we reach the page level limit.
203      * @param currentPageLevel
204      * @throws JahiaException
205      */

206     public void processLastContentPageField(
207             ContentObject contentObject,
208             int currentPageLevel)
209     throws JahiaException{
210
211         ContentPage page = null;
212         String JavaDoc 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                     // no page attached with this page field
219
contentObject.undoStaging(this.jParams);
220                     return;
221                 } else {
222                     page = ContentPage.getPage(pageId);
223                 }
224             } catch ( Throwable JavaDoc t ){
225             }
226         }
227
228         if ( page == null ){
229             // if the page doesn't exist, we just undo this field
230
contentObject.undoStaging(this.jParams);
231             return;
232         }
233
234         // we handle differently direct and link page
235
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                     // this page field exists only in staging,
245
// we avoid undo staging if the start page is not the one pointed by this page field
246
ContentPageField contentPageField = (ContentPageField)
247                         contentObject;
248                     if (contentPageField.getContainerID() > 0) {
249                         // we don't allow the parent container to undo staging
250
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                     // There is an issue with container containing more than one direct page
264
// If one of these page field reach this point, set the parentStatus to not continue
265
// after child will never undo the container !!
266
// We can do so only if the startPageId does not have the same parent container as this
267
// page
268
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                                             // We allows the container to undo staging
288
return;
289                                         }
290                                     }
291                                 } catch ( Throwable JavaDoc t ){
292                                     logger.debug(t);
293                                 }
294
295                             }
296                         }
297
298                     }
299                     // in all other case, we don't allow the parent container to undo staging
300
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             // not a direct page, so we can undo the page field and the page link
315
// too.
316
contentObject.undoStaging(jParams);
317             page.undoStaging(jParams);
318         }
319     }
320
321     /**
322      * Returns an array list of childs for a given ContentObject
323      *
324      * @param contentObject
325      * @param currentPageLevel
326      * @return
327      */

328     public ArrayList JavaDoc getChilds(ContentObject contentObject,
329                                int currentPageLevel)
330     throws JahiaException {
331
332         return super.getChilds(contentObject,currentPageLevel);
333     }
334
335     /**
336      * Undo Staging on container containing a page field marked for delete.
337      *
338      * @param contentObject
339      */

340     private void undoStagingDeletedContainer(ContentObject contentObject)
341     throws JahiaException {
342         SortedSet JavaDoc entryStates = contentObject.getActiveAndStagingEntryStates();
343         Iterator JavaDoc 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                 // was marked for delete, so undo mark for delete on parent container
354
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                 // undo staging on page field too
365
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     /**
380      * Perform undo of a page move
381      *
382      * @param pageId int
383      * @param rootContentObject ContentObject
384      */

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