1 3 package org.jahia.data.fields; 4 5 import java.util.Arrays ; 6 import java.util.ArrayList ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Locale ; 10 import java.util.Properties ; 11 import java.util.Vector ; 12 13 import org.jahia.data.ConnectionTypes; 14 import org.jahia.data.FormDataManager; 15 import org.jahia.exceptions.JahiaException; 16 import org.jahia.params.ParamBean; 17 import org.jahia.registries.ServicesRegistry; 18 import org.jahia.resourcebundle.ResourceBundleMarker; 19 import org.jahia.services.fields.ContentField; 20 import org.jahia.services.fields.ContentSmallTextSharedLangField; 21 import org.jahia.services.sites.JahiaSite; 22 import org.jahia.services.sites.SiteLanguageSettings; 23 import org.jahia.services.version.EntrySaveRequest; 24 import org.jahia.sharing.FieldSharingManager; 25 import org.jahia.utils.LanguageCodeConverters; 26 27 public class JahiaSmallTextSharedLangField extends JahiaField implements JahiaSimpleField, JahiaAllowApplyChangeToAllLangField 28 { 29 30 47 public JahiaSmallTextSharedLangField( 48 Integer ID, 49 Integer jahiaID, 50 Integer pageID, 51 Integer ctnid, 52 Integer fieldDefID, 53 Integer fieldType, 54 Integer connectType, 55 String fieldValue, 56 Integer rank, 57 Integer aclID, 58 Integer versionID, 59 Integer versionStatus, 60 String languageCode) { 61 62 super(ID, jahiaID, pageID, ctnid, fieldDefID, fieldType, connectType, 63 fieldValue, rank, aclID, versionID, versionStatus, languageCode); 64 65 if ( isShared() ){ 66 this.languageCode = ContentField.SHARED_LANGUAGE; 67 } 68 69 } 71 72 public void load(int loadFlag, ParamBean jParams) 73 throws JahiaException 74 { 75 switch (this.getConnectType()) 76 { 77 case (ConnectionTypes.LOCAL) : 78 if (!this.getValue().equals("<empty>")) { 80 this.objectItem = ExpressionMarker.getValue(this.getValue(), jParams); 81 this.objectItem = ResourceBundleMarker.getValue((String )this.objectItem,jParams.getLocale()); 82 this.setRawValue(this.getValue()); 83 this.setValue(FormDataManager.getInstance().htmlEncode(this.getValue())); 84 } 85 break; 86 case (ConnectionTypes.DATASOURCE) : 87 if ((loadFlag & LoadFlags.DATASOURCE) != 0) { 88 this.setValue( FieldSharingManager.getInstance().getRemoteFieldValue( 89 this.getValue() )); 90 } 91 } 92 } 93 94 public String getEngineName() 95 { 96 return "org.jahia.engines.shared.SmallText_Field"; 97 } 98 99 public boolean save(ParamBean jParams) 100 throws JahiaException 101 { 102 106 EntrySaveRequest saveRequest = new EntrySaveRequest(jParams.getUser(),getLanguageCode()); 108 ContentSmallTextSharedLangField contentField = (ContentSmallTextSharedLangField)ContentField.getField(getID()); 109 contentField.setText(getValue(),saveRequest); 110 ServicesRegistry.getInstance().getJahiaSearchService().indexField(getID(),true, jParams, true); 111 return true; 112 } 113 114 public void delete(ParamBean jParams) 115 throws JahiaException 116 { 117 ; 118 } 119 120 public String getFieldContent4Ranking() 121 { 122 String result = this.getValue(); 123 try { 124 131 ResourceBundleMarker marker = 132 ResourceBundleMarker.parseMarkerValue(result); 133 if ( marker != null ){ 134 result = (String )this.getObject(); 135 } 136 } catch ( Throwable t ){ 137 } 138 return result; 139 } 140 141 public String getIconNameOff() 142 { 143 return "smalltext"; 144 } 145 146 public String getIconNameOn() 147 { 148 return "smalltext_on"; 149 } 150 151 public JahiaField cloneField(int newctnid, int newPageID, int clonedAclID, boolean childrenCloned) 152 throws JahiaException 153 { 154 JahiaField clonedField = ServicesRegistry.getInstance().getJahiaFieldService(). 155 createJahiaField(0, this.getJahiaID(), 156 newPageID, newctnid, 157 this.getFieldDefID(), this.getType(), 158 this.getConnectType(), 159 this.getValue(), this.getRank(), 160 clonedAclID, this.getVersionID(), this.getWorkflowState(), 161 this.getLanguageCode()); 162 163 if (clonedField == null) 165 { 166 throw new JahiaException ("Could not clone field.", 167 "Could not instanciate a new JahiaField object.", 168 JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY); 169 } 170 171 clonedField.setRawValue(this.getRawValue()); 172 clonedField.setObject( this.getObject() ); 173 clonedField.setProperties( (Properties )(this.getProperties()).clone() ); 174 175 return clonedField; 176 } 177 178 public Object clone() 179 { 180 Object objItem = this.getObject(); 181 182 JahiaSmallTextSharedLangField st = 183 new JahiaSmallTextSharedLangField ( 184 new Integer (ID), 185 new Integer (jahiaID), 186 new Integer (pageID), 187 new Integer (ctnid), 188 new Integer (fieldDefID), 189 new Integer (fieldType), 190 new Integer (connectType), 191 fieldValue, new Integer (rank), 192 new Integer (aclID), 193 new Integer (versionID), 194 new Integer (workflowState), 195 new String (getLanguageCode()) ); 196 st.setObject(objItem); 197 return st; 198 } 199 200 204 public boolean isShared (){ 205 return true; 206 } 207 208 214 public void copyValueInAnotherLanguage (JahiaField aField,ParamBean jParams) 215 throws JahiaException { 216 if ( aField == null ){ 217 return; 218 } 219 aField.setValue(this.getValue()); 220 aField.setRawValue(this.getValue()); 221 aField.setObject(this.getObject()); 222 } 223 224 230 public HashMap getValuesForSearch () throws JahiaException { 231 232 String lang = this.getLanguageCode(); 233 if ( this.isShared() ){ 234 lang = ContentField.SHARED_LANGUAGE; 235 } 236 237 238 HashMap values = new HashMap (); 239 ArrayList arrayList = new ArrayList (); 240 String [] strVals = this.getValues(); 241 if ( strVals != null ){ 242 arrayList.addAll(Arrays.asList(strVals)); 243 } 244 245 Iterator iterator = arrayList.iterator(); 246 while ( iterator.hasNext() ){ 247 String val = (String )iterator.next(); 248 249 ResourceBundleMarker resMarker = 250 ResourceBundleMarker.parseMarkerValue(val); 251 if ( resMarker == null ){ 252 Vector vals = (Vector )values.get(lang); 253 if ( vals == null ){ 254 vals = new Vector (); 255 } 256 vals.add(val); 257 values.put(lang,vals); 258 } else { 259 ArrayList localeList = new ArrayList (); 260 JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService().getSite(this.getJahiaID()); 261 Vector siteLanguageSettings = site.getLanguageSettings(); 262 if (siteLanguageSettings != null) { 263 for (int i = 0; i < siteLanguageSettings.size(); i++) { 264 SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLanguageSettings.elementAt(i); 265 if (curSetting.isActivated()) { 266 Locale tempLocale = LanguageCodeConverters.languageCodeToLocale(curSetting.getCode()); 267 String value = resMarker.getValue(tempLocale); 268 if ( value == null ){ 269 value = ""; 270 } 271 Vector vals = (Vector )values.get(tempLocale.toString()); 272 if ( vals == null ){ 273 vals = new Vector (); 274 } 275 vals.add(value); 276 values.put(tempLocale.toString(),vals); 277 } 278 } 279 } 280 } 281 } 282 283 iterator = values.keySet().iterator(); 284 while ( iterator.hasNext() ){ 285 String key = (String )iterator.next(); 286 Vector v = (Vector )values.get(key); 287 values.put(key,v.toArray()); 288 } 289 290 291 318 return values; 319 } 320 321 } 322 | Popular Tags |