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/15)
018: *
019: ********************************************************************************/package org.openi.web.controller.admin;
020:
021: import java.net.URLEncoder;
022: import java.sql.Connection;
023: import java.sql.DriverManager;
024: import java.sql.SQLException;
025: import java.util.ArrayList;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033: import javax.servlet.jsp.JspException;
034:
035: import org.apache.log4j.Logger;
036: import org.openi.analysis.Datasource;
037: import org.openi.application.Application;
038: import org.openi.feeds.FeedsDatasource;
039: import org.openi.feeds.client.FeedsService;
040: import org.openi.feeds.client.FeedsServiceFactory;
041: import org.openi.project.JdbcDatasource;
042: import org.openi.project.Project;
043: import org.openi.project.ProjectContext;
044: import org.openi.util.FileItem;
045: import org.openi.util.Folder;
046: import org.openi.util.FolderVisitor;
047: import org.openi.util.NameValue;
048: import org.springframework.validation.BindException;
049: import org.springframework.web.servlet.ModelAndView;
050: import org.springframework.web.servlet.mvc.SimpleFormController;
051:
052: import java.util.LinkedHashMap;
053: import com.tonbeller.jpivot.olap.model.OlapException;
054: import com.tonbeller.jpivot.olap.model.OlapItem;
055: import com.tonbeller.jpivot.xmla.XMLA_SOAP;
056:
057: /**
058: * @author Dipendra Pokhrel <br>
059: *
060: * Controller for handling <strong>manage datasource</strong> request.
061: * This class is responsible for:
062: * <ul>
063: * <li> adding new datasource
064: * <li> editing existing datasource
065: * <li> deleting existing datasource
066: * </ul>
067: *
068: */
069: public class DatasourceFormController extends SimpleFormController {
070: private static Logger logger = Logger
071: .getLogger(DatasourceFormController.class);
072:
073: private static final String MAIN_VIEW = "mainDatasourceView";
074: private static final String EDIT_VIEW = "editDatasourceView";
075: private static final String JASPER_VIEW = "manageJasperDatasourceView";
076: private static final String TYPE_SELECT_VIEW = "selectDatasourceTypeView";
077:
078: private String mainView = MAIN_VIEW;
079: private String editView = EDIT_VIEW;
080: private String jasperView = JASPER_VIEW;
081: private String typeSelectView = TYPE_SELECT_VIEW;
082:
083: private ProjectContext projectContext = null;
084:
085: private int maxFolderLevel = 5;
086:
087: /**
088: * Retrieve a backing object for the current form from the given request
089: * @param request HttpServletRequest
090: * @return Object as application config object
091: * @throws Exception
092: */
093: protected Object formBackingObject(HttpServletRequest request)
094: throws Exception {
095: logger.debug("formBackingObject");
096:
097: //get project context from session
098: projectContext = (ProjectContext) request.getSession()
099: .getAttribute("projectContext");
100: Map map = new HashMap();
101: initModel(map);
102: return map;
103: }
104:
105: private void initModel(Map map) {
106: Project project = projectContext.getProject();
107: Iterator iterator = project.getDataSourceMap().keySet()
108: .iterator();
109: List items = new ArrayList();
110:
111: while (iterator.hasNext()) {
112: String ds = (String) iterator.next();
113: NameValue item = new NameValue(ds, projectContext
114: .getDatasourceObject(ds));
115: items.add(item);
116: }
117:
118: map.put("datasourceList", items);
119: map.put("projectContext", projectContext);
120: map.put("datasourceMap", project.getDataSourceMap());
121: }
122:
123: protected boolean validateParam(HttpServletRequest request, Map map)
124: throws Exception {
125: if (request.getParameter("save") != null) {
126: String dataSource;
127: dataSource = request.getParameter("dataSource");
128:
129: //dataSource.trim();
130: if ((dataSource == null) || dataSource.equalsIgnoreCase("")) {
131: map.put("errorMessage",
132: "java_DatasourceFormController.dataSource");
133:
134: return false;
135: }
136:
137: String server = request.getParameter("server");
138:
139: //server.trim();
140: if ((server == null) || server.equalsIgnoreCase("")) {
141: map.put("errorMessage",
142: "java_DatasourceFormController.server");
143:
144: return false;
145: }
146:
147: String catalog = request.getParameter("catalog");
148:
149: //catalog.trim();
150: if ((catalog == null) || catalog.equalsIgnoreCase("")) {
151: map.put("errorMessage",
152: "java_DatasourceFormController.catalog");
153:
154: return false;
155: }
156: }
157:
158: return true;
159: }
160:
161: protected boolean validateJdbcParam(HttpServletRequest request,
162: Map map) throws Exception {
163: if (request.getParameter("save") != null) {
164: String dataSource;
165: dataSource = request.getParameter("dataSource");
166:
167: //dataSource.trim();
168: if ((dataSource == null) || dataSource.equalsIgnoreCase("")) {
169: map.put("errorMessage",
170: "java_DatasourceFormController.dataSource");
171: return false;
172: }
173:
174: String driver = request.getParameter("driver");
175:
176: //server.trim();
177: if ((driver == null) || driver.equalsIgnoreCase("")) {
178: map.put("errorMessage",
179: "java_DatasourceFormController.driver");
180: return false;
181: }
182:
183: String url = request.getParameter("url");
184:
185: //catalog.trim();
186: if ((url == null) || url.equalsIgnoreCase("")) {
187: map.put("errorMessage",
188: "java_DatasourceFormController.jdbcurl");
189: return false;
190: }
191:
192: String user = request.getParameter("username");
193: String password = request.getParameter("password");
194:
195: try {
196: Class.forName(driver);
197: Connection con = DriverManager.getConnection(url, user,
198: password);
199: try {
200: con.close();
201: } catch (SQLException ex) {
202: }
203: con = null;
204: } catch (ClassNotFoundException ex) {
205: map.put("errorMessage",
206: "java_DatasourceFormController.drivernotfound");
207: } catch (SQLException ex) {
208: map
209: .put("errorMessage",
210: "java_DatasourceFormController.connectionerror");
211: }
212: }
213:
214: return true;
215: }
216:
217: private boolean validateFsParam(HttpServletRequest request, Map map) {
218: if (request.getParameter("save") != null) {
219: String dataSource;
220: dataSource = request.getParameter("dataSource");
221:
222: //dataSource.trim();
223: if ((dataSource == null) || dataSource.equalsIgnoreCase("")) {
224: map.put("errorMessage",
225: "java_DatasourceFormController.dataSource");
226: return false;
227: }
228: String url = request.getParameter("url");
229:
230: //catalog.trim();
231: if ((url == null) || url.equalsIgnoreCase("")) {
232: map.put("errorMessage",
233: "java_DatasourceFormController.fileServerUrl");
234: return false;
235: }
236: }
237:
238: return true;
239: }
240:
241: /**
242: * Submit callback with all parameters. Called in case of submit without
243: * errors reported by the registered validator, or on every submit if no validator.
244: * @param request HttpServletRequest
245: * @param response HttpServletResponse
246: * @param command Object
247: * @param errors BindException
248: * @return ModelAndView
249: * @throws Exception
250: */
251: protected ModelAndView onSubmit(HttpServletRequest request,
252: HttpServletResponse response, Object command,
253: BindException errors) throws Exception {
254: if (request.getParameter("new") != null) {
255: return handleNew(request, response, command, errors);
256: } else if (request.getParameter("select") != null) {
257: return handleTypeSelect(request, response, command, errors);
258: } else if (request.getParameter("edit") != null) {
259: return handleEdit(request, response, command, errors);
260: } else if (request.getParameter("delete") != null) {
261: return handleDelete(request, response, command, errors);
262: } else if (request.getParameter("save") != null) {
263: if ("jdbc".equals(request.getParameter("selectedType"))) {
264: return handleJdbcSave(request, response, command,
265: errors);
266: } else if ("xmla".equals(request
267: .getParameter("selectedType"))) {
268: return handleSave(request, response, command, errors);
269: } else if ("fileServer".equals(request
270: .getParameter("selectedType"))) {
271: return handleFsSave(request, response, command, errors);
272: } else {
273: throw new JspException(
274: "Invalid datasource type specified :"
275: + request.getParameter("selectedType"));
276: }
277: } else if (request.getParameter("cancel") != null) {
278: return handleCancel(request, response, command, errors);
279: } else if (request.getParameter("getCatalog") != null) {
280: return handleCatalog(request, response, command, errors);
281: } else if (request.getParameter("getFolderList") != null) {
282: return handleGetFolder(request, response, command, errors);
283: } else if (request.getParameter("mapRDBMS") != null) {
284: return handleJasper(request, response, command, errors);
285: } else if (request.getParameter("saveall") != null) {
286: return handleJasperSave(request, response, command, errors);
287: } else if (request.getParameter("dataSource") != null) {
288: return handleSelection(request, response, command, errors);
289: } else {
290: return super .onSubmit(request, response, command, errors);
291: }
292: }
293:
294: private ModelAndView handleGetFolder(HttpServletRequest request,
295: HttpServletResponse response, Object command,
296: BindException errors) throws Exception {
297: String datasource = request.getParameter("dataSource");
298: String url = request.getParameter("url");
299: String remoteDir = request.getParameter("remoteDir");
300:
301: Map map = (Map) command;
302: map.put("dataSource", datasource);
303: map.put("url", url);
304: map.put("remoteDir", remoteDir);
305: map.put("formType", request.getParameter("formType"));
306: map.put("selectedType", request.getParameter("selectedType"));
307:
308: if (url != null && !"".equals(url)) {
309: try {
310: map.put("folderList", buildFolderList(url, request));
311: } catch (Exception ex) {
312: logger.error(ex);
313: }
314: }
315: return new ModelAndView(editView, getCommandName(), command);
316: }
317:
318: private Map buildFolderList(String url, HttpServletRequest request)
319: throws Exception {
320: if (url == null || "".equals(url))
321: return null;
322: String user = URLEncoder.encode(projectContext.getUser()
323: .getId(), "UTF-8");
324: String password = (String) request.getSession().getAttribute(
325: "user.credentials");
326: if (password != null && !"".equals(password))
327: password = URLEncoder.encode(password, "UTF-8");
328: FeedsService svc = FeedsServiceFactory.createRemote(url, "",
329: user, password);
330: Folder folder = svc.listFolders("", true, null);
331: FeedsFolderListBuilder feedsFolder = new FeedsFolderListBuilder();
332: folder.accept(feedsFolder);
333: return feedsFolder.getFolders();
334: }
335:
336: private ModelAndView handleNew(HttpServletRequest request,
337: HttpServletResponse response, Object command,
338: BindException errors) throws Exception {
339: return new ModelAndView(typeSelectView, "model", command);
340: }
341:
342: private ModelAndView handleTypeSelect(HttpServletRequest request,
343: HttpServletResponse response, Object command,
344: BindException errors) throws Exception {
345: String type = request.getParameter("datasourceType");
346:
347: if ("xmla".equals(type)) {
348: return handleAdd(request, response, command, errors);
349: } else if ("jdbc".equals(type)) {
350: return handleJdbcAdd(request, response, command, errors);
351: } else if ("fileServer".equals(type)) {
352: return handleFsAdd(request, response, command, errors);
353: } else {
354: return handleNew(request, response, command, errors);
355: }
356: }
357:
358: private ModelAndView handleSelection(HttpServletRequest request,
359: HttpServletResponse response, Object command,
360: BindException errors) throws Exception {
361: Map map = (Map) command;
362:
363: if (request.getParameter("dataSource") != null) {
364: Object obj = projectContext.getDatasourceObject(request
365: .getParameter("dataSource"));
366:
367: if (obj instanceof Datasource) {
368: map.put("selectedType", "xmla");
369: } else if (obj instanceof JdbcDatasource) {
370: map.put("selectedType", "jdbc");
371: } else if (obj instanceof FeedsDatasource) {
372: map.put("selectedType", "fileServer");
373: }
374:
375: map.put("selectedDatasource", request
376: .getParameter("dataSource"));
377: map.put("selectedDatasourceInstance", obj);
378: }
379:
380: return new ModelAndView(mainView, "model", map);
381: }
382:
383: private ModelAndView handleEdit(HttpServletRequest request,
384: HttpServletResponse response, Object command,
385: BindException errors) throws Exception {
386: ModelAndView modelview;
387:
388: String datasource = request.getParameter("dataSource");
389:
390: Map map = (Map) command;
391: map.put("dataSource", datasource);
392:
393: if (datasource != null) {
394: Object obj = projectContext.getDatasourceObject(datasource);
395: map.put("selectedDatasourceInstance", obj);
396:
397: if (obj instanceof Datasource) {
398: map.put("selectedType", "xmla");
399:
400: String server = projectContext
401: .getDatasource(datasource).getServer();
402: String catelog = projectContext.getDatasource(
403: datasource).getCatalog();
404:
405: List catalogList = null;
406:
407: try {
408: catalogList = getCatalogList(server,
409: getUser(request), getPassword(request));
410: } catch (Exception e) {
411: logger.error("Exception:", e);
412: map.put("olapError", e.getMessage());
413: }
414:
415: map.put("server", server);
416: map.put("catalog", catelog);
417: map.put("catalogList", catalogList);
418: } else if (obj instanceof JdbcDatasource) {
419: JdbcDatasource jdbc = (JdbcDatasource) obj;
420: map.put("selectedType", "jdbc");
421: map.put("driver", jdbc.getDriverClassName());
422: map.put("url", jdbc.getUrl());
423: map.put("username", jdbc.getUsername());
424: map.put("password", jdbc.getPassword());
425: } else if (obj instanceof FeedsDatasource) {
426: FeedsDatasource feedsDs = (FeedsDatasource) obj;
427: map.put("selectedType", "fileServer");
428: map.put("url", feedsDs.getServiceUri());
429: map.put("remoteDir", feedsDs.getRemoteDir());
430: try {
431: map.put("folderList", buildFolderList(feedsDs
432: .getServiceUri(), request));
433: } catch (Exception ex) {
434: logger.error(ex);
435: }
436: }
437:
438: map.put("formType", "Edit");
439: modelview = new ModelAndView(editView, "model", map);
440: } else {
441: modelview = super .onSubmit(request, response, command,
442: errors);
443: }
444:
445: return modelview;
446: }
447:
448: private ModelAndView handleAdd(HttpServletRequest request,
449: HttpServletResponse response, Object command,
450: BindException errors) throws Exception {
451: Map map = (Map) command;
452: map.put("formType", "Add");
453: map.put("selectedType", "xmla");
454:
455: return new ModelAndView(editView, "model", command);
456: }
457:
458: private ModelAndView handleJdbcAdd(HttpServletRequest request,
459: HttpServletResponse response, Object command,
460: BindException errors) throws Exception {
461: Map map = (Map) command;
462: map.put("formType", "Add");
463: map.put("selectedType", "jdbc");
464: map.put("driver", "net.sourceforge.jtds.jdbc.Driver");
465: map
466: .put("url",
467: "jdbc:jtds:sqlserver://yourhostname;DATABASENAME=yourdatabase");
468: return new ModelAndView(editView, "model", command);
469: }
470:
471: private ModelAndView handleFsAdd(HttpServletRequest request,
472: HttpServletResponse response, Object command,
473: BindException errors) throws Exception {
474: Map map = (Map) command;
475: map.put("formType", "Add");
476: map.put("selectedType", "fileServer");
477: map.put("url", "http://");
478: return new ModelAndView(editView, "model", command);
479: }
480:
481: private ModelAndView handleCatalog(HttpServletRequest request,
482: HttpServletResponse response, Object command,
483: BindException errors) {
484: String server = request.getParameter("server");
485: Map map = (Map) command;
486: map.put("dataSource", request.getParameter("dataSource"));
487:
488: if (server != null) {
489: List catalogList = null;
490:
491: try {
492: catalogList = getCatalogList(server, getUser(request),
493: getPassword(request));
494: } catch (Exception e) {
495: logger.error("Exception:", e);
496: map.put("olapError", e.getMessage());
497: }
498:
499: map.put("catalogList", catalogList);
500: map.put("server", server);
501: map.put("selectedType", request
502: .getParameter("selectedType"));
503: map.put("formType", request.getParameter("formType"));
504: }
505:
506: return new ModelAndView(editView, "model", command);
507: }
508:
509: private ModelAndView handleDelete(HttpServletRequest request,
510: HttpServletResponse response, Object command,
511: BindException errors) throws Exception {
512: String datasource = request.getParameter("dataSource");
513:
514: if (datasource != null) {
515: Map map = (Map) command;
516: ((Map) map.get("datasourceMap")).remove(datasource);
517: projectContext.saveProject();
518: initModel(map); //command = formBackingObject(request);
519: }
520: return new ModelAndView(mainView, "model", command);
521: }
522:
523: private ModelAndView handleCancel(HttpServletRequest request,
524: HttpServletResponse response, Object command,
525: BindException errors) throws Exception {
526: if ("Edit".equals(request.getParameter("formType"))) {
527: return handleSelection(request, response, command, errors);
528: } else {
529: return super .onSubmit(request, response, command, errors);
530: }
531: }
532:
533: private ModelAndView handleSave(HttpServletRequest request,
534: HttpServletResponse response, Object command,
535: BindException errors) throws Exception {
536: String formtype = request.getParameter("formType");
537:
538: if (formtype != null) {
539: String datasource = request.getParameter("dataSource");
540: String server = request.getParameter("server");
541: String catalog = request.getParameter("catalog");
542: Map map = (Map) command;
543: map.put("server", server);
544: map.put("dataSource", datasource);
545: map.put("catalog", catalog);
546: map.put("formType", request.getParameter("formType"));
547: map.put("selectedType", request
548: .getParameter("selectedType"));
549:
550: if (formtype.equalsIgnoreCase("Edit")) {
551: if (validateParam(request, (Map) command)) {
552: Datasource ds = projectContext
553: .getDatasource(datasource);
554:
555: if (ds != null) {
556: ds.setServer(server);
557: ds.setCatalog(catalog);
558: projectContext.saveProject();
559: initModel(map);//command = formBackingObject(request);
560:
561: return handleSelection(request, response,
562: command, errors);
563: //return new ModelAndView(mainView, "model", command);
564: }
565: } else {
566: return new ModelAndView(editView, "model", command);
567: }
568: } else if (formtype.equalsIgnoreCase("Add")) {
569: if (validateParam(request, (Map) command)) {
570: Datasource ds = projectContext
571: .getDatasource(datasource);
572:
573: if (ds == null) {
574: ds = new Datasource(server, catalog);
575:
576: projectContext.setDatasource(datasource, ds);
577: projectContext.saveProject();
578:
579: initModel(map);//command = formBackingObject(request);
580:
581: return handleSelection(request, response,
582: command, errors);
583: //return new ModelAndView(mainView, "model", command);
584: }
585: }
586: } else {
587: return new ModelAndView(editView, "model", command);
588: }
589: }
590:
591: return new ModelAndView(editView, "model", command);
592: }
593:
594: private ModelAndView handleJdbcSave(HttpServletRequest request,
595: HttpServletResponse response, Object command,
596: BindException errors) throws Exception {
597:
598: String formtype = request.getParameter("formType");
599:
600: if (formtype != null) {
601: String datasource = request.getParameter("dataSource");
602: String driver = request.getParameter("driver");
603: String url = request.getParameter("url");
604: String user = request.getParameter("username");
605: String password = request.getParameter("password");
606: Map map = (Map) command;
607: map.put("dataSource", datasource);
608: map.put("driver", driver);
609: map.put("url", url);
610: map.put("username", user);
611: map.put("password", password);
612: map.put("formType", request.getParameter("formType"));
613: map.put("selectedType", request
614: .getParameter("selectedType"));
615:
616: if (formtype.equalsIgnoreCase("Edit")) {
617: if (validateJdbcParam(request, (Map) command)) {
618: JdbcDatasource ds = (JdbcDatasource) projectContext
619: .getDatasourceObject(datasource);
620: if (ds != null) {
621: ds.setDriverClassName(driver);
622: ds.setUrl(url);
623: ds.setUsername(user);
624: ds.setPassword(password);
625: projectContext.saveProject();
626: //command = formBackingObject(request);
627: return handleSelection(request, response,
628: command, errors);
629: //return new ModelAndView(mainView, "model", command);
630: }
631: } else {
632: return new ModelAndView(editView, "model", command);
633: }
634: } else if (formtype.equalsIgnoreCase("Add")) {
635: if (validateJdbcParam(request, (Map) command)) {
636: JdbcDatasource ds = (JdbcDatasource) projectContext
637: .getDatasourceObject(datasource);
638: if (ds == null) {
639: ds = new JdbcDatasource();
640: ds.setDriverClassName(driver);
641: ds.setUrl(url);
642: ds.setUsername(user);
643: ds.setPassword(password);
644: projectContext.setDatasource(datasource, ds);
645: projectContext.saveProject();
646: initModel(map);//command = formBackingObject(request);
647: return handleSelection(request, response,
648: command, errors);
649: //return new ModelAndView(mainView, "model",
650: // command);
651: }
652: }
653: } else {
654: return new ModelAndView(editView, "model", command);
655: }
656: }
657:
658: return new ModelAndView(editView, "model", command);
659: }
660:
661: private ModelAndView handleFsSave(HttpServletRequest request,
662: HttpServletResponse response, Object command,
663: BindException errors) throws Exception {
664: String formtype = request.getParameter("formType");
665:
666: if (formtype != null) {
667: String datasource = request.getParameter("dataSource");
668: String url = request.getParameter("url");
669: String remoteDir = request.getParameter("remoteDir");
670:
671: Map map = (Map) command;
672: map.put("dataSource", datasource);
673: map.put("url", url);
674: map.put("remoteDir", remoteDir);
675: map.put("formType", request.getParameter("formType"));
676: map.put("selectedType", request
677: .getParameter("selectedType"));
678:
679: if (formtype.equalsIgnoreCase("Edit")) {
680: if (validateFsParam(request, (Map) command)) {
681: FeedsDatasource ds = (FeedsDatasource) projectContext
682: .getFeedsDatasource(datasource);
683: if (ds != null) {
684:
685: ds.setServiceUri(url);
686: ds.setRemoteDir(remoteDir);
687:
688: projectContext.saveProject();
689: //command = formBackingObject(request);
690: return handleSelection(request, response,
691: command, errors);
692: //return new ModelAndView(mainView, "model", command);
693: }
694: } else {
695: return new ModelAndView(editView, "model", command);
696: }
697: } else if (formtype.equalsIgnoreCase("Add")) {
698: if (validateFsParam(request, (Map) command)) {
699: FeedsDatasource ds = (FeedsDatasource) projectContext
700: .getDatasourceObject(datasource);
701: if (ds == null) {
702: ds = new FeedsDatasource();
703: ds.setServiceUri(url);
704: ds.setRemoteDir(remoteDir);
705:
706: projectContext.setDatasource(datasource, ds);
707: projectContext.saveProject();
708: initModel(map);//command = formBackingObject(request);
709: return handleSelection(request, response,
710: command, errors);
711: //return new ModelAndView(mainView, "model",
712: // command);
713: }
714: }
715: } else {
716: return new ModelAndView(editView, "model", command);
717: }
718: }
719:
720: return new ModelAndView(editView, "model", command);
721: }
722:
723: private ModelAndView handleJasper(HttpServletRequest request,
724: HttpServletResponse response, Object command,
725: BindException errors) throws Exception {
726: Map map = (Map) command;
727: Map files = buildJasperFileList();
728: if (files.size() == 0) {
729: map.put("errorMessage",
730: "java_DatasourceFormController.nojaspertemplates");
731: return handleSelection(request, response, command, errors);
732: }
733: map.put("jdbcMap", files);
734: map
735: .put("jdbcDatasource", projectContext
736: .getJdbcDatasourceMap());
737: return new ModelAndView(jasperView, "model", command);
738: }
739:
740: private ModelAndView handleJasperSave(HttpServletRequest request,
741: HttpServletResponse response, Object command,
742: BindException errors) throws Exception {
743: String[] files = request.getParameterValues("templatePath");
744: String[] datasource = request.getParameterValues("datasource");
745: for (int i = 0; i < files.length; i++) {
746: if (files[i] != null && !"".equals(files[i])
747: && datasource[i] != null
748: && !"".equals(datasource[i])) {
749: Map jdbcMap = projectContext.getProject().getJdbcMap();
750: if (jdbcMap == null) {
751: jdbcMap = new HashMap();
752: projectContext.getProject().setJdbcMap(jdbcMap);
753: }
754: jdbcMap.put(files[i], datasource[i]);
755: ((Map) command).put("jdbcMap", buildJasperFileList());
756: }
757: }
758: projectContext.saveProject();
759:
760: return super .onSubmit(request, response, command, errors);
761: }
762:
763: private Map buildJasperFileList() throws Exception {
764: List files = projectContext.getFilesByExtension(".jrxml");
765: Iterator iter = files.iterator();
766: Map fileMap = new HashMap();
767: Map dsMap = projectContext.getJdbcDatasourceMap();
768: while (iter.hasNext()) {
769: String path = (String) iter.next();
770: String datasource = "";
771: if (projectContext.getProject().getJdbcMap() != null
772: && projectContext.getProject().getJdbcMap()
773: .containsKey(path)) {
774: if (dsMap != null) {
775: datasource = (String) projectContext.getProject()
776: .getJdbcMap().get(path);
777: }
778: }
779: fileMap.put(path, datasource);
780: }
781: return fileMap;
782: }
783:
784: private List getCatalogList(String server, String user, String pwd)
785: throws OlapException {
786: XMLA_SOAP olap = null;
787:
788: if (Application.getInstance().isBasicAuthentication()) {
789: olap = new XMLA_SOAP(server, user, pwd);
790: } else {
791: // user password should not be null
792: olap = new XMLA_SOAP(server, "", "");
793: }
794:
795: if (olap == null) {
796: return null;
797: }
798:
799: List catalogList = new ArrayList();
800:
801: Iterator iterator = olap.discoverCat().iterator();
802:
803: while (iterator.hasNext()) {
804: OlapItem item = (OlapItem) iterator.next();
805: catalogList.add(item.getName());
806: }
807:
808: return catalogList;
809: }
810:
811: /**
812: * It will be better to place encoded user and password in current project so that it can
813: * be accessable easily without any helper method.**
814: * User or password passed to XMLA_SOAP c'tor or XMLAQueryTag as 'null' creates an exception.
815: * User can not be null in our case (beacuse we are using form based authentication
816: * however password can be if user does not use login valve.
817: * TODO: needs to add user/password accessor in project class and refactor this class
818: */
819: private String getUser(HttpServletRequest request) throws Exception {
820: return URLEncoder.encode(request.getUserPrincipal().getName(),
821: "UTF-8");
822: }
823:
824: private String getPassword(HttpServletRequest request)
825: throws Exception {
826: String pwd = (String) request.getSession().getAttribute(
827: "user.credentials");
828:
829: if (pwd != null) {
830: pwd = URLEncoder.encode(pwd, "UTF-8");
831: } else {
832: pwd = "";
833: }
834:
835: return pwd;
836: }
837:
838: class FeedsFolderListBuilder implements FolderVisitor {
839: LinkedHashMap folders = new LinkedHashMap();
840: int maxLevel = getMaxFolderLevel();
841: char prefixChar = '-';
842:
843: public void visit(Folder folder) {
844: String path[] = folder.getPath().replace('\\', '/').split(
845: "/");
846: if (path.length <= maxLevel) {
847: folders.put(folder.getPath(),
848: generatePrefix(path.length)
849: + folder.getDisplayName());
850: ;
851: }
852: }
853:
854: private String generatePrefix(int count) {
855: String prefix = "";
856: for (int i = 1; i < count; i++) {
857: prefix = prefix + prefixChar;
858: }
859: return prefix;
860: }
861:
862: public void visit(FileItem file) {
863: // TODO Auto-generated method stub
864:
865: }
866:
867: public Map getFolders() {
868: return this .folders;
869: }
870: }
871:
872: public int getMaxFolderLevel() {
873: return maxFolderLevel;
874: }
875:
876: public void setMaxFolderLevel(int maxFolderLevel) {
877: this.maxFolderLevel = maxFolderLevel;
878: }
879: }
|