KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > EngineToolBox


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
// $Id: EngineToolBox.java 7090 2004-08-06 18:01:19Z shuber $
14

15 //
16
// EV 10.01.20001
17
//
18

19 package org.jahia.engines;
20
21 import java.lang.reflect.InvocationTargetException JavaDoc;
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25
26 import org.jahia.data.fields.FieldTypes;
27 import org.jahia.data.fields.JahiaField;
28 import org.jahia.engines.audit.ManageLogs_Engine;
29 import org.jahia.engines.shared.Undefined_Field;
30 import org.jahia.exceptions.JahiaException;
31 import org.jahia.exceptions.JahiaUpdateLockException;
32 import org.jahia.params.ParamBean;
33 import org.jahia.registries.ServicesRegistry;
34 import org.jahia.services.audit.JahiaAuditLogManagerService;
35 import org.jahia.utils.JahiaObjectTool;
36
37
38 public class EngineToolBox {
39
40     private static EngineToolBox instance = null;
41
42     private static final String JavaDoc MSG_INTERNAL_ERROR = new String JavaDoc (
43             "Audit Log Manager internal error");
44     private static final String JavaDoc JSP_LOGFORM = "/jsp/jahia/engines/audit/viewlogs.jsp";
45     //private static final String JSP_SENDLOG = "/jsp/jahia/engines/audit/sendlogs.jsp";
46
private static final String JavaDoc JSP_FLUSHCONFIRM = "/jsp/jahia/engines/audit/flushconfirm.jsp";
47
48     // references to needed services.
49
private JahiaAuditLogManagerService mAuditLogManager = null;
50
51     /** Logger instance */
52     private static final org.apache.log4j.Logger logger =
53             org.apache.log4j.Logger.getLogger (EngineToolBox.class);
54
55
56     /**
57      * constructor
58      */

59     private EngineToolBox () {
60         logger.info ("***** Starting EngineToolBox *****");
61     }
62
63
64     /**
65      * returns a single instance of the object
66      */

67     public static synchronized EngineToolBox getInstance () {
68         if (instance == null) {
69             instance = new EngineToolBox ();
70         }
71         return instance;
72     }
73
74
75     /**
76      * authoriseRender
77      */

78     public boolean authoriseRender (ParamBean jParams) {
79         return (jParams.getOperationMode () == ParamBean.EDIT);
80     } // end authoriseRender
81

82
83     /**
84      * dispatches to an engine according to the field type
85      *
86      * @param jParams a ParamBean object
87      */

88     public boolean processFieldTypes (ParamBean jParams,
89                                       int mode,
90                                       HashMap JavaDoc engineMap)
91             throws JahiaException,
92             JahiaUpdateLockException {
93         boolean out = true;
94         JahiaField theField = (JahiaField) engineMap.get ("theField");
95
96         int pageDefID = jParams.getPage ().getPageTemplateID ();
97
98         int fieldType = theField.getDefinition ().getType (pageDefID);
99         logger.debug(" field type is " + fieldType);
100         if ((fieldType == FieldTypes.UNDEFINED) || (fieldType < 0)) {
101             fieldType = theField.getType ();
102         }
103         // get the engine className
104

105         String JavaDoc engineName = theField.getEngineName ();
106         if (fieldType == FieldTypes.UNDEFINED) {
107             out = Undefined_Field.getInstance ().
108                     handleField (jParams, new Integer JavaDoc (mode), engineMap);
109         } else {
110
111             // replace the switch-case statements
112
// avoid to modify the method when adding a field type
113
try {
114                 Class JavaDoc theParams[] = null;
115                 //System.out.println("EngineToolBox - processFieldTypes - engineName: "
116
// +engineName+", call getInstance");
117
Method JavaDoc thisMethod = Class.forName (engineName).
118                         getDeclaredMethod ("getInstance", theParams);
119                 Object JavaDoc args[] = null;
120                 Object JavaDoc engine = thisMethod.invoke (null, args);
121
122                 Class JavaDoc theParams2[] = {Class.forName (
123                         "org.jahia.params.ParamBean"),
124                                       Class.forName ("java.lang.Integer"),
125                                       Class.forName ("java.util.HashMap")};
126
127                 //System.out.println("EngineToolBox - processFieldTypes - engineName: "
128
// +engineName+", call handleField");
129
Method JavaDoc thisMethod2 = Class.forName (engineName).
130                         getDeclaredMethod ("handleField", theParams2);
131
132                 Object JavaDoc args2[] = {jParams, new Integer JavaDoc (mode), engineMap};
133                 out = ((Boolean JavaDoc) thisMethod2.invoke (engine, args2)).booleanValue ();
134
135                 logger.debug("Editing " + engineName.
136                         substring (engineName.lastIndexOf (".")) + " !");
137
138             } catch (ClassNotFoundException JavaDoc cnfe) {
139                 throw new JahiaException ("EngineToolBox:processFieldTypes",
140                         "Class not found!",
141                         JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, cnfe);
142
143             } catch (NoSuchMethodException JavaDoc nsme) {
144                 throw new JahiaException ("EngineToolBox:processFieldTypes",
145                         "Method not found!",
146                         JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, nsme);
147
148             } catch (IllegalAccessException JavaDoc iae) {
149                 throw new JahiaException ("EngineToolBox:processFieldTypes",
150                         "Illegal access",
151                         JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, iae);
152
153             } catch (InvocationTargetException JavaDoc ite) {
154
155                 ite.printStackTrace ();
156
157                 throw new JahiaException ("EngineToolBox:processFieldTypes",
158                         "InvocationTarget exception",
159                         JahiaException.PAGE_ERROR, JahiaException.CRITICAL_SEVERITY, ite);
160             }
161         }
162
163         // we flush the corresponding cache entry, since we assume we are
164
// updating it if we call this method (although we might only be
165
// performing a load operation)
166
/** @todo is there a way to do this only when we really update a
167          * field ? Maybe move this to a field.save() generic method.
168          */

169         ServicesRegistry.getInstance().getJahiaFieldService().invalidateCacheField(theField.getID());
170         return out;
171     } // end processFieldTypes
172

173
174     /**
175      * displays the screen requested by the user
176      *
177      * @param jParams a ParamBean object
178      */

179     public void displayScreen (ParamBean jParams, HashMap JavaDoc engineMap)
180             throws JahiaException {
181         EngineRenderer.getInstance ().render (jParams, engineMap);
182     } // end displayScreen
183

184
185     /**
186      * loads log data for the JSP file
187      *
188      * @param jParams a ParamBean object (with request and response)
189      * @param objectType an <code>int</code> representing the type of the object processed
190      * @param engineMap then engine map, to be forwarded to the JSP file
191      */

192     public void loadLogData (ParamBean jParams, int objectType, HashMap JavaDoc engineMap)
193             throws JahiaException {
194
195         // set default values
196
int flushLogs = 0;
197         String JavaDoc output;
198         int deletedRows = 0;
199
200         // get parameters
201
String JavaDoc userAgent = jParams.getRequest ().getHeader ("user-agent");
202         //boolean sendAsFile = (jParams.getRequest ().getParameter ("send") != null);
203
if (jParams.getRequest ().getParameter ("flush") != null) {
204             flushLogs = Integer.parseInt (jParams.getRequest ().getParameter ("flush"));
205         }
206         int objectID = JahiaObjectTool.getInstance ().getObjectID (objectType, engineMap);
207         String JavaDoc objectName = JahiaObjectTool.getInstance ().getObjectName (objectType,
208                 engineMap);
209
210         // Try to get the Audit Log Manager Service
211
ServicesRegistry registry = ServicesRegistry.getInstance ();
212         if (registry != null) {
213             mAuditLogManager = registry.getJahiaAuditLogManagerService ();
214             if (mAuditLogManager == null) {
215                 throw new JahiaException (MSG_INTERNAL_ERROR, "Properties Engine could not get the Audit Log Manager Service instance.",
216                         JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY);
217             }
218         } else {
219             throw new JahiaException (MSG_INTERNAL_ERROR, "Properties Engine could not get the Service Registry instance.",
220                     JahiaException.REGISTRY_ERROR, JahiaException.CRITICAL_SEVERITY);
221         }
222
223         // logs
224
engineMap.put ("flushLogs", new Integer JavaDoc (flushLogs));
225         engineMap.put ("sendLogsURL",
226                 ManageLogs_Engine.getInstance ().renderLink (jParams, ""));
227         engineMap.put ("objectTypeName",
228                 JahiaObjectTool.getInstance ().getObjectTypeName (objectType));
229         engineMap.put ("objectIDObj", new Integer JavaDoc (objectID));
230         engineMap.put ("objectName", objectName);
231         engineMap.put ("userAgent", userAgent);
232
233         // flushLogs cases :
234
// - null : just display current log entries
235
// - 1 : user has clicked "Flush" in viewlogs.jsp -> display confirmation request
236
// - 2 : user has confirmed flush in flushconfirm.jsp -> call flushlogs() method
237
// and display clean log window
238
switch (flushLogs) {
239             case 1:
240                 output = registry.getJahiaFetcherService ().fetchServlet (jParams,
241                         JSP_FLUSHCONFIRM);
242                 break;
243
244             case 2:
245                 deletedRows = mAuditLogManager.flushLogs (objectType, objectID, jParams);
246
247             default:
248                 ArrayList JavaDoc logData = mAuditLogManager.getLog (objectType, objectID,
249                         jParams);
250                 engineMap.put ("logData", logData);
251                 engineMap.put ("deletedRows", new Integer JavaDoc (deletedRows));
252                 output = registry.getJahiaFetcherService ().fetchServlet (jParams, JSP_LOGFORM);
253         }
254         engineMap.put ("logForm", output);
255
256     } // end loadLogData
257

258 } // end EngineToolBox
259
Popular Tags