KickJava   Java API By Example, From Geeks To Geeks.

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


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 package org.jahia.data.fields;
14
15 import java.io.Serializable JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22 import java.util.Vector JavaDoc;
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 /**
37  * Used to hold a set of JahiaField instance in multiple language for a given
38  * field id.
39  *
40  * @author Khue Nguyen
41  */

42 public class JahiaContentFieldFacade implements Serializable JavaDoc {
43
44     private static Logger logger = Logger.getLogger(JahiaContentFieldFacade.class);
45
46     private int fieldID = -1;
47
48     // this is a Vector of EntryState listing ALL different Entry States
49
// for a field, that are ACTIVE or STAGED (including all languages)
50
private Vector JavaDoc activeAndStagingEntryStates;
51
52     private Hashtable JavaDoc fields;
53
54     private List JavaDoc locales;
55
56     //--------------------------------------------------------------------------
57
/**
58      * Constructor for existing Field only
59      *
60      * @param int aFieldID, the unique field identifier
61      * @param int loadFlag
62      * @param ParamBean jParams
63      * @param List localeList, the list of locales
64      * @param boolean createMissingLanguage,
65      * if true, create missing instance for locales not found in db.
66      */

67     public JahiaContentFieldFacade( int aFieldID,
68                                     int loadFlag,
69                                     ParamBean jParams,
70                                     List JavaDoc localeList,
71                                     boolean createFieldForMissingLanguage )
72     throws JahiaException
73     {
74         this.fieldID = aFieldID;
75         this.fields = new Hashtable JavaDoc();
76         this.activeAndStagingEntryStates = new Vector JavaDoc();
77         this.locales = localeList;
78         instanceFields(loadFlag, jParams, localeList, createFieldForMissingLanguage);
79     }
80
81     //--------------------------------------------------------------------------
82
/**
83      * Constructor for a new Field. The field is only stored in memory,
84      * nothing stored in persistance.
85      *
86      */

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 JavaDoc fieldValue,
95                                      int aclID,
96                                      ParamBean jParams,
97                                      List JavaDoc localeList )
98     throws JahiaException
99     {
100
101         this.fieldID = aFieldID;
102         this.fields = new Hashtable JavaDoc();
103         this.activeAndStagingEntryStates = new Vector JavaDoc();
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     //--------------------------------------------------------------------------
119
public Enumeration JavaDoc getFields(){
120         return fields.elements();
121     }
122
123
124     //--------------------------------------------------------------------------
125
/**
126      * Return a field for a entryLoadRequest using resolve entry state mechanism.
127      *
128      * @param EntryLoadRequest entryLoadRequest
129      * @param boolean stagingIfActiveNotFound
130      */

131     public JahiaField getField( EntryLoadRequest entryLoadRequest,
132                                 boolean activeIfStagingNotFound ){
133
134         logger.debug("EntryLoadRequest :" + entryLoadRequest.toString());
135
136         Locale JavaDoc 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             // we only want the staging entry
167
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     //--------------------------------------------------------------------------
186
private void instanceFields( int loadFlag,
187                                  ParamBean jParams,
188                                  List JavaDoc localeList,
189                                  boolean createFieldForMissingLanguage )
190     throws JahiaException
191     {
192
193         ContentField contentField = ContentField.getField(fieldID);
194         EntryLoadRequest elr = null;
195         Vector JavaDoc v = new Vector JavaDoc();
196         v.addAll(contentField.getActiveAndStagingEntryStates());
197         Enumeration JavaDoc 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 JavaDoc entryLocales = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc 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 JavaDoc 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     //--------------------------------------------------------------------------
264
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 JavaDoc fieldValue,
272                                                 int aclID,
273                                                 ParamBean jParams,
274                                                 List JavaDoc localeList )
275     throws JahiaException
276     {
277          EntryLoadRequest entryLoadRequest = null;
278          ContentObjectEntryState entryState = null;
279
280          // create entry state for all languages
281
Iterator JavaDoc iterator = localeList.iterator();
282          Locale JavaDoc locale = null;
283          while ( iterator.hasNext() )
284          {
285              locale = (Locale JavaDoc)iterator.next();
286
287              ArrayList JavaDoc entryLocales = new ArrayList JavaDoc();
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                  // second case can happen because we might have resolved to "simpler" language
312
// in case of "composed" language code. For exemple we resolved "en" from "en_US".
313
// in the case of resolving to a shared language, we ignore the check of a simpler
314
// language.
315

316                  // have to create a staged
317
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, // ranking
330
aclID,
331                                  0, // versionID
332
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     //--------------------------------------------------------------------------
355
/**
356      * Return the field ID
357      *
358      * @param fieldID
359      */

360     public int getFieldID(){
361         return this.fieldID;
362     }
363
364     //--------------------------------------------------------------------------
365
/**
366      * Change the field ID for all field.
367      * It can be used to change temporary instance of fields
368      *
369      * @param aFieldID
370      */

371     public void setFieldID(int aFieldID){
372         Enumeration JavaDoc enumeration = getFields();
373         JahiaField field = null;
374         while ( enumeration.hasMoreElements() ){
375             field = (JahiaField)enumeration.nextElement();
376             field.setID(aFieldID);
377         }
378     }
379
380     //--------------------------------------------------------------------------
381
/**
382      * Change the Acl for all field.
383      * It can be used to change temporary instance of fields
384      *
385      * @param aclID
386      */

387     public void setAclID(int aclID){
388         Enumeration JavaDoc enumeration = getFields();
389         JahiaField field = null;
390         while ( enumeration.hasMoreElements() ){
391             field = (JahiaField)enumeration.nextElement();
392             field.setAclID(aclID);
393         }
394     }
395
396     /**
397      * Change the field type ( from undefined to another type )
398      *
399      * @param type
400      */

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