1 13 package org.jahia.data.fields; 14 15 import java.io.Serializable ; 16 import java.util.ArrayList ; 17 import java.util.Enumeration ; 18 import java.util.Hashtable ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Locale ; 22 import java.util.Vector ; 23 24 import org.apache.log4j.Logger; 25 import org.jahia.data.FormDataManager; 26 import org.jahia.exceptions.JahiaException; 27 import org.jahia.params.ParamBean; 28 import org.jahia.registries.JahiaFieldDefinitionsRegistry; 29 import org.jahia.registries.ServicesRegistry; 30 import org.jahia.services.fields.ContentField; 31 import org.jahia.services.fields.PublicContentFieldEntryState; 32 import org.jahia.services.version.ContentObjectEntryState; 33 import org.jahia.services.version.EntryLoadRequest; 34 import org.jahia.utils.LanguageCodeConverters; 35 36 42 public class JahiaContentFieldFacade implements Serializable { 43 44 private static Logger logger = Logger.getLogger(JahiaContentFieldFacade.class); 45 46 private int fieldID = -1; 47 48 private Vector activeAndStagingEntryStates; 51 52 private Hashtable fields; 53 54 private List locales; 55 56 67 public JahiaContentFieldFacade( int aFieldID, 68 int loadFlag, 69 ParamBean jParams, 70 List localeList, 71 boolean createFieldForMissingLanguage ) 72 throws JahiaException 73 { 74 this.fieldID = aFieldID; 75 this.fields = new Hashtable (); 76 this.activeAndStagingEntryStates = new Vector (); 77 this.locales = localeList; 78 instanceFields(loadFlag, jParams, localeList, createFieldForMissingLanguage); 79 } 80 81 87 public JahiaContentFieldFacade ( int aFieldID, 88 int jahiaID, 89 int pageID, 90 int ctnID, 91 int fieldDefID, 92 int fieldType, 93 int connectType, 94 String fieldValue, 95 int aclID, 96 ParamBean jParams, 97 List localeList ) 98 throws JahiaException 99 { 100 101 this.fieldID = aFieldID; 102 this.fields = new Hashtable (); 103 this.activeAndStagingEntryStates = new Vector (); 104 this.locales = localeList; 105 createFieldForMissingLanguage( aFieldID, 106 jahiaID, 107 pageID, 108 ctnID, 109 fieldDefID, 110 fieldType, 111 connectType, 112 fieldValue, 113 aclID, 114 jParams, 115 localeList ); 116 } 117 118 public Enumeration getFields(){ 120 return fields.elements(); 121 } 122 123 124 131 public JahiaField getField( EntryLoadRequest entryLoadRequest, 132 boolean activeIfStagingNotFound ){ 133 134 logger.debug("EntryLoadRequest :" + entryLoadRequest.toString()); 135 136 Locale locale = entryLoadRequest.getFirstLocale(true); 137 if ( locale != null ){ 138 logger.debug("EntryLoadRequest locale :" + locale.toString()); 139 } else { 140 logger.debug("EntryLoadRequest locale is null !?"); 141 } 142 143 ContentObjectEntryState entryState = 144 (ContentObjectEntryState)ServicesRegistry.getInstance() 145 .getJahiaVersionService() 146 .resolveEntry(activeAndStagingEntryStates, 147 entryLoadRequest); 148 149 if ( entryState != null ){ 150 logger.debug("Resolved entryState :" + entryState.toString()); 151 } 152 if ( entryLoadRequest.isStaging() && entryState == null 153 && activeIfStagingNotFound ){ 154 155 EntryLoadRequest newEntryLoadRequest = new EntryLoadRequest( 156 ContentObjectEntryState.WORKFLOW_STATE_ACTIVE, 157 0, 158 entryLoadRequest.getLocales()); 159 160 entryState = (ContentObjectEntryState)ServicesRegistry.getInstance() 161 .getJahiaVersionService() 162 .resolveEntry(activeAndStagingEntryStates, 163 newEntryLoadRequest); 164 } else if ( entryLoadRequest.isStaging() && entryState != null 165 && entryState.isActive() && !activeIfStagingNotFound ){ 166 return null; 168 } 169 JahiaField field = null; 170 if ( entryState != null ){ 171 field = 172 (JahiaField)fields.get(new PublicContentFieldEntryState(entryState)); 173 } 174 175 if ( field != null ){ 176 logger.debug("Returned entryState :" + entryState.toString()); 177 logger.debug("Field Value :" + field.getValue() 178 + ", langCode=" + field.getLanguageCode()); 179 } else { 180 logger.debug("Returned entryState is null "); 181 } 182 return field; 183 } 184 185 private void instanceFields( int loadFlag, 187 ParamBean jParams, 188 List localeList, 189 boolean createFieldForMissingLanguage ) 190 throws JahiaException 191 { 192 193 ContentField contentField = ContentField.getField(fieldID); 194 EntryLoadRequest elr = null; 195 Vector v = new Vector (); 196 v.addAll(contentField.getActiveAndStagingEntryStates()); 197 Enumeration entryStates = v.elements(); 198 ContentObjectEntryState entryState = null; 199 while ( entryStates.hasMoreElements() ) 200 { 201 entryState = (ContentObjectEntryState)entryStates.nextElement(); 202 203 PublicContentFieldEntryState entryStateKey = 204 new PublicContentFieldEntryState(entryState); 205 206 ArrayList entryLocales = new ArrayList (); 207 entryLocales.add(LanguageCodeConverters 208 .languageCodeToLocale(entryState.getLanguageCode())); 209 elr = new EntryLoadRequest(entryState.getWorkflowState(), 210 entryState.getVersionID(), 211 entryLocales); 212 elr.setWithMarkedForDeletion(true); 213 214 try { 215 jParams.setSubstituteEntryLoadRequest(elr); 216 JahiaField field = ServicesRegistry.getInstance() 217 .getJahiaFieldService() 218 .loadField(fieldID, 219 loadFlag, 220 jParams, 221 elr); 222 jParams.resetSubstituteEntryLoadRequest(); 223 if ( field != null ){ 224 String rawValue = field.getRawValue(); 225 if ( rawValue == null ){ 226 rawValue = ""; 227 } 228 field.setValue( rawValue ); 229 fields.put(entryStateKey,field); 230 activeAndStagingEntryStates.add(entryStateKey); 231 } 232 } catch ( Throwable t ){ 233 t.printStackTrace(); 234 } 235 } 236 237 if ( createFieldForMissingLanguage ) 238 { 239 240 JahiaFieldDefinition fieldDef = 241 JahiaFieldDefinitionsRegistry 242 .getInstance() 243 .getDefinition( contentField.getFieldDefID() ); 244 245 String fieldValue = 246 fieldDef.getDefaultValue( jParams.getPage().getPageTemplateID() ); 247 248 createFieldForMissingLanguage( contentField.getID(), 249 contentField.getSiteID(), 250 contentField.getPageID(), 251 contentField.getContainerID(), 252 contentField.getFieldDefID(), 253 contentField.getType(), 254 contentField.getConnectType(), 255 FormDataManager.getInstance().htmlEncode(fieldValue), 256 contentField.getAclID(), 257 jParams, 258 localeList ); 259 260 } 261 } 262 263 private void createFieldForMissingLanguage( int aFieldID, 265 int jahiaID, 266 int pageID, 267 int ctnID, 268 int fieldDefID, 269 int fieldType, 270 int connectType, 271 String fieldValue, 272 int aclID, 273 ParamBean jParams, 274 List localeList ) 275 throws JahiaException 276 { 277 EntryLoadRequest entryLoadRequest = null; 278 ContentObjectEntryState entryState = null; 279 280 Iterator iterator = localeList.iterator(); 282 Locale locale = null; 283 while ( iterator.hasNext() ) 284 { 285 locale = (Locale )iterator.next(); 286 287 ArrayList entryLocales = new ArrayList (); 288 entryLocales.add(locale); 289 290 entryLoadRequest = new EntryLoadRequest( 291 ContentObjectEntryState.WORKFLOW_STATE_ACTIVE, 292 0, entryLocales); 293 294 entryState = (ContentObjectEntryState)ServicesRegistry.getInstance() 295 .getJahiaVersionService() 296 .resolveEntry(activeAndStagingEntryStates, 297 entryLoadRequest); 298 if ( entryState == null ){ 299 300 entryLoadRequest = new EntryLoadRequest( 301 ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, 302 0, entryLocales); 303 entryState = (ContentObjectEntryState)ServicesRegistry.getInstance() 304 .getJahiaVersionService() 305 .resolveEntry(activeAndStagingEntryStates, 306 entryLoadRequest); 307 } 308 if ( ( entryState == null ) || 309 ((!entryState.getLanguageCode().equals("shared")) && 310 (!entryState.getLanguageCode().equals(locale.toString()))) ) { 311 316 JahiaField field = 318 ServicesRegistry.getInstance() 319 .getJahiaFieldService() 320 .createJahiaField( 321 aFieldID, 322 jahiaID, 323 pageID, 324 ctnID, 325 fieldDefID, 326 fieldType, 327 connectType, 328 fieldValue, 329 0, aclID, 331 0, ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, 333 locale.toString()); 334 335 PublicContentFieldEntryState entryStateKey = 336 new PublicContentFieldEntryState( 337 ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, 338 0, 339 field.getLanguageCode()); 340 341 if ( fields.get(entryStateKey) == null ){ 342 activeAndStagingEntryStates.add(entryStateKey); 343 } 344 field.setRawValue(field.getValue()); 345 fields.put(entryStateKey,field); 346 347 logger.debug( "Created new Field for entryState :" 348 + entryStateKey.toString() + " langCode=" 349 + locale.toString()); 350 } 351 } 352 } 353 354 360 public int getFieldID(){ 361 return this.fieldID; 362 } 363 364 371 public void setFieldID(int aFieldID){ 372 Enumeration enumeration = getFields(); 373 JahiaField field = null; 374 while ( enumeration.hasMoreElements() ){ 375 field = (JahiaField)enumeration.nextElement(); 376 field.setID(aFieldID); 377 } 378 } 379 380 387 public void setAclID(int aclID){ 388 Enumeration enumeration = getFields(); 389 JahiaField field = null; 390 while ( enumeration.hasMoreElements() ){ 391 field = (JahiaField)enumeration.nextElement(); 392 field.setAclID(aclID); 393 } 394 } 395 396 401 public JahiaContentFieldFacade changeType(int type, 402 ParamBean jParams) throws JahiaException { 403 JahiaField aField = (JahiaField)this.fields.values().iterator().next(); 404 JahiaContentFieldFacade facade = 405 new JahiaContentFieldFacade(this.getFieldID(),aField.getJahiaID(), 406 aField.getPageID(),aField.getctnid(),aField.getFieldDefID(), 407 type,aField.getConnectType(),"",aField.getAclID(),jParams, 408 this.locales); 409 return facade; 410 } 411 412 } 413 | Popular Tags |