KickJava   Java API By Example, From Geeks To Geeks.

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


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// DJ 12/04/02
14
//
15

16 package org.jahia.services.version;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ListIterator JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.jahia.services.fields.ContentField;
26 import org.jahia.utils.LanguageCodeConverters;
27
28 /**
29  * Class describing which entry of a field/container/containerlist to
30  * load
31  */

32 public class EntryLoadRequest implements Cloneable JavaDoc, Serializable JavaDoc {
33
34     private static final long serialVersionUID = 3825216878831901437L;
35
36     private int workflowState; // -1=deleted, 0=versioned, 1 = active, >= 2 = staging
37
private int versionID;
38     private List JavaDoc locales;
39
40     final static public int ACTIVE_WORKFLOW_STATE = 1;
41     final static public int STAGING_WORKFLOW_STATE = 2;
42     final static public int WAITING_WORKFLOW_STATE = 3;
43     final static public int VERSIONED_WORKFLOW_STATE = 0;
44     final static public int DELETED_WORKFLOW_STATE = -1;
45
46     final static private List JavaDoc defaultLocaleList;
47     static {
48         defaultLocaleList = new Vector JavaDoc();
49         defaultLocaleList.add(Locale.ENGLISH);
50     }
51
52     final static public EntryLoadRequest CURRENT = new EntryLoadRequest (ACTIVE_WORKFLOW_STATE, 0, (List JavaDoc)((Vector JavaDoc)defaultLocaleList).clone());
53     final static public EntryLoadRequest STAGED = new EntryLoadRequest (STAGING_WORKFLOW_STATE, 0, (List JavaDoc)((Vector JavaDoc)defaultLocaleList).clone());
54     final static public EntryLoadRequest VERSIONED = new EntryLoadRequest (VERSIONED_WORKFLOW_STATE, 0, (List JavaDoc)((Vector JavaDoc)defaultLocaleList).clone());
55     final static public EntryLoadRequest WAITING = new EntryLoadRequest (WAITING_WORKFLOW_STATE, 0, (List JavaDoc)((Vector JavaDoc)defaultLocaleList).clone());
56     final static public EntryLoadRequest DELETED = new EntryLoadRequest (DELETED_WORKFLOW_STATE, 0, (List JavaDoc)((Vector JavaDoc)defaultLocaleList).clone());
57     private boolean withMarkedForDeletion;
58     private boolean withDeleted;
59
60     public EntryLoadRequest(int workflowState_, int versionID_, List JavaDoc locales_) {
61         this.workflowState = workflowState_;
62         this.versionID = versionID_;
63         this.locales = locales_;
64         this.withMarkedForDeletion = false;
65         this.withDeleted = true;
66     }
67
68     /**
69      * Constructor with added parameter to specify whether we also want to load
70      * the content objects that have been marked for deletion when loading
71      * staged entries.
72      * @param workflowState_
73      * @param versionID_
74      * @param locales_
75      * @param withMarkedForDeletion_
76      */

77     public EntryLoadRequest(int workflowState_, int versionID_, List JavaDoc locales_, boolean withMarkedForDeletion_) {
78         this.workflowState = workflowState_;
79         this.versionID = versionID_;
80         this.locales = locales_;
81         this.withMarkedForDeletion = withMarkedForDeletion_;
82         this.withDeleted = true;
83     }
84
85     /**
86      * Copy constructor for EntryLoadRequest
87      * @param sourceRequest the EntryLoadRequest to copy from.
88      */

89     public EntryLoadRequest (EntryLoadRequest sourceRequest) {
90         this(sourceRequest.getWorkflowState(), sourceRequest.getVersionID(),
91              sourceRequest.getLocales(), sourceRequest.isWithMarkedForDeletion());
92     }
93
94     /**
95      * Contructor for any entrystateable.
96      *
97      * @param entryState
98      */

99     public EntryLoadRequest(EntryStateable entryState) {
100         this.workflowState = entryState.getWorkflowState();
101         this.versionID = entryState.getVersionID();
102         this.withMarkedForDeletion = false;
103         this.withDeleted = true;
104         this.locales = new Vector JavaDoc();
105         this.locales.add(org.jahia.utils.LanguageCodeConverters
106                          .languageCodeToLocale(entryState.getLanguageCode()));
107     }
108
109     /**
110      * Contructor for any entrystateable.
111      *
112      * @param entryState
113      * @param withMarkedForDeletion_
114      */

115     public EntryLoadRequest(EntryStateable entryState,
116                             boolean withMarkedForDeletion_) {
117         this.workflowState = entryState.getWorkflowState();
118         this.versionID = entryState.getVersionID();
119         this.withMarkedForDeletion = withMarkedForDeletion_;
120         this.withDeleted = true;
121         this.locales = new Vector JavaDoc();
122         this.locales.add(org.jahia.utils.LanguageCodeConverters
123                          .languageCodeToLocale(entryState.getLanguageCode()));
124     }
125
126     public int getWorkflowState() {
127         return workflowState;
128     }
129
130     public int getVersionID() {
131         return versionID;
132     }
133
134     public boolean isCurrent() {
135         return (workflowState == ACTIVE_WORKFLOW_STATE);
136     }
137
138     public boolean isStaging() {
139         return (workflowState == STAGING_WORKFLOW_STATE);
140     }
141
142     public boolean isWaiting() {
143         return (workflowState == WAITING_WORKFLOW_STATE);
144     }
145
146     public boolean isVersioned() {
147         return (workflowState <= VERSIONED_WORKFLOW_STATE);
148     }
149
150     public boolean isDeleted() {
151         return (workflowState == DELETED_WORKFLOW_STATE);
152     }
153
154     private void setVersionID(int versionID_){
155         this.versionID = versionID_;
156     }
157
158     public String JavaDoc toString() {
159         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
160         result.append("[workflowState=");
161         result.append(workflowState);
162         result.append(" versionID=");
163         result.append(versionID);
164
165         result.append(" languages=");
166         List JavaDoc tempLocales = getLocales();
167         ListIterator JavaDoc localeIter = tempLocales.listIterator();
168         while (localeIter.hasNext()) {
169             Locale JavaDoc curLocale = (Locale JavaDoc) localeIter.next();
170             if ( curLocale != null ){
171                 result.append(curLocale.toString());
172             } else {
173                 result.append("null");
174             }
175             if (localeIter.hasNext()) {
176                 result.append(",");
177             }
178         }
179
180         result.append("]");
181         return result.toString();
182     }
183
184     public List JavaDoc getLocales() {
185         return locales;
186     }
187
188     /**
189      * Return the first available locale
190      *
191      * @param withoutShared if true, return the first Locale that is different than "shared"
192      * @return return the first available locale
193      */

194     public Locale JavaDoc getFirstLocale(boolean withoutShared){
195
196         if ( locales == null ){
197             return null;
198         }
199         Locale JavaDoc resultLocale = null;
200         if ( !withoutShared ){
201             resultLocale = (Locale JavaDoc) locales.get(0);
202             return resultLocale;
203         }
204
205         ListIterator JavaDoc localeIter = locales.listIterator();
206         while (localeIter.hasNext() && (resultLocale == null)) {
207             Locale JavaDoc curLocale = (Locale JavaDoc) localeIter.next();
208             if (!ContentField.SHARED_LANGUAGE.equals(curLocale.toString())) {
209                 resultLocale = curLocale;
210                 break;
211             }
212         }
213
214         return resultLocale;
215     }
216
217     /**
218      * Set the first locale locales.set(1,locale). 0 = shared
219      *
220      * @param languageCode
221      */

222     public void setFirstLocale(String JavaDoc languageCode){
223
224         if ( locales == null || languageCode == null ){
225             return;
226         }
227         Locale JavaDoc locale = (Locale JavaDoc)locales.get(0);
228         if ( !locale.toString().equals(ContentField.SHARED_LANGUAGE) ){
229             locales.set(0,LanguageCodeConverters
230                                  .languageCodeToLocale(languageCode));
231         } else {
232             locales.set(1,LanguageCodeConverters
233                                  .languageCodeToLocale(languageCode));
234         }
235     }
236
237     public void setLocales(List JavaDoc locales_) {
238         this.locales = locales_;
239     }
240     public boolean isWithMarkedForDeletion() {
241         return withMarkedForDeletion;
242     }
243     public void setWithMarkedForDeletion(boolean withMarkedForDeletion_) {
244         this.withMarkedForDeletion = withMarkedForDeletion_;
245     }
246
247     /**
248      * Flag that can be used to return or not deleted content.
249      */

250     public boolean isWithDeleted() {
251         return withDeleted;
252     }
253
254     /**
255      * Flag that can be used to return or not deleted content.
256      */

257     public void setWithDeleted(boolean withDeleted_) {
258         this.withDeleted = withDeleted_;
259     }
260
261     public Object JavaDoc clone(){
262         List JavaDoc tempLocales = new Vector JavaDoc();
263         for ( int i=0; i< locales.size() ; i++ ){
264             Locale JavaDoc loc = (Locale JavaDoc)locales.get(i);
265             tempLocales.add(loc.clone());
266         }
267         EntryLoadRequest clone = new EntryLoadRequest(this.workflowState,this.versionID,tempLocales);
268         clone.setWithDeleted(this.withDeleted);
269         clone.setWithMarkedForDeletion(this.withMarkedForDeletion);
270         return clone;
271     }
272
273     /**
274      * Returns a same load request but in Staging if versionID == 2 or
275      * Versioned for any other value
276      *
277      * @param versionID
278      */

279     public static EntryLoadRequest getEntryLoadRequest(int versionID, String JavaDoc languageCode){
280         List JavaDoc locales = new Vector JavaDoc();
281         locales.add(LanguageCodeConverters.languageCodeToLocale(languageCode));
282         return getEntryLoadRequest(versionID,locales);
283     }
284
285     /**
286      * Returns a same load request but in Staging if versionID == 0 or
287      * Versioned for any other value
288      *
289      * @param versionID
290      */

291     public static EntryLoadRequest getEntryLoadRequest(int versionID, List JavaDoc locales){
292        EntryLoadRequest loadRequest = null;
293        if ( versionID == 2 ){
294            loadRequest = EntryLoadRequest.STAGED;
295            loadRequest.setWithMarkedForDeletion(true);
296        } else if ( versionID == 1 ){
297            loadRequest = EntryLoadRequest.CURRENT;
298            loadRequest.setWithMarkedForDeletion(true);
299        } else {
300            loadRequest = EntryLoadRequest.VERSIONED;
301            loadRequest.setVersionID(versionID);
302        }
303        loadRequest.getLocales().clear();
304        loadRequest.getLocales().addAll(locales);
305        return loadRequest;
306     }
307     
308     public boolean equals(Object JavaDoc obj) {
309         if (obj == this)
310             return true;
311         if (!(obj instanceof EntryLoadRequest))
312             return false;
313
314         EntryLoadRequest otherItem = (EntryLoadRequest) obj;
315
316         return workflowState == otherItem.getWorkflowState()
317                 && versionID == otherItem.getVersionID()
318                 && withMarkedForDeletion == otherItem.isWithMarkedForDeletion()
319                 && locales.equals(otherItem.getLocales());
320     }
321
322 } // end EntryLoadVersion
323
Popular Tags