All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
net.sf.jasperreports.data.xmla.XmlaDataAdapterService Maven / Gradle / Ivy
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2013 Jaspersoft Corporation. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JasperReports is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JasperReports. If not, see .
*/
package net.sf.jasperreports.data.xmla;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.olap4j.OlapConnection;
import org.olap4j.OlapDatabaseMetaData;
import net.sf.jasperreports.data.AbstractDataAdapterService;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.olap.xmla.JRXmlaQueryExecuterFactory;
import net.sf.jasperreports.olap.xmla.Olap4jXmlaQueryExecuter;
import net.sf.jasperreports.util.SecretsUtil;
/**
* @author Veaceslov Chicu ([email protected] )
* @version $Id: XmlaDataAdapterService.java 7053 2014-04-25 15:32:01Z lucianc $
*/
public class XmlaDataAdapterService extends AbstractDataAdapterService
{
private static final Log log = LogFactory.getLog(XmlaDataAdapterService.class);
/**
*
*/
public XmlaDataAdapterService(JasperReportsContext jasperReportsContext, XmlaDataAdapter dataAdapter)
{
super(jasperReportsContext, dataAdapter);
}
/**
* @deprecated Replaced by {@link #XmlaDataAdapterService(JasperReportsContext, XmlaDataAdapter)}.
*/
public XmlaDataAdapterService(XmlaDataAdapter dataAdapter)
{
this(DefaultJasperReportsContext.getInstance(), dataAdapter);
}
public XmlaDataAdapter getXmlaDataAdapter()
{
return (XmlaDataAdapter) getDataAdapter();
}
@Override
public void contributeParameters(Map parameters)
throws JRException {
XmlaDataAdapter xmlaDA = getXmlaDataAdapter();
if (xmlaDA != null) {
parameters.put(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_URL,
xmlaDA.getXmlaUrl());
parameters.put(
JRXmlaQueryExecuterFactory.PARAMETER_XMLA_DATASOURCE,
xmlaDA.getDatasource());
parameters.put(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_CATALOG,
xmlaDA.getCatalog());
String username = xmlaDA.getUsername();
if (username != null && !username.isEmpty())
parameters.put(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_USER,
username);
String password = xmlaDA.getPassword();
SecretsUtil secretService = SecretsUtil.getInstance(getJasperReportsContext());
if (secretService != null)
password = secretService.getSecret(SECRETS_CATEGORY, password);
if (password != null && !password.isEmpty())
parameters.put(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_PASSWORD, password);
}
}
@Override
public void test() throws JRException
{
Map params = new HashMap();
contributeParameters(params);
Properties props = new Properties();
putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_SERVER, params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_URL));
putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_CATALOG, params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_CATALOG));
putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_DATA_SOURCE, params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_DATASOURCE));
putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_USER, params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_USER));
putNonNull(props, Olap4jXmlaQueryExecuter.XMLA_PASSWORD, params.get(JRXmlaQueryExecuterFactory.PARAMETER_XMLA_PASSWORD));
putNonNull(props, Olap4jXmlaQueryExecuter.OLAP4J_DRIVER, Olap4jXmlaQueryExecuter.OLAP4J_XMLA_DRIVER_CLASS);
putNonNull(props, Olap4jXmlaQueryExecuter.OLAP4J_URL_PREFIX, Olap4jXmlaQueryExecuter.OLAP4J_XMLA_URL_PREFIX);
try
{
// load driver and Connection
Class.forName(Olap4jXmlaQueryExecuter.OLAP4J_XMLA_DRIVER_CLASS);
Connection connection = DriverManager.getConnection(Olap4jXmlaQueryExecuter.OLAP4J_XMLA_URL_PREFIX, props);
OlapConnection olapConnection = connection.unwrap(OlapConnection.class);
// doing something to validate the connection
OlapDatabaseMetaData metaData = olapConnection.getMetaData();
ResultSet datasources = null;
try
{
// try olap4j 1.1 first
Method method = OlapDatabaseMetaData.class.getMethod("getDatabases");
datasources = (ResultSet) method.invoke(metaData);
}
catch (NoSuchMethodException e)
{
// not olap4j 1.1
if (log.isDebugEnabled())
{
log.debug("OlapDatabaseMetaData.getDatabases method not found: " + e.getMessage());
}
}
if (datasources == null)
{
try
{
// try olap4j 0.9
Method method = OlapDatabaseMetaData.class.getMethod("getDatasources");
datasources = (ResultSet) method.invoke(metaData);
}
catch (NoSuchMethodException e)
{
// not olap4j 0.9? giving up
if (log.isDebugEnabled())
{
log.debug("OlapDatabaseMetaData.getDatasources method not found: " + e.getMessage());
}
}
}
if (datasources != null)
{
// making sure the request is sent
datasources.next();
datasources.close();
}
connection.close();
}
catch (ClassNotFoundException e)
{
throw new JRException("Error creating XMLA connection", e);
}
catch (IllegalAccessException e)
{
throw new JRException("Error creating XMLA connection", e);
}
catch (InvocationTargetException e)
{
throw new JRException("Error creating XMLA connection", e);
}
catch (SQLException e)
{
throw new JRException("Error creating XMLA connection", e);
}
dispose();
}
private void putNonNull(Properties props, String key, Object value)
{
if (value != null)
{
props.put(key, value);
}
}
}