Switch Data Source Based on user logged in Jasper Server

Posted on by By Nikhilesh, in Business Intelligence, Jaspersoft, Open Source Business Intelligence | 0

Switch Datasource based on user logged in Jasper Server

This tutorial shows you how to switch two different jdbc datasource using custom datasource in jasper server.

Cosider you have two jdbc datasource in jasper server and you want to select datasource1 if jasperadmin logged in to jasper server otherwise it will pick other datasource2.

Implementation requires two java class and one spring confrigrution file.

1) A jar file needs (contains those two java class) to be put in jasper server class path at location <JasperServer webapp dir>\WEB-INF\lib.

Jar implementation:

-> ReportDataSourceService Interface
A custom data source requires an implementation of the ReportDataSourceService interface, which is responsible for setting up and tearing down data source connections in JasperReports Server. It relies on:

void setReportParameterValues(Map parameterValues): called before running a report – creates resources needed by JasperReports to obtain a JRDataSource and adds them to the parameter map
void closeConnection(): clean up any resources allocated in setReportParameterValues()


public void setReportParameterValues(Map parameterValues) {
    MetadataUserDetails userDetails = (MetadataUserDetails)
                                      SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String userName = userDetails.getUsername();
    // obtain a connection based on the username.
    String dataSourceURI = "/datasources/test2";
    if (userName.equalsIgnoreCase("jasperadmin")) {
        dataSourceURI = "/datasources/ds_1";
    }
    connection = getRepositoryDatasource(dataSourceURI);
    try {
        parameterValues.put(JRParameter.REPORT_CONNECTION,
                            connection.getDataSource().getConnection());
    } catch (SQLException sqle){
        sqle.printStackTrace();
    }
}
public JdbcDataSourceService getRepositoryDatasource(String repositoryURI) {
    ExecutionContext context = JasperserverUtil.getExecutionContext(LocaleContextHolder.getLocale());
    ReportDataSource datasource = (ReportDataSource)
                                  repositoryService.getResource(context, repositoryURI);
    JdbcReportDataSourceServiceFactory factory = (JdbcReportDataSourceServiceFactory)
                                                 dataSourceService.getBean(datasource.getClass());
    JdbcDataSourceService DSservice = (JdbcDataSourceService)
                                      factory.createService(datasource);
    return DSservice;
}

-> Factory Bean Implementation

You will need to develop a bean that manages all the custom data sources. JasperReports Server provides an interface ReportDataSourceServiceFactory.


private RepositoryService repositoryService;
private BeanForInterfaceImplementationFactory dataSourceService;
 
//  ..  add getter and setter
 
public SwitchingDataSource createDataSourceService(){
    return new SwitchingDataSource(repositoryService, dataSourceService);
}
 
public ReportDataSourceService createService(ReportDataSource dataSource) {
    return new SwitchingDataSource(repositoryService, dataSourceService);
}

2) Spring Confrigrution File

To register the custom data source in JasperReports Server you will need to add the spring bean configuration. Just add a new file named applicationContext-customds.xml and add this content. This file needs to be deployed to <JasperServer webapp home>\WEB-INF.

The beanId SwitcherDS will be used within JasperReports Server configuration to identify the custom data source.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="SwitcherDS"
          class="com.jaspersoft.ps.examples.SwitchingDataSourceFactory" >
        <property name="repositoryService">
            <ref bean="repositoryService"/>
        </property>
        <property name="dataSourceServiceFactories">
            <ref bean="dataSourceServiceFactories"/>
        </property>
    </bean>
</beans>

For further details click goto this link http://community.jaspersoft.com/wiki/switching-datasource-based-user

Thanks
Fahad Anjum

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments