KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > versioning > VersioningEngine


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

14 package org.jahia.engines.versioning;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19 import org.jahia.data.fields.JahiaField;
20 import org.jahia.data.fields.JahiaVersioningContentFieldFacade;
21 import org.jahia.data.fields.LoadFlags;
22 import org.jahia.data.JahiaData;
23 import org.jahia.exceptions.JahiaException;
24 import org.jahia.params.ParamBean;
25 import org.jahia.registries.ServicesRegistry;
26 import org.jahia.services.version.EntryLoadRequest;
27 import org.jahia.utils.LanguageCodeConverters;
28 import org.jahia.engines.JahiaEngine;
29
30 /**
31  * Revision Engine, only forward to Struts Action
32  *
33  * @author Khue Nguyen
34  */

35 public class VersioningEngine implements JahiaEngine {
36
37     public static final String JavaDoc ENGINE_NAME = "versioning";
38     public static final String JavaDoc JSP_FILE = "/jsp/jahia/engines/versioning/versioning.jsp";
39     private static VersioningEngine instance = null;
40
41     /** logging */
42     private static final org.apache.log4j.Logger logger =
43             org.apache.log4j.Logger.getLogger (VersioningEngine.class);
44
45
46     /**
47      *
48      */

49     private VersioningEngine () {
50         logger.debug ("***** Starting " + VersioningEngine.class.getName () + " engine *****");
51     }
52
53     /**
54      * returns a single instance of the object
55      */

56     public static synchronized VersioningEngine getInstance () {
57         if (instance == null) {
58             instance = new VersioningEngine ();
59         }
60         return instance;
61     }
62
63     /**
64      * handles the actions
65      *
66      * @param jParams a ParamBean object
67      * @param mode the mode, according to JahiaEngine
68      * @param engineMap engineMap
69      * @param objType the object type defined by JahiaObjectTool, ie JahiaObjectTool.FIELD_TYPE
70      *
71      * @return true if everything went okay, false if not
72      *
73      * @see org.jahia.engines.JahiaEngine
74      */

75     public boolean handleAction (ParamBean jParams, int mode, HashMap JavaDoc engineMap, int objType,
76                                  Object JavaDoc obj)
77             throws JahiaException {
78         logger.debug ("Started : mode:" + mode);
79         return true;
80
81         /*
82         switch (mode)
83         {
84             case (JahiaEngine.LOAD_MODE) : return load( jParams, engineMap, objType, obj );
85             case (JahiaEngine.UPDATE_MODE) : return update( jParams, engineMap, objType, obj );
86             case (JahiaEngine.SAVE_MODE) : return save( jParams, engineMap, objType, obj );
87         }
88         return false;
89         */

90     }
91
92
93     /**
94      * Handle Load Mode
95      *
96      * @param jParams a ParamBean object
97      * @param engineMap engineMap
98      * @param objType the object type defined by JahiaObjectTool, ie JahiaObjectTool.FIELD_TYPE
99      *
100      * @return true if everything went okay, false if not
101      *
102      * @see org.jahia.engines.JahiaEngine
103      */

104     public boolean load (ParamBean jParams, HashMap JavaDoc engineMap, int objType, Object JavaDoc obj)
105             throws JahiaException {
106         if (obj != null && obj instanceof JahiaField) {
107             JahiaField theField = (JahiaField) obj;
108
109             ArrayList JavaDoc localeList = jParams.getSite ().getLanguageSettingsAsLocales (false);
110
111             JahiaVersioningContentFieldFacade versioningContentFieldFacade =
112                     (JahiaVersioningContentFieldFacade) engineMap
113                     .get ("versioningContentFieldFacade_" + theField.getID ());
114             if (versioningContentFieldFacade == null) {
115                 versioningContentFieldFacade =
116                         this.loadVersioningContentFieldFacade (theField.getID (), LoadFlags.ALL,
117                                 jParams, localeList);
118                 engineMap.put ("versioningContentFieldFacade_" + theField.getID (),
119                         versioningContentFieldFacade);
120             }
121             jParams.getRequest ().setAttribute ("versioningContentFieldFacade",
122                     versioningContentFieldFacade);
123
124         }
125         String JavaDoc output = ServicesRegistry.getInstance ().getJahiaFetcherService ().fetchServlet (
126                 jParams, JSP_FILE);
127         engineMap.put ("versioningForm", output);
128         return true;
129     }
130
131
132     /**
133      * Handle Update Mode
134      *
135      * @param jParams a ParamBean object
136      * @param engineMap engineMap
137      * @param objType the object type defined by JahiaObjectTool, ie JahiaObjectTool.FIELD_TYPE
138      *
139      * @return true if everything went okay, false if not
140      *
141      * @see org.jahia.engines.JahiaEngine
142      */

143     public boolean update (ParamBean jParams, HashMap JavaDoc engineMap, int objType, Object JavaDoc obj)
144             throws JahiaException {
145         String JavaDoc restore_action = jParams.getRequest ().getParameter ("restore_action");
146
147         if ("restore".equals (restore_action)) {
148             String JavaDoc versionID = jParams.getRequest ().getParameter ("versionid");
149             if (versionID == null) {
150                 return true;
151             }
152             if (obj != null && obj instanceof JahiaField) {
153                 JahiaField theField = (JahiaField) obj;
154                 JahiaVersioningContentFieldFacade versioningContentFieldFacade =
155                         (JahiaVersioningContentFieldFacade) engineMap
156                         .get ("versioningContentFieldFacade_" + theField.getID ());
157                 if (versioningContentFieldFacade == null) {
158                     return false;
159                 }
160
161                 String JavaDoc languageCode = (String JavaDoc) engineMap.get ("languageCode");
162                 ArrayList JavaDoc locales = new ArrayList JavaDoc ();
163                 locales.add (LanguageCodeConverters.languageCodeToLocale (languageCode));
164
165                 EntryLoadRequest entryLoadRequest =
166                         new EntryLoadRequest (EntryLoadRequest.VERSIONED_WORKFLOW_STATE,
167                                 Integer.parseInt (versionID), locales);
168                 JahiaField versionedField = versioningContentFieldFacade.getField (
169                         entryLoadRequest);
170                 if (versionedField == null) {
171                     return false;
172                 }
173                 /*
174                 JahiaBaseACL parentACL = versionedField.getACL();
175                 ServicesRegistry.getInstance()
176                         .getJahiaFieldService()
177                         .saveField(versionedField,parentACL.getID(),jParams);
178                 */

179
180                 // replace the current active field in the update engine
181
JahiaField updatedField = (JahiaField) engineMap.get ("theField");
182                 if (updatedField == null) {
183                     return false;
184                 } else {
185                     versionedField.copyValueToAnotherField (updatedField, jParams);
186                 }
187             }
188         }
189         return true;
190     }
191
192     /**
193      * Handle Save Mode
194      *
195      * @param jParams a ParamBean object
196      * @param engineMap engineMap
197      * @param objType the object type defined by JahiaObjectTool, ie JahiaObjectTool.FIELD_TYPE
198      *
199      * @return true if everything went okay, false if not
200      *
201      * @see org.jahia.engines.JahiaEngine
202      */

203     public boolean save (ParamBean jParams, HashMap JavaDoc engineMap, int objType, Object JavaDoc obj)
204             throws JahiaException {
205         return true;
206     }
207
208     /**
209      * @param fieldID
210      *
211      * @return
212      */

213     private JahiaVersioningContentFieldFacade loadVersioningContentFieldFacade (int fieldID,
214                                                                                 int loadFlag,
215                                                                                 ParamBean jParams,
216                                                                                 ArrayList JavaDoc locales)
217             throws JahiaException {
218         JahiaVersioningContentFieldFacade versContentFieldFacade =
219                 new JahiaVersioningContentFieldFacade (fieldID, loadFlag, jParams, locales);
220
221         return versContentFieldFacade;
222     }
223
224     public boolean authoriseRender (ParamBean jParams) {
225         return false;
226     }
227
228     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj) throws JahiaException {
229         return ENGINE_NAME;
230     }
231
232     public boolean needsJahiaData (ParamBean jParams) {
233         return false;
234     }
235
236     public void handleActions (ParamBean jParams, JahiaData jData) throws JahiaException {
237     }
238
239     /**
240      * Retrieve the engine name.
241      *
242      * @return the engine name.
243      */

244     public final String JavaDoc getName () {
245         return ENGINE_NAME; //To change body of implemented methods use Options | File Templates.
246
}
247
248 }
Popular Tags