KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > data > fields > JahiaSmallTextSharedLangField


1 // JahiaSmalltextSharedLangField
2

3 package org.jahia.data.fields;
4
5 import java.util.Arrays JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Locale JavaDoc;
10 import java.util.Properties JavaDoc;
11 import java.util.Vector JavaDoc;
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     /**
31      *
32      * @param ID
33      * @param jahiaID
34      * @param pageID
35      * @param ctnid
36      * @param fieldDefID
37      * @param fieldType
38      * @param connectType
39      * @param fieldValue
40      * @param rank
41      * @param aclID
42      * @param versionID
43      * @param versionStatus
44      * @param languageCode
45      * @author NK
46      */

47     public JahiaSmallTextSharedLangField(
48             Integer JavaDoc ID,
49             Integer JavaDoc jahiaID,
50             Integer JavaDoc pageID,
51             Integer JavaDoc ctnid,
52             Integer JavaDoc fieldDefID,
53             Integer JavaDoc fieldType,
54             Integer JavaDoc connectType,
55             String JavaDoc fieldValue,
56             Integer JavaDoc rank,
57             Integer JavaDoc aclID,
58             Integer JavaDoc versionID,
59             Integer JavaDoc versionStatus,
60             String JavaDoc 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     } // end constructor
70

71
72     public void load(int loadFlag, ParamBean jParams)
73     throws JahiaException
74     {
75         switch (this.getConnectType())
76         {
77             case (ConnectionTypes.LOCAL) :
78                 //this.setValue(this.getValue());
79
if (!this.getValue().equals("<empty>")) {
80                     this.objectItem = ExpressionMarker.getValue(this.getValue(), jParams);
81                     this.objectItem = ResourceBundleMarker.getValue((String JavaDoc)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 JavaDoc getEngineName()
95     {
96         return "org.jahia.engines.shared.SmallText_Field";
97     }
98
99     public boolean save(ParamBean jParams)
100     throws JahiaException
101     {
102         // 0 for parentAclID in saveField, because field already exists
103
// -> field already has an aclID
104
// -> no need to create a new one
105

106         //ServicesRegistry.getInstance().getJahiaFieldService().saveField( theField, 0, jParams );
107
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 JavaDoc getFieldContent4Ranking()
121     {
122         String JavaDoc result = this.getValue();
123         try {
124             /* Before we can activate this we need a paramBean to be passed !
125             ExpressionMarker marker =
126                 ExpressionMarker.parseMarkerValue(result);
127             if (marker != null) {
128                 result = marker.getValue();
129             }
130             */

131             ResourceBundleMarker marker =
132                     ResourceBundleMarker.parseMarkerValue(result);
133             if ( marker != null ){
134                 result = (String JavaDoc)this.getObject();
135             }
136         } catch ( Throwable JavaDoc t ){
137         }
138         return result;
139     }
140
141     public String JavaDoc getIconNameOff()
142     {
143         return "smalltext";
144     }
145
146     public String JavaDoc 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         //toDebug("cloneField(): value = "+this.getValue());
164
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 JavaDoc)(this.getProperties()).clone() );
174
175         return clonedField;
176     }
177
178     public Object JavaDoc clone()
179     {
180         Object JavaDoc objItem = this.getObject();
181
182         JahiaSmallTextSharedLangField st =
183                 new JahiaSmallTextSharedLangField (
184                                         new Integer JavaDoc(ID),
185                                         new Integer JavaDoc(jahiaID),
186                                         new Integer JavaDoc(pageID),
187                                         new Integer JavaDoc(ctnid),
188                                         new Integer JavaDoc(fieldDefID),
189                                         new Integer JavaDoc(fieldType),
190                                         new Integer JavaDoc(connectType),
191                                         fieldValue, new Integer JavaDoc(rank),
192                                         new Integer JavaDoc(aclID),
193                                         new Integer JavaDoc(versionID),
194                                         new Integer JavaDoc(workflowState),
195                                         new String JavaDoc(getLanguageCode()) );
196         st.setObject(objItem);
197         return st;
198     }
199
200     /**
201      * Is this kind of field shared (i.e. not one version for each language,
202      * but one version for every language)
203      */

204     public boolean isShared (){
205         return true;
206     }
207
208     /**
209      * Copy the internal value of current language to another language.
210      * Must be implemented by conctrete field for specific implementation.
211      *
212      * @param aField A same field in another language
213      */

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     /**
225      * Returns an Hashmap of language_code/value used by search index engine
226      * the value is an array of String
227      *
228      * @return
229      */

230     public HashMap JavaDoc getValuesForSearch () throws JahiaException {
231
232         String JavaDoc lang = this.getLanguageCode();
233         if ( this.isShared() ){
234             lang = ContentField.SHARED_LANGUAGE;
235         }
236
237
238         HashMap JavaDoc values = new HashMap JavaDoc();
239         ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
240         String JavaDoc[] strVals = this.getValues();
241         if ( strVals != null ){
242             arrayList.addAll(Arrays.asList(strVals));
243         }
244
245         Iterator JavaDoc iterator = arrayList.iterator();
246         while ( iterator.hasNext() ){
247             String JavaDoc val = (String JavaDoc)iterator.next();
248
249             ResourceBundleMarker resMarker =
250                     ResourceBundleMarker.parseMarkerValue(val);
251             if ( resMarker == null ){
252                 Vector JavaDoc vals = (Vector JavaDoc)values.get(lang);
253                 if ( vals == null ){
254                     vals = new Vector JavaDoc();
255                 }
256                 vals.add(val);
257                 values.put(lang,vals);
258             } else {
259                 ArrayList JavaDoc localeList = new ArrayList JavaDoc();
260                 JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService().getSite(this.getJahiaID());
261                 Vector JavaDoc 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 JavaDoc tempLocale = LanguageCodeConverters.languageCodeToLocale(curSetting.getCode());
267                             String JavaDoc value = resMarker.getValue(tempLocale);
268                             if ( value == null ){
269                                 value = "";
270                             }
271                             Vector JavaDoc vals = (Vector JavaDoc)values.get(tempLocale.toString());
272                             if ( vals == null ){
273                                 vals = new Vector JavaDoc();
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 JavaDoc key = (String JavaDoc)iterator.next();
286             Vector JavaDoc v = (Vector JavaDoc)values.get(key);
287             values.put(key,v.toArray());
288         }
289
290
291         /*
292         ResourceBundleMarker resMarker = ResourceBundleMarker.parseMarkerValue(this.getValue());
293         if ( resMarker == null ){
294             if ( this.isShared() ){
295                 values.put(ContentField.SHARED_LANGUAGE,this.getValue());
296             } else {
297                 values.put(this.getLanguageCode(),this.getValue());
298             }
299         } else {
300             ArrayList localeList = new ArrayList();
301             JahiaSite site = ServicesRegistry.getInstance().getJahiaSitesService().getSite(this.getJahiaID());
302             Vector siteLanguageSettings = site.getLanguageSettings();
303             if (siteLanguageSettings != null) {
304                 for (int i = 0; i < siteLanguageSettings.size(); i++) {
305                     SiteLanguageSettings curSetting = (SiteLanguageSettings) siteLanguageSettings.elementAt(i);
306                     if (curSetting.isActivated()) {
307                         Locale tempLocale = LanguageCodeConverters.languageCodeToLocale(curSetting.getCode());
308                         String value = resMarker.getValue(tempLocale);
309                         if ( value == null ){
310                             value = "";
311                         }
312                         values.put(tempLocale.toString(),value);
313                     }
314                 }
315             }
316
317         }*/

318         return values;
319     }
320
321 }
322
Popular Tags