KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > web > controller > analysis > SaveAnalysisFormController


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.analysis;
21
22 import com.tonbeller.jpivot.chart.*;
23 import com.tonbeller.jpivot.olap.model.OlapModel;
24 import com.tonbeller.jpivot.table.TableComponent;
25 import com.tonbeller.jpivot.xmla.*;
26 import org.apache.log4j.Logger;
27 import org.openi.analysis.Analysis;
28 import org.openi.analysis.AnalysisAdapter;
29 import org.openi.chart.*;
30 import org.openi.chart.EnhancedChartComponent;
31 import org.openi.project.*;
32 import org.openi.security.Permission;
33 import org.openi.util.NameValue;
34 import org.springframework.validation.BindException;
35 import org.springframework.validation.Errors;
36 import org.springframework.web.servlet.ModelAndView;
37 import org.springframework.web.servlet.mvc.*;
38 import java.io.File JavaDoc;
39 import java.util.*;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42 import javax.servlet.http.HttpSession JavaDoc;
43
44
45 /**
46  * @author Dipendra Pokhrel<br>
47  * @version $Revision: 1.15 $ $Date: 2006/04/12 00:39:12 $ <br>
48  *
49  * Controller for handling <strong> save/save as analysis </strong> request.
50  *
51  */

52 public class SaveAnalysisFormController extends AbstractController {
53     private static Logger logger = Logger.getLogger(SaveAnalysisFormController.class);
54
55     /**
56      * handleRequestInternal
57      *
58      * @param httpServletRequest HttpServletRequest
59      * @param httpServletResponse HttpServletResponse
60      * @return ModelAndView
61      * @throws Exception
62      * @todo Implement this
63      * org.springframework.web.servlet.mvc.AbstractController method
64      */

65     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request,
66         HttpServletResponse JavaDoc response) throws Exception JavaDoc {
67         Map model = (Map) getModel(request);
68
69         if ("true".equalsIgnoreCase(request.getParameter("saveOnly"))) {
70             saveAndRedirect(request, response);
71
72             return null;
73         } else if (request.getMethod().equalsIgnoreCase("POST")) {
74             if ("Save Analysis".equalsIgnoreCase(request.getParameter("action"))) {
75                 if (saveAnalysis(request, response, model)) {
76                     return null;
77                 }
78             } else if ("Cancel".equalsIgnoreCase(request.getParameter("action"))) {
79                 response.sendRedirect("analysis.htm?message=Save cancelled");
80
81                 return null;
82             }
83         }
84
85         return new ModelAndView("saveAnalysisView", "model", model);
86     }
87
88     private void saveAndRedirect(HttpServletRequest JavaDoc request,
89         HttpServletResponse JavaDoc response) throws Exception JavaDoc {
90         Analysis analysis = (Analysis) request.getSession()
91                                               .getAttribute("analysis01");
92
93         String JavaDoc configPath = (String JavaDoc) request.getSession()
94                                             .getAttribute("analysisConfigName");
95         configPath = replaceSlace(configPath);
96
97         if ("true".equalsIgnoreCase(request.getParameter("saveOnly"))) {
98             ProjectContext context = (ProjectContext) request.getSession()
99                                                              .getAttribute("projectContext");
100
101             HttpSession JavaDoc session = request.getSession();
102
103             EnhancedChartComponent chart = (EnhancedChartComponent) session
104                     .getAttribute("chart01");
105
106                 TableComponent table = (TableComponent) session.getAttribute(
107                         "table01");
108
109                 OlapModel olapModel = (OlapModel) session.getAttribute(
110                         "query01");
111
112                 analysis = AnalysisAdapter.createAnalysis(analysis, chart,
113                         table, olapModel);
114
115                 context.save(configPath, analysis, "");
116                 response.sendRedirect("analysis.htm?message=Analysis '"
117                     + analysis.getAnalysisTitle() + "' saved successfully");
118             }
119         }
120
121         private Object JavaDoc getModel(HttpServletRequest JavaDoc request)
122             throws Exception JavaDoc {
123             Map map = new HashMap();
124
125             Analysis analysis = (Analysis) request.getSession()
126                                                   .getAttribute("analysis01");
127
128             String JavaDoc configPath = (String JavaDoc) request.getSession()
129                                                 .getAttribute("analysisConfigName");
130             configPath = replaceSlace(configPath);
131
132             int index;
133             index = configPath.lastIndexOf("/");
134
135             if (index != -1) {
136                 configPath = configPath.substring(0, index);
137             }
138
139             map.put("dirList", getFolderList(request));
140             map.put("path", configPath);
141             map.put("analysisTitle", analysis.getAnalysisTitle());
142
143             return map;
144         }
145
146         /**
147          *
148          * @param request HttpServletRequest
149          * @param response HttpServletResponse
150          * @param command Object
151          * @param errors BindException
152          * @return ModelAndView
153          * @throws Exception
154          */

155
156         /* private void onSubmit(
157            HttpServletRequest request, HttpServletResponse response,
158            Object command
159            ) throws Exception {
160            saveAnalysis(request, response, command);
161            }
162          */

163         boolean validateParam(HttpServletRequest JavaDoc request, Map map) {
164             String JavaDoc folder = request.getParameter("dirList");
165             String JavaDoc filename = request.getParameter("moduleAnalysis");
166             String JavaDoc comments = request.getParameter("comments");
167
168             if ((folder == null) || folder.equalsIgnoreCase("")) {
169                 map.put("errorMessage", "folder.message");
170
171                 return false;
172             }
173
174             if ((filename == null) || filename.equalsIgnoreCase("")) {
175                 map.put("errorMessage", "analysisName.message");
176
177                 return false;
178             }
179
180             if (folder.equalsIgnoreCase(request.getUserPrincipal().getName())
181                     && ((filename.indexOf("\\") != -1)
182                     || (filename.indexOf("/") != -1))) {
183                 map.put("errorMessage", "analysisNameWithSlash.message");
184
185                 return false;
186             }
187
188             return true;
189         }
190
191         private boolean saveAnalysis(HttpServletRequest JavaDoc request,
192             HttpServletResponse JavaDoc response, Object JavaDoc command)
193             throws Exception JavaDoc {
194             HttpSession JavaDoc session = request.getSession();
195
196             String JavaDoc folder = request.getParameter("dirList");
197             String JavaDoc filename = request.getParameter("moduleAnalysis");
198             String JavaDoc comments = request.getParameter("comments");
199
200             if (!validateParam(request, (Map) command)) {
201                 Map map = (Map) command;
202                 map.put("path", folder);
203                 map.put("analysisTitle", filename);
204                 map.put("comments", comments);
205
206                 return false;
207             }
208
209             ProjectContext context = (ProjectContext) session.getAttribute(
210                     "projectContext");
211
212             EnhancedChartComponent chart = (EnhancedChartComponent) session
213                     .getAttribute("chart01");
214
215                 TableComponent table = (TableComponent) session.getAttribute(
216                         "table01");
217
218                 OlapModel olapModel = (OlapModel) session.getAttribute(
219                         "query01");
220
221                 Analysis analysis = (Analysis) request.getSession()
222                                                       .getAttribute("analysis01");
223
224                 analysis = AnalysisAdapter.createAnalysis(analysis, chart,
225                         table, olapModel);
226
227                 String JavaDoc path;
228
229                 filename = parseAnalysisFile(filename);
230
231                 if (request.getUserPrincipal().getName().equalsIgnoreCase(folder)) {
232                     path = folder + "/" + filename;
233                     context.save(path, analysis, comments);
234                 } else {
235                     path = folder + "/" + filename;
236                     context.save(path, analysis, comments);
237                 }
238
239                 response.sendRedirect("analysis.htm?config=" + path);
240
241                 return true;
242             }
243
244             private String JavaDoc parseAnalysisFile(String JavaDoc path) {
245                 String JavaDoc file = path;
246                 file = file.trim();
247
248                 String JavaDoc[] paths = file.split("\\\\|/");
249                 file = "";
250
251                 for (int i = 0; i < paths.length; i++) {
252                     String JavaDoc temp = paths[i].trim();
253                     temp = temp.replaceAll(" ", "_");
254
255                     if (!temp.equals("")) {
256                         file = file + "/" + temp;
257                     }
258                 }
259
260                 file = file.substring(1);
261
262                 if (!file.toLowerCase().endsWith(".analysis")) {
263                     file = file + ".analysis";
264                 }
265
266                 return file;
267             }
268
269             private List getFolderList(HttpServletRequest JavaDoc request)
270                 throws Exception JavaDoc {
271                 ProjectContext context = (ProjectContext) request.getSession()
272                                                                  .getAttribute("projectContext");
273                 List dirlist = new ArrayList();
274
275                 dirlist = DirectoryLister.findProjectDirectories(context
276                             .getProjectDirectory(),
277                             context.getProjectModules(false,
278                                 context.hasPermission(Permission.SAVE_PUBLIC)));
279
280                     if (context.hasPermission(Permission.SAVE_PRIVATE)) {
281                         dirlist.add(new NameValue("personal",
282                                 context.getUsername()));
283                     }
284
285                     for (int i = 0; i < dirlist.size(); i++) {
286                         NameValue nv = (NameValue) dirlist.get(i);
287
288                         String JavaDoc path = (String JavaDoc) nv.getValue();
289                         String JavaDoc prjdir = new File JavaDoc(context.getProjectDirectory())
290                             .getCanonicalPath();
291                         prjdir = replaceSlace(prjdir);
292                         path = replaceSlace(path);
293
294                         path = path.trim();
295                         prjdir = prjdir.trim();
296
297                         if (path.length() > prjdir.length()) {
298                             path = path.substring(prjdir.length() + 1);
299                             nv.setValue(path);
300                         }
301                     }
302
303                     return dirlist;
304                 }
305
306                 private String JavaDoc replaceSlace(String JavaDoc source) {
307                     char[] out = source.toCharArray();
308
309                     for (int i = 0; i < out.length; i++) {
310                         if (out[i] == '\\') {
311                             out[i] = '/';
312                         }
313                     }
314
315                     return new String JavaDoc(out);
316                 }
317             }
318
Popular Tags