KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > action > OlapClientConnectionAction


1 package com.jaspersoft.jasperserver.war.action;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.springframework.validation.DataBinder;
9 import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;
10 import org.springframework.webflow.Event;
11 import org.springframework.webflow.RequestContext;
12 import org.springframework.webflow.ScopeType;
13 import org.springframework.webflow.action.FormAction;
14
15 import com.jaspersoft.jasperserver.api.JSException;
16 import com.jaspersoft.jasperserver.api.engine.common.service.EngineService;
17 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource;
18 import com.jaspersoft.jasperserver.api.metadata.common.domain.Folder;
19 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource;
20 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup;
21 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference;
22 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService;
23 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource;
24 import com.jaspersoft.jasperserver.api.metadata.olap.domain.JdbcOlapDataSource;
25 import com.jaspersoft.jasperserver.api.metadata.olap.domain.JndiJdbcOlapDataSource;
26 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection;
27 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianXMLADefinition;
28 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapClientConnection;
29 import com.jaspersoft.jasperserver.api.metadata.olap.domain.XMLAConnection;
30 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService;
31 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria;
32 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor;
33 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl;
34 import com.jaspersoft.jasperserver.war.dto.BaseDTO;
35 import com.jaspersoft.jasperserver.war.dto.FileResourceWrapper;
36 import com.jaspersoft.jasperserver.war.dto.OlapClientConnectionWrapper;
37 import com.jaspersoft.jasperserver.war.dto.OlapUnitWrapper;
38 import com.jaspersoft.jasperserver.war.dto.ReportDataSourceWrapper;
39 import com.jaspersoft.jasperserver.war.validation.OlapClientConnectionValidator;
40
41 /**
42  *
43  * OlapClientConnectionAction provides the actions to olap client connection views
44  *
45  * @author jshih
46  */

47 public class OlapClientConnectionAction extends FormAction {
48     private static final String JavaDoc OU_URI_PARAM = "resource";
49     private static final String JavaDoc FORM_OBJECT_KEY = "connectionWrapper";
50     private static final String JavaDoc OLAP_UNIT_SUFFIX = "_unit";
51     private static final String JavaDoc OLAP_UNIT_KEY = "unit";
52     private static final String JavaDoc CONTROL_OBJECT_KEY = "control";
53     private static final String JavaDoc RESOURCE_OBJECT_KEY = "resource";
54     private static final String JavaDoc DATASOURCE_OBJECT_KEY = "dataResource";
55     private static final String JavaDoc SCHEMA_OBJECT_KEY = "fileResource";
56     private static final String JavaDoc PARENT_FOLDER_ATTR = "parentFolder";
57     private static final String JavaDoc PARAM_FROM_PAGE = "frompage";
58     private static final String JavaDoc PARAM_RESOURCE_NAME = "resourceName";
59     private static final String JavaDoc LOCAL_SCHEMA_SUFFIX = "_schema";
60     private static final String JavaDoc UNPARSABLE_SCHEMA_ATTR = "schemaUnparsable";
61     private static final String JavaDoc INVALID_XMLA_CONNECTION_ATTR = "xmlaConnectionInvalid";
62     private static final String JavaDoc UNPARSABLE_XMLA_ATTR = "xmlaUnparsable";
63     private static final String JavaDoc UNPARSABLE_CONNECTION_ATTR = "connectionUnparsable";
64     protected final Log log = LogFactory.getLog(this.getClass());
65     protected RepositoryService repository;
66     protected OlapConnectionService olapConnection;
67     private EngineService engine;
68     protected JasperServerConstImpl constants = new JasperServerConstImpl();
69
70     /**
71      * initialize OlapClientConnectionAction.class object
72      */

73     public OlapClientConnectionAction() {
74         setFormObjectClass(OlapClientConnectionWrapper.class);
75         setFormObjectName(FORM_OBJECT_KEY);
76         setFormObjectScope(ScopeType.FLOW);
77         setValidator(new OlapClientConnectionValidator());
78     }
79
80     /**
81      * initAction performs the initialization for the olap client connection web flow
82      *
83      * @param context
84      * @return
85      * @throws Exception
86      */

87     public Event initAction(RequestContext context) throws Exception JavaDoc {
88         OlapClientConnectionWrapper wrapper =
89             (OlapClientConnectionWrapper) getFormObject(context);
90         OlapUnitWrapper parentWrapper = (OlapUnitWrapper) wrapper
91                 .getParentFlowObject();
92         OlapClientConnection connection = null;
93         if (wrapper.isNewMode()) {
94             // create new olap client connection
95
String JavaDoc folderURI = (String JavaDoc) context.getFlowScope().get(
96                     PARENT_FOLDER_ATTR);
97             if (folderURI == null) {
98                 folderURI = "/";
99             }
100             FilterCriteria resourcesInFolder = FilterCriteria.createFilter();
101             resourcesInFolder.addFilterElement(FilterCriteria
102                     .createParentFolderFilter(folderURI));
103             log("Searching for resources in the chosen folder:" + folderURI);
104             ResourceLookup[] existingResources = repository.findResource(null,
105                     resourcesInFolder);
106
107             if (existingResources != null && existingResources.length != 0) {
108                 log("res lookup size=" + existingResources.length);
109                 List JavaDoc allResources = new ArrayList JavaDoc();
110                 for (int i = 0; i < existingResources.length; i++) {
111                     ResourceLookup rLookup = existingResources[i];
112                     allResources.add(rLookup.getName());
113                     log("adding resource: " + rLookup.getName()
114                             + " to the list");
115                 }
116                 wrapper.setExistingResources(allResources);
117             }
118         } else {
119             // modify existing olap client connection
120
if (parentWrapper == null) {
121                 // main flow
122
connection = wrapper.getOlapClientConnection();
123             } else {
124                 // subflow
125
connection = parentWrapper.getOlapClientConnection();
126             }
127
128             if (connection == null) {
129                 throw new JSException("No OLAP client connection");
130             }
131
132             wrapper.setOlapClientConnection(connection);
133             wrapper.setConnectionName(connection.getName());
134             wrapper.setConnectionLabel(connection.getLabel());
135             wrapper.setConnectionDescription(connection.getDescription());
136
137             // set the connection type to enable testConnectionType in webflow
138
if (wrapper.getOlapClientConnection() instanceof MondrianConnection) {
139                 // for mondrian connection
140
wrapper.setOlapClientSchema((FileResource) repository
141                         .getResource(null, ((MondrianConnection) connection)
142                                 .getSchema().getReferenceURI()));
143                 wrapper.setOlapClientDatasource((ReportDataSource) repository
144                         .getResource(null, ((MondrianConnection) connection)
145                                 .getDataSource().getReferenceURI()));
146                 // TODO subflow
147
if (parentWrapper != null) {
148                     wrapper.setSchemaUri(parentWrapper.getSchemaUri());
149                     wrapper.setDatasourceUri(parentWrapper.getOlapUnit()
150                             .getDataSource().getReferenceURI()); // FIXME
151
}
152                 wrapper.setType(constants.getOlapMondrianConnectionType());
153             } else if (wrapper.getOlapClientConnection() instanceof XMLAConnection) {
154                 // for xmla connection
155
wrapper.setXmlaCatalog(((XMLAConnection) connection)
156                         .getCatalog());
157                 wrapper.setXmlaDatasource(((XMLAConnection) connection)
158                         .getDataSource());
159                 wrapper.setXmlaConnectionUri(((XMLAConnection) connection)
160                         .getURI());
161                 wrapper.setUsername(((XMLAConnection) connection)
162                         .getUsername());
163                 wrapper.setPassword(((XMLAConnection) connection)
164                         .getPassword());
165                 wrapper.setType(constants.getOlapXmlaConnectionType());
166             } else {
167                 throw new JSException("Unknown connection type");
168             }
169             wrapper.setSource(constants.FIELD_CHOICE_CONT_REPO);
170         }
171         // get reusable resources
172
if (!wrapper.isSubflowMode()) {
173             getAllConnections(wrapper);
174             findAllSchemas(wrapper);
175             getAllXmlaSources(wrapper);
176         } else {
177             // get resource from main flow
178
wrapper.setReusableMondrianConnections(parentWrapper
179                     .getReusableMondrianConnections());
180             wrapper.setReusableXmlaConnections(parentWrapper
181                     .getReusableXmlaConnections());
182             wrapper.setReusableSchemas(parentWrapper.getReusableSchemas());
183             wrapper.setReusableXmlaDefinitions(parentWrapper
184                     .getReusableXmlaDefinitions());
185         }
186         getAllFolders(wrapper);
187         wrapper.setParentFolder((String JavaDoc) context.getFlowScope().get(
188                 PARENT_FOLDER_ATTR)); // for olap conneciton main flow
189
context.getFlowScope().put(FORM_OBJECT_KEY, wrapper);
190         context.getFlowScope().put("constants", constants);
191         return success();
192     }
193
194     /**
195      * findAllSchemas finds all mondrian olap schema
196      *
197      * @param wrapper
198      */

199     private void findAllSchemas(OlapClientConnectionWrapper wrapper) {
200         FilterCriteria filterCriteria = FilterCriteria
201                 .createFilter(FileResource.class);
202         filterCriteria.addFilterElement(FilterCriteria
203                 .createPropertyEqualsFilter("fileType",
204                         ResourceDescriptor.TYPE_MONDRIAN_SCHEMA));
205         ResourceLookup[] resourceLookup = repository.findResource(null,
206                 filterCriteria);
207         List JavaDoc allSources = null;
208         if (resourceLookup != null && resourceLookup.length != 0) {
209             log("Found source lookups size=" + resourceLookup.length);
210             allSources = new ArrayList JavaDoc(resourceLookup.length);
211             for (int i = 0; i < resourceLookup.length; i++) {
212                 Resource resource = (Resource) resourceLookup[i];
213                 Object JavaDoc resourceObj = repository.getResource(null, resource
214                         .getURIString());
215                 if (!allSources.contains(((FileResource) resourceObj)
216                         .getURIString())) {
217                     allSources.add(((FileResource) resourceObj).getURIString());
218                 }
219                 log("added uri=" + resource.getURIString());
220             }
221             wrapper.setReusableSchemas(allSources);
222         }
223     }
224     
225     /**
226      * getAllConnections finds all olap client connections
227      *
228      * @param wrapper
229      */

230     private void getAllConnections (OlapClientConnectionWrapper wrapper) {
231         FilterCriteria filterCriteria =
232             FilterCriteria.createFilter(OlapClientConnection.class);
233         ResourceLookup[] resourceLookup = repository.findResource(null, filterCriteria);
234         List JavaDoc allMondrianConnections = null;
235         List JavaDoc allXmlaConnections = null;
236         if (resourceLookup != null && resourceLookup.length != 0) {
237             log("Found conneciton lookups size=" + resourceLookup.length);
238             allMondrianConnections = new ArrayList JavaDoc(resourceLookup.length);
239             allXmlaConnections = new ArrayList JavaDoc(resourceLookup.length);
240             for (int i = 0; i < resourceLookup.length; i++) {
241                 Resource resource = (Resource) resourceLookup[i];
242                 Object JavaDoc resourceObj =
243                     repository.getResource(null, resource.getURIString());
244                 if (resourceObj instanceof MondrianConnection) {
245                     if (!allMondrianConnections.contains(
246                             ((OlapClientConnection) resourceObj).getURIString())) {
247                         allMondrianConnections.add(
248                                 ((OlapClientConnection) resourceObj).getURIString());
249                     }
250                 }
251                 else if (resourceObj instanceof XMLAConnection) {
252                     if (!allXmlaConnections.contains(
253                             ((OlapClientConnection) resourceObj).getURIString())) {
254                         allXmlaConnections.add(
255                                 ((OlapClientConnection) resourceObj).getURIString());
256                     }
257                 }
258                 else {
259                     throw new JSException("Unknown connection type");
260                 }
261             }
262             wrapper.setReusableMondrianConnections(allMondrianConnections);
263             wrapper.setReusableXmlaConnections(allXmlaConnections);
264         }
265     }
266     
267     /**
268      * getAllXmlaConnections finds all xmla mondrian sources
269      *
270      * @param wrapper
271      */

272     private void getAllXmlaSources(OlapClientConnectionWrapper wrapper)
273     {
274         FilterCriteria filterCriteria =
275             FilterCriteria.createFilter(MondrianXMLADefinition.class);
276         ResourceLookup[] resourceLookup = repository.findResource(null, filterCriteria);
277         List JavaDoc allXmlaDefinitions = null;
278         if (resourceLookup != null && resourceLookup.length != 0) {
279             log("Found xmla definition lookups size=" + resourceLookup.length);
280             allXmlaDefinitions = new ArrayList JavaDoc(resourceLookup.length);
281             for (int i = 0; i < resourceLookup.length; i++) {
282                 Resource resource = (Resource) resourceLookup[i];
283                 Object JavaDoc resourceObj = repository.getResource(null, resource.getURIString());
284                 if (!allXmlaDefinitions.contains(
285                         ((MondrianXMLADefinition) resourceObj).getURIString())) {
286                     allXmlaDefinitions.add(
287                             ((MondrianXMLADefinition) resourceObj).getCatalog());
288                 }
289             }
290             wrapper.setReusableXmlaDefinitions(allXmlaDefinitions);
291         }
292     }
293
294     /**
295      * getAllFolders finds all folder resources
296      *
297      * @param wrapper
298      */

299     private void getAllFolders(OlapClientConnectionWrapper wrapper) {
300         List JavaDoc allFolders = repository.getAllFolders(null);
301         wrapper.setAllFolders(new ArrayList JavaDoc());
302         for (int i = 0; i < allFolders.size(); i++) {
303             String JavaDoc folderUri = ((Folder) allFolders.get(i)).getURIString();
304             wrapper.getAllFolders().add(folderUri);
305         }
306     }
307
308     /**
309      * handleTypeSelection receives input from mouse click and
310      * converts to the connection type
311      *
312      * @param context
313      * @return
314      * @throws Exception
315      */

316     public Event handleTypeSelection(RequestContext context) throws Exception JavaDoc {
317         OlapClientConnectionWrapper wrapper =
318             (OlapClientConnectionWrapper) getFormObject(context);
319         String JavaDoc strType = wrapper.getType();
320         log("Type=" + strType);
321
322         if (strType != null) {
323             if (strType.equals(JasperServerConstImpl
324                     .getOlapMondrianConnectionType())) {
325                 wrapper
326                         .setOlapConnectionType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA);
327             } else if (strType.equals(JasperServerConstImpl
328                     .getOlapXmlaConnectionType())) {
329                 wrapper
330                         .setOlapConnectionType(ResourceDescriptor.TYPE_XMLA_CONNTCTION);
331             } else {
332                 throw new JSException("Connection type not suported.");
333             }
334         } else {
335             throw new JSException("No connection is selected.");
336         }
337
338         return success();
339     }
340     
341     /**
342      * locateSchemaResource performs the action to load or create schema resource
343      *
344      * @param context
345      * @return
346      * @throws Exception
347      */

348     public Event locateSchemaResource(RequestContext context) throws Exception JavaDoc {
349         log("In locateSchemaResource");
350         OlapClientConnectionWrapper occWrapper =
351             (OlapClientConnectionWrapper) getFormObject(context);
352         if (occWrapper == null) {
353             throw new JSException("No olap client connection is found.");
354         } else {
355             log("Olap client connection name=" + occWrapper.getConnectionName());
356         }
357         FileResourceWrapper frW = new FileResourceWrapper();
358         FileResource fileR = (FileResource) repository.newResource(null,
359                 FileResource.class);
360         fileR.setParentFolder((String JavaDoc) context.getFlowScope().get(
361                 PARENT_FOLDER_ATTR));
362         frW.setMode(BaseDTO.MODE_SUB_FLOW_NEW);
363         frW.setFileResource(fileR);
364         // Set current form object to validate duplicate resource names
365
frW.setParentFlowObject(occWrapper);
366         // Set the FileResourceWrapper object into scope for the subflow
367
frW.setNewUri(occWrapper.getSchemaUri());
368         frW.setSource(occWrapper.getSource());
369         context.getFlowScope().put(PARENT_FOLDER_ATTR,
370                 (String JavaDoc) context.getFlowScope().get(PARENT_FOLDER_ATTR));
371         context.getFlowScope()
372                 .put(FileResourceAction.getFORM_OBJECT_KEY(), frW);
373         return success();
374     }
375
376     /**
377      * locateDataSource performs the action to load or create data source
378      *
379      * @param context
380      * @return
381      * @throws Exception
382      */

383     public Event locateDataSource(RequestContext context) throws Exception JavaDoc {
384         log("In locate data source");
385         OlapClientConnectionWrapper wrapper =
386             (OlapClientConnectionWrapper) getFormObject(context);
387         ResourceReference dsRef = new ResourceReference(wrapper
388                 .getDatasourceUri());
389         ReportDataSourceWrapper odWrapper = new ReportDataSourceWrapper();
390         odWrapper.setMode(BaseDTO.MODE_SUB_FLOW_NEW);
391         odWrapper.setParentFlowObject(wrapper);
392         odWrapper.setParentType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA);
393         if (dsRef == null) {
394             log("Found no previous OlapDataSource, creating new");
395             ReportDataSource ods = (ReportDataSource) repository.newResource(
396                     null, JdbcOlapDataSource.class);
397             ods.setParentFolder((String JavaDoc) context.getFlowScope().get(
398                     PARENT_FOLDER_ATTR));
399             dsRef = new ResourceReference(ods);
400             odWrapper.setSource(constants.getFieldChoiceLocal());
401             odWrapper.setType(constants.getJDBCDatasourceType());
402             odWrapper.setReportDataSource(ods);
403         } else {
404             // if the dataSource exists decide source and type and set in
405
// wrapper
406
if (!dsRef.isLocal()) { // [OK]
407
// DataSource object is a lookup
408
log("Found OlapDataSourceLookup");
409                 odWrapper.setSource(constants.getFieldChoiceRepo());
410             } else {
411                 odWrapper.setSource(constants.getFieldChoiceLocal());
412                 ReportDataSource ods = (ReportDataSource) dsRef
413                         .getLocalResource();
414                 if (JdbcOlapDataSource.class.isAssignableFrom(ods.getClass())) {
415                     log("Found JDBCOlapDataSource");
416                     odWrapper.setType(constants.getJDBCDatasourceType());
417                 } else {
418                     log("Found JndiJdbcOlapDataSourceLookup");
419                     if (JndiJdbcOlapDataSource.class.isAssignableFrom(ods
420                             .getClass()))
421                         odWrapper.setType(constants.getJNDIDatasourceType());
422                 }
423                 odWrapper.setReportDataSource(ods);
424             }
425             // set the current selection
426
odWrapper.setSelectedUri(dsRef.getReferenceURI());
427         }
428         // Set the object into scope with the name that the olapDataSourceFlow
429
// can pickup
430
context.getFlowScope().put(PARENT_FOLDER_ATTR,
431                 (String JavaDoc) context.getFlowScope().get(PARENT_FOLDER_ATTR));
432         context.getFlowScope().put(OlapDataSourceAction.getFORM_OBJECT_KEY(),
433                 odWrapper);
434         return success();
435     }
436     
437     /**
438      * saveSchemaResource performs the action to save schema resource
439      *
440      * @param context
441      * @return
442      * @throws Exception
443      */

444     public Event saveSchemaResource(RequestContext context) throws Exception JavaDoc {
445         log("In save schema resource");
446         OlapClientConnectionWrapper wrapper =
447             (OlapClientConnectionWrapper) getFormObject(context);
448         FileResourceWrapper frWrapper = (FileResourceWrapper) context
449                 .getFlowScope().get(SCHEMA_OBJECT_KEY);
450         if (frWrapper == null) {
451             throw new JSException(
452                     "Got null schema source wrapper back from subflow");
453         } else
454             log("type was " + frWrapper.getClass().getName());
455
456         FileResource fileResource = frWrapper.getFileResource();
457         if (fileResource == null) {
458             throw new JSException("Got null schema source back from subflow");
459         } else
460             log("type was " + frWrapper.getFileResource().getFileType());
461
462         wrapper.setOlapClientSchema(fileResource);
463
464         if (frWrapper.getSource().equals(constants.getFieldChoiceFile())) {
465             wrapper.setSchemaLoaded(true);
466         } else {
467             wrapper.setSchemaUri(frWrapper.getFileResource().getReferenceURI());
468         }
469         wrapper.setConnectionModified(true);
470         return success();
471     }
472     
473     /**
474      * saveDatasource performs the action to save data source
475      *
476      * @param context
477      * @return
478      * @throws Exception
479      */

480     public Event saveDatasource(RequestContext context) throws Exception JavaDoc {
481         // Save the returned datasource info
482
OlapClientConnectionWrapper wrapper =
483             (OlapClientConnectionWrapper) getFormObject(context);
484         ReportDataSourceWrapper resource = (ReportDataSourceWrapper) context
485                 .getFlowScope().get(DATASOURCE_OBJECT_KEY);
486         if (resource == null)
487             log("Got null datatsource back from subflow");
488         else
489             log("type was " + resource.getType());
490
491         if (resource.getSource().equals(constants.getFieldChoiceRepo())) {
492             if (wrapper.getOlapClientDatasource() == null) {
493                 // new olap unit using existing datasource
494
ReportDataSource datasource = (ReportDataSource) repository
495                         .getResource(null, resource.getSelectedUri());
496                 resource.setReportDataSource(datasource);
497             }
498             wrapper.setOlapClientDatasource(resource.getReportDataSource());
499             wrapper.setDatasourceUri(resource.getSelectedUri());
500         } else {
501             ReportDataSource datasource = resource.getReportDataSource();
502             wrapper.setOlapClientDatasource(datasource);
503             wrapper.setDatasourceAdded(true);
504         }
505         wrapper.setConnectionModified(true);
506         return success();
507     }
508
509     /**
510      * saveMondrianConnection performs the action to save mondrian connection resource
511      *
512      * @param context
513      * @return
514      * @throws Exception
515      */

516     public Event saveMondrianConnection(RequestContext context)
517             throws Exception JavaDoc {
518         log("In saveMondrianConnection");
519         OlapClientConnectionWrapper wrapper = (OlapClientConnectionWrapper) getFormObject(context);
520         OlapUnitWrapper parentWrapper = (OlapUnitWrapper) wrapper
521                 .getParentFlowObject();
522         MondrianConnection mondrianConnection = null;
523         if (wrapper.getSource().equals(constants.FIELD_CHOICE_CONT_REPO)) {
524             if (wrapper.getMode() == BaseDTO.MODE_SUB_FLOW_NEW) {
525                 // modify existing connection
526
if (wrapper.getConnectionUri() != null) {
527                     mondrianConnection = (MondrianConnection) repository
528                             .getResource(null, wrapper.getConnectionUri());
529                 } else {
530                     throw new JSException("Connection URI is not specified");
531                 }
532                 parentWrapper.setOlapClientConnection(mondrianConnection);
533                 parentWrapper.setConnectionUri(wrapper.getConnectionUri());
534                 wrapper.setSchemaUri(mondrianConnection.getSchema()
535                         .getReferenceURI());
536                 wrapper.setDatasourceUri(mondrianConnection.getDataSource()
537                         .getReferenceURI());
538                 wrapper.setConnectionModified(false);
539             } else {
540                 if (wrapper.getMode() == BaseDTO.MODE_STAND_ALONE_EDIT) {
541                     mondrianConnection = (MondrianConnection) wrapper
542                             .getOlapClientConnection();
543                     setMondrianConnectionDetails(wrapper, mondrianConnection);
544                     repository.saveResource(null, mondrianConnection);
545                 } else {
546                     mondrianConnection = (MondrianConnection) repository
547                             .getResource(null, wrapper.getConnectionUri());
548                     getMondrianConnectionDetails(wrapper, mondrianConnection);
549                     wrapper.setConnectionModified(true);
550
551                 }
552             }
553         } else {
554             // create new connection
555
try {
556                 mondrianConnection = (MondrianConnection) repository
557                         .newResource(null, MondrianConnection.class);
558             } catch (Exception JavaDoc e) {
559                 throw new JSException("Failed to create connection");
560             }
561             // save schema resource
562
if (wrapper.isSchemaLoaded()) {
563                 FileResource schema = wrapper.getOlapClientSchema();
564                 if (!wrapper.isSubflowMode()) {
565                     schema.setURIString(wrapper.getSchemaUri());
566                     olapConnection.saveResource(null, wrapper
567                             .getOlapClientSchema().getParentFolder(), schema); //
568
} else {
569                     parentWrapper.setOlapClientSchema(schema);
570                     parentWrapper.setSchemaLoaded(true);
571                 }
572             }
573             // save data source
574
if (wrapper.isDatasourceAdded()) {
575                 ReportDataSource datasource = wrapper.getOlapClientDatasource();
576                 if (!wrapper.isSubflowMode()) {
577                     datasource.setURIString(wrapper.getDatasourceUri());
578                     repository.saveResource(null, datasource);
579                 } else {
580                     parentWrapper.setOlapClientDatasource(datasource);
581                     parentWrapper.setDatasourceAdded(true);
582                 }
583             }
584             // persist connection, if new or changed
585
if (wrapper.isConnectionModified()) {
586                 setMondrianConnectionDetails(wrapper, mondrianConnection);
587             }
588             // save conneciton depending on main flow vs. subflow
589
if (!wrapper.isSubflowMode()) {
590                 // main flow
591
mondrianConnection.setParentFolder((String JavaDoc) context
592                         .getFlowScope().get("parentFolder"));
593                 try {
594                     repository.saveResource(null, mondrianConnection);
595                 } catch (Exception JavaDoc e) {
596                     throw new JSException("Failed to save connection changes.");
597                 }
598             } else {
599                 // subflow
600
mondrianConnection
601                         .setParentFolder(((OlapUnitWrapper) wrapper
602                                 .getParentFlowObject()).getOlapUnit()
603                                 .getParentFolder());
604                 parentWrapper.setOlapClientConnection(mondrianConnection);
605                 parentWrapper.setConnectionModified(true);
606             }
607         }
608         wrapper.setOlapConnectionType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA);
609         wrapper.setOlapClientConnection(mondrianConnection);
610         return success();
611     }
612
613     /**
614      * setMondrianConnectionDetails saves the changes to mondrian connection
615      *
616      * @param wrapper
617      * @param mondrianConnection
618      */

619     private void setMondrianConnectionDetails(
620             OlapClientConnectionWrapper wrapper, MondrianConnection mondrianConnection) {
621         mondrianConnection.setName(wrapper.getConnectionName());
622         mondrianConnection.setLabel(wrapper.getConnectionLabel());
623         mondrianConnection.setDescription(wrapper.getConnectionDescription());
624         mondrianConnection.setURIString(
625                 mondrianConnection.getParentFolder() + "/" + mondrianConnection.getName());
626         // schema
627
((MondrianConnection) mondrianConnection)
628                 .setSchemaReference(wrapper.getOlapClientSchema().getURIString());
629         if (wrapper.getMode() == BaseDTO.MODE_SUB_FLOW_EDIT) {
630             try {
631                 repository.getResource(
632                         null, wrapper.getOlapClientSchema().getReferenceURI());
633             }
634             catch (Exception JavaDoc e) {
635                 // TODO check exception type
636
repository.saveResource(null, wrapper.getOlapClientSchema());
637             }
638         }
639         // datasource
640
((MondrianConnection) mondrianConnection)
641                 .setDataSourceReference(wrapper.getOlapClientDatasource().getURIString());
642         if (wrapper.getMode() == BaseDTO.MODE_SUB_FLOW_EDIT) {
643             try {
644                 repository.getResource(
645                         null, wrapper.getOlapClientDatasource().getURIString());
646             }
647             catch (Exception JavaDoc e) {
648                 // TODO check exception type
649
repository.saveResource(null, wrapper.getOlapClientDatasource());
650             }
651         }
652     }
653     
654     /**
655      * getMondrianConnectionDetails buffers the changes to mondrian connection
656      *
657      * @param wrapper
658      * @param mondrianConnection
659      */

660     private void getMondrianConnectionDetails(
661             OlapClientConnectionWrapper wrapper, MondrianConnection mondrianConnection) {
662         wrapper.setConnectionName(mondrianConnection.getName());
663         wrapper.setConnectionLabel(mondrianConnection.getLabel());
664         wrapper.setConnectionDescription(mondrianConnection.getDescription());
665         // schema
666
wrapper.setSchemaUri(
667                 ((MondrianConnection) mondrianConnection).getSchema().getReferenceURI());
668         // data source
669
wrapper.setDatasourceUri(
670                 ((MondrianConnection) mondrianConnection).getDataSource().getReferenceURI());
671     }
672     
673     /**
674      * saveXmlaConnection stores XML/A connection information and
675      * places its resource reference in olap unit.
676      *
677      * @param context
678      * @return
679      * @throws Exception
680      */

681     public Event saveXmlaConnection(RequestContext context) throws Exception JavaDoc {
682         log("In saveXmlaConnection");
683         OlapClientConnectionWrapper wrapper =
684             (OlapClientConnectionWrapper) getFormObject(context);
685         OlapUnitWrapper parentWrapper = (OlapUnitWrapper) wrapper
686                 .getParentFlowObject();
687         XMLAConnection xmlaConnection = (XMLAConnection) wrapper
688                 .getOlapClientConnection();
689         if (wrapper.getSource().equals(constants.FIELD_CHOICE_CONT_REPO)) {
690             if (wrapper.getMode() == BaseDTO.MODE_SUB_FLOW_NEW) {
691                 if (wrapper.getConnectionUri() != null) {
692                     xmlaConnection = (XMLAConnection) repository.getResource(
693                             null, wrapper.getConnectionUri());
694                 } else {
695                     throw new JSException("Connection URI is not specified");
696                 }
697                 parentWrapper.setOlapClientConnection(xmlaConnection);
698                 parentWrapper.setConnectionUri(wrapper.getConnectionUri());
699                 wrapper.setConnectionModified(false);
700             } else {
701                 if (wrapper.getMode() == BaseDTO.MODE_STAND_ALONE_EDIT) {
702                     setXmlaConnectionDetails(wrapper, xmlaConnection);
703                     repository.saveResource(null, xmlaConnection);
704                 } else {
705                     xmlaConnection = (XMLAConnection) repository.getResource(
706                             null, wrapper.getConnectionUri());
707                     getXmlaConnectionDetails(wrapper, xmlaConnection);
708                     wrapper.setConnectionModified(true);
709                 }
710             }
711         } else {
712             // new xmla connection
713
xmlaConnection = (XMLAConnection) repository.newResource(null,
714                     XMLAConnection.class);
715             xmlaConnection.setParentFolder(wrapper.getParentFolder());
716             setXmlaConnectionDetails(wrapper, xmlaConnection);
717             if (!wrapper.isSubflowMode()) {
718                 try {
719                     repository.saveResource(null, xmlaConnection); // wrapper.setConnectionChanged(true);
720
} catch (Exception JavaDoc e) {
721                     throw new JSException("Failed to save connection");
722                 }
723             } else {
724                 xmlaConnection
725                         .setParentFolder(((OlapUnitWrapper) wrapper
726                                 .getParentFlowObject()).getOlapUnit()
727                                 .getParentFolder());
728                 parentWrapper.setOlapClientConnection(xmlaConnection);
729                 parentWrapper.setConnectionModified(true);
730             }
731         }
732         wrapper.setOlapConnectionType(ResourceDescriptor.TYPE_XMLA_CONNTCTION);
733         wrapper.setOlapClientConnection(xmlaConnection);
734         return success();
735     }
736     
737     /**
738      * setXmlaConnectionDetails saves changes to xmla connection
739      *
740      * @param wrapper
741      * @param xmlaConnection
742      */

743     private void setXmlaConnectionDetails(
744             OlapClientConnectionWrapper wrapper, XMLAConnection xmlaConnection)
745     {
746         xmlaConnection.setName(wrapper.getConnectionName());
747         xmlaConnection.setLabel(wrapper.getConnectionLabel());
748         xmlaConnection.setDescription(wrapper.getConnectionDescription());
749         
750         xmlaConnection.setCatalog(wrapper.getXmlaCatalog());
751         xmlaConnection.setDataSource(wrapper.getXmlaDatasource());
752         xmlaConnection.setURI(wrapper.getXmlaConnectionUri());
753         
754         xmlaConnection.setUsername(wrapper.getUsername());
755         xmlaConnection.setPassword(wrapper.getPassword());
756     }
757
758     /**
759      * getXmlaConnectionDetails buffers changes to xmla connection
760      * @param wrapper
761      * @param xmlaConnection
762      */

763     private void getXmlaConnectionDetails(
764             OlapClientConnectionWrapper wrapper, XMLAConnection xmlaConnection)
765     {
766         wrapper.setConnectionName(xmlaConnection.getName());
767         wrapper.setConnectionLabel(xmlaConnection.getLabel());
768         wrapper.setConnectionDescription(xmlaConnection.getDescription());
769         
770         wrapper.setXmlaCatalog(xmlaConnection.getCatalog());
771         wrapper.setXmlaDatasource(xmlaConnection.getDataSource());
772         wrapper.setXmlaConnectionUri(xmlaConnection.getURI());
773         
774         wrapper.setUsername(xmlaConnection.getUsername());
775         wrapper.setPassword(xmlaConnection.getPassword());
776     }
777     
778     /**
779      * loadFormObject set the form object
780      * @param context
781      * @return Object
782      */

783     public Object JavaDoc loadFormObject(RequestContext context) {
784         OlapClientConnectionWrapper formObject = null;
785         String JavaDoc resourceUri = context.getRequestParameters().get(OU_URI_PARAM);
786         if (resourceUri != null && resourceUri.trim().length() != 0) {
787             formObject = new OlapClientConnectionWrapper();
788             
789             OlapClientConnection occ =
790                 (OlapClientConnection) repository.getResource(null, resourceUri);
791             
792             // connection info
793
formObject.setOlapClientConnection(occ);
794             formObject.setConnectionName(occ.getName());
795             formObject.setConnectionLabel(occ.getLabel());
796             formObject.setConnectionDescription(occ.getDescription());
797             
798             // connection-specifics
799
if (occ instanceof MondrianConnection) {
800                 MondrianConnection mc = (MondrianConnection) occ;
801                 formObject.setOlapConnectionType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA);
802                 String JavaDoc uri = mc.getSchema().getReferenceURI();
803                 formObject.setOriginalSchemaUri(uri);
804                 formObject.setSchemaUri(uri);
805                 formObject.setDatasourceUri(mc.getDataSource().getReferenceURI());
806                 formObject.setSchemaLocated(true);
807             }
808             else if (occ instanceof XMLAConnection) {
809                 XMLAConnection xmlac = (XMLAConnection) occ;
810                 formObject.setOlapConnectionType(ResourceDescriptor.TYPE_XMLA_CONNTCTION);
811                 formObject.setXmlaCatalog(xmlac.getCatalog());
812                 formObject.setXmlaDatasource(xmlac.getDataSource());
813                 formObject.setXmlaConnectionUri(xmlac.getURI());
814                 formObject.setUsername(xmlac.getUsername());
815                 formObject.setPassword(xmlac.getPassword());
816             }
817             else {
818                 log("Unknown connection type");
819             }
820             
821             formObject.setMode(BaseDTO.MODE_STAND_ALONE_EDIT);
822             formObject.setDatasourceIdentified(true);
823             formObject.setNamed(true);
824         }
825         
826         if (formObject == null) {
827             log("OlapUnitAction: Stand alone new mode");
828             formObject = new OlapClientConnectionWrapper();
829             formObject.setMode(BaseDTO.MODE_STAND_ALONE_NEW);
830             String JavaDoc parentFolder = (String JavaDoc) context.getFlowScope().get(
831                     PARENT_FOLDER_ATTR);
832             formObject.setSource(constants.FIELD_CHOICE_FILE_SYSTEM);
833         }
834         return formObject;
835     }
836
837     /**
838      * validateOlapConnection performs validate for olap view
839      *
840      * @param context
841      * @return
842      * @throws Exception
843      */

844     public Event validateOlapConnection(RequestContext context) throws Exception JavaDoc {
845         log("In Validate OLAP Connection");
846         // TODO
847
OlapClientConnectionWrapper wrapper =
848             (OlapClientConnectionWrapper) getFormObject(context);
849         OlapClientConnection connection = wrapper.getOlapClientConnection();
850         if (wrapper.getOlapConnectionType().equals(
851                 ResourceDescriptor.TYPE_MONDRIAN_SCHEMA)) {
852             // validate mondrian connection
853
}
854         else if (wrapper.getOlapConnectionType().equals(
855                 ResourceDescriptor.TYPE_XMLA_CONNTCTION)) {
856             // validate xmla connection
857
}
858         else {
859             throw new JSException("Unknown connection type");
860         }
861         // TODO activate
862
// ValidationResult result = olapConnection.validate(null, conection);
863
// wrapper.setResult(result.getValidationState().equals(
864
// ValidationResult.STATE_VALID));
865
return success();
866     }
867     
868     /**
869      * getRepository returns repository service property
870      *
871      * @return
872      */

873     public RepositoryService getRepository() {
874         return repository;
875     }
876     
877     /**
878      * setRepository sets repository service property
879      *
880      * @param repository
881      */

882     public void setRepository(RepositoryService repository) {
883         this.repository = repository;
884     }
885     
886     /**
887      * method to get the olap connection service object
888      *
889      * @return
890      */

891     public OlapConnectionService getOlapConnection() {
892         return this.olapConnection;
893     }
894     
895     /**
896      * method to set the olap connection service object
897      *
898      * @param olapConnection
899      */

900     public void setOlapConnection(OlapConnectionService olapConnection) {
901         this.olapConnection = olapConnection;
902     }
903
904     /**
905      * method to get the engine service object
906      *
907      * @return
908      */

909     public EngineService getEngine() {
910         return engine;
911     }
912
913     /**
914      * method to set the engine service object
915      *
916      * @param engine
917      */

918     public void setEngine(EngineService engine) {
919         this.engine = engine;
920     }
921
922     /**
923      * Registers a byte array editor to allow spring handle File uploads as byte
924      * arrays
925      *
926      * @param context
927      * @param binder
928      */

929     protected void initBinder(RequestContext context, DataBinder binder) {
930         binder.registerCustomEditor(byte[].class,
931                 new ByteArrayMultipartFileEditor());
932     }
933
934     /**
935      * log logs debug message
936      *
937      * @param text
938      */

939     protected void log(String JavaDoc text) {
940         log.debug(text);
941     }
942 }
943
Popular Tags