KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Reader JavaDoc;
29 import java.io.StringReader JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import org.apache.log4j.Logger;
38 import org.apache.velocity.VelocityContext;
39 import org.apache.velocity.app.Velocity;
40 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
41 import org.infoglue.cms.applications.structuretool.actions.ViewStructureTreeForInlineLinkAction;
42 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController;
43 import org.infoglue.cms.controllers.kernel.impl.simple.ContentControllerProxy;
44 import org.infoglue.cms.controllers.kernel.impl.simple.ContentVersionController;
45 import org.infoglue.cms.controllers.kernel.impl.simple.LanguageController;
46 import org.infoglue.cms.entities.content.ContentVO;
47 import org.infoglue.cms.entities.content.ContentVersionVO;
48 import org.infoglue.cms.entities.management.LanguageVO;
49
50 public class ViewExecuteTaskAction extends InfoGlueAbstractAction
51 {
52     private final static Logger logger = Logger.getLogger(ViewExecuteTaskAction.class.getName());
53
54     private static final long serialVersionUID = 1L;
55
56     private Integer JavaDoc taskContentId = null;
57     private Integer JavaDoc contentId = null;
58
59     public ViewExecuteTaskAction()
60     {
61     }
62     
63     /**
64      * This method which is the default one only serves to show a list
65      * of tasks to the user so he/she can select one to run.
66      */

67     
68     public String JavaDoc doExecute() throws Exception JavaDoc
69     {
70         return "success";
71     }
72
73     public String JavaDoc doUserInput() throws Exception JavaDoc
74     {
75         ContentVO contentVO = ContentController.getContentController().getContentVOWithId(this.getTaskContentId());
76         
77         logger.info("Language:" + LanguageController.getController().getMasterLanguage((Integer JavaDoc)getHttpSession().getAttribute("repositoryId")).getId());
78         
79         ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), LanguageController.getController().getMasterLanguage((Integer JavaDoc)getHttpSession().getAttribute("repositoryId")).getId());
80
81         //TODO - should not do this but find one available version probably
82
if(contentVersionVO == null)
83             contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), ((LanguageVO)LanguageController.getController().getLanguageVOList().get(0)).getId());
84         
85         //logger.info("contentVersionVO:" + contentVersionVO);
86

87         String JavaDoc userInputHTML = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO.getId(), "UserInputHTML", false);
88     
89         //logger.info("Found userInputHTML:" + userInputHTML);
90

91         ScriptController scriptController = getScriptController();
92         scriptController.setRequest(this.getRequest());
93         scriptController.beginTransaction();
94
95         Map JavaDoc context = new HashMap JavaDoc();
96         context.put("scriptLogic", scriptController);
97
98         StringWriter JavaDoc cacheString = new StringWriter JavaDoc();
99         PrintWriter JavaDoc cachedStream = new PrintWriter JavaDoc(cacheString);
100         renderTemplate(context, cachedStream, userInputHTML);
101         String JavaDoc result = cacheString.toString();
102
103         scriptController.commitTransaction();
104                 
105         getResponse().setContentType("text/html");
106         PrintWriter JavaDoc out = getResponse().getWriter();
107         out.println(result);
108         out.flush();
109         out.close();
110         
111         return NONE;
112     }
113
114     /**
115      * This method serves as the invoker of a task/script. It uses the velocity-engine to run logic.
116      * It allways run the script within it's own transaction as of now.
117      *
118      * @throws Exception
119      */

120
121     public String JavaDoc doExecuteTask() throws Exception JavaDoc
122     {
123         ContentVO contentVO = ContentController.getContentController().getContentVOWithId(this.getTaskContentId());
124         ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), LanguageController.getController().getMasterLanguage((Integer JavaDoc)getHttpSession().getAttribute("repositoryId")).getId());
125         
126         //TODO - should not do this but find one available version probably
127
if(contentVersionVO == null)
128             contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), ((LanguageVO)LanguageController.getController().getLanguageVOList().get(0)).getId());
129         
130         String JavaDoc code = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO.getId(), "ScriptCode", false);
131         String JavaDoc userOutputHTML = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO.getId(), "UserOutputHTML", false);
132         
133         ScriptController scriptController = getScriptController();
134         scriptController.setRequest(this.getRequest());
135         scriptController.beginTransaction();
136
137         Map JavaDoc context = new HashMap JavaDoc();
138         context.put("scriptLogic", scriptController);
139
140         StringWriter JavaDoc cacheString = new StringWriter JavaDoc();
141         PrintWriter JavaDoc cachedStream = new PrintWriter JavaDoc(cacheString);
142         renderTemplate(context, cachedStream, code);
143         String JavaDoc result = cacheString.toString();
144         
145         scriptController.commitTransaction();
146         
147         cacheString = new StringWriter JavaDoc();
148         cachedStream = new PrintWriter JavaDoc(cacheString);
149         renderTemplate(context, cachedStream, userOutputHTML);
150         result = cacheString.toString();
151
152         getResponse().setContentType("text/html");
153         PrintWriter JavaDoc out = getResponse().getWriter();
154         out.println(result);
155         out.flush();
156         out.close();
157                 
158         return NONE;
159     }
160
161     /**
162      * This method prepares the script-object which should be supplied to the scripting engine.
163      */

164     
165     private ScriptController getScriptController() throws Exception JavaDoc
166     {
167         ScriptController scriptController = new BasicScriptController(this.getInfoGluePrincipal());
168         
169         return scriptController;
170     }
171
172     /**
173      * This method takes arguments and renders a template given as a string to the specified outputstream.
174      * Improve later - cache for example the engine.
175      */

176     
177     public void renderTemplate(Map JavaDoc params, PrintWriter JavaDoc pw, String JavaDoc templateAsString) throws Exception JavaDoc
178     {
179         Velocity.init();
180
181         VelocityContext context = new VelocityContext();
182         Iterator JavaDoc i = params.keySet().iterator();
183         while(i.hasNext())
184         {
185             String JavaDoc key = (String JavaDoc)i.next();
186             context.put(key, params.get(key));
187         }
188         
189         Reader JavaDoc reader = new StringReader JavaDoc(templateAsString);
190         boolean finished = Velocity.evaluate(context, pw, "Generator Error", reader);
191     }
192     
193
194     /**
195      * This method returns the contents that are of contentTypeDefinition "HTMLTemplate"
196      */

197     public List JavaDoc getTasks() throws Exception JavaDoc
198     {
199         HashMap JavaDoc arguments = new HashMap JavaDoc();
200         arguments.put("method", "selectListOnContentTypeName");
201         
202         List JavaDoc argumentList = new ArrayList JavaDoc();
203         HashMap JavaDoc argument = new HashMap JavaDoc();
204         argument.put("contentTypeDefinitionName", "TaskDefinition");
205         argumentList.add(argument);
206         arguments.put("arguments", argumentList);
207         
208         return ContentControllerProxy.getController().getACContentVOList(this.getInfoGluePrincipal(), arguments);
209         //return ContentController.getContentController().getContentVOList(arguments);
210
}
211          
212     public Integer JavaDoc getTaskContentId()
213     {
214         return taskContentId;
215     }
216
217     public void setTaskContentId(Integer JavaDoc taskContentId)
218     {
219         this.taskContentId = taskContentId;
220     }
221
222     public Integer JavaDoc getContentId()
223     {
224         return contentId;
225     }
226
227     public void setContentId(Integer JavaDoc contentId)
228     {
229         this.contentId = contentId;
230     }
231 }
232
Popular Tags