1 20 package org.openi.web.controller.admin; 21 22 import com.tonbeller.jpivot.olap.model.*; 23 import com.tonbeller.jpivot.xmla.*; 24 import org.apache.log4j.Logger; 25 import org.openi.analysis.Datasource; 26 import org.openi.application.Application; 27 import org.openi.project.JdbcDatasource; 28 import org.openi.project.Project; 29 import org.openi.project.ProjectContext; 30 import org.openi.util.*; 31 import org.springframework.validation.BindException; 32 import org.springframework.web.servlet.ModelAndView; 33 import org.springframework.web.servlet.mvc.SimpleFormController; 34 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 35 import java.net.URLEncoder ; 36 import java.util.*; 37 import java.util.Map ; 38 import javax.servlet.ServletException ; 39 import javax.servlet.http.HttpServletRequest ; 40 import javax.servlet.http.HttpServletResponse ; 41 import javax.servlet.http.HttpSession ; 42 import java.sql.SQLException ; 43 import java.sql.Connection ; 44 import java.sql.DriverManager ; 45 46 47 60 public class DatasourceFormController extends SimpleFormController { 61 private static Logger logger = Logger.getLogger(DatasourceFormController.class); 62 63 private static final String MAIN_VIEW = "mainDatasourceView"; 64 private static final String EDIT_VIEW = "editDatasourceView"; 65 private static final String JASPER_VIEW = "manageJasperDatasourceView"; 66 private static final String TYPE_SELECT_VIEW = "selectDatasourceTypeView"; 67 68 private String mainView = MAIN_VIEW; 69 private String editView = EDIT_VIEW; 70 private String jasperView = JASPER_VIEW; 71 private String typeSelectView = TYPE_SELECT_VIEW; 72 73 private ProjectContext projectContext = null; 74 75 81 protected Object formBackingObject(HttpServletRequest request) 82 throws Exception { 83 logger.debug("formBackingObject"); 84 85 projectContext = (ProjectContext) request.getSession() 87 .getAttribute("projectContext"); 88 Map map = new HashMap(); 89 initModel(map); 90 return map; 91 } 92 93 private void initModel(Map map) { 94 Project project = projectContext.getProject(); 95 Iterator iterator = project.getDataSourceMap().keySet().iterator(); 96 List items = new ArrayList(); 97 98 while (iterator.hasNext()) { 99 String ds = (String ) iterator.next(); 100 NameValue item = new NameValue(ds, 101 projectContext.getDatasourceObject(ds)); 102 items.add(item); 103 } 104 105 map.put("datasourceList", items); 106 map.put("projectContext", projectContext); 107 map.put("datasourceMap", project.getDataSourceMap()); 108 } 109 110 protected boolean validateParam(HttpServletRequest request, Map map) 111 throws Exception { 112 if ("Save".equalsIgnoreCase(request.getParameter("action"))) { 113 String dataSource; 114 dataSource = request.getParameter("dataSource"); 115 116 if ((dataSource == null) || dataSource.equalsIgnoreCase("")) { 118 map.put("errorMessage", "dataSource.message"); 119 120 return false; 121 } 122 123 String server = request.getParameter("server"); 124 125 if ((server == null) || server.equalsIgnoreCase("")) { 127 map.put("errorMessage", "server.message"); 128 129 return false; 130 } 131 132 String catalog = request.getParameter("catalog"); 133 134 if ((catalog == null) || catalog.equalsIgnoreCase("")) { 136 map.put("errorMessage", "catalog.message"); 137 138 return false; 139 } 140 } 141 142 return true; 143 } 144 145 protected boolean validateJdbcParam(HttpServletRequest request, Map map) 146 throws Exception { 147 if ("Save".equalsIgnoreCase(request.getParameter("action"))) { 148 String dataSource; 149 dataSource = request.getParameter("dataSource"); 150 151 if ((dataSource == null) || dataSource.equalsIgnoreCase("")) { 153 map.put("errorMessage", "dataSource.message"); 154 return false; 155 } 156 157 String driver = request.getParameter("driver"); 158 159 if ((driver == null) || driver.equalsIgnoreCase("")) { 161 map.put("errorMessage", "driver.message"); 162 return false; 163 } 164 165 String url = request.getParameter("url"); 166 167 if ((url == null) || url.equalsIgnoreCase("")) { 169 map.put("errorMessage", "jdbcurl.message"); 170 return false; 171 } 172 173 String user = request.getParameter("username"); 174 String password = request.getParameter("password"); 175 176 try { 177 Class.forName(driver); 178 Connection con = DriverManager.getConnection(url, user, password); 179 try { 180 con.close(); 181 } catch (SQLException ex) {} 182 con = null; 183 } catch(ClassNotFoundException ex) { 184 map.put("errorMessage", "driver.notfoundmessage"); 185 } catch(SQLException ex) { 186 map.put("errorMessage", "connection.errormessage"); 187 } 188 } 189 190 return true; 191 } 192 193 203 protected ModelAndView onSubmit(HttpServletRequest request, 204 HttpServletResponse response, 205 Object command, BindException errors) throws 206 Exception { 207 String action = request.getParameter("action"); 208 logger.debug("onSubmit action=" + action); 209 210 211 if ("New".equals(action)) { 212 return handleNew(request, response, command, errors); 213 } else if("Select".equals(action)) { 214 return handleTypeSelect(request, response, command, errors); 215 } else if ("Edit".equals(action)) { 216 return handleEdit(request, response, command, errors); 217 } else if ("Delete".equals(action)) { 218 return handleDelete(request, response, command, errors); 219 } else if ("Save".equals(action)) { 220 if ("jdbc".equals(request.getParameter("selectedType"))) { 221 return handleJdbcSave(request, response, command, errors); 222 } else { 223 return handleSave(request, response, command, errors); 224 } 225 } else if ("Cancel".equals(action)) { 226 return handleCancel(request, response, command, errors); 227 } else if ("Get Catalog".equals(action)) { 228 return handleCatalog(request, response, command, errors); 229 } else if("Configure RDBMS Mapping".equals(action)) { 230 return handleJasper(request, response, command, errors); 231 } else if("Save All".equals(action)) { 232 return handleJasperSave(request, response, command, errors); 233 } else if (request.getParameter("dataSource") != null) { 234 return handleSelection(request, response, command, errors); 235 } else { 236 return super.onSubmit(request, response, command, errors); 237 } 238 } 239 240 private ModelAndView handleNew(HttpServletRequest request, 241 HttpServletResponse response, Object command, 242 BindException errors) throws Exception { 243 return new ModelAndView(typeSelectView, "model", command); 244 } 245 246 private ModelAndView handleTypeSelect(HttpServletRequest request, 247 HttpServletResponse response, 248 Object command, BindException errors) throws 249 Exception { 250 String type = request.getParameter("datasourceType"); 251 252 if ("xmla".equals(type)) { 253 return handleAdd(request, response, command, errors); 254 } else if ("jdbc".equals(type)) { 255 return handleJdbcAdd(request, response, command, errors); 256 } else { 257 return handleNew(request, response, command, errors); 258 } 259 } 260 261 private ModelAndView handleSelection(HttpServletRequest request, 262 HttpServletResponse response, Object command, BindException errors) 263 throws Exception { 264 Map map = (Map ) command; 265 266 if (request.getParameter("dataSource") != null) { 267 Object obj = projectContext.getDatasourceObject(request.getParameter( 268 "dataSource")); 269 270 if (obj instanceof Datasource) { 271 map.put("selectedType", "xmla"); 272 } else { 273 map.put("selectedType", "jdbc"); 274 } 275 276 map.put("selectedDatasource", request.getParameter("dataSource")); 277 map.put("selectedDatasourceInstance", obj); 278 } 279 280 return new ModelAndView(mainView, "model", map); 281 } 282 283 private ModelAndView handleEdit(HttpServletRequest request, 284 HttpServletResponse response, Object command, BindException errors) 285 throws Exception { 286 ModelAndView modelview; 287 288 String datasource = request.getParameter("dataSource"); 289 290 Map map = (Map ) command; 291 map.put("dataSource", datasource); 292 293 if (datasource != null) { 294 Object obj = projectContext.getDatasourceObject(datasource); 295 map.put("selectedDatasourceInstance", obj); 296 297 if (obj instanceof Datasource) { 298 map.put("selectedType", "xmla"); 299 300 String server = projectContext.getDatasource(datasource) 301 .getServer(); 302 String catelog = projectContext.getDatasource(datasource) 303 .getCatalog(); 304 305 List catalogList = null; 306 307 try { 308 catalogList = getCatalogList(server, getUser(request), 309 getPassword(request)); 310 } catch (Exception e) { 311 logger.error("Exception:", e); 312 map.put("olapError", e.getMessage()); 313 } 314 315 map.put("server", server); 316 map.put("catalog", catelog); 317 map.put("catalogList", catalogList); 318 } else { 319 JdbcDatasource jdbc = (JdbcDatasource) obj; 320 map.put("selectedType", "jdbc"); 321 map.put("driver", jdbc.getDriverClassName()); 322 map.put("url", jdbc.getUrl()); 323 map.put("username", jdbc.getUsername()); 324 map.put("password", jdbc.getPassword()); 325 } 326 327 map.put("formType", "Edit"); 328 modelview = new ModelAndView(editView, "model", map); 329 } else { 330 modelview = super.onSubmit(request, response, command, errors); 331 } 332 333 return modelview; 334 } 335 336 private ModelAndView handleAdd(HttpServletRequest request, 337 HttpServletResponse response, Object command, BindException errors) 338 throws Exception { 339 Map map = (Map ) command; 340 map.put("formType", "Add"); 341 map.put("selectedType", "xmla"); 342 343 return new ModelAndView(editView, "model", command); 344 } 345 346 private ModelAndView handleJdbcAdd(HttpServletRequest request, 347 HttpServletResponse response, Object command, BindException errors) 348 throws Exception { 349 Map map = (Map ) command; 350 map.put("formType", "Add"); 351 map.put("selectedType", "jdbc"); 352 map.put("driver", "net.sourceforge.jtds.jdbc.Driver"); 353 map.put("url", "jdbc:jtds:sqlserver://yourhostname;DATABASENAME=yourdatabase"); 354 return new ModelAndView(editView, "model", command); 355 } 356 357 private ModelAndView handleCatalog(HttpServletRequest request, 358 HttpServletResponse response, Object command, BindException errors) { 359 String server = request.getParameter("server"); 360 Map map = (Map ) command; 361 map.put("dataSource", request.getParameter("dataSource")); 362 363 if (server != null) { 364 List catalogList = null; 365 366 try { 367 catalogList = getCatalogList(server, getUser(request), 368 getPassword(request)); 369 } catch (Exception e) { 370 logger.error("Exception:", e); 371 map.put("olapError", e.getMessage()); 372 } 373 374 map.put("catalogList", catalogList); 375 map.put("server", server); 376 map.put("selectedType", request.getParameter("selectedType")); 377 map.put("formType", request.getParameter("formType")); 378 } 379 380 return new ModelAndView(editView, "model", command); 381 } 382 383 private ModelAndView handleDelete(HttpServletRequest request, 384 HttpServletResponse response, Object command, BindException errors) 385 throws Exception { 386 String datasource = request.getParameter("dataSource"); 387 388 if (datasource != null) { 389 Map map = (Map ) command; 390 ((Map ) map.get("datasourceMap")).remove(datasource); 391 projectContext.saveProject(); 392 initModel(map); } 394 return new ModelAndView(mainView, "model", command); 395 } 396 397 private ModelAndView handleCancel(HttpServletRequest request, 398 HttpServletResponse response, Object command, BindException errors) 399 throws Exception { 400 if("Edit".equals(request.getParameter("formType"))) { 401 return handleSelection(request, response, command, errors); 402 } else { 403 return super.onSubmit(request, response, command, errors); 404 } 405 } 406 407 private ModelAndView handleSave(HttpServletRequest request, 408 HttpServletResponse response, Object command, BindException errors) 409 throws Exception { 410 String formtype = request.getParameter("formType"); 411 412 if (formtype != null) { 413 String datasource = request.getParameter("dataSource"); 414 String server = request.getParameter("server"); 415 String catalog = request.getParameter("catalog"); 416 Map map = (Map ) command; 417 map.put("server", server); 418 map.put("dataSource", datasource); 419 map.put("catalog", catalog); 420 map.put("formType", request.getParameter("formType")); 421 map.put("selectedType", request.getParameter("selectedType")); 422 423 if (formtype.equalsIgnoreCase("Edit")) { 424 if (validateParam(request, (Map ) command)) { 425 Datasource ds = projectContext.getDatasource(datasource); 426 427 if (ds != null) { 428 ds.setServer(server); 429 ds.setCatalog(catalog); 430 projectContext.saveProject(); 431 initModel(map); 433 return handleSelection(request, response, command, errors); 434 } 436 } else { 437 return new ModelAndView(editView, "model", command); 438 } 439 } else if (formtype.equalsIgnoreCase("Add")) { 440 if (validateParam(request, (Map ) command)) { 441 Datasource ds = projectContext.getDatasource(datasource); 442 443 if (ds == null) { 444 ds = new Datasource(server, catalog); 445 446 projectContext.setDatasource(datasource, ds); 447 projectContext.saveProject(); 448 449 initModel(map); 451 return handleSelection(request, response, command, errors); 452 } 454 } 455 } else { 456 return new ModelAndView(editView, "model", command); 457 } 458 } 459 460 return new ModelAndView(editView, "model", command); 461 } 462 463 private ModelAndView handleJdbcSave(HttpServletRequest request, 464 HttpServletResponse response, 465 Object command, BindException errors) throws 466 Exception { 467 468 String formtype = request.getParameter("formType"); 469 470 if (formtype != null) { 471 String datasource = request.getParameter("dataSource"); 472 String driver = request.getParameter("driver"); 473 String url = request.getParameter("url"); 474 String user = request.getParameter("username"); 475 String password = request.getParameter("password"); 476 Map map = (Map ) command; 477 map.put("dataSource", datasource); 478 map.put("driver", driver); 479 map.put("url", url); 480 map.put("username", user); 481 map.put("password", password); 482 map.put("formType", request.getParameter("formType")); 483 map.put("selectedType", request.getParameter("selectedType")); 484 485 if (formtype.equalsIgnoreCase("Edit")) { 486 if (validateJdbcParam(request, (Map ) command)) { 487 JdbcDatasource ds = (JdbcDatasource) projectContext 488 .getDatasourceObject(datasource); 489 if (ds != null) { 490 ds.setDriverClassName(driver); 491 ds.setUrl(url); 492 ds.setUsername(user); 493 ds.setPassword(password); 494 projectContext.saveProject(); 495 return handleSelection(request, response, command, 497 errors); 498 } 500 } else { 501 return new ModelAndView(editView, "model", command); 502 } 503 } else if (formtype.equalsIgnoreCase("Add")) { 504 if (validateJdbcParam(request, (Map ) command)) { 505 JdbcDatasource ds = (JdbcDatasource) projectContext 506 .getDatasourceObject(datasource); 507 if (ds == null) { 508 ds = new JdbcDatasource(); 509 ds.setDriverClassName(driver); 510 ds.setUrl(url); 511 ds.setUsername(user); 512 ds.setPassword(password); 513 projectContext.setDatasource(datasource, ds); 514 projectContext.saveProject(); 515 initModel(map); return handleSelection(request, response, command, 517 errors); 518 } 521 } 522 } else { 523 return new ModelAndView(editView, "model", command); 524 } 525 } 526 527 return new ModelAndView(editView, "model", command); 528 } 529 530 private ModelAndView handleJasper(HttpServletRequest request, 531 HttpServletResponse response, 532 Object command, BindException errors) throws 533 Exception { 534 Map map = (Map ) command; 535 Map files = buildJasperFileList(); 536 if(files.size() == 0) { 537 map.put("errorMessage", "nojaspertemplates.errormessage"); 538 return handleSelection(request, response, command, errors); 539 } 540 map.put("jdbcMap", files); 541 map.put("jdbcDatasource", projectContext.getJdbcDatasourceMap()); 542 return new ModelAndView(jasperView, "model", command); 543 } 544 545 private ModelAndView handleJasperSave(HttpServletRequest request, 546 HttpServletResponse response, 547 Object command, BindException errors) throws 548 Exception { 549 String [] files = request.getParameterValues("templatePath"); 550 String [] datasource = request.getParameterValues("datasource"); 551 for (int i = 0; i < files.length; i++) { 552 if (files[i] != null && !"".equals(files[i]) && datasource[i] != null && 553 !"".equals(datasource[i])) { 554 Map jdbcMap = projectContext.getProject().getJdbcMap(); 555 if (jdbcMap == null) { 556 jdbcMap = new HashMap(); 557 projectContext.getProject().setJdbcMap(jdbcMap); 558 } 559 jdbcMap.put(files[i], datasource[i]); 560 ((Map ) command).put("jdbcMap", buildJasperFileList()); 561 } 562 } 563 projectContext.saveProject(); 564 565 return super.onSubmit(request, response, command, errors); 566 } 567 568 private Map buildJasperFileList() { 569 List files = projectContext.getFilesByExtension(".jrxml"); 570 Iterator iter = files.iterator(); 571 Map fileMap = new HashMap(); 572 Map dsMap = projectContext.getJdbcDatasourceMap(); 573 while (iter.hasNext()) { 574 String path = (String ) iter.next(); 575 String datasource = ""; 576 if (projectContext.getProject().getJdbcMap() != null 577 && projectContext.getProject().getJdbcMap().containsKey(path)) { 578 if (dsMap != null) { 579 datasource = (String ) projectContext.getProject(). 580 getJdbcMap().get(path); 581 } 582 } 583 fileMap.put(path, datasource); 584 } 585 return fileMap; 586 } 587 588 589 private List getCatalogList(String server, String user, String pwd) throws 590 OlapException { 591 XMLA_SOAP olap = null; 592 593 if (Application.getInstance().isBasicAuthentication()) { 594 olap = new XMLA_SOAP(server, user, pwd); 595 } else { 596 olap = new XMLA_SOAP(server, "", ""); 598 } 599 600 if (olap == null) { 601 return null; 602 } 603 604 List catalogList = new ArrayList(); 605 606 Iterator iterator = olap.discoverCat().iterator(); 607 608 while (iterator.hasNext()) { 609 OlapItem item = (OlapItem) iterator.next(); 610 catalogList.add(item.getName()); 611 } 612 613 return catalogList; 614 } 615 616 624 private String getUser(HttpServletRequest request) throws Exception { 625 return URLEncoder.encode(request.getUserPrincipal().getName(), 626 "UTF-8"); 627 } 628 629 private String getPassword(HttpServletRequest request) throws Exception { 630 String pwd = (String ) request.getSession() 631 .getAttribute("user.credentials"); 632 633 if (pwd != null) { 634 pwd = URLEncoder.encode(pwd, "UTF-8"); 635 } else { 636 pwd = ""; 637 } 638 639 return pwd; 640 } 641 } 642 | Popular Tags |