KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Vector JavaDoc;
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 /**
33  * Used to hold a set of versioned JahiaField instance in multiple language for a given
34  * field id.
35  *
36  * @author Khue Nguyen
37  */

38 public class JahiaVersioningContentFieldFacade {
39
40     private static Logger logger = Logger.getLogger(JahiaVersioningContentFieldFacade.class);
41
42     private int fieldID = -1;
43
44     private Vector JavaDoc versioningEntryStates;
45
46     private Hashtable JavaDoc fields;
47
48     //--------------------------------------------------------------------------
49
/**
50      * Constructor for existing Field only
51      *
52      * @param int fieldID, the unique field identifier
53      * @param int loadFlag
54      * @param ParamBean jParams
55      * @param ArrayList locales, the list of locales
56      */

57     public JahiaVersioningContentFieldFacade( int aFieldID,
58                                               int loadFlag,
59                                               ParamBean jParams,
60                                               ArrayList JavaDoc locales)
61     throws JahiaException
62     {
63         this.fieldID = aFieldID;
64         this.fields = new Hashtable JavaDoc();
65         this.versioningEntryStates = new Vector JavaDoc();
66         instanceFields(loadFlag, jParams, locales);
67     }
68
69     //--------------------------------------------------------------------------
70
public Enumeration JavaDoc getFields(){
71         return fields.elements();
72     }
73
74     //--------------------------------------------------------------------------
75
/**
76      *
77      * @param languageCode
78      * @return
79      */

80     public Enumeration JavaDoc getFields(String JavaDoc languageCode){
81         int size = this.versioningEntryStates.size();
82         Vector JavaDoc result = new Vector JavaDoc();
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     //--------------------------------------------------------------------------
98
/**
99      * Return a field for a entryLoadRequest using resolve entry state mechanism.
100      *
101      * @param EntryLoadRequest entryLoadRequest
102      */

103     public JahiaField getField( EntryLoadRequest entryLoadRequest){
104
105         logger.debug("EntryLoadRequest :" + entryLoadRequest.toString());
106
107         Locale JavaDoc 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     //--------------------------------------------------------------------------
138
private void instanceFields( int loadFlag,
139                                  ParamBean jParams, ArrayList JavaDoc locales)
140     throws JahiaException
141     {
142
143         ContentField contentField = ContentField.getField(fieldID);
144         EntryLoadRequest elr = null;
145
146         // we ensure to load all versioning entry state.
147
contentField.getEntryState(EntryLoadRequest.VERSIONED);
148
149         Iterator JavaDoc 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 JavaDoc entryLocales = new ArrayList JavaDoc();
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