KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > web > controller > RFunctionController


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

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 JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Enumeration JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Vector JavaDoc;
41 import java.util.zip.ZipEntry JavaDoc;
42 import java.util.zip.ZipOutputStream JavaDoc;
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import javax.xml.transform.TransformerException JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import org.openi.xml.BeanStorage;
48 import java.util.Date JavaDoc;
49 import java.text.SimpleDateFormat JavaDoc;
50 import java.io.File JavaDoc;
51
52 /**
53  * @author Uddhab Pant <br>
54  * @version $Revision: 1.10 $ $Date: 2006/04/12 00:39:12 $ <br>
55  *
56  * Controller for R functions. This controller generates UI for R functions
57  * and executes R functions.
58  *
59  */

60 public class RFunctionController extends AbstractController {
61     private static Logger logger = Logger.getLogger(RFunctionController.class);
62
63     private static final String JavaDoc MAIN_VIEW = "rFunctionView";
64
65     // xslt to transform xml
66
private String JavaDoc xslFile;
67
68     // xml file contains r functions
69
private String JavaDoc xmlFile;
70     private String JavaDoc mainView = MAIN_VIEW;
71
72     private ProjectContext projectContext;
73     private RFunctionList rFunctionList;
74
75     /**
76      *
77      * @param request HttpServletRequest
78      * @param response HttpServletResponse
79      * @return ModelAndView
80      * @throws ServletException
81      * @throws IOException
82      * @throws TransformerException
83      * @throws RSrvException
84      */

85     public ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request,
86                                               HttpServletResponse JavaDoc response) throws
87             Exception JavaDoc {
88
89         projectContext = (ProjectContext) request.getSession().getAttribute(
90                 "projectContext");
91
92         rFunctionList = projectContext.getRFunctionList();
93
94         String JavaDoc action = request.getParameter("action");
95         String JavaDoc ui = null;
96
97         // for sumbit action, connects to R server and executes R script
98
if ("Submit".equalsIgnoreCase(action)) {
99             RFunction function = rFunctionList.getFunction(request.getParameter(
100                     "functionName"));
101             if (function == null) {
102                 throw new Exception JavaDoc(
103                         "Invalid request. Function name is not specified");
104             }
105             //set absoulte path of the files
106
String JavaDoc 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 JavaDoc(dataSetFile).exists()) {
116                   throw new FileNotFoundException JavaDoc("R script file '"+ function.getFile() +"' does not exist");
117                 }
118             }
119
120             function.setDataSetFile(dataSetFile);
121             String JavaDoc rFile = projectContext.getProjectDirectory() + "/" +
122                              function.getFile();
123             if(!new File JavaDoc(dataSetFile).exists()) {
124                 throw new FileNotFoundException JavaDoc("Dataset file '"+ function.getDataSetFile() +"' does not exist");
125             }
126
127             function.setFile(rFile);
128
129             Iterator JavaDoc 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 JavaDoc outPath;
140             outPath = projectContext.getProjectDirectory() + "/" + projectContext.getUsername();
141             outPath = outPath + "/" + function.getName();
142             outPath = outPath + "/" + new SimpleDateFormat JavaDoc("yyyyMMdd_hhmmss").format(new Date JavaDoc());
143             File JavaDoc outfilePath = new File JavaDoc(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 JavaDoc 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     /**
170      * Generates UI for r function by delegating UI generation to RFunctionUIBuilder
171      * @param request HttpServletRequest
172      * @return String
173      * @throws TransformerException
174      * @throws IOException
175      */

176     private String JavaDoc generateUI(HttpServletRequest JavaDoc request, String JavaDoc functionName) throws
177             TransformerException JavaDoc, IOException JavaDoc {
178
179         String JavaDoc xslFileNamePath = this.getServletContext().getRealPath(xslFile);
180         //String xmlFileNamePath = this.getServletContext().getRealPath(xmlFile);
181

182         logger.debug(xslFileNamePath);
183         logger.debug(xmlFile);
184
185         Map JavaDoc param = new HashMap JavaDoc();
186         functionName = ( functionName != null)
187                               ? functionName : "";
188
189         String JavaDoc functionFile = listToString(projectContext.getFilesByExtension(
190                 ".r"), ";");
191         String JavaDoc 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 JavaDoc listToString(List JavaDoc list, String JavaDoc separator) {
206         Iterator JavaDoc it = list.iterator();
207         String JavaDoc files = "";
208         while (it.hasNext()) {
209             files += (String JavaDoc) it.next();
210             files += separator;
211         }
212         return files;
213     }
214
215     public void setXslFile(String JavaDoc xslFile) {
216         this.xslFile = xslFile;
217     }
218 }
219
Popular Tags