1 20 package org.openi.web.controller; 21 22 import org.apache.log4j.Logger; 23 24 import org.openi.stat.r.*; 25 import org.openi.stat.r.rserve.RServeClient; 26 import org.openi.project.ProjectContext; 27 28 import org.springframework.web.servlet.ModelAndView; 29 import org.springframework.web.servlet.mvc.AbstractController; 30 import java.io.BufferedOutputStream ; 31 import java.io.FileNotFoundException ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 import java.io.OutputStream ; 35 import java.util.ArrayList ; 36 import java.util.Enumeration ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Vector ; 41 import java.util.zip.ZipEntry ; 42 import java.util.zip.ZipOutputStream ; 43 import javax.servlet.http.HttpServletRequest ; 44 import javax.servlet.http.HttpServletResponse ; 45 import javax.xml.transform.TransformerException ; 46 import java.util.Iterator ; 47 import org.openi.xml.BeanStorage; 48 import java.util.Date ; 49 import java.text.SimpleDateFormat ; 50 import java.io.File ; 51 52 60 public class RFunctionController extends AbstractController { 61 private static Logger logger = Logger.getLogger(RFunctionController.class); 62 63 private static final String MAIN_VIEW = "rFunctionView"; 64 65 private String xslFile; 67 68 private String xmlFile; 70 private String mainView = MAIN_VIEW; 71 72 private ProjectContext projectContext; 73 private RFunctionList rFunctionList; 74 75 85 public ModelAndView handleRequestInternal(HttpServletRequest request, 86 HttpServletResponse response) throws 87 Exception { 88 89 projectContext = (ProjectContext) request.getSession().getAttribute( 90 "projectContext"); 91 92 rFunctionList = projectContext.getRFunctionList(); 93 94 String action = request.getParameter("action"); 95 String ui = null; 96 97 if ("Submit".equalsIgnoreCase(action)) { 99 RFunction function = rFunctionList.getFunction(request.getParameter( 100 "functionName")); 101 if (function == null) { 102 throw new Exception ( 103 "Invalid request. Function name is not specified"); 104 } 105 String dataSetFile = request.getParameter("dataSetFile"); 107 if (dataSetFile != null && !dataSetFile.equals("")) { 108 dataSetFile = projectContext.getProjectDirectory() + "/" + 109 dataSetFile; 110 } else { 111 dataSetFile = ""; 112 } 113 114 if(!"".equals(dataSetFile)) { 115 if(!new File (dataSetFile).exists()) { 116 throw new FileNotFoundException ("R script file '"+ function.getFile() +"' does not exist"); 117 } 118 } 119 120 function.setDataSetFile(dataSetFile); 121 String rFile = projectContext.getProjectDirectory() + "/" + 122 function.getFile(); 123 if(!new File (dataSetFile).exists()) { 124 throw new FileNotFoundException ("Dataset file '"+ function.getDataSetFile() +"' does not exist"); 125 } 126 127 function.setFile(rFile); 128 129 Iterator iter = function.getParams().iterator(); 130 while (iter.hasNext()) { 131 RFunctionParam param = (RFunctionParam) iter.next(); 132 param.setValue(request.getParameter(param.getName())); 133 } 134 135 logger.debug("Server IP:" + request.getParameter("rServerIP")); 136 137 RClient rclient = new RServeClient(request.getParameter("rServerIP"), 138 6311); 139 String outPath; 140 outPath = projectContext.getProjectDirectory() + "/" + projectContext.getUsername(); 141 outPath = outPath + "/" + function.getName(); 142 outPath = outPath + "/" + new SimpleDateFormat ("yyyyMMdd_hhmmss").format(new Date ()); 143 File outfilePath = new File (outPath); 144 if(!outfilePath.exists()) { 145 logger.info("Creating output dir " + outfilePath.getCanonicalPath()); 146 outfilePath.mkdirs(); 147 } 148 rclient.execute(function, outPath); 149 150 } else { 151 String rFile = request.getParameter("rFile"); 152 if(rFile != null) { 153 RFunction function = rFunctionList.getFunctionByFile(rFile); 154 if(function != null) { 155 ui = generateUI(request, function.getName()); 156 return new ModelAndView(mainView, "ui", ui); 157 } else { 158 response.sendRedirect("managerfunction.htm?rFile=" + rFile); 159 return null; 160 } 161 } 162 163 } 164 ui = generateUI(request, request.getParameter("functionName")); 165 return new ModelAndView(mainView, "ui", ui); 166 } 167 168 169 176 private String generateUI(HttpServletRequest request, String functionName) throws 177 TransformerException , IOException { 178 179 String xslFileNamePath = this.getServletContext().getRealPath(xslFile); 180 182 logger.debug(xslFileNamePath); 183 logger.debug(xmlFile); 184 185 Map param = new HashMap (); 186 functionName = ( functionName != null) 187 ? functionName : ""; 188 189 String functionFile = listToString(projectContext.getFilesByExtension( 190 ".r"), ";"); 191 String dataSetFile = listToString(projectContext.getFilesByExtension( 192 ".tab"), ";"); 193 dataSetFile += 194 listToString(projectContext.getFilesByExtension(".data"), ";"); 195 dataSetFile += 196 listToString(projectContext.getFilesByExtension(".txt"), ";"); 197 198 param.put("functionName", functionName); 199 param.put("functionFile", functionFile); 200 param.put("dataSetFile", dataSetFile); 201 202 return RFunctionUIBuilder.build(rFunctionList, xslFileNamePath, param); 203 } 204 205 private String listToString(List list, String separator) { 206 Iterator it = list.iterator(); 207 String files = ""; 208 while (it.hasNext()) { 209 files += (String ) it.next(); 210 files += separator; 211 } 212 return files; 213 } 214 215 public void setXslFile(String xslFile) { 216 this.xslFile = xslFile; 217 } 218 } 219 | Popular Tags |