1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
package com;
import java.io.File;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IRenderTask;
import org.eclipse.birt.report.engine.api.IReportDocument;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunTask;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.birt.report.engine.api.script.ScriptException;
import org.eclipse.birt.report.engine.api.script.element.IDataSource;
/**
* Cette classe permet la génération dans n'importe quel format
* de n'importe quelle édition.
* Il faut d'abord l'instancier, puis l'initialiser et la lancer.
*
* Si nécessaire, cette classe peut être threadée facilement.
*/
public final class Edition {
private static Edition m_Instance = null;
private IReportEngine engine;
private String realPath;
private IRenderOption option;
private String etat;
private String chemin;
private Map<String, Object> parametres;
private String format;
private Locale langue;
/**
* Constructeur
* Il configure le moteur BIRT de sorte à ce qu'il puisse retrouver
* les ressources BIRT mais également les librairies le faisant tourner
* Il est alors démarrer avec cette configuration.
* @throws BirtException
*/
Edition() throws BirtException {
EngineConfig config = new EngineConfig();
realPath = "/home/atef/workspace1.0/Reporting/WebContent";
config.setResourcePath(new File(realPath, "WEB-INF/classes/ressources").getAbsolutePath());
config.setBIRTHome(new File(realPath, "WEB-INF/ReportEngine/lib").getAbsolutePath());//endroit où se trouvent les librairies du Runtime
Platform.startup(config);
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
engine = factory.createReportEngine(config);
option = new RenderOption();
}
/**
* Instanciation du service
* @return l'instance en cours du service
* @throws BirtException
*/
public static Edition getInstance() throws BirtException {
if (m_Instance == null) {
m_Instance = new Edition();
}
return m_Instance;
}
/**
* Initialisation
* @param etat nom du rptdesign
* @param chemin chemin et nom du fichier de sortie
* @param parametres paramètres à passer à l'état
* @param format format de sortie (PDF, XLS, DOC, etc.)
* @param langue langue de l'édition
*/
public void init(String etat, String chemin, Map<String, Object> parametres, String format, String langue) {
this.etat = etat;
this.chemin = chemin;
this.parametres = parametres;
this.format = format;
this.langue = new Locale(langue);
}
/**
* Lancement de l'état
* @throws ScriptException
*/
public void runBirt(){
try {
/* Récupération du rptDesign */
IReportRunnable design = engine.openReportDesign(new File(realPath, "WEB-INF/etats/" + etat + ".rptdesign").getAbsolutePath());
IDataSource dataSource = design.getDesignInstance().getDataSource("ReportingDB");
try {
dataSource.setPrivateDriverProperty("odaDriverClass", "com.mysql.jdbc.Driver");
dataSource.setPrivateDriverProperty("odaURL", "jdbc:mysql://****:3306/*****");
dataSource.setPrivateDriverProperty("odaUser", "root");
dataSource.setPrivateDriverProperty("odaPassword", "****");
}
catch (ScriptException e) {
e.printStackTrace();
}
System.out.println("Récupération du rptDesign");
/* Exécution */
IRunTask runTask = engine.createRunTask(design);
System.out.println("Exécution");
/* Paramètres */
for (String clef : parametres.keySet()) {
runTask.setParameterValue(clef, parametres.get(clef));
}
System.out.println("Paramètres");
/* Langue */
runTask.setLocale(langue);
System.out.println("Langue Chmn:"+chemin);
runTask.run(chemin+".rptdocument");
/* Récupération du rptDocument */
IReportDocument doc = engine.openReportDocument(chemin+".rptdocument");
//IDataSet dat=design.getDesignInstance().getDataSet("MyData");
System.out.println("Récupération du rptDocument 2");
/* Rendu */
IRenderTask renderTask = engine.createRenderTask(doc);
System.out.println("Rendu");
option.setOutputFileName(chemin);//Nom du fichier de sortie
option.setOutputFormat(format);//Format du fichier de sortie
renderTask.setRenderOption(option);//Chargement du fichier
renderTask.render();//Création du fichier
}
catch (EngineException e) {
e.printStackTrace();
}
}
public void destructeur() {
engine.destroy();
}
public static void main(String[] args) throws BirtException {
HashMap<String, Object> map = new HashMap<String, Object>();
Edition e= new Edition();
e.init("test", "test", map, "pdf", "");
e.runBirt();
}
} |
Partager