001: /*********************************************************************************
002: * The contents of this file are subject to the OpenI Public License Version 1.0
003: * ("License"); You may not use this file except in compliance with the
004: * License. You may obtain a copy of the License at
005: * http://www.openi.org/docs/LICENSE.txt
006: *
007: * Software distributed under the License is distributed on an "AS IS" basis,
008: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009: * the specific language governing rights and limitations under the License.
010: *
011: * The Original Code is: OpenI Open Source
012: *
013: * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
014: * Portions created by Loyalty Matrix, Inc. are
015: * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
016: *
017: * Contributor(s): i18n: Pedro Casals Fradera (2006/05/12)
018: *
019: ********************************************************************************/package org.openi.web.controller.analysis;
020:
021: import java.io.File;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.ResourceBundle;
027:
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030: import javax.servlet.http.HttpSession;
031:
032: import org.apache.log4j.Logger;
033: import org.openi.analysis.Analysis;
034: import org.openi.analysis.AnalysisAdapter;
035: import org.openi.chart.EnhancedChartComponent;
036: import org.openi.project.DirectoryLister;
037: import org.openi.project.ProjectContext;
038: import org.openi.util.NameValue;
039: import org.springframework.web.servlet.ModelAndView;
040: import org.springframework.web.servlet.mvc.AbstractController;
041:
042: import com.tonbeller.jpivot.olap.model.OlapModel;
043: import com.tonbeller.jpivot.table.TableComponent;
044:
045: /**
046: * @author Dipendra Pokhrel<br>
047: * @version $Revision: 1.18.2.1 $ $Date: 2006/09/12 12:45:21 $ <br>
048: *
049: * Controller for handling <strong> save/save as analysis </strong> request.
050: *
051: */
052: public class SaveAnalysisFormController extends AbstractController {
053: private static Logger logger = Logger
054: .getLogger(SaveAnalysisFormController.class);
055:
056: public static final String SERVLET_PATH = "/saveanalysis.htm";
057:
058: ProjectContext prjCtx = null;
059:
060: /**
061: * handleRequestInternal
062: *
063: * @param httpServletRequest HttpServletRequest
064: * @param httpServletResponse HttpServletResponse
065: * @return ModelAndView
066: * @throws Exception
067: * @todo Implement this
068: * org.springframework.web.servlet.mvc.AbstractController method
069: */
070: protected ModelAndView handleRequestInternal(
071: HttpServletRequest request, HttpServletResponse response)
072: throws Exception {
073:
074: prjCtx = (ProjectContext) request.getSession().getAttribute(
075: "projectContext");
076:
077: Map model = (Map) getModel(request);
078:
079: if ("true".equalsIgnoreCase(request.getParameter("saveOnly"))) {
080: saveAndRedirect(request, response);
081: return null;
082: } else if (request.getMethod().equalsIgnoreCase("POST")) {
083: if (request.getParameter("save") != null) {
084: if (saveAnalysis(request, response, model)) {
085: return null;
086: }
087: } else if (request.getParameter("cancel") != null) {
088: ResourceBundle rb = ResourceBundle.getBundle(
089: "org.openi.labels", prjCtx.getUser()
090: .getLocale());
091:
092: String message = rb
093: .getString("java_SaveAnalysisFormController.cancelled");
094:
095: response
096: .sendRedirect("analysis.htm?message=" + message);
097:
098: return null;
099: }
100: }
101:
102: return new ModelAndView("saveAnalysisView", "model", model);
103: }
104:
105: private boolean requestIsPrivate(HttpServletRequest request) {
106: if (request.getServletPath().startsWith(SERVLET_PATH)) {
107: return false;
108: } else {
109: return true;
110: }
111: }
112:
113: private void saveAndRedirect(HttpServletRequest request,
114: HttpServletResponse response) throws Exception {
115:
116: Analysis analysis = (Analysis) request.getSession()
117: .getAttribute("analysis01");
118:
119: String configPath = (String) request.getSession().getAttribute(
120: "analysisConfigName");
121: configPath = configPath.replace('\\', '/');
122:
123: if ("true".equalsIgnoreCase(request.getParameter("saveOnly"))) {
124: if (requestIsPrivate(request)
125: && !prjCtx.isPathBeneathUserDir(configPath)) {
126: response.sendError(HttpServletResponse.SC_FORBIDDEN,
127: "Target path is not allowed");
128: return;
129: }
130: HttpSession session = request.getSession();
131:
132: EnhancedChartComponent chart = (EnhancedChartComponent) session
133: .getAttribute("chart01");
134:
135: TableComponent table = (TableComponent) session
136: .getAttribute("table01");
137:
138: OlapModel olapModel = (OlapModel) session
139: .getAttribute("query01");
140:
141: analysis = AnalysisAdapter.createAnalysis(analysis, chart,
142: table, olapModel);
143:
144: prjCtx.save(configPath, analysis, "");
145:
146: ResourceBundle rb = ResourceBundle.getBundle(
147: "org.openi.labels", prjCtx.getUser().getLocale());
148:
149: String message = rb
150: .getString("java_SaveAnalysisFormController.saved1");
151: message += " '" + analysis.getAnalysisTitle() + "' ";
152: message += rb
153: .getString("java_SaveAnalysisFormController.saved2");
154: response.sendRedirect("analysis.htm?message=" + message);
155: }
156: }
157:
158: private Object getModel(HttpServletRequest request)
159: throws Exception {
160: Map map = new HashMap();
161:
162: Analysis analysis = (Analysis) request.getSession()
163: .getAttribute("analysis01");
164:
165: String configPath = (String) request.getSession().getAttribute(
166: "analysisConfigName");
167: configPath = configPath.replace('\\', '/');
168:
169: int index;
170: index = configPath.lastIndexOf("/");
171:
172: if (index != -1) {
173: configPath = configPath.substring(0, index);
174: }
175:
176: map.put("dirList", getFolderList(request));
177: map.put("path", configPath);
178: map.put("analysisTitle", analysis.getAnalysisTitle());
179:
180: return map;
181: }
182:
183: boolean validateParam(HttpServletRequest request, Map map) {
184: String folder = request.getParameter("dirList");
185: String filename = request.getParameter("moduleAnalysis");
186:
187: if ((folder == null) || folder.equalsIgnoreCase("")) {
188: map.put("errorMessage",
189: "java_SaveAnalysisFormController.folder");
190:
191: return false;
192: }
193:
194: if ((filename == null) || filename.equalsIgnoreCase("")) {
195: map.put("errorMessage",
196: "java_SaveAnalysisFormController.analysisName");
197:
198: return false;
199: }
200:
201: if (folder.equalsIgnoreCase(request.getUserPrincipal()
202: .getName())
203: && ((filename.indexOf("\\") != -1) || (filename
204: .indexOf("/") != -1))) {
205: map
206: .put("errorMessage",
207: "java_SaveAnalysisFormController.analysisNameWithSlash");
208:
209: return false;
210: }
211:
212: return true;
213: }
214:
215: private boolean saveAnalysis(HttpServletRequest request,
216: HttpServletResponse response, Object command)
217: throws Exception {
218:
219: HttpSession session = request.getSession();
220:
221: String folder = request.getParameter("dirList");
222: String filename = request.getParameter("moduleAnalysis");
223: String comments = request.getParameter("comments");
224:
225: if (requestIsPrivate(request)
226: && !prjCtx.isPathBeneathUserDir(folder)) {
227: response.sendError(HttpServletResponse.SC_FORBIDDEN,
228: "Target path is not allowed");
229: return false;
230: }
231:
232: if (!validateParam(request, (Map) command)) {
233: Map map = (Map) command;
234: map.put("path", folder);
235: map.put("analysisTitle", filename);
236: map.put("comments", comments);
237:
238: return false;
239: }
240:
241: EnhancedChartComponent chart = (EnhancedChartComponent) session
242: .getAttribute("chart01");
243:
244: TableComponent table = (TableComponent) session
245: .getAttribute("table01");
246:
247: OlapModel olapModel = (OlapModel) session
248: .getAttribute("query01");
249:
250: Analysis analysis = (Analysis) request.getSession()
251: .getAttribute("analysis01");
252:
253: analysis = AnalysisAdapter.createAnalysis(analysis, chart,
254: table, olapModel);
255:
256: String path;
257:
258: filename = parseAnalysisFile(filename);
259:
260: if (request.getUserPrincipal().getName().equalsIgnoreCase(
261: folder)) {
262: path = folder + "/" + filename;
263: prjCtx.save(path, analysis, comments);
264: } else {
265: path = folder + "/" + filename;
266: prjCtx.save(path, analysis, comments);
267: }
268:
269: response.sendRedirect("analysis.htm?config=" + path);
270:
271: return true;
272: }
273:
274: private String parseAnalysisFile(String path) {
275: String file = path;
276: file = file.trim();
277:
278: String[] paths = file.split("\\\\|/");
279: file = "";
280:
281: for (int i = 0; i < paths.length; i++) {
282: String temp = paths[i].trim();
283: temp = temp.replaceAll(" ", "_");
284:
285: if (!temp.equals("")) {
286: file = file + "/" + temp;
287: }
288: }
289:
290: file = file.substring(1);
291:
292: if (!file.toLowerCase().endsWith(".analysis")) {
293: file = file + ".analysis";
294: }
295:
296: return file;
297: }
298:
299: private List getFolderList(HttpServletRequest request)
300: throws Exception {
301:
302: List dirlist = new ArrayList();
303:
304: dirlist = DirectoryLister.findProjectDirectories(prjCtx
305: .getProjectDirectory(), prjCtx.getProjectModules(false,
306: !requestIsPrivate(request)));
307: List privateList = DirectoryLister.findProjectDirectories(
308: prjCtx.getProjectDirectory(), prjCtx.getProjectModules(
309: true, false));
310: if (privateList.size() == 0)
311: privateList.add(new NameValue(prjCtx.getUser().getId(),
312: prjCtx.getUser().getId()));
313: dirlist.addAll(privateList);
314:
315: for (int i = 0; i < dirlist.size(); i++) {
316: NameValue nv = (NameValue) dirlist.get(i);
317:
318: String path = (String) nv.getValue();
319: String prjdir = new File(prjCtx.getProjectDirectory())
320: .getCanonicalPath();
321: prjdir = prjdir.replace('\\', '/');
322: path = path.replace('\\', '/');
323:
324: path = path.trim();
325: prjdir = prjdir.trim();
326:
327: if (path.length() > prjdir.length()) {
328: path = path.substring(prjdir.length() + 1);
329: nv.setValue(path);
330: }
331: }
332:
333: return dirlist;
334: }
335: }
|