1 23 24 package org.infoglue.cms.applications.tasktool.actions; 25 26 27 import java.io.PrintWriter ; 28 import java.io.Reader ; 29 import java.io.StringReader ; 30 import java.io.StringWriter ; 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 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 taskContentId = null; 57 private Integer contentId = null; 58 59 public ViewExecuteTaskAction() 60 { 61 } 62 63 67 68 public String doExecute() throws Exception 69 { 70 return "success"; 71 } 72 73 public String doUserInput() throws Exception 74 { 75 ContentVO contentVO = ContentController.getContentController().getContentVOWithId(this.getTaskContentId()); 76 77 logger.info("Language:" + LanguageController.getController().getMasterLanguage((Integer )getHttpSession().getAttribute("repositoryId")).getId()); 78 79 ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), LanguageController.getController().getMasterLanguage((Integer )getHttpSession().getAttribute("repositoryId")).getId()); 80 81 if(contentVersionVO == null) 83 contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), ((LanguageVO)LanguageController.getController().getLanguageVOList().get(0)).getId()); 84 85 87 String userInputHTML = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO.getId(), "UserInputHTML", false); 88 89 91 ScriptController scriptController = getScriptController(); 92 scriptController.setRequest(this.getRequest()); 93 scriptController.beginTransaction(); 94 95 Map context = new HashMap (); 96 context.put("scriptLogic", scriptController); 97 98 StringWriter cacheString = new StringWriter (); 99 PrintWriter cachedStream = new PrintWriter (cacheString); 100 renderTemplate(context, cachedStream, userInputHTML); 101 String result = cacheString.toString(); 102 103 scriptController.commitTransaction(); 104 105 getResponse().setContentType("text/html"); 106 PrintWriter out = getResponse().getWriter(); 107 out.println(result); 108 out.flush(); 109 out.close(); 110 111 return NONE; 112 } 113 114 120 121 public String doExecuteTask() throws Exception 122 { 123 ContentVO contentVO = ContentController.getContentController().getContentVOWithId(this.getTaskContentId()); 124 ContentVersionVO contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), LanguageController.getController().getMasterLanguage((Integer )getHttpSession().getAttribute("repositoryId")).getId()); 125 126 if(contentVersionVO == null) 128 contentVersionVO = ContentVersionController.getContentVersionController().getLatestContentVersionVO(contentVO.getId(), ((LanguageVO)LanguageController.getController().getLanguageVOList().get(0)).getId()); 129 130 String code = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO.getId(), "ScriptCode", false); 131 String userOutputHTML = ContentVersionController.getContentVersionController().getAttributeValue(contentVersionVO.getId(), "UserOutputHTML", false); 132 133 ScriptController scriptController = getScriptController(); 134 scriptController.setRequest(this.getRequest()); 135 scriptController.beginTransaction(); 136 137 Map context = new HashMap (); 138 context.put("scriptLogic", scriptController); 139 140 StringWriter cacheString = new StringWriter (); 141 PrintWriter cachedStream = new PrintWriter (cacheString); 142 renderTemplate(context, cachedStream, code); 143 String result = cacheString.toString(); 144 145 scriptController.commitTransaction(); 146 147 cacheString = new StringWriter (); 148 cachedStream = new PrintWriter (cacheString); 149 renderTemplate(context, cachedStream, userOutputHTML); 150 result = cacheString.toString(); 151 152 getResponse().setContentType("text/html"); 153 PrintWriter out = getResponse().getWriter(); 154 out.println(result); 155 out.flush(); 156 out.close(); 157 158 return NONE; 159 } 160 161 164 165 private ScriptController getScriptController() throws Exception 166 { 167 ScriptController scriptController = new BasicScriptController(this.getInfoGluePrincipal()); 168 169 return scriptController; 170 } 171 172 176 177 public void renderTemplate(Map params, PrintWriter pw, String templateAsString) throws Exception 178 { 179 Velocity.init(); 180 181 VelocityContext context = new VelocityContext(); 182 Iterator i = params.keySet().iterator(); 183 while(i.hasNext()) 184 { 185 String key = (String )i.next(); 186 context.put(key, params.get(key)); 187 } 188 189 Reader reader = new StringReader (templateAsString); 190 boolean finished = Velocity.evaluate(context, pw, "Generator Error", reader); 191 } 192 193 194 197 public List getTasks() throws Exception 198 { 199 HashMap arguments = new HashMap (); 200 arguments.put("method", "selectListOnContentTypeName"); 201 202 List argumentList = new ArrayList (); 203 HashMap argument = new HashMap (); 204 argument.put("contentTypeDefinitionName", "TaskDefinition"); 205 argumentList.add(argument); 206 arguments.put("arguments", argumentList); 207 208 return ContentControllerProxy.getController().getACContentVOList(this.getInfoGluePrincipal(), arguments); 209 } 211 212 public Integer getTaskContentId() 213 { 214 return taskContentId; 215 } 216 217 public void setTaskContentId(Integer taskContentId) 218 { 219 this.taskContentId = taskContentId; 220 } 221 222 public Integer getContentId() 223 { 224 return contentId; 225 } 226 227 public void setContentId(Integer contentId) 228 { 229 this.contentId = contentId; 230 } 231 } 232 | Popular Tags |