1 package com.jaspersoft.jasperserver.war.action; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 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.common.domain.ValidationResult; 17 import com.jaspersoft.jasperserver.api.common.domain.impl.ExecutionContextImpl; 18 import com.jaspersoft.jasperserver.api.engine.common.service.EngineService; 19 import com.jaspersoft.jasperserver.api.metadata.common.domain.FileResource; 20 import com.jaspersoft.jasperserver.api.metadata.common.domain.Resource; 21 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceLookup; 22 import com.jaspersoft.jasperserver.api.metadata.common.domain.ResourceReference; 23 import com.jaspersoft.jasperserver.api.metadata.common.service.RepositoryService; 24 import com.jaspersoft.jasperserver.api.metadata.jasperreports.domain.ReportDataSource; 25 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianConnection; 26 import com.jaspersoft.jasperserver.api.metadata.olap.domain.MondrianXMLADefinition; 27 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapClientConnection; 28 import com.jaspersoft.jasperserver.api.metadata.olap.domain.OlapUnit; 29 import com.jaspersoft.jasperserver.api.metadata.olap.domain.XMLAConnection; 30 import com.jaspersoft.jasperserver.api.metadata.olap.domain.client.MondrianConnectionImpl; 31 import com.jaspersoft.jasperserver.api.metadata.olap.service.OlapConnectionService; 32 import com.jaspersoft.jasperserver.api.metadata.view.domain.FilterCriteria; 33 import com.jaspersoft.jasperserver.api.metadata.xml.domain.impl.ResourceDescriptor; 34 import com.jaspersoft.jasperserver.war.common.JasperServerConstImpl; 35 import com.jaspersoft.jasperserver.war.dto.BaseDTO; 36 import com.jaspersoft.jasperserver.war.dto.OlapClientConnectionWrapper; 37 import com.jaspersoft.jasperserver.war.dto.OlapUnitWrapper; 38 import com.jaspersoft.jasperserver.war.validation.OlapUnitValidator; 39 40 47 public class OlapUnitAction extends FormAction { 48 private static final String OU_URI_PARAM = "resource"; 49 private static final String FORM_OBJECT_KEY = "wrapper"; 50 private static final String OLAP_UNIT_SUFFIX = "_unit"; 51 private static final String OLAP_UNIT_KEY = "unit"; 52 private static final String CONTROL_OBJECT_KEY = "control"; 53 private static final String RESOURCE_OBJECT_KEY = "resource"; 54 private static final String DATASOURCE_OBJECT_KEY = "dataResource"; 55 private static final String CONNECTION_OBJECT_KEY = "connectionWrapper"; 56 private static final String PARENT_FOLDER_ATTR = "parentFolder"; 57 private static final String PARAM_FROM_PAGE = "frompage"; 58 private static final String PARAM_RESOURCE_NAME = "resourceName"; 59 private static final String LOCAL_SCHEMA_SUFFIX = "_schema"; 60 private static final String UNPARSABLE_SCHEMA_ATTR = "schemaUnparsable"; 61 private static final String INVALID_XMLA_CONNECTION_ATTR = "xmlaConnectionInvalid"; 62 private static final String UNPARSABLE_XMLA_ATTR = "xmlaUnparsable"; 63 private static final String 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 73 public OlapUnitAction() { 74 setFormObjectClass(OlapUnitWrapper.class); 75 setFormObjectName(FORM_OBJECT_KEY); 76 setFormObjectScope(ScopeType.FLOW); 77 setValidator(new OlapUnitValidator()); 78 } 79 80 87 public Event initAction(RequestContext context) throws Exception { 88 ((OlapUnitValidator)getValidator()).setConnectionService(olapConnection); 89 OlapUnitWrapper wrapper = (OlapUnitWrapper) getFormObject(context); 90 if (wrapper.isNewMode()) { 91 String folderURI = wrapper.getOlapUnit().getParentFolder(); 95 if (folderURI == null) { 96 folderURI = "/"; 97 } 98 FilterCriteria resourcesInFolder = FilterCriteria.createFilter(); 99 resourcesInFolder.addFilterElement(FilterCriteria 100 .createParentFolderFilter(folderURI)); 101 log("Searching for resources in the chosen folder:" + folderURI); 102 ResourceLookup[] existingResources = repository.findResource(null, 103 resourcesInFolder); 104 105 if (existingResources != null && existingResources.length != 0) { 106 log("res lookup size=" + existingResources.length); 107 List allResources = new ArrayList (); 108 for (int i = 0; i < existingResources.length; i++) { 109 ResourceLookup rLookup = existingResources[i]; 110 allResources.add(rLookup.getName()); 111 log("adding resource: " + rLookup.getName() 112 + " to the list"); 113 } 114 wrapper.setExistingResources(allResources); 115 } 116 } else { 117 OlapUnit ou = wrapper.getOlapUnit(); 121 wrapper.setOlapUnitName(ou.getName()); 122 wrapper.setOlapUnitLabel(ou.getLabel()); 123 wrapper.setOlapUnitDescription(ou.getDescription()); 124 wrapper.setOlapClientConnection((OlapClientConnection) repository 125 .getResource(null, ou.getOlapClientConnection() 126 .getReferenceURI())); 127 wrapper.setOlapUnitMdxQuery(ou.getMdxQuery()); 128 if (wrapper.getOlapClientConnection() instanceof MondrianConnection) { 130 wrapper.setType(constants.getOlapMondrianConnectionType()); 131 } else if (wrapper.getOlapClientConnection() instanceof XMLAConnection) { 132 wrapper.setType(constants.getOlapXmlaConnectionType()); 133 } else { 134 log("Unknown connection type."); 135 } 136 } 137 getAllConnections(wrapper); 139 findAllSchemas(wrapper); 140 getAllXmlaSources(wrapper); 141 wrapper.setParentFolder((String ) context.getFlowScope().get( 142 PARENT_FOLDER_ATTR)); 143 context.getFlowScope().put(FORM_OBJECT_KEY, wrapper); 144 context.getFlowScope().put("constants", constants); 145 return success(); 146 } 147 148 153 private void findAllSchemas(OlapUnitWrapper wrapper) 154 { 155 FilterCriteria filterCriteria = FilterCriteria 156 .createFilter(FileResource.class); 157 filterCriteria.addFilterElement(FilterCriteria.createPropertyEqualsFilter( 158 "fileType", ResourceDescriptor.TYPE_MONDRIAN_SCHEMA)); 159 ResourceLookup[] resourceLookup = repository.findResource(null, 160 filterCriteria); 161 List allSources = null; 162 if (resourceLookup != null && resourceLookup.length != 0) { 163 log("Found source lookups size=" + resourceLookup.length); 164 allSources = new ArrayList (resourceLookup.length); 165 for (int i = 0; i < resourceLookup.length; i++) { 166 Resource resource = (Resource) resourceLookup[i]; 167 Object resourceObj = repository.getResource(null, resource.getURIString()); 168 if (!allSources.contains(((FileResource) resourceObj).getURIString())) { 169 allSources.add(((FileResource) resourceObj).getURIString()); 170 } 171 log("added uri=" + resource.getURIString()); 172 } 173 wrapper.setReusableSchemas(allSources); 174 } 175 } 176 177 182 private void getAllConnections(OlapUnitWrapper wrapper) { 183 FilterCriteria filterCriteria = FilterCriteria 187 .createFilter(OlapClientConnection.class); 188 ResourceLookup[] resourceLookup = repository.findResource(null, 189 filterCriteria); 190 List allMondrianConnections = null; 191 List allXmlaConnections = null; 192 if (resourceLookup != null && resourceLookup.length != 0) { 193 log("Found conneciton lookups size=" + resourceLookup.length); 194 allMondrianConnections = new ArrayList (resourceLookup.length); 195 allXmlaConnections = new ArrayList (resourceLookup.length); 196 for (int i = 0; i < resourceLookup.length; i++) { 197 Resource resource = (Resource) resourceLookup[i]; 198 Object resourceObj = repository.getResource(null, resource 199 .getURIString()); 200 if (resourceObj instanceof MondrianConnection) { 201 if (!allMondrianConnections 202 .contains(((OlapClientConnection) resourceObj) 203 .getURIString())) { 204 allMondrianConnections 205 .add(((OlapClientConnection) resourceObj) 206 .getURIString()); 207 } 208 } else if (resourceObj instanceof XMLAConnection) { 209 if (!allXmlaConnections 210 .contains(((OlapClientConnection) resourceObj) 211 .getURIString())) { 212 allXmlaConnections 213 .add(((OlapClientConnection) resourceObj) 214 .getURIString()); 215 } 216 } else { 217 throw new JSException("Unknown connection type."); 218 } 219 } 220 wrapper.setReusableMondrianConnections(allMondrianConnections); 221 wrapper.setReusableXmlaConnections(allXmlaConnections); 222 } 223 } 224 225 230 private void getAllXmlaSources(OlapUnitWrapper wrapper) { 231 FilterCriteria filterCriteria = 232 FilterCriteria.createFilter(MondrianXMLADefinition.class); 233 ResourceLookup[] resourceLookup = repository.findResource(null, filterCriteria); 234 List allXmlaDefinitions = null; 235 if (resourceLookup != null && resourceLookup.length != 0) { 236 log("Found xmla definition lookups size=" + resourceLookup.length); 237 allXmlaDefinitions = new ArrayList (resourceLookup.length); 238 for (int i = 0; i < resourceLookup.length; i++) { 239 Resource resource = (Resource) resourceLookup[i]; 240 Object resourceObj = repository.getResource(null, resource.getURIString()); 241 if (!allXmlaDefinitions.contains(((MondrianXMLADefinition) resourceObj).getURIString())) { 242 allXmlaDefinitions.add(((MondrianXMLADefinition) resourceObj).getCatalog()); 243 } 244 } 245 wrapper.setReusableXmlaDefinitions(allXmlaDefinitions); 246 } 247 } 248 249 257 public Event handleTypeSelection(RequestContext context) throws Exception { 258 OlapUnitWrapper wrapper = (OlapUnitWrapper) getFormObject(context); 259 String strType = wrapper.getType(); 260 log("Type" + strType); 261 262 if (strType != null) { 263 if (strType.equals(JasperServerConstImpl 264 .getOlapMondrianConnectionType())) { 265 wrapper.setOlapConnectionType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA); 266 } else if (strType.equals(JasperServerConstImpl 267 .getOlapXmlaConnectionType())) { 268 wrapper.setOlapConnectionType(ResourceDescriptor.TYPE_XMLA_CONNTCTION); 269 } else { 270 throw new JSException("Connection type not suported."); 271 } 272 } else { 273 throw new JSException("No connection is selected."); 274 } 275 276 return success(); 277 } 278 279 286 public Event locateOlapConnection(RequestContext context) throws Exception { 288 log("In locate olap connection"); 295 OlapUnitWrapper wrapper = (OlapUnitWrapper) getFormObject(context); 296 OlapClientConnection connection = wrapper.getOlapClientConnection(); 297 OlapClientConnectionWrapper connectionWrapper = new OlapClientConnectionWrapper(); 298 if (connection == null) { 299 log("Found no previous OlapClientConnection, creating new"); 301 connectionWrapper.setMode(BaseDTO.MODE_SUB_FLOW_NEW); 302 connectionWrapper.setParentFolder(wrapper.getParentFolder()); 303 } else { 304 log("Found previous OlapClientConnection"); 306 connectionWrapper.setOlapConnectionType(wrapper 307 .getOlapConnectionType()); 308 connectionWrapper.setReusableMondrianConnections(wrapper 309 .getReusableMondrianConnections()); 310 connectionWrapper.setReusableSchemas(wrapper.getReusableSchemas()); 311 connectionWrapper.setReusableXmlaConnections(wrapper 312 .getReusableXmlaConnections()); 313 connectionWrapper.setSource(wrapper.getSource()); 314 connectionWrapper.setType(wrapper.getType()); 315 connectionWrapper.setConnectionUri(wrapper.getOlapUnit() 316 .getOlapClientConnection().getReferenceURI()); connectionWrapper.setParentFolder(connection.getParentFolder()); 318 connectionWrapper.setMode(BaseDTO.MODE_SUB_FLOW_EDIT); 319 } 320 connectionWrapper.setParentFlowObject(wrapper); 321 context.getFlowScope().put(CONNECTION_OBJECT_KEY, connectionWrapper); 322 context.getFlowScope().put("constants", constants); 323 return success(); 324 } 325 326 333 public Event saveOlapClientConnection(RequestContext context) 334 throws Exception { 335 OlapUnitWrapper wrapper = (OlapUnitWrapper) getFormObject(context); 337 338 OlapClientConnectionWrapper connectionWrapper = (OlapClientConnectionWrapper) context 339 .getFlowScope().get(CONNECTION_OBJECT_KEY); 340 341 OlapClientConnection connection = connectionWrapper 342 .getOlapClientConnection(); 343 344 if (connection == null) { 345 throw new JSException("Got null connection back from subflow"); 346 } else 347 log("type was " + connectionWrapper.getOlapConnectionType()); 348 return success(); 349 } 350 351 358 public Event saveOlapUnit(RequestContext context) throws Exception { 359 log("In saveOlapUnit"); 362 OlapUnitWrapper wrapper = (OlapUnitWrapper) getFormObject(context); 363 OlapUnit ou = wrapper.getOlapUnit(); 364 OlapClientConnection connection = wrapper.getOlapClientConnection(); 365 OlapClientConnectionWrapper connectionWrapper = 366 (OlapClientConnectionWrapper) context.getFlowScope().get(CONNECTION_OBJECT_KEY); 367 wrapper.setConnectionModified(connectionWrapper.isConnectionModified()); 368 if (wrapper.isConnectionModified()) { 369 if (connection instanceof MondrianConnection) { 370 FileResource schema = wrapper.getOlapClientSchema(); 372 if (wrapper.isSchemaLoaded()) { 373 schema = wrapper.getOlapClientSchema(); 374 try { 375 repository.saveResource(null, schema); 376 } catch (Exception e) { 377 throw new JSException(e.getMessage()); 378 } 379 schema.setReferenceURI(schema.getURIString()); 380 ((MondrianConnection) connection).setSchemaReference( 381 schema.getReferenceURI()); 382 } else { 383 ((MondrianConnection) connection).setSchemaReference( 384 connectionWrapper.getSchemaUri()); 385 } 386 ReportDataSource datasource = wrapper.getOlapClientDatasource(); 388 if (wrapper.isDatasourceAdded()) { 389 try { 390 repository.saveResource(null, datasource); 391 } catch (Exception e) { 392 throw new JSException(e.getMessage()); 393 } 394 395 ((MondrianConnection) connection).setDataSourceReference( 396 datasource.getURIString()); 397 } else { 398 ((MondrianConnection) connection).setDataSourceReference( 399 connectionWrapper.getDatasourceUri()); 400 } 401 } else if (connection instanceof XMLAConnection) { 402 connection.setName(connectionWrapper.getConnectionName()); 403 connection.setLabel(connectionWrapper.getConnectionLabel()); 404 connection.setDescription(connectionWrapper 405 .getConnectionDescription()); 406 407 ((XMLAConnection) connection).setCatalog( 408 connectionWrapper.getXmlaCatalog()); 409 ((XMLAConnection) connection).setURI( 410 connectionWrapper.getXmlaConnectionUri()); 411 ((XMLAConnection) connection).setDataSource( 412 connectionWrapper.getXmlaDatasource()); 413 ((XMLAConnection) connection).setUsername( 414 connectionWrapper.getUsername()); 415 ((XMLAConnection) connection).setPassword( 416 connectionWrapper.getPassword()); 417 } else { 418 throw new JSException("Unknown connection type"); 419 } 420 421 if (connectionWrapper.getMode() == BaseDTO.MODE_SUB_FLOW_EDIT) { 422 connection.setParentFolder(wrapper.getParentFolder()); 423 } else { 424 connection.setParentFolder(connectionWrapper.getParentFolder()); 425 try { 426 repository.saveResource(null, connection); 427 } catch (Exception e) { 428 throw new JSException(e.getMessage()); 429 } 430 } 431 } 432 ou.setOlapClientConnection(connection); 436 ou.setOlapClientConnectionReference(connectionWrapper.getConnectionUri()); 437 repository.saveResource(null, ou); 438 return success(); 439 } 440 441 449 public Event saveXmlaConnection(RequestContext context) throws Exception { 450 OlapUnitWrapper wrapper = (OlapUnitWrapper) getFormObject(context); 451 OlapUnit ou = wrapper.getOlapUnit(); 452 453 XMLAConnection xmlaConnection = 454 (XMLAConnection) repository.newResource(null, XMLAConnection.class); 455 456 xmlaConnection.setName(wrapper.getOlapUnitName()); 457 xmlaConnection.setLabel(wrapper.getOlapUnitLabel()); 458 xmlaConnection.setDescription(wrapper.getOlapUnitDescription()); 459 460 xmlaConnection.setCatalog(wrapper.getXmlaCatalog()); 461 xmlaConnection.setDataSource(wrapper.getXmlaDatasource()); 462 xmlaConnection.setURI(wrapper.getXmlaConnectionUri()); 463 464 ou.setOlapClientConnection(xmlaConnection); 465 ou.setOlapClientConnectionReference(xmlaConnection.getURIString()); 466 wrapper.setOlapConnectionType(ResourceDescriptor.TYPE_XMLA_CONNTCTION); 467 wrapper.setOlapClientConnection(xmlaConnection); 468 469 wrapper.setConnectionModified(true); 470 return success(); 471 } 472 473 476 public Object loadFormObject(RequestContext context) { 477 OlapUnitWrapper formObject = null; 478 String resourceUri = context.getRequestParameters().get(OU_URI_PARAM); 479 if (resourceUri != null && resourceUri.trim().length() != 0) { 480 OlapUnit ou = (OlapUnit) repository.getResource(null, 481 resourceUri); 482 if (ou == null) 483 throw new JSException("Could not find resource with URI " 484 + resourceUri); 485 log("Found resource with uri=" + resourceUri); 486 formObject = new OlapUnitWrapper(); 487 formObject.setOlapUnit(ou); 488 ResourceReference olapClientConnectionRef = ou.getOlapClientConnection(); 489 OlapClientConnection occ = 490 (OlapClientConnection) repository.getResource( 491 null, olapClientConnectionRef.getReferenceURI()); 492 formObject.setOlapClientConnection(occ); 493 if (occ instanceof MondrianConnection) { 494 formObject.getOlapUnit().setDataSource(((MondrianConnection) occ).getDataSource()); formObject.setOlapConnectionType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA); 496 } 497 else if (occ instanceof XMLAConnection) { 498 formObject.setOlapConnectionType(ResourceDescriptor.TYPE_XMLA_CONNTCTION); 499 } 500 else { 501 throw new JSException("Unknown connection type"); 502 } 503 504 String uri = null; 505 if (!olapClientConnectionRef.isLocal()) { 506 formObject.setSource(constants.FIELD_CHOICE_CONT_REPO); 507 if (formObject.getOlapConnectionType().equals(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA)) { 508 uri = ((MondrianConnectionImpl) occ).getSchema().getReferenceURI(); 509 } 510 formObject.setSchemaUri(uri); 511 } else { 512 formObject.setSource(constants.FIELD_CHOICE_FILE_SYSTEM); 513 uri = olapClientConnectionRef.getLocalResource().getURIString(); 514 } 515 formObject.setOldSchemaUri(uri); 516 formObject.setOriginalSchemaUri(uri); 517 formObject.setMode(BaseDTO.MODE_STAND_ALONE_EDIT); 518 formObject.setDatasourceIdentified(true); 519 formObject.setNamed(true); 520 formObject.setSchemaLocated(true); 521 } 522 523 if (formObject == null) { 524 log("OlapUnitAction: Stand alone new mode"); 525 formObject = new OlapUnitWrapper(); 526 OlapUnit ou = (OlapUnit) repository.newResource(null, 527 OlapUnit.class); 528 FileResource schema = (FileResource) repository.newResource(null, 529 FileResource.class); 530 schema.setFileType(ResourceDescriptor.TYPE_MONDRIAN_SCHEMA); 531 ou.setOlapClientConnectionReference((String ) schema.getReferenceURI()); 532 formObject.setMode(BaseDTO.MODE_STAND_ALONE_NEW); 533 String parentFolder = (String ) context.getFlowScope().get( 534 PARENT_FOLDER_ATTR); 535 ou.setParentFolder(parentFolder); 536 formObject.setOlapUnit(ou); 537 formObject.setSource(constants.FIELD_CHOICE_FILE_SYSTEM); 538 } 539 return formObject; 540 } 541 542 549 public Event validateMdxQuery(RequestContext context) throws Exception { 550 System.out.println("I think this is dead code, but not sure"); 552 return success(); 553 } 554 555 public Event validateOlapUnit(RequestContext context) throws Exception { 556 log("In Validate OLAP Unit"); 558 OlapUnitWrapper wrapper = (OlapUnitWrapper) getFormObject(context); 559 OlapUnit ou = wrapper.getOlapUnit(); 560 ou.setName(wrapper.getOlapUnitName()); 561 ou.setLabel(wrapper.getOlapUnitLabel()); 562 ou.setDescription(wrapper.getOlapUnitDescription()); 563 ou.setOlapClientConnectionReference(wrapper.getConnectionUri()); ou.setOlapClientConnection(wrapper.getOlapClientConnection()); 568 ou.setMdxQuery(wrapper.getOlapUnitMdxQuery()); 569 ExecutionContextImpl executionContext = new ExecutionContextImpl(); 570 ValidationResult result = null; 571 try { 572 result = olapConnection.validate(executionContext, ou); 573 wrapper.setResult(result.getValidationState(). 574 equals(ValidationResult.STATE_VALID)); 575 if (result.getValidationState().equals(ValidationResult.STATE_ERROR)) { 576 return error(); 577 } 578 } 579 catch (Exception e) { 580 wrapper.setResult(result.getValidationState().equals( 582 ValidationResult.STATE_ERROR)); 583 log.error(e.getStackTrace()); 584 } 585 wrapper.setOlapUnit(ou); 586 return success(); 587 } 588 589 594 public RepositoryService getRepository() { 595 return repository; 596 } 597 598 603 public void setRepository(RepositoryService repository) { 604 this.repository = repository; 605 } 606 607 612 public OlapConnectionService getOlapConnection() { 613 return this.olapConnection; 614 } 615 616 621 public void setOlapConnection(OlapConnectionService olapConnection) { 622 this.olapConnection = olapConnection; 623 } 624 625 630 public EngineService getEngine() { 631 return engine; 632 } 633 634 639 public void setEngine(EngineService engine) { 640 this.engine = engine; 641 } 642 643 650 protected void initBinder(RequestContext context, DataBinder binder) { 651 binder.registerCustomEditor(byte[].class, 652 new ByteArrayMultipartFileEditor()); 653 } 654 655 660 protected void log(String text) { 661 log.debug(text); 662 } 663 } 664 | Popular Tags |