2010-07-14 16 views
11

Ho un report BIRT che si collega al nostro database di test. Nell'ambiente produttivo vorrei fornire un'origine dati fornita dal contenitore tramite jndi.Come impostare un'origine dati per un report BIRT a livello di codice?

Come impostare l'origine dati in modo programmatico per il rapporto specificato?

... 
    IReportRunnable design = birtEngine.openReportDesign (new File (properties.getProperty ("reportPath"), report + ".rptdesign").getAbsolutePath()); 
    IRunAndRenderTask task = birtEngine.createRunAndRenderTask (design); 

    PDFRenderOption options = new PDFRenderOption(); 
    options.setOutputFormat (PDFRenderOption.OUTPUT_FORMAT_PDF); 
    options.setOutputStream (out); 
    task.setRenderOption (options); 
    for (Entry<String, Object> entry : parameters.entrySet()) 
    { 
     task.setParameterValue (entry.getKey(), entry.getValue()); 
    } 

    task.run(); 
    task.close(); 
    ... 

immagino che avrei dovuto modificare il design, ma d'altra parte task ha un metodo di setDataSource ma che sembra un po 'come avrei dovuto fornire alcuni elementi DOM XML.

risposta

2

L'impostazione solo dell'origine dati in fase di esecuzione sarà problematica poiché il set di dati è associato a una singola origine dati e i controlli sul report vengono quindi associati a un set di dati specifico. Questa gerarchia sarebbe abbastanza appiccicosa da provare a creare te stesso ogni volta che viene eseguito il rapporto.

È possibile parametrizzare tutti gli aspetti della definizione dell'origine dati rendendo il progetto portatile in tutti gli ambienti. Quando si modifica la propria origine dati, osservare il raggruppamento Binding proprietà sul lato sinistro. Ciò dovrebbe darti ampia flessibilità per rendere più portatile la tua fonte di dati. È possibile specificare i parametri di runtime per gli elementi URL JDBC o un profilo JNDI di runtime.

Spero che questo aiuti.

3

È possibile creare un parametro di rapporto per la stringa di connessione del database.

Quindi, impostare l'URL JNDI sotto Data Source -> Proprietà Binding -> URL JNDI, come:. Params [ "Database"] Valore
(Dove "Database" è il nome del parametro di report)

5

Guardare il seguente codice si può ottenere un aiuto nella fornitura di origine dati in fase di esecuzione.

Per le mie esigenze funziona bene.

Ho ricevuto questo da qualche sito non ricordo.

import java.io.IOException; 
import java.util.ArrayList; 

import org.eclipse.birt.core.framework.Platform; 
import org.eclipse.birt.report.model.api.CellHandle; 
import org.eclipse.birt.report.model.api.DataItemHandle; 
import org.eclipse.birt.report.model.api.DesignConfig; 
import org.eclipse.birt.report.model.api.ElementFactory; 
import org.eclipse.birt.report.model.api.IDesignEngine; 
import org.eclipse.birt.report.model.api.IDesignEngineFactory; 
import org.eclipse.birt.report.model.api.LabelHandle; 
import org.eclipse.birt.report.model.api.OdaDataSetHandle; 
import org.eclipse.birt.report.model.api.OdaDataSourceHandle; 
import org.eclipse.birt.report.model.api.PropertyHandle; 
import org.eclipse.birt.report.model.api.ReportDesignHandle; 
import org.eclipse.birt.report.model.api.RowHandle; 
import org.eclipse.birt.report.model.api.SessionHandle; 
import org.eclipse.birt.report.model.api.StructureFactory; 
import org.eclipse.birt.report.model.api.TableHandle; 
import org.eclipse.birt.report.model.api.activity.SemanticException; 
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn; 

import com.ibm.icu.util.ULocale; 

/** 
* Dynamic Table BIRT Design Engine API (DEAPI) demo. 
*/ 

public class DECreateDynamicTable 
{ 
    ReportDesignHandle designHandle = null; 
    ElementFactory designFactory = null; 
    StructureFactory structFactory = null; 

    public static void main(String[] args) 
    { 
     try 
     { 
      DECreateDynamicTable de = new DECreateDynamicTable(); 
      ArrayList al = new ArrayList(); 
      al.add("USERNAME"); 
      al.add("COUNTRY"); 
      de.buildReport(al, "From GTM_REPORT_APP_USER"); 
     } 
     catch (IOException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (SemanticException e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    void buildDataSource() throws SemanticException 
    { 

     OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(
       "Data Source", "org.eclipse.birt.report.data.oda.jdbc"); 
     dsHandle.setProperty("odaDriverClass", 
       "oracle.jdbc.driver.OracleDriver"); 
     dsHandle.setProperty("odaURL", "jdbc:oracle:thin:@xeon:1521:ora9i"); 
     dsHandle.setProperty("odaUser", "AIMS_GTMNE"); 
     dsHandle.setProperty("odaPassword", "AIMS_GTMNE"); 

     designHandle.getDataSources().add(dsHandle); 

    } 

    void buildDataSet(ArrayList cols, String fromClause) throws SemanticException 
    { 

     OdaDataSetHandle dsHandle = designFactory.newOdaDataSet("ds", 
       "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet"); 
     dsHandle.setDataSource("Data Source"); 
     String qry = "Select "; 
     for(int i=0; i < cols.size(); i++){ 
      qry += " " + cols.get(i); 
      if(i != (cols.size() -1)){ 
       qry += ","; 
      } 

     } 
     qry += " " + fromClause; 

     dsHandle.setQueryText(qry); 

     designHandle.getDataSets().add(dsHandle); 



    } 
    void buildReport(ArrayList cols, String fromClause) throws IOException, SemanticException 
    { 


     //Configure the Engine and start the Platform 
     DesignConfig config = new DesignConfig(); 

     config.setProperty("BIRT_HOME", "D:/Softwares/Frame Works - APIs-Tools/birt-runtime-2_6_1/birt-runtime-2_6_1/ReportEngine"); 

     IDesignEngine engine = null; 
     try{ 


      Platform.startup(config); 
      IDesignEngineFactory factory = (IDesignEngineFactory) Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY); 
      engine = factory.createDesignEngine(config); 

     }catch(Exception ex){ 
      ex.printStackTrace(); 
     }  


     SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH) ; 



     try{ 
      //open a design or a template 

      designHandle = session.openDesign("D:/tempBirtReport/test.rptdesign"); 

      designFactory = designHandle.getElementFactory(); 

      buildDataSource(); 
      buildDataSet(cols, fromClause); 

      TableHandle table = designFactory.newTableItem("table", cols.size()); 
      table.setWidth("100%"); 
      table.setDataSet(designHandle.findDataSet("ds")); 


      PropertyHandle computedSet = table.getColumnBindings(); 
      ComputedColumn cs1 = null; 

      for(int i=0; i < cols.size(); i++){ 
       cs1 = StructureFactory.createComputedColumn(); 
       cs1.setName((String)cols.get(i)); 
       cs1.setExpression("dataSetRow[\"" + (String)cols.get(i) + "\"]"); 
       computedSet.addItem(cs1); 
      } 


      // table header 
      RowHandle tableheader = (RowHandle) table.getHeader().get(0); 


      for(int i=0; i < cols.size(); i++){ 
       LabelHandle label1 = designFactory.newLabel((String)cols.get(i)); 
       label1.setText((String)cols.get(i)); 
       CellHandle cell = (CellHandle) tableheader.getCells().get(i); 
       cell.getContent().add(label1); 
      }       

      // table detail 
      RowHandle tabledetail = (RowHandle) table.getDetail().get(0); 
      for(int i=0; i < cols.size(); i++){ 
       CellHandle cell = (CellHandle) tabledetail.getCells().get(i); 
       DataItemHandle data = designFactory.newDataItem("data_"+(String)cols.get(i)); 
       data.setResultSetColumn((String)cols.get(i)); 
       cell.getContent().add(data); 
      } 

      designHandle.getBody().add(table); 

      // Save the design and close it. 

      designHandle.saveAs("D:/tempBirtReport/test.rptdesign"); //$NON-NLS-1$ 
      designHandle.close(); 
      System.out.println("Finished"); 
     }catch (Exception e){ 
      e.printStackTrace(); 
     }  

    } 
} 
1

Mi piace l'approccio di Adams. Ecco come lo facciamo:

/* 
* Change the data sources in the .rptdesign 
*/ 
void changeDataSource(ElementFactory designFactory, 
     ReportDesignHandle designHandle, String userConnect) 
     throws SemanticException { 

    SlotHandle datasources = designHandle.getDataSources(); 
    SlotIterator iter = (SlotIterator) datasources.iterator(); 
    while (iter.hasNext()) { 
     DesignElementHandle dsHandle = (DesignElementHandle) iter.next(); 
     if (dsHandle instanceof OdaDataSourceHandle && dsHandle.getName().equals("lisa")) { 
      log.debug("changeDataSource: Changing datasource " 
        + dsHandle.getName() + " new url=" + getLisaDbUrl()); 
      dsHandle.setProperty("odaDriverClass", 
        "oracle.jdbc.driver.OracleDriver"); 
      dsHandle.setProperty("odaURL", getLisaDbUrl()); 
      dsHandle.setProperty("odaUser", getLisaUser()); 
      dsHandle.setProperty("odaPassword", getLisaPassword()); 
     } else { 
      log.debug("changeDataSource: Ignoring DS " + dsHandle.getName()); 
     } 
    } 
} 

Qui, "lisa" ist ha nome dell'origine dati che vorremmo cambiare in fase di esecuzione. La funzione get ... restituisce i valori necessari al tempo di esecuzione "produzione".

0

Questo ha funzionato per me. Ho ottenuto il contesto e dal contesto ottenuto il dataSource e passato la connessione al report di Birt come sotto

Context initialContext = new InitialContext(); 
    if (initialContext == null){ 
    System.out.println("JNDI problem. Cannot get InitialContext."); 
     } 
     DataSource datasource = (DataSource)initialContext.lookup("java:/datasources/SAMPLE"); 
    task.getAppContext().put("OdaJDBCDriverPassInConnection", datasource.getConnection());