1 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 ; 39 import java.util.*; 40 import javax.servlet.http.HttpServletRequest ; 41 import javax.servlet.http.HttpServletResponse ; 42 import javax.servlet.http.HttpSession ; 43 44 45 52 public class SaveAnalysisFormController extends AbstractController { 53 private static Logger logger = Logger.getLogger(SaveAnalysisFormController.class); 54 55 65 protected ModelAndView handleRequestInternal(HttpServletRequest request, 66 HttpServletResponse response) throws Exception { 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 request, 89 HttpServletResponse response) throws Exception { 90 Analysis analysis = (Analysis) request.getSession() 91 .getAttribute("analysis01"); 92 93 String configPath = (String ) 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 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 getModel(HttpServletRequest request) 122 throws Exception { 123 Map map = new HashMap(); 124 125 Analysis analysis = (Analysis) request.getSession() 126 .getAttribute("analysis01"); 127 128 String configPath = (String ) 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 155 156 163 boolean validateParam(HttpServletRequest request, Map map) { 164 String folder = request.getParameter("dirList"); 165 String filename = request.getParameter("moduleAnalysis"); 166 String 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 request, 192 HttpServletResponse response, Object command) 193 throws Exception { 194 HttpSession session = request.getSession(); 195 196 String folder = request.getParameter("dirList"); 197 String filename = request.getParameter("moduleAnalysis"); 198 String 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 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 parseAnalysisFile(String path) { 245 String file = path; 246 file = file.trim(); 247 248 String [] paths = file.split("\\\\|/"); 249 file = ""; 250 251 for (int i = 0; i < paths.length; i++) { 252 String 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 request) 270 throws Exception { 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 path = (String ) nv.getValue(); 289 String prjdir = new File (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 replaceSlace(String 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 (out); 316 } 317 } 318 | Popular Tags |