001/* 002 * (C) Copyright 2006-20011 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018 019package org.nuxeo.ecm.platform.reporting.report; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.Iterator; 026import java.util.List; 027import java.util.Map; 028 029import org.apache.commons.logging.Log; 030import org.apache.commons.logging.LogFactory; 031import org.eclipse.birt.core.exception.BirtException; 032import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask; 033import org.eclipse.birt.report.engine.api.IParameterDefn; 034import org.eclipse.birt.report.engine.api.IReportRunnable; 035import org.eclipse.birt.report.model.api.IDesignEngine; 036import org.eclipse.birt.report.model.api.OdaDataSourceHandle; 037import org.eclipse.birt.report.model.api.ReportDesignHandle; 038import org.eclipse.birt.report.model.api.SessionHandle; 039import org.eclipse.birt.report.model.elements.OdaDataSource; 040import org.nuxeo.ecm.core.api.NuxeoException; 041import org.nuxeo.ecm.core.api.repository.RepositoryManager; 042import org.nuxeo.ecm.platform.reporting.engine.BirtEngine; 043import org.nuxeo.runtime.api.Framework; 044import org.nuxeo.runtime.datasource.DataSourceHelper; 045 046import com.ibm.icu.util.ULocale; 047 048/** 049 * Helper class to make Birt API easier to use 050 * 051 * @author Tiry (tdelprat@nuxeo.com) 052 */ 053public class ReportHelper { 054 055 protected static final Log log = LogFactory.getLog(ReportHelper.class); 056 057 public static IReportRunnable getReport(String reportPath) { 058 try { 059 return BirtEngine.getBirtEngine().openReportDesign(reportPath); 060 } catch (BirtException e) { 061 throw new NuxeoException(e); 062 } 063 } 064 065 public static IReportRunnable getReport(InputStream stream) { 066 try { 067 return BirtEngine.getBirtEngine().openReportDesign(stream); 068 } catch (BirtException e) { 069 throw new NuxeoException(e); 070 } 071 } 072 073 public static IReportRunnable getNuxeoReport(InputStream stream) { 074 return getNuxeoReport(stream, Framework.getService(RepositoryManager.class).getDefaultRepositoryName()); 075 } 076 077 public static Map<String, String> getReportMetaData(InputStream stream) { 078 IDesignEngine dEngine = BirtEngine.getBirtDesignEngine(); 079 SessionHandle sh = dEngine.newSessionHandle(ULocale.ENGLISH); 080 ReportDesignHandle designHandle; 081 try { 082 designHandle = sh.openDesign((String) null, stream); 083 } catch (BirtException e) { 084 throw new NuxeoException(e); 085 } 086 087 Map<String, String> meta = new HashMap<String, String>(); 088 meta.put("title", designHandle.getTitle()); 089 meta.put("author", designHandle.getAuthor()); 090 meta.put("description", designHandle.getDescription()); 091 meta.put("displayName", designHandle.getDisplayName()); 092 093 try { 094 sh.closeAll(false); 095 } catch (IOException e) { 096 throw new NuxeoException(e); 097 } 098 return meta; 099 } 100 101 public static IReportRunnable getNuxeoReport(InputStream stream, String repositoryName) { 102 IDesignEngine dEngine = BirtEngine.getBirtDesignEngine(); 103 SessionHandle sh = dEngine.newSessionHandle(ULocale.ENGLISH); 104 ReportDesignHandle designHandle; 105 try { 106 designHandle = sh.openDesign((String) null, stream); 107 } catch (BirtException e) { 108 throw new NuxeoException(e); 109 } 110 111 String dsName = DataSourceHelper.getDataSourceRepositoryJNDIName(repositoryName); 112 for (Iterator<?> i = designHandle.getDataSources().iterator(); i.hasNext();) { 113 OdaDataSourceHandle dsh = (OdaDataSourceHandle) i.next(); 114 OdaDataSource ds = (OdaDataSource) dsh.getElement(); 115 ds.setProperty("odaJndiName", DataSourceHelper.getDataSourceJNDIName(dsName)); 116 } 117 118 IReportRunnable modifiedReport; 119 try { 120 modifiedReport = BirtEngine.getBirtEngine().openReportDesign(designHandle); 121 } catch (BirtException e) { 122 throw new NuxeoException(e); 123 } 124 // Can we really? 125 try { 126 sh.closeAll(false); 127 } catch (IOException e) { 128 throw new NuxeoException(e); 129 } 130 131 return modifiedReport; 132 } 133 134 public static List<IParameterDefn> getReportParameter(IReportRunnable report) { 135 List<IParameterDefn> params = new ArrayList<IParameterDefn>(); 136 IGetParameterDefinitionTask task = BirtEngine.getBirtEngine().createGetParameterDefinitionTask(report); 137 for (Object paramDefn : task.getParameterDefns(false)) { 138 if (paramDefn instanceof IParameterDefn) { 139 IParameterDefn param = (IParameterDefn) paramDefn; 140 params.add(param); 141 } 142 } 143 return params; 144 } 145 146}