1 16 package org.jahia.services.version; 17 18 import java.io.Serializable ; 19 import java.util.List ; 20 import java.util.ListIterator ; 21 import java.util.Locale ; 22 import java.util.Vector ; 23 24 import org.apache.log4j.Logger; 25 import org.jahia.services.fields.ContentField; 26 import org.jahia.utils.LanguageCodeConverters; 27 28 32 public class EntryLoadRequest implements Cloneable , Serializable { 33 34 private static final long serialVersionUID = 3825216878831901437L; 35 36 private int workflowState; private int versionID; 38 private List 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 defaultLocaleList; 47 static { 48 defaultLocaleList = new Vector (); 49 defaultLocaleList.add(Locale.ENGLISH); 50 } 51 52 final static public EntryLoadRequest CURRENT = new EntryLoadRequest (ACTIVE_WORKFLOW_STATE, 0, (List )((Vector )defaultLocaleList).clone()); 53 final static public EntryLoadRequest STAGED = new EntryLoadRequest (STAGING_WORKFLOW_STATE, 0, (List )((Vector )defaultLocaleList).clone()); 54 final static public EntryLoadRequest VERSIONED = new EntryLoadRequest (VERSIONED_WORKFLOW_STATE, 0, (List )((Vector )defaultLocaleList).clone()); 55 final static public EntryLoadRequest WAITING = new EntryLoadRequest (WAITING_WORKFLOW_STATE, 0, (List )((Vector )defaultLocaleList).clone()); 56 final static public EntryLoadRequest DELETED = new EntryLoadRequest (DELETED_WORKFLOW_STATE, 0, (List )((Vector )defaultLocaleList).clone()); 57 private boolean withMarkedForDeletion; 58 private boolean withDeleted; 59 60 public EntryLoadRequest(int workflowState_, int versionID_, List locales_) { 61 this.workflowState = workflowState_; 62 this.versionID = versionID_; 63 this.locales = locales_; 64 this.withMarkedForDeletion = false; 65 this.withDeleted = true; 66 } 67 68 77 public EntryLoadRequest(int workflowState_, int versionID_, List 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 89 public EntryLoadRequest (EntryLoadRequest sourceRequest) { 90 this(sourceRequest.getWorkflowState(), sourceRequest.getVersionID(), 91 sourceRequest.getLocales(), sourceRequest.isWithMarkedForDeletion()); 92 } 93 94 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 (); 105 this.locales.add(org.jahia.utils.LanguageCodeConverters 106 .languageCodeToLocale(entryState.getLanguageCode())); 107 } 108 109 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 (); 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 toString() { 159 StringBuffer result = new StringBuffer (); 160 result.append("[workflowState="); 161 result.append(workflowState); 162 result.append(" versionID="); 163 result.append(versionID); 164 165 result.append(" languages="); 166 List tempLocales = getLocales(); 167 ListIterator localeIter = tempLocales.listIterator(); 168 while (localeIter.hasNext()) { 169 Locale curLocale = (Locale ) 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 getLocales() { 185 return locales; 186 } 187 188 194 public Locale getFirstLocale(boolean withoutShared){ 195 196 if ( locales == null ){ 197 return null; 198 } 199 Locale resultLocale = null; 200 if ( !withoutShared ){ 201 resultLocale = (Locale ) locales.get(0); 202 return resultLocale; 203 } 204 205 ListIterator localeIter = locales.listIterator(); 206 while (localeIter.hasNext() && (resultLocale == null)) { 207 Locale curLocale = (Locale ) localeIter.next(); 208 if (!ContentField.SHARED_LANGUAGE.equals(curLocale.toString())) { 209 resultLocale = curLocale; 210 break; 211 } 212 } 213 214 return resultLocale; 215 } 216 217 222 public void setFirstLocale(String languageCode){ 223 224 if ( locales == null || languageCode == null ){ 225 return; 226 } 227 Locale locale = (Locale )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 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 250 public boolean isWithDeleted() { 251 return withDeleted; 252 } 253 254 257 public void setWithDeleted(boolean withDeleted_) { 258 this.withDeleted = withDeleted_; 259 } 260 261 public Object clone(){ 262 List tempLocales = new Vector (); 263 for ( int i=0; i< locales.size() ; i++ ){ 264 Locale loc = (Locale )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 279 public static EntryLoadRequest getEntryLoadRequest(int versionID, String languageCode){ 280 List locales = new Vector (); 281 locales.add(LanguageCodeConverters.languageCodeToLocale(languageCode)); 282 return getEntryLoadRequest(versionID,locales); 283 } 284 285 291 public static EntryLoadRequest getEntryLoadRequest(int versionID, List 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 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 } | Popular Tags |