KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > tasktool > actions > BasicScriptController


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.tasktool.actions;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30
31 import org.apache.log4j.Logger;
32 import org.exolab.castor.jdo.Database;
33 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService;
34 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController;
35 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController;
36 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
37 import org.infoglue.cms.controllers.kernel.impl.simple.RepositoryController;
38 import org.infoglue.cms.controllers.kernel.impl.simple.ServiceBindingController;
39 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeController;
40 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeVersionController;
41 import org.infoglue.cms.exception.SystemException;
42 import org.infoglue.cms.security.InfoGluePrincipal;
43
44 import webwork.action.ActionContext;
45
46 /**
47  * This is the script controller interface. This is the entry point for scripts running tasks.
48  */

49
50 public class BasicScriptController implements ScriptController
51 {
52     private final static Logger logger = Logger.getLogger(BasicScriptController.class.getName());
53
54     private Database db = null;
55     private HttpServletRequest JavaDoc request = null;
56     private Map JavaDoc outputParameters = new HashMap JavaDoc();
57     private final InfoGluePrincipal infoGluePrincipal;
58     
59
60     /**
61      * This is the public constructor which automatically locates the database object.
62      * All scripts are run within it's own transaction for now.
63      */

64
65     public BasicScriptController(InfoGluePrincipal infoGluePrincipal) throws SystemException, Exception JavaDoc
66     {
67         this.db = CastorDatabaseService.getDatabase();
68         this.infoGluePrincipal = infoGluePrincipal;
69     }
70     
71     /**
72      * A method to set the request for this script
73      */

74     
75     public HttpServletRequest JavaDoc getRequest()
76     {
77         return this.request;
78     }
79
80     /**
81      * A method to get the request for this script
82      */

83     
84     public void setRequest(HttpServletRequest JavaDoc request)
85     {
86         this.request = request;
87     }
88     
89     
90     /**
91      * This is a method to get hold of the SiteNodeController.
92      */

93     
94     public SiteNodeController getSiteNodeController()
95     {
96         return new SiteNodeController();
97     }
98     
99     /**
100      * This is a method to get hold of the SiteNodeController.
101      */

102     
103     public RepositoryController getRepositoryController()
104     {
105         return new RepositoryController();
106     }
107     
108     /**
109      * This is a method to get hold of the ServiceBindingController.
110      */

111     
112     public ServiceBindingController getServiceBindingController()
113     {
114         return new ServiceBindingController();
115     }
116
117     /**
118      * This is a method to get hold of the SiteNodeVersionController.
119      */

120     
121     public SiteNodeVersionController getSiteNodeVersionController()
122     {
123         return new SiteNodeVersionController();
124     }
125
126
127     /**
128      * This is a method to get hold of the SiteNodeVersionController.
129      */

130     
131     public ContentController getContentController()
132     {
133         return new ContentController();
134     }
135
136     /**
137      * This is a method to get hold of the SiteNodeVersionController.
138      */

139     
140     public ContentVersionController getContentVersionController()
141     {
142         return new ContentVersionController();
143     }
144
145     /**
146      * This is a method to get hold of the SiteNodeVersionController.
147      */

148     
149     public LanguageController getLanguageController()
150     {
151         return new LanguageController();
152     }
153     
154     /**
155      * Returns the InfoGluePrincipal running the task
156      */

157
158     public InfoGluePrincipal getInfoGluePrincipal()
159     {
160         return infoGluePrincipal;
161     }
162
163     /**
164      * This method returns an Integer from a String.
165      */

166     
167     public Integer JavaDoc getInteger(String JavaDoc value)
168     {
169         return new Integer JavaDoc(value);
170     }
171     
172     /**
173      * This method returns true if the object is null - false otherwise.
174      */

175     
176     public boolean isNull(Object JavaDoc object)
177     {
178         if(object == null)
179             return true;
180         else
181             return false;
182     }
183     
184     /**
185      * This method returns the ActionContext for the action
186      */

187     
188     public ActionContext getActionContext()
189     {
190         return ActionContext.getContext();
191     }
192     
193     /**
194      * This method instansiate a new object of the given class
195      */

196     
197     public Object JavaDoc getObjectWithName(String JavaDoc className)
198     {
199         try
200         {
201             Class JavaDoc theClass = null;
202             try
203             {
204                 theClass = Thread.currentThread().getContextClassLoader().loadClass( className );
205             }
206             catch (ClassNotFoundException JavaDoc e)
207             {
208                 theClass = getClass().getClassLoader().loadClass( className );
209             }
210             return theClass.newInstance();
211             
212         }
213         catch (InstantiationException JavaDoc e)
214         {
215             e.printStackTrace();
216         }
217         catch (IllegalAccessException JavaDoc e)
218         {
219             e.printStackTrace();
220         }
221         catch (ClassNotFoundException JavaDoc e)
222         {
223             e.printStackTrace();
224         }
225         
226         return null;
227     }
228     
229     /**
230      * This method gets the database for the script
231      */

232     
233     public Database getDatabase()
234     {
235         return this.db;
236     }
237     
238     /**
239      * Gets a parameter for output
240      */

241
242     public Object JavaDoc getOutputParameter(String JavaDoc parameter)
243     {
244         return this.outputParameters.get(parameter);
245     }
246
247     /**
248      * Sets a parameter for output later
249      */

250
251     public void setOutputParameter(String JavaDoc parameter, Object JavaDoc value)
252     {
253         this.outputParameters.put(parameter, value);
254     }
255
256
257     /**
258      * Begins a transaction on the named database
259      */

260          
261     public void beginTransaction() throws SystemException
262     {
263         try
264         {
265             this.db.begin();
266         }
267         catch(Exception JavaDoc e)
268         {
269             e.printStackTrace();
270             throw new SystemException("An error occurred when we tried to begin an transaction. Reason:" + e.getMessage(), e);
271         }
272     }
273        
274     /**
275      * Ends a transaction on the named database
276      */

277      
278     public void commitTransaction() throws SystemException
279     {
280         try
281         {
282             this.db.commit();
283             this.db.close();
284         }
285         catch(Exception JavaDoc e)
286         {
287             e.printStackTrace();
288             throw new SystemException("An error occurred when we tried to commit an transaction. Reason:" + e.getMessage(), e);
289         }
290     }
291  
292  
293     /**
294      * Rollbacks a transaction on the named database
295      */

296      
297     public void rollbackTransaction() throws SystemException
298     {
299         try
300         {
301             if (this.db.isActive())
302             {
303                 this.db.rollback();
304                 this.db.close();
305             }
306         }
307         catch(Exception JavaDoc e)
308         {
309             logger.info("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage());
310             //throw new SystemException("An error occurred when we tried to rollback an transaction. Reason:" + e.getMessage(), e);
311
}
312     }
313
314
315     /**
316      * This is a logmethod used to debug scripts.
317      */

318     
319     public void logInfo(String JavaDoc info)
320     {
321         logger.info(info);
322     }
323     
324     /**
325      * This is a logmethod used to debug scripts.
326      */

327     
328     public void logInfo(String JavaDoc header, String JavaDoc info)
329     {
330         logger.info(header + ": " + info);
331     }
332     
333     /**
334      * This is a logmethod used to debug scripts.
335      */

336     
337     public void logInfo(String JavaDoc header, boolean info)
338     {
339         logger.info(header + ": " + info);
340     }
341
342     /**
343      * This is a logmethod used to debug scripts.
344      */

345     
346     public void logInfo(String JavaDoc header, int info)
347     {
348         logger.info(header + ": " + info);
349     }
350     
351     /**
352      * This is a logmethod used to debug scripts.
353      */

354     
355     public void logInfo(String JavaDoc header, Object JavaDoc info)
356     {
357         logger.info(header + ": " + info);
358     }
359
360
361     /**
362      * This is a logmethod used to debug scripts.
363      */

364     
365     public void logWarning(String JavaDoc info)
366     {
367         logger.warn(info);
368     }
369     
370     /**
371      * This is a logmethod used to debug scripts.
372      */

373     
374     public void logWarning(String JavaDoc header, String JavaDoc info)
375     {
376         logger.warn(header + ": " + info);
377     }
378     
379     /**
380      * This is a logmethod used to debug scripts.
381      */

382     
383     public void logWarning(String JavaDoc header, boolean info)
384     {
385         logger.warn(header + ": " + info);
386     }
387
388     /**
389      * This is a logmethod used to debug scripts.
390      */

391     
392     public void logWarning(String JavaDoc header, int info)
393     {
394         logger.info(header + ": " + info);
395     }
396     
397     /**
398      * This is a logmethod used to debug scripts.
399      */

400     
401     public void logWarning(String JavaDoc header, Object JavaDoc info)
402     {
403         logger.warn(header + ": " + info);
404     }
405
406 }
407
Popular Tags