KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > fields > ContentBigTextField


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.services.fields;
14
15 import org.jahia.data.ConnectionTypes;
16 import org.jahia.data.fields.JahiaFieldDefinition;
17 import org.jahia.exceptions.JahiaException;
18 import org.jahia.params.ParamBean;
19 import org.jahia.registries.JahiaFieldDefinitionsRegistry;
20 import org.jahia.registries.ServicesRegistry;
21 import org.jahia.services.pages.ContentPage;
22 import org.jahia.services.usermanager.JahiaUser;
23 import org.jahia.services.version.*;
24 import org.jahia.utils.LanguageCodeConverters;
25 import org.jahia.utils.xml.XMLSerializationOptions;
26 import org.jahia.utils.xml.XmlWriter;
27
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.Vector JavaDoc;
33
34
35 public class ContentBigTextField extends ContentField {
36
37     private static org.apache.log4j.Logger logger
38             = org.apache.log4j.Logger.getLogger (ContentBigTextField.class);
39
40
41     protected ContentBigTextField (Integer JavaDoc ID,
42                                    Integer JavaDoc jahiaID,
43                                    Integer JavaDoc pageID,
44                                    Integer JavaDoc ctnid,
45                                    Integer JavaDoc fieldDefID,
46                                    Integer JavaDoc fieldType,
47                                    Integer JavaDoc connectType,
48                                    Integer JavaDoc aclID,
49                                    Vector JavaDoc activeAndStagingEntryStates,
50                                    Hashtable JavaDoc activeAndStagedDBValues) {
51         super (ID.intValue (), jahiaID.intValue (), pageID.intValue (),
52                 ctnid.intValue (), fieldDefID.intValue (), fieldType.intValue (),
53                 connectType.intValue (), aclID.intValue (),
54                 activeAndStagingEntryStates, activeAndStagedDBValues);
55     }
56
57     //--------------------------------------------------------------------------
58
public static synchronized ContentBigTextField
59             createBigText (int siteID, int pageID, int containerID,
60                            int fieldDefID, int parentAclID, int aclID,
61                            String JavaDoc text, ParamBean jParams)
62             throws JahiaException {
63
64         ContentBigTextField result =
65                 (ContentBigTextField) ContentField.createField (siteID, pageID,
66                         containerID, fieldDefID,
67                         ContentFieldTypes.BIGTEXT,
68                         ConnectionTypes.LOCAL,
69                         parentAclID, aclID);
70         EntrySaveRequest saveRequest =
71                 new EntrySaveRequest (jParams.getUser (),
72                         jParams.getLocale ().toString ());
73
74         result.setText (text, saveRequest);
75         return result;
76     }
77
78     //--------------------------------------------------------------------------
79
/**
80      * Gets the String representation of this field. In case of an Application,
81      * it will be the output of the application, in case of a bigtext it will
82      * be the full content of the bigtext, etc. This is called by the public
83      * method getValue of ContentField, which does the entryState resolving
84      * This method should call getDBValue to get the DBValue
85      */

86     public String JavaDoc getValue (ParamBean jParams,
87                                ContentObjectEntryState entryState)
88             throws JahiaException {
89
90         logger.debug ("Loading big text field...");
91
92         JahiaFieldDefinition theDef = JahiaFieldDefinitionsRegistry.
93                 getInstance ().getDefinition (
94                         this.getFieldDefID ());
95         int pageDefID = -1;
96         if (entryState != null) {
97             pageDefID =
98                     ContentPage.getPage (this.getPageID ()).getDefinitionID (
99                             new EntryLoadRequest (entryState));
100         } else {
101             pageDefID = ServicesRegistry.getInstance ().getJahiaPageService ().
102                     lookupPage (this.getPageID (), jParams).
103                     getPageTemplateID ();
104         }
105
106         String JavaDoc result = ServicesRegistry.getInstance ()
107                 .getJahiaTextFileService ()
108                 .loadBigTextValue (this.getSiteID (),
109                         this.getPageID (),
110                         this.getID (),
111                         theDef.getDefaultValue (pageDefID),
112                         entryState.getVersionID (),
113                         entryState.getWorkflowState (),
114                         entryState.getLanguageCode ());
115
116         if (result == null || result.equals ("<empty>")) {
117             result = new String JavaDoc ();
118         }
119         /*
120         else
121         {
122             result = FormDataManager.getInstance().formDecode(result);
123         }
124         */

125         return result;
126     }
127
128     //--------------------------------------------------------------------------
129
/**
130      * Sets the String representation of this field.
131      * This method should call preSet and postSet.
132      */

133     public void setText (String JavaDoc value, EntrySaveRequest saveRequest) throws JahiaException {
134         ContentObjectEntryState verInfo = preSet ("<text>", saveRequest);
135         logger.debug ("Saving big text field..." + verInfo.toString ());
136
137         ServicesRegistry.getInstance ().getJahiaTextFileService ().saveContents (
138                 this.getSiteID (),
139                 this.getPageID (),
140                 this.getID (),
141                 value,
142                 verInfo.getVersionID (),
143                 verInfo.getWorkflowState (),
144                 verInfo.getLanguageCode ());
145
146     }
147
148     //--------------------------------------------------------------------------
149
/**
150      * get the Value that will be added to the search engine for this field.
151      * for a bigtext it will be the content of the bigtext, for an application
152      * the string will be empty!
153      * Do not return null, return an empty string instead.
154      */

155     public String JavaDoc getValueForSearch (ParamBean jParams, ContentObjectEntryState entryState) {
156         return new String JavaDoc (); /** @todo fixme */
157     }
158
159     //--------------------------------------------------------------------------
160
/**
161      * This method is called when there is a workflow state change
162      * Such as staged mode -> active mode (validation), active -> inactive (for versioning)
163      * and also staged mode -> other staged mode (workflow)
164      * This method should not write/change the DBValue, the service handles that.
165      *
166      * @param fromEntryState the entry state that is currently was in the database
167      * @param toEntryState the entry state that will be written to the database
168      * @param jParams ParamBean object used to get information about the user
169      * doing the request, the current locale, etc...
170      *
171      * @return null if the entry state change wasn't an activation, otherwise it
172      * returns an object that contains the status of the activation (whether
173      * successfull, partial or failed, as well as messages describing the
174      * warnings during the activation process)
175      */

176     public ActivationTestResults changeEntryState (ContentObjectEntryState fromEntryState,
177                                                    ContentObjectEntryState toEntryState,
178                                                    ParamBean jParams,
179                                                    StateModificationContext stateModifContext)
180             throws JahiaException {
181
182         logger.debug ("From " + fromEntryState.toString ()
183                 + " To " + toEntryState.toString ());
184
185         ActivationTestResults activationResults = new ActivationTestResults ();
186         try {
187             ServicesRegistry.getInstance ().getJahiaTextFileService ().
188                     renameFile (
189                             this.getSiteID (),
190                             this.getPageID (),
191                             this.getID (),
192                             fromEntryState.getVersionID (),
193                             fromEntryState.getWorkflowState (),
194                             fromEntryState.getLanguageCode (),
195
196                             this.getSiteID (),
197                             this.getPageID (),
198                             this.getID (),
199                             toEntryState.getVersionID (),
200                             toEntryState.getWorkflowState (),
201                             toEntryState.getLanguageCode ()
202                     );
203             activationResults.setStatus (ActivationTestResults.COMPLETED_OPERATION_STATUS);
204         } catch (Throwable JavaDoc t) {
205             t.printStackTrace ();
206             activationResults.setStatus (ActivationTestResults.FAILED_OPERATION_STATUS);
207         }
208         return activationResults;
209     }
210
211     //--------------------------------------------------------------------------
212
protected ActivationTestResults isContentValidForActivation (
213             Set JavaDoc languageCodes,
214             ParamBean jParams,
215             StateModificationContext stateModifContext)
216             throws JahiaException {
217
218         /** @todo to be implemented */
219         return new ActivationTestResults ();
220     }
221
222     //--------------------------------------------------------------------------
223
/**
224      * This method is called when a entry should be copied into a new entry
225      * it is called when an old version -> active version move occurs
226      * This method should not write/change the DBValue, the service handles that.
227      *
228      * @param fromEntryState the entry state that is currently was in the database
229      * @param toEntryState the entry state that will be written to the database
230      */

231     public void copyEntry (EntryStateable fromEntryState,
232                            EntryStateable toEntryState)
233             throws JahiaException {
234
235         int fromVersionID = fromEntryState.getVersionID ();
236
237         if (fromEntryState.getWorkflowState ()
238                 == ContentObjectEntryState.WORKFLOW_STATE_VERSIONING_DELETED) {
239             // lookup for the last archive done before the deleted entryState
240
// and restore it.
241
ContentObjectEntryState entryState =
242                     new ContentObjectEntryState (fromEntryState);
243             ContentObjectEntryState archiveEntryState =
244                     getClosestVersionedEntryState (entryState, true);
245             if (archiveEntryState != null) {
246                 fromVersionID = archiveEntryState.getVersionID ();
247             }
248         }
249
250         ServicesRegistry.getInstance ().getJahiaTextFileService ().copyFile (
251                 this.getSiteID (),
252                 this.getPageID (),
253                 this.getID (),
254                 fromVersionID,
255                 fromEntryState.getWorkflowState (),
256                 fromEntryState.getLanguageCode (),
257
258                 this.getSiteID (),
259                 this.getPageID (),
260                 this.getID (),
261                 toEntryState.getVersionID (),
262                 toEntryState.getWorkflowState (),
263                 toEntryState.getLanguageCode ()
264         );
265
266         super.copyEntry (fromEntryState, toEntryState);
267     }
268
269     //--------------------------------------------------------------------------
270
/**
271      * This method is called when an entry should be deleted for real.
272      * It is called when a field is deleted, and versioning is disabled, or
273      * when staging values are undone.
274      * For a bigtext for instance, this method should delete the text file
275      * corresponding to this field entry
276      *
277      * @param deleteEntryState the entry state to delete
278      */

279     public void deleteEntry (EntryStateable deleteEntryState)
280             throws JahiaException {
281         ServicesRegistry.getInstance ().getJahiaTextFileService ().deleteFile (
282                 this.getSiteID (),
283                 this.getPageID (),
284                 this.getID (),
285                 deleteEntryState.getVersionID (),
286                 deleteEntryState.getWorkflowState (),
287                 deleteEntryState.getLanguageCode ());
288
289         super.deleteEntry (deleteEntryState);
290     }
291
292     //--------------------------------------------------------------------------
293
/**
294      * Is this kind of field shared (i.e. not one version for each language,
295      * but one version for every language)
296      */

297     public boolean isShared () {
298         return false;
299     }
300
301     /**
302      * This is called on all content fields to have them serialized only their
303      * specific part. The actual field metadata seriliazing is handled by the
304      * ContentField class. This method is called multiple times per field
305      * according to the workflow state, languages and versioning entries we
306      * want to serialize.
307      *
308      * @param xmlWriter the XML writer object in which to write the XML output
309      * @param xmlSerializationOptions options used to activate/bypass certain
310      * output of elements.
311      * @param entryState the ContentFieldEntryState for which to generate the
312      * XML export.
313      * @param paramBean specifies context of serialization, such as current
314      * user, current request parameters, entry load request, URL generation
315      * information such as ServerName, ServerPort, ContextPath, etc... URL
316      * generation is an important part of XML serialization and this is why
317      * we pass this parameter down, as well as user rights checking.
318      *
319      * @throws IOException in case there was an error writing to the Writer
320      * output object.
321      */

322     protected void serializeContentToXML (XmlWriter xmlWriter,
323                                           XMLSerializationOptions xmlSerializationOptions,
324                                           ContentObjectEntryState entryState,
325                                           ParamBean paramBean) throws IOException JavaDoc {
326         try {
327             JahiaFieldDefinition theDef = JahiaFieldDefinitionsRegistry.
328                     getInstance ().getDefinition (
329                             this.getFieldDefID ());
330
331             int pageDefID = ServicesRegistry.getInstance ().getJahiaPageService ().
332                     lookupContentPage (this.getPageID (), true).
333                     getPageTemplateID (EntryLoadRequest.CURRENT);
334
335             String JavaDoc result = ServicesRegistry.getInstance ()
336                     .getJahiaTextFileService ()
337                     .loadBigTextValue (this.getSiteID (),
338                             this.getPageID (),
339                             this.getID (),
340                             theDef.getDefaultValue (pageDefID),
341                             entryState.getVersionID (),
342                             entryState.getWorkflowState (),
343                             entryState.getLanguageCode ());
344
345             if (result == null || result.equals ("<empty>")) {
346                 result = new String JavaDoc ();
347             } else {
348                 //result = FormDataManager.getInstance().formDecode(result);
349
}
350             xmlWriter.writeCData (result);
351         } catch (JahiaException je) {
352             logger.debug ("Error while serializing bigtext to XML : ", je);
353         }
354     }
355
356     protected void purgeContent ()
357             throws JahiaException {
358         // not necessary since already done in site admin interface, but we
359
// should implement this if we want to be able to do atomic purging.
360
}
361
362     /**
363      * Called when marking a language for deletion on a field. This is done
364      * first to allow field implementation to provide a custom behavior when
365      * marking fields for deletion. It isn't abstract because most fields will
366      * not need to redefine this method.
367      *
368      * @param saveRequest the EntrySaveRequest object containing the user and
369      * language code for this deletion marking
370      * @param jParams the current ParamBean object passed in case the field
371      * implementation need it.
372      * @param stateModifContext used to detect loops in deletion marking.
373      *
374      * @throws JahiaException in the case there was an error processing the
375      * marking of the content.
376      */

377     protected void markContentLanguageForDeletion (JahiaUser user,
378                                                    String JavaDoc languageCode,
379                                                    StateModificationContext stateModifContext)
380             throws JahiaException {
381         ArrayList JavaDoc locales = new ArrayList JavaDoc ();
382         locales.add (LanguageCodeConverters.languageCodeToLocale (languageCode));
383         EntryLoadRequest loadRequest = new EntryLoadRequest (
384                 EntryLoadRequest.ACTIVE_WORKFLOW_STATE, 0, locales);
385         ContentObjectEntryState fromEntryState = getEntryState (loadRequest);
386         ContentObjectEntryState toEntryState = new ContentObjectEntryState (
387                 ContentObjectEntryState.WORKFLOW_STATE_START_STAGING, -1, languageCode);
388         if (fromEntryState != null && toEntryState != null) {
389             ServicesRegistry.getInstance ().getJahiaTextFileService ().copyFile (
390                     this.getSiteID (),
391                     this.getPageID (),
392                     this.getID (),
393                     fromEntryState.getVersionID (),
394                     fromEntryState.getWorkflowState (),
395                     fromEntryState.getLanguageCode (),
396
397                     this.getSiteID (),
398                     this.getPageID (),
399                     this.getID (),
400                     toEntryState.getVersionID (),
401                     toEntryState.getWorkflowState (),
402                     toEntryState.getLanguageCode ()
403             );
404         }
405         return;
406     }
407
408
409 }
Popular Tags