Pentaho Reporting 3.5 for Java Developers- P4

50 483 1
Pentaho Reporting 3.5 for Java Developers- P4

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 5 [ 133 ] StaticSessionProvider The StaticSessionProvider simply takes in an org.hibernate.Session object as a constructor parameter, making available the already existing Session object to the HQLDataFactory . This would be used if your system already has an initialized Hibernate session. DefaultSessionProvider The DefaultSessionProvider requires no constructor parameters, and uses the following API call to generate a SessionFactory from Hibernate: sessionFactory = new Configuration().configure(). buildSessionFactory(); The created sessionFactory instance is used to create new sessions, which the HQLDataFactory uses to query Hibernate. The HQLDataFactory provides two constructors. The rst constructor takes in a SessionProvider , as described above. The second constructor simply takes in a Hibernate Session instance, which it uses to query Hibernate. This constructor uses a StaticSessionProvider , under the covers, to pass in the Session to HQLDataFactory . Once you've instantiated your factory, you may add named queries to the factory by making the following API call: void setQuery(String name, String queryString); The setQuery method takes in the name of the query, and the Hibernate query, in order to execute. HQLDataFactory uses Hibernate's query language, which is well-documented at http://www.hibernate.org/hib_docs/reference/en/html/queryhql. html You may include report parameters in your query by using the HQL syntax ":ParameterName" The max results and query timeout parameters are supported by HQLDataFactory . This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Working with Data Sources [ 134 ] HQLDataFactory Example To demonstrate using HQLDataFactory , you must rst set up a simple Hibernate application. To begin, download the latest version of Hibernate from http://www.hibernate.org . This example uses version 3.2.6.ga. Place the hibernate.jar le and all the JAR les from the Hibernate distribution's lib folder into the chapter5/lib folder. You must also deploy the pentaho- reporting-engine-classic-extensions-hibernate.jar le, located in Pentaho Report Designer's lib folder, into the chapter5/lib folder. In the SQLReportDataFactory example given earlier, you dened an HSQLDB data source. You'll reuse that data source in this example. Once you've moved the appropriate JAR les into Chapter 5, you'll need to dene a simple Java class, chapter5/src/LibraryInfo.java , which maps to your HSQLDB data source: public class LibraryInfo { private String name; private String description; private long size; public LibraryInfo() {} public void setName(String name) { this.name = name; } public String getName() { return name; } public void setDescription(String description) { this.description = description; } public String getDescription() { return description; } public void setSize(long size) { this.size = size; } public long getSize() { return size; } } This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 5 [ 135 ] Dene the Hibernate mapping between the HSQLDB database and the LibraryInfo class, saved as chapter5/src/LibraryInfo.hbm.xml : <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="LibraryInfo" table="LIBRARYINFO"> <id name="name" column="name" type="string"/> <property name="description" type="string"/> <property name="size" type="long"/> </class> </hibernate-mapping> Now, you're ready to congure the Hibernate settings le with the appropriate JDBC information and mapping input. Save the following as chapter5/src/ hibernate.cfg.xml : <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration- 3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class"> org.hsqldb.jdbcDriver</property> <property name="connection.url"> jdbc:hsqldb:file:data/libraryinfo </property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.HSQLDialect </property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class"> thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class"> org.hibernate.cache.NoCacheProvider</property> <mapping resource="LibraryInfo.hbm.xml"/> </session-factory> </hibernate-configuration> This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Working with Data Sources [ 136 ] At this point, you're ready to add a load data section to the onPreview method within a freshly copied Chapter2SwingApp , renamed to HQLDataFactoryApp : // load hql data source DefaultSessionProvider sessionProvider = new DefaultSessionProvider(); HQLDataFactory factory = new HQLDataFactory(sessionProvider); factory.setQuery("default", "select name as NAME, description as DESCRIPTION, size as SIZE from LibraryInfo"); report.setDataFactory(factory); Be sure to add the following import statements at the beginning of the le: import org.pentaho.reporting.engine.classic.extensions.datasources. hibernate.DefaultSessionProvider; import org.pentaho.reporting.engine.classic.extensions.datasources. hibernate.HQLDataFactory; Due to the naming of column headers in HQLDataFactory being mapped to the attributes of queried objects, you must also modify the sample report. Copy chapter2_report.prpt to chapter5/data/hql_report.prpt , and change the column names, as shown in the following list: • Library Name to NAME • Library Description to DESCRIPTION • Library Size to SIZE Also change the Total Library Size function's Field Name to SIZE . Once you've saved your changes, update the HQLDataFactoryApp class with the new location of the report XML le. As the last step, you'll need to add the following Ant target to your build.xml le: <target name="runhql" depends="compile"> <java fork="true" classpathref="runtime_classpath" classname="HQLDataFactoryApp"/> </target> Type ant runhql on the command line to view the results! This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 5 [ 137 ] PmdDataFactory The org.pentaho.reporting.engine.classic.extensions.datasources.pmd. PmdDataFactory class allows you to populate your report, using a Pentaho Metadata Query. Pentaho Metadata allows a database administrator to dene a business layer of their relational data for end users, simplifying the ability to query the data, as well as protecting users from the complexities that may exist in a database schema. Pentaho's Metadata Query Language (MQL) is an XML-based query model that simplies querying databases, and is currently used within the Pentaho Report Designer and Pentaho Web Ad Hoc Report client tools. In order for PmdDataFactory to initialize properly, it must have access to certain Pentaho Metadata conguration properties that can be congured at runtime, or be passed in by a conguration le. XMI le The XMI le contains a serialized version of the dened metadata model, and is required in order to execute MQL queries. The XMI le contains information including how to connect to the relational data source, as well as the business model mapping of the relational data. This le is loaded at runtime into the congured repository of Pentaho Metadata. The XMI le may be congured by calling the setXmiFile method. This le is loaded with Pentaho Reporting Engine's ResourceManager . Domain Id The metadata domain id is used to map a name to the XMI le within the metadata repository. This name is also referenced in the MQL query le. Therefore, it is important to use the same name in the MQL query, as well as the PmdDataFactory . The domain may be set by the setDomainId method. IPmdConnectionProvider PmdDataFactory uses the IPmdConnectionProvider interface to obtain the metadata domain objects as well as the database connection for the query. The IPmdConnectionProvider must be specied via the setConnectionProvider method. A default implementation, PmdConnectionProvider , manages loading the XMI le as well as determining the database connection to be used based on metadata information provided in the XMI le. The IPmdConnectionProvider denes the following methods: This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Working with Data Sources [ 138 ] // returns a connection object based on the relational data source Connection gu8etConnection(DatabaseMeta databaseMeta) throws ReportDataFactoryException; // returns a metadata repository based on the domain id and xmi file IMetadataDomainRepository getMetadataDomainRepository(String domain, ResourceManager resourceManager, ResourceKey contextKey, String xmiFile) throws ReportDataFactoryException; Registering MQL Queries Once you've congured the PmdDataFactory correctly, you need to provide named MQL queries via the setQuery(String name, String query) method. Please see http://wiki.pentaho.com/display/ServerDoc2x/03.+Pentaho+Metadata+MQL+ Schema to learn more about the MQL query format. PmdDataFactory example To begin, you'll need to build a very simple Pentaho Metadata model. First, download Pentaho Metadata Editor from SourceForge: http://sourceforge.net/ projects/pentaho . Click on the Download link, and select the Pentaho Metadata package. Download the latest "pme-ce" zip or tar distribution, depending on your operating system environment. For Windows, unzip the download, and run metadata-editor.bat . For Linux and Mac, untar the download and run metadata-editor.sh . From the main window, select File | new Domain File . Now, it's time to dene your physical model. Right-click on the Connections tree item and select New Connection . Name the Connection Library Info and select Hypersonic as the connection type. Set the Host Name to le: and the Database Name to the full path to your example libraryinfo.script le minus the .script le extension. Set the Port Number to blank, and nally set the username to sa and password to blank. This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 5 [ 139 ] Click Test to make sure you are connected properly, and then click OK. This will bring up an Import Tables dialog. Select LIBRARYINFO and click OK. This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Working with Data Sources [ 140 ] This will generate a default physical model. Now that you've dened the physical model, you'll need to build a business model. Right-click on Business Models and select the New Business Model menu item. Give this model the ID of LIBRARYINFO_MODEL, and select Library Info as the connection. Finally, under the Settings section, set the Name to Library Info. In the main window, drag-and-drop the LIBRARYINFO table from the Library Info connection into the Business Tables tree. This will bring up a new Business Table Properties dialog. Click OK. Double-click on the Business View tree element to bring up the Manage Categories dialog. Select the LIBRARYINFO business table and click the Add Arrow in between the two list boxes. This will create a new category with the same name as the business table. This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Chapter 5 [ 141 ] Once completed, the main Business Model Tree should look like this: Now that you've dened your metadata model, export the model as an XMI le by selecting the File | Export to XMI File . menu item. First, you will be prompted to save the Domain le. Name the Domain Library Info. Finally, save your XMI le as chapter5/data/libraryinfo.xmi . This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Working with Data Sources [ 142 ] Once you've exported your metadata model, you must set up your environment with the necessary JAR les. Copy all the JAR les located in the lib and lib-ext folders from the Pentaho Metadata Editor distribution into the chapter5/lib folder. Also, copy the pentaho-reporting-engine-classic-extensions-pmd.jar le, located in the Pentaho Report Designer lib folder, into the chapter5/lib folder. After copying the correct JAR les, go ahead and add a new load data section of the onPreview method within a freshly copied Chapter2SwingApp , renamed to PmdDataFactoryApp : // load MQL data source PmdDataFactory factory = new PmdDataFactory(); factory.setConnectionProvider(new PmdConnectionProvider()); factory.setXmiFile("data/libraryinfo.xmi"); factory.setDomainId("Library Info"); factory.setQuery("default", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<mql>" + " <domain_type>relational</domain_type>" + " <domain_id>Library Info</domain_id>" + " <model_id>LIBRARYINFO_MODEL</model_id>" + " <model_name>Library Info</model_name>" + " <selections>" + " <selection>" + " <view>BC_LIBRARYINFO</view>" + " <column>BC_LIBRARYINFO_NAME</column>" + " </selection>" + " <selection>" + " <view>BC_LIBRARYINFO</view>" + " <column>BC_LIBRARYINFO_DESCRIPTION</column>" + " </selection>" + " <selection>" + " <view>BC_LIBRARYINFO</view>" + " <column>BC_LIBRARYINFO_SIZE</column>" + " </selection>" + " </selections>" + "</mql>"); Notice that MQL is in XML format. Much like your other queries, you've selected library name, description, and size from the data source. This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 710 South Avenue West, , Westfield, , 07090Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... true for this value to be relevant date-format If the item value is a date, a Java date format string may be provided to format the date appropriately Please see Java' s SimpleDateFormat JavaDoc for formatting details numeric-format If the item value is a decimal number, a Java decimal format string may be provided to format the number appropriately Please see Java' s DecimalFormat JavaDoc for formatting... the example: import org .pentaho. di.core.exception.KettleException; import org .pentaho. di.core.util.EnvUtil; import org .pentaho. di.job.JobEntryLoader; import org .pentaho. di.trans.StepLoader; import org .pentaho. reporting. engine.classic.core.ParameterMapping; import org .pentaho. reporting. engine.classic.extensions.datasources kettle.KettleTransFromFileProducer; import org .pentaho. reporting. engine.classic.extensions.datasources... name="runkettle" depends="compile"> Type ant runkettle on the command line to view the results! BandedMDXDataFactory The org .pentaho. reporting. engine.classic.extensions.datasources olap4j.BandedMDXDataFactory class allows you to populate your report from An olap4j data source olap4j is a Java API for connecting to multi-dimensional... configure them in Pentaho Report Designer You'll also learn how to populate a chart with various types of data In addition to learning all about charts, this chapter also covers the various methods for including visual information in your report, including embedding images and Java graphics in your report Supported charts Pentaho Reporting relies on JFreeChart, an open source Java chart library, for charting... defined, you're ready to begin interfacing the OLAP data source with Pentaho Reporting First, you must copy over the necessary JAR files Place all the JAR files that exist in the workbench/lib folder in chapter5/lib folder Also, place the pentaho- reporting- engine-classicextensions-olap4j.jar and olap4j.jar files, found in Pentaho Reporting' s lib folder, into the chapter5/lib folder Add the following... report.setDataFactory(factory); You must also add the following imports to complete the example: import org .pentaho. reporting. engine.classic.extensions.datasources olap4j.DriverConnectionProvider; import org .pentaho. reporting. engine.classic.extensions.datasources olap4j.BandedMDXDataFactory; [ 151 ] This material is copyright and is licensed for the sole use by David Martone on 16th September 2009 Please purchase PDF Split-Merge... depends="compile"> Type ant runmdx on the command line to view the results You may also consider doing this example without the necessity of the load data section, by adding an olap4j data source to your report within Pentaho Report Designer DenormalizedMDXDataFactory The org .pentaho. reporting. engine.classic.extensions.datasources... transformation Download Pentaho Data Integration 3.2 from SourceForge: http://sourceforge.net/ projects /pentaho Click on the Download link, and select the Data Integration package Download the latest "pdi-ce" ZIP (compressed file), TAR, or DMG distribution, depending on your operating system environment Install the distribution To bring up the user interface, run Kettle.exe if you are a Windows user For. .. The rows show the row axis information selected For instance, if a year was selected from the time dimension on the column axis, in the column header you would see the member name [Time].[1997] To learn more about olap4j and Mondrian's Relational OLAP engine, please visit http://www.olap4j.org and http://mondrian .pentaho. org [ 148 ] This material is copyright and is licensed for the sole use by David... ParameterMapping[] definedVariableNames // the names of reporting properties to be passed into Kettle via Transformation Parameters The KettleDataFactory has a default constructor To add Kettle transformation queries to the KettleDataFactory, call the setQuery(String, KettleTransformationProducer) method [ 144 ] This material is copyright and is licensed for the sole use by David Martone on 16th September . place the pentaho- reporting- engine-classic- extensions-olap4j.jar and olap4j.jar les, found in Pentaho Reporting& apos;s lib folder, into the chapter5/lib. Chapter 5 [ 1 45 ] KettleDataFactory example To start the example, you rst need to build a Kettle transformation. Download Pentaho Data Integration 3. 2 from

Ngày đăng: 20/10/2013, 13:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan