KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.services.version;
2
3 import java.util.HashSet JavaDoc;
4 import java.util.Set JavaDoc;
5 import java.util.Stack JavaDoc;
6
7 import org.jahia.content.ObjectKey;
8
9 /**
10  * <p>Title: Contains the "context" of an activation operation</p>
11  * <p>Description: This object is passed to the whole hierarchy of content
12  * objects during an activation process and can be used both as a configuration
13  * bean and as a storage mechanism. Notably it is used to detect activation
14  * "looping" when we have link objects.</p>
15  * <p>Copyright: Copyright (c) 2002</p>
16  * <p>Company: Jahia Solutions Sarl</p>
17  * @author Serge Huber
18  * @version 1.0
19  */

20
21 public class StateModificationContext {
22
23     private static org.apache.log4j.Logger logger =
24             org.apache.log4j.Logger.getLogger(StateModificationContext.class);
25
26     // contains the set of language codes that select the languages for which
27
// we are activating the content objects.
28
private Set JavaDoc languageCodes;
29
30     // specifies whether we do a recursive activation by following all links
31
// to sub pages.
32
private boolean descendingInSubPages = false;
33
34     // contains the current path in the content tree. Used to detect looping
35
// when following links.
36
private Stack JavaDoc currentTreePath = new Stack JavaDoc();
37
38     // specifies whether the operations should be done for all languages or
39
// just some specific ones. This is notably used when performing marking
40
// for deletion operations because we might break content tree nodes.
41
private Stack JavaDoc allLanguagesStack = new Stack JavaDoc();
42
43     /**
44      * Making the default constructor private because we want to enforce the
45      * creation of this object only with valid language codes.
46      */

47     private StateModificationContext() {
48         allLanguagesStack.push(new Boolean JavaDoc(false));
49     }
50
51     /**
52      * Activation context constructor
53      * @param startObject specifies the object at which the state modification
54      * was triggered, giving us a "start point" that we can insert in the
55      * tree path.
56      * @param languageCodes contains a Set of String object that contain the
57      * language codes for which we want to perform an activation of the content
58      * objects.
59      */

60     public StateModificationContext(ObjectKey startObject, Set JavaDoc languageCodes) {
61         this();
62         currentTreePath.push(startObject);
63         if (languageCodes != null) {
64             this.languageCodes = languageCodes;
65         } else {
66             logger.debug("ActivationContext constructor passed null languageCode set !!!");
67             this.languageCodes = new HashSet JavaDoc();
68         }
69     }
70
71     /**
72      * Utility constructor to construct a context that has the descendingInSubPages
73      * boolean directly initialized to a value.
74      * @param startObject specifies the object at which the state modification
75      * was triggered, giving us a "start point" that we can insert in the
76      * tree path.
77      * @param languageCodes contains a Set of String object that contain the
78      * language codes for which we want to perform an activation of the content
79      * objects.
80      * @param descendingInSubPages true specifies that we want the activation
81      * to be recursive and descend in sub pages. This is useful notably to
82      * delete whole subsets of pages.
83      */

84     public StateModificationContext(ObjectKey startObject,
85                                     Set JavaDoc languageCodes, boolean descendingInSubPages) {
86         this(startObject, languageCodes);
87         this.descendingInSubPages = descendingInSubPages;
88     }
89
90     /**
91      * If true it means that the activation will descend in sub pages to activate
92      * them also. Otherwise the activation will not activate fields and containers
93      * that contain subpages that have no active version.
94      * @return see the description for the meaning of the values returned.
95      */

96     public boolean isDescendingInSubPages() {
97         return descendingInSubPages;
98     }
99
100     /**
101      * If true it means that the activation will descend in sub pages to activate
102      * them also. Otherwise the activation will not activate fields and containers
103      * that contain subpages that have no active version.
104      *
105      * @param descendingInSubPages see the description for the meaning of the
106      * values returned.
107      */

108     public void setDescendingInSubPages(boolean descendingInSubPages) {
109         this.descendingInSubPages = descendingInSubPages;
110     }
111
112     /**
113      * Returns the current path in the content object tree of validation. This
114      * can be used to detect loops in the validation process by testing the
115      * position of a content object in the stack.
116      * @return a Stack containing ObjectKey object that contain
117      * identifier to Jahia objects.
118      */

119     public java.util.Stack JavaDoc getCurrentTreePath() {
120         return currentTreePath;
121     }
122
123     /**
124      * Add an object to the current content object path tree.
125      * @param objectID the activation object ID to be added to the stack.
126      */

127     public void pushObjectID(ObjectKey objectID) {
128         currentTreePath.push(objectID);
129     }
130
131     /**
132      * Remove an object from the current content object path tree represented
133      * by a stack. If we try to remove the start object it is returned but NOT
134      * removed from the stack.
135      * @return the object removed from the stack.
136      */

137     public ObjectKey popObjectID() {
138         if (currentTreePath.size() > 1) {
139             return (ObjectKey) currentTreePath.pop();
140         } else {
141             return (ObjectKey) currentTreePath.firstElement();
142         }
143     }
144
145     public ObjectKey getStartObject() {
146         return (ObjectKey) currentTreePath.firstElement();
147     }
148
149     /**
150      * Tests for the presence of an object ID in the current tree path. This
151      * is used to detect looping in the activation process.
152      * @param objectID the object ID to check for presence in the current
153      * content object path tree stack.
154      * @return true if the object ID was found in the path, false otherwise.
155      */

156     public boolean isObjectIDInPath(ObjectKey objectID) {
157         return currentTreePath.contains(objectID);
158     }
159
160     public Set JavaDoc getLanguageCodes() {
161         return languageCodes;
162     }
163
164     public String JavaDoc toString() {
165         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
166         result.append("stateModifContext=[");
167         result.append("descendingInSubPages=");
168         result.append(isDescendingInSubPages());
169         result.append(",currentTreePath=");
170         result.append(currentTreePath.toString());
171         result.append(",languageCodes=");
172         result.append(languageCodes.toString());
173         result.append("]");
174         return result.toString();
175     }
176
177     public boolean isAllLanguages() {
178         Boolean JavaDoc firstElement = (Boolean JavaDoc) allLanguagesStack.peek();
179         return firstElement.booleanValue();
180     }
181
182     /**
183      * Specifies whether all languages should be targeted by this operation.
184      * This is more significant than the language code set attached to this
185      * object.
186      * @param allLanguages set to true if all objects should be affected from
187      * here on.
188      */

189     public void pushAllLanguages(boolean allLanguages) {
190         if (allLanguages) {
191             logger.debug("Warning : all languages for content object will be "+
192                          "deleted from here down !");
193         }
194         this.allLanguagesStack.push(new Boolean JavaDoc(allLanguages));
195     }
196
197     public boolean popAllLanguages() {
198         Boolean JavaDoc removedElement = (Boolean JavaDoc) allLanguagesStack.pop();
199         return removedElement.booleanValue();
200     }
201
202 }
Popular Tags