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/16)
018: *
019: ********************************************************************************/package org.openi.web.controller.analysis;
020:
021: import org.apache.log4j.Logger;
022: import org.openi.project.ProjectContext;
023: import org.springframework.validation.BindException;
024: import org.springframework.web.servlet.ModelAndView;
025: import org.springframework.web.servlet.ModelAndViewDefiningException;
026: import org.springframework.web.servlet.mvc.SimpleFormController;
027: import java.util.HashMap;
028: import java.util.Map;
029: import java.util.ResourceBundle;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import javax.servlet.http.HttpSession;
034:
035: /**
036: * @author Uddhab Pant <br>
037: * @version $Revision: 1.8.2.3 $ $Date: 2007/01/04 12:29:40 $ <br>
038: *
039: * This controller handles delete analysis request.
040: *
041: */
042: public class DeleteAnalysisFormController extends SimpleFormController {
043: private static Logger logger = Logger
044: .getLogger(DeleteAnalysisFormController.class);
045:
046: private static final String SERVLET_PATH = "deleteanalysis.htm";
047:
048: /**
049: * This method will create the command object and set it in its initial state.
050: * This method is called before the user is directed to the first page of the wizard
051: *
052: * @param request HttpServletRequest
053: * @return Object
054: * @throws Exception
055: *
056: */
057: protected Object formBackingObject(HttpServletRequest request)
058: throws Exception {
059:
060: Map model;
061:
062: String message;
063:
064: model = new HashMap();
065:
066: String analysisConfigName = (String) request
067: .getParameter("config");
068:
069: if (analysisConfigName == null || "".equals(analysisConfigName))
070: throw new IllegalArgumentException(
071: "Missing request parameter");
072:
073: // construct message to display on page.
074: ProjectContext prjCtx = (ProjectContext) request.getSession()
075: .getAttribute("projectContext");
076:
077: ResourceBundle rb = ResourceBundle.getBundle(
078: "org.openi.labels", prjCtx.getUser().getLocale());
079:
080: if (prjCtx.isAppAdminUser() && !requestIsPrivate(request)
081: && !prjCtx.isPathBeneathUserDir(analysisConfigName)) {
082: ModelAndView mav = new ModelAndView("forbiddenView");
083: mav.addObject("message", "error.forbidden");
084: throw new ModelAndViewDefiningException(mav);
085: }
086:
087: if (prjCtx.isPathBeneathUserDir(analysisConfigName)) {
088: message = rb
089: .getString("java_DeleteAnalysisFormController.personal")
090: + " ";
091: } else {
092: message = rb
093: .getString("java_DeleteAnalysisFormController.public")
094: + " ";
095: }
096:
097: analysisConfigName = analysisConfigName.replaceAll("public/",
098: "");
099: analysisConfigName = analysisConfigName.replaceAll("personal/",
100: "");
101: analysisConfigName = analysisConfigName.replaceAll(request
102: .getUserPrincipal().getName()
103: + "/", "");
104:
105: message += analysisConfigName.replaceAll("\\,", "<br>");
106: message += " ?";
107: model.put("message", message);
108: model.put("config", analysisConfigName);
109: return model;
110: }
111:
112: private boolean requestIsPrivate(HttpServletRequest request) {
113: if (request.getServletPath().startsWith(SERVLET_PATH)) {
114: return false;
115: } else {
116: return true;
117: }
118: }
119:
120: /**
121: * Submit callback with all parameters. Called in case of submit without
122: * errors reported by the registered validator, or on every submit if no validator.
123: * @param request HttpServletRequest
124: * @param response HttpServletResponse
125: * @param command Object
126: * @param errors BindException
127: * @return ModelAndView
128: * @throws Exception
129: */
130: protected ModelAndView onSubmit(HttpServletRequest request,
131: HttpServletResponse response, Object command,
132: BindException errors) throws Exception {
133: try {
134:
135: // if user selects delete button
136: if (request.getParameter("delete") != null) {
137: logger.debug("delete analysis");
138: return handleDelete(request, response, command, errors);
139: }
140: //if user selects cancel button,
141: else if (request.getParameter("cancel") != null) {
142: logger.debug("cancel analysis");
143: return handleCancel(request, response, command, errors);
144: }
145:
146: return super .onSubmit(request, response, command, errors);
147: } catch (Exception e) {
148: logger.error("Exception:", e);
149: throw e;
150: }
151: }
152:
153: /**
154: * This method handles delete action.
155: *
156: * @param request HttpServletRequest
157: * @param response HttpServletResponse
158: * @param command Object
159: * @param errors BindException
160: * @return ModelAndView
161: * @throws Exception
162: */
163: protected ModelAndView handleDelete(HttpServletRequest request,
164: HttpServletResponse response, Object command,
165: BindException errors) throws Exception {
166: HttpSession session = request.getSession();
167:
168: ProjectContext projectContext = (ProjectContext) session
169: .getAttribute("projectContext");
170:
171: String analysisConfigName = request.getParameter("config");//(String) session.getAttribute("analysisConfigName");
172:
173: // delegate delete analysis task to project context.
174: projectContext.deleteAnalysis(analysisConfigName);
175:
176: session.removeAttribute("analysisConfigName");
177:
178: // redirect to splash page
179: return new ModelAndView("splashRedirect");
180:
181: //need to redirect to splash page when splash page is implemented in spring
182: //return super.onSubmit(request, response, command, errors);
183: }
184:
185: /**
186: * This method handle cancel action and redirect to analysis page.
187: *
188: * @param request HttpServletRequest
189: * @param response HttpServletResponse
190: * @param command Object
191: * @param errors BindException
192: * @return ModelAndView
193: * @throws Exception
194: */
195: protected ModelAndView handleCancel(HttpServletRequest request,
196: HttpServletResponse response, Object command,
197: BindException errors) throws Exception {
198: return super.onSubmit(request, response, command, errors);
199: }
200: }
|