2010-07-14 8 views
11

테스트 데이터베이스에 연결하는 BIRT 보고서가 있습니다. 생산적인 환경에서 jndi를 통해 컨테이너가 제공하는 데이터 소스를 제공하고 싶습니다.프로그래밍 방식으로 BIRT 보고서의 데이터 소스를 설정하는 방법은 무엇입니까?

주어진 보고서에 대해 프로그래밍 방식으로 데이터 소스를 설정하는 방법은 무엇입니까?

... 
    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(); 
    ... 

나는 내가 design을 수정해야하지만, 다른 한편으로 task에 방법 setDataSource을 가지고하지만 일부 XML DOM 요소를 제공 할 것 같은이 조금 보이는 것 같아요.

답변

2

데이터 집합을 단일 데이터 원본에 바인딩하고 보고서의 컨트롤을 특정 데이터 집합에 바인딩하기 때문에 런타임에 데이터 원본 만 설정하면 문제가 발생합니다. 이 계층 구조는 보고서가 실행될 때마다 스스로를 구축하고 구축하기에 꽤 끈적 할 것입니다.

모든 환경에서 디자인을 이식성있게 만드는 데이터 소스 정의의 모든 측면을 매개 변수화 할 수 있습니다. 데이터 소스를 편집 할 때 왼쪽에있는 특성 바인딩 그룹을보십시오. 이렇게하면 데이터 소스를 더 이식성있게 만들 수있는 충분한 유연성이 제공됩니다. JDBC URL 요소 또는 런타임 JNDI 프로파일에 대한 런타임 매개 변수를 지정할 수 있습니다.

희망이 도움이됩니다.

3

데이터베이스 연결 문자열에 대한 보고서 매개 변수를 만들 수 있습니다.

그런 다음 데이터 소스 아래의 JNDI URL을 설정 -> 속성 바인딩 -> JNDI의 같은 URL :. PARAMS [ "데이터베이스"] 값을

5
("데이터베이스"는 보고서 매개 변수의 이름입니다)

다음 코드를 보면 런타임에 데이터 소스를 제공하는 데 도움이 될 수 있습니다.

내 요구 사항에 잘 작동합니다.

나는 이것을 일부 사이트에서 얻지 못합니다.

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

저는 Adams 방식을 선호합니다. 는 여기에 우리가 그것을 할 방법은 다음과 같습니다

/* 
* 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()); 
     } 
    } 
} 

을 여기에, "리사"우리가 런타임에 변경하려는 데이터 소스의 그 이름 IST. get ... 함수는 "프로덕션"런타임에 필요한 값을 반환합니다.

0

이것은 나를 위해 일했습니다. 컨텍스트를 가지고 컨텍스트에서 데이터 원본을 가져 와서 아래와 같은 Birt 보고서에 연결을 전달했습니다.

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()); 
관련 문제