1 13 package org.jahia.data.fields; 14 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.Hashtable ; 18 import java.util.Iterator ; 19 import java.util.Locale ; 20 import java.util.Vector ; 21 22 import org.apache.log4j.Logger; 23 import org.jahia.exceptions.JahiaException; 24 import org.jahia.params.ParamBean; 25 import org.jahia.registries.ServicesRegistry; 26 import org.jahia.services.fields.ContentField; 27 import org.jahia.services.fields.PublicContentFieldEntryState; 28 import org.jahia.services.version.ContentObjectEntryState; 29 import org.jahia.services.version.EntryLoadRequest; 30 import org.jahia.utils.LanguageCodeConverters; 31 32 38 public class JahiaVersioningContentFieldFacade { 39 40 private static Logger logger = Logger.getLogger(JahiaVersioningContentFieldFacade.class); 41 42 private int fieldID = -1; 43 44 private Vector versioningEntryStates; 45 46 private Hashtable fields; 47 48 57 public JahiaVersioningContentFieldFacade( int aFieldID, 58 int loadFlag, 59 ParamBean jParams, 60 ArrayList locales) 61 throws JahiaException 62 { 63 this.fieldID = aFieldID; 64 this.fields = new Hashtable (); 65 this.versioningEntryStates = new Vector (); 66 instanceFields(loadFlag, jParams, locales); 67 } 68 69 public Enumeration getFields(){ 71 return fields.elements(); 72 } 73 74 80 public Enumeration getFields(String languageCode){ 81 int size = this.versioningEntryStates.size(); 82 Vector result = new Vector (); 83 for ( int i=0 ; i<size ; i++ ){ 84 ContentObjectEntryState entryState = 85 (ContentObjectEntryState)this.versioningEntryStates.get(i); 86 if ( entryState.getLanguageCode().equals(ContentField.SHARED_LANGUAGE) 87 || entryState.getLanguageCode().equals(languageCode) ){ 88 JahiaField aField = (JahiaField)this.fields.get(entryState); 89 if ( aField != null ){ 90 result.add(aField); 91 } 92 } 93 } 94 return result.elements(); 95 } 96 97 103 public JahiaField getField( EntryLoadRequest entryLoadRequest){ 104 105 logger.debug("EntryLoadRequest :" + entryLoadRequest.toString()); 106 107 Locale locale = entryLoadRequest.getFirstLocale(true); 108 logger.debug("EntryLoadRequest locale :" + locale.toString()); 109 110 ContentObjectEntryState entryState = 111 (ContentObjectEntryState)ServicesRegistry.getInstance() 112 .getJahiaVersionService() 113 .resolveEntry(this.versioningEntryStates, 114 entryLoadRequest); 115 116 if ( entryState != null ){ 117 logger.debug("Resolved entryState :" + entryState.toString()); 118 } else { 119 return null; 120 } 121 JahiaField field = null; 122 if ( entryState != null ){ 123 field = 124 (JahiaField)fields.get(new PublicContentFieldEntryState(entryState)); 125 } 126 127 if ( field != null ){ 128 logger.debug("Returned entryState :" + entryState.toString()); 129 logger.debug("Field Value :" + field.getValue() 130 + ", langCode=" + field.getLanguageCode()); 131 } else { 132 logger.debug("Returned entryState is null "); 133 } 134 return field; 135 } 136 137 private void instanceFields( int loadFlag, 139 ParamBean jParams, ArrayList locales) 140 throws JahiaException 141 { 142 143 ContentField contentField = ContentField.getField(fieldID); 144 EntryLoadRequest elr = null; 145 146 contentField.getEntryState(EntryLoadRequest.VERSIONED); 148 149 Iterator entryStates = contentField.getEntryStates().iterator(); 150 ContentObjectEntryState entryState = null; 151 while ( entryStates.hasNext() ) 152 { 153 entryState = (ContentObjectEntryState)entryStates.next(); 154 155 PublicContentFieldEntryState entryStateKey = 156 new PublicContentFieldEntryState(entryState); 157 158 ArrayList entryLocales = new ArrayList (); 159 entryLocales.add(LanguageCodeConverters 160 .languageCodeToLocale(entryState.getLanguageCode())); 161 elr = new EntryLoadRequest(entryState.getWorkflowState(), 162 entryState.getVersionID(), 163 entryLocales); 164 165 JahiaField field = ServicesRegistry.getInstance() 166 .getJahiaFieldService() 167 .loadField(fieldID, 168 loadFlag, 169 jParams, 170 elr); 171 if ( field != null ){ 172 field.setRawValue(field.getValue()); 173 fields.put(entryStateKey,field); 174 this.versioningEntryStates.add(entryStateKey); 175 } 176 177 } 178 } 179 180 } 181 | Popular Tags |