CRJavaHelper

/**
 * This sample code is an example of how to use the Business Objects APIs.
 * Because the sample code is designed for demonstration only, it is 
 * unsupported.  You are free to modify and distribute the sample code as needed.
 * *此示例代码是如何使用业务对象API的示例。
 * *由于示例代码仅用于演示,因此
 * *不受支持。您可以根据需要自由修改和分发示例代码。
 */
package com.businessobjects.samples;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import com.crystaldecisions.sdk.occa.report.application.DataDefController;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.data.FieldDisplayNameType;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.data.ITable;
import com.crystaldecisions.sdk.occa.report.data.ParameterField;
import com.crystaldecisions.sdk.occa.report.data.ParameterFieldDiscreteValue;
import com.crystaldecisions.sdk.occa.report.data.ParameterFieldRangeValue;
import com.crystaldecisions.sdk.occa.report.data.RangeValueBoundType;
import com.crystaldecisions.sdk.occa.report.data.Tables;
import com.crystaldecisions.sdk.occa.report.data.Values;
import com.crystaldecisions.sdk.occa.report.document.PaperSize;
import com.crystaldecisions.sdk.occa.report.document.PaperSource;
import com.crystaldecisions.sdk.occa.report.document.PrintReportOptions;
import com.crystaldecisions.sdk.occa.report.document.PrinterDuplex;
import com.crystaldecisions.sdk.occa.report.exportoptions.CharacterSeparatedValuesExportFormatOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.DataOnlyExcelExportFormatOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.EditableRTFExportFormatOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.ExportOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.PDFExportFormatOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.RTFWordExportFormatOptions;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.crystaldecisions.sdk.occa.report.lib.IStrings;
import com.crystaldecisions.sdk.occa.report.lib.PropertyBag;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKExceptionBase;

/**
 * Crystal Reports Java Helper Sample.
 * crystal Reports Java Helper示例。
 * ************************
 * Please note that you need to define a runtime server in order for this class to compile.
 * 请注意,为了编译此类,您需要定义一个运行时服务器。
 * ************************
 * @author Business Objects
 */


//上面是文字说明,底下是代码实现



public class CRJavaHelper {


// datasource: 数据源,它包含连接池和连接池管理两部分。
// 数据源就是连接到数据库的一条路径,它不存储数据,就是条线路,它记录的是连接的哪条数据库,如何连接。
// 系统初始化时,将数据库连接对象存储到内存中,当用户需要访问数据库的时候,
// 并不是去重新建立一个连接,而是去连接池中取出一个已经建立好的空闲连接。


    /**
     * Logs on to all existing datasource  登录到现有的数据源(连接池和管理连接池)
     * parameter:参数
     * @param clientDoc The reportClientDocument representing (表示) the report being used 正在使用的表
     * @param username    The DB logon user name    数据库登录用户名
     * @param password    The DB logon password     密码
     * @throws ReportSDKException
     */
    public static void logonDataSource(ReportClientDocument clientDoc,  //logonDataSource   登录数据源
                String username, String password) throws ReportSDKException {

        clientDoc.getDatabaseController().logon(username, password);    //正在使用的表

        System.out.println("CRJavaHelper,logonDataSource,登录到现有的数据源(连接池和管理连接池)");

    }





    /**
     * Changes the DataSource for each Table   给每个表都要改变数据源(连接池和连接池管理)
     * @param clientDoc The reportClientDocument representing the report being used
     * @param username  The DB logon user name
     * @param password  The DB logon password
     * @param connectionURL  The connection URL
     * (URL: uniform resource locator 统一资源定位符,每一个网页都有自己的名称,是唯一的,就是网页最上面的那个地址)
     * @param driverName    The driver Name     驱动名称
     * @param jndiName        The JNDI name     JNDI名称
     *(JNDI: java naming and directory interface java命名和目录接口)
     * @throws ReportSDKException
     */
    public static void changeDataSource(ReportClientDocument clientDoc,
                String username, String password, String connectionURL,
                String driverName,String jndiName) throws ReportSDKException {

        changeDataSource(clientDoc, null, null, username, password, connectionURL, driverName, jndiName);

        System.out.println("CRJavaHelper,changeDataSource,给每个表都要更改数据源(连接池和管理连接池)");
    }




    /**
     * Changes the DataSource for a specific Table   给特定的表更改数据源(连接池和连接池管理)
     * @param clientDoc The reportClientDocument representing the report being used
     * @param reportName    "" for main report 主报表, name of subreport for subreport, null for all reports
     * @param tableName        name of table to change. 要更改表的名称   null for all tables. 所有表为 null
     * @param username  The DB logon user name
     * @param password  The DB logon password
     * @param connectionURL  The connection URL
     * @param driverName    The driver Name
     * @param jndiName        The JNDI name  (java naming and directory interface java命名和目录接口)
     * @throws ReportSDKException
     */
    public static void changeDataSource(   //更改数据源
            ReportClientDocument clientDoc, //正在使用的表
                String reportName, String tableName, //主报表 、 要更改的表
                String username, String password, String connectionURL,
                String driverName,String jndiName) throws ReportSDKException {

        System.out.println("CRJavaHelper,changeDataSource,给特定的表更改数据源(连接池和管理连接池),");

        PropertyBag propertyBag = null;
        IConnectionInfo connectionInfo = null;
        ITable origTable = null;
        ITable newTable = null;




        /**
         *Declare variables to hold ConnectionInfo values. 声明用于保存ConnectionInfo值的变量
         *Below is the list of values required to switch to use a JDBC/JNDI   以下是切换到使用JDBC或者JNDI连接所需要的值。
         */

        String TRUSTED_CONNECTION = "false";
        String SERVER_TYPE = "JDBC (JNDI)";
        String USE_JDBC = "true";
        String DATABASE_DLL = "crdb_jdbc.dll";
        String JNDI_DATASOURCE_NAME = jndiName;
        String CONNECTION_URL = connectionURL;
        String DATABASE_CLASS_NAME = driverName;


   
        /**
         *  which you may want to uncomment
         *         接下来的几个参数是可选参数,你可能需要取消注释
         *          You may wish to adjust the arguments of the method to pass these values in if necessary
         *          如果有必要,你可能希望调整方法的自变量以去传递这些值
         *
         *          String TABLE_NAME_QUALIFIER = "new_table_name";
         *          String SERVER_NAME = "new_server_name";
         *          String CONNECTION_STRING = "new_connection_string";
         *          String DATABASE_NAME = "new_database_name";
         *          String URI = "new_URI";
         *
         *          Declare variables to hold database User Name and Password values
         *          声明用于保存数据库用户和密码的变量
         */

        String DB_USER_NAME = username;
        String DB_PASSWORD = password;



        // Obtain collection of tables from this database controller. 从这个数据库控制器中获取表的集合
        if (reportName == null || reportName.equals("")) {
            Tables tables = clientDoc.getDatabaseController().getDatabase().getTables();
            for(int i = 0;i < tables.size();i++){
                origTable = tables.getTable(i);
                if (tableName == null || origTable.getName().equals(tableName)) {
                    newTable = (ITable)origTable.clone(true);

                    // We set the Fully qualified name to the Table Alias to keep the method generic. 我们将完全限定名称设置为表别名,以保持方法的通用性。
                    // This workflow may not work in all scenarios and should likely be customized to work in the developer's specific situation.此工作流程可能不适用于所有场景,应该根据开发人员的具体情况进行自定义。
                    // The end result of this statement will be to strip the existing table of it's db specific identifiers.该语句的最终结果是从现有表中删除特定于数据库的标识符。
                    // For example Xtreme.dbo.Customer becomes just Customer
                    newTable.setQualifiedName(origTable.getAlias());



                    // Change properties that are different from the original datasource.更改那些与原始数据源不同的属性
                    // For example, if the table name has changed you will be required to change it during this routine table.setQualifiedName(TABLE_NAME_QUALIFIER);
                    //例如,如果表明已更改,则需要在此例程表中更改它。
                    // Change connection information properties.更改连接信息属性
                    connectionInfo = newTable.getConnectionInfo();


                    // Set new table connection property attributes. 设置新表连接属性
                    propertyBag = new PropertyBag();


                    // Overwrite any existing properties with updated values.用更新过后的值来覆盖现有的任何属性
                    propertyBag.put("Trusted_Connection", TRUSTED_CONNECTION);
                    propertyBag.put("Server Type", SERVER_TYPE);
                    propertyBag.put("Use JDBC", USE_JDBC);
                    propertyBag.put("Database DLL",DATABASE_DLL );
                    propertyBag.put("JNDI Datasource Name",JNDI_DATASOURCE_NAME );
                    propertyBag.put("Connection URL", CONNECTION_URL);
                    propertyBag.put("Database Class Name", DATABASE_CLASS_NAME);
                    // propertyBag.put("Server Name", SERVER_NAME); //Optional property
                    // propertyBag.put("Connection String", CONNECTION_STRING); //Optional property
                    // propertyBag.put("Database Name", DATABASE_NAME); //Optional property
                    // propertyBag.put("URI", URI); //Optional property
                    connectionInfo.setAttributes(propertyBag);



                    // Set database username and password 设置数据库名字和密码
                    // NOTE: Even if the username and password properties do not change when switching databases,切记:即使在切换数据库时用户名和密码没有更改,
                    // the database password is *not* saved in the report and must be set at runtime if the database is secured.
                    // 如果数据库是安全的,密码也不能保存在报告中,且必须在运行时设置。
                    connectionInfo.setUserName(DB_USER_NAME);
                    connectionInfo.setPassword(DB_PASSWORD);

                    // Update the table information 更新表的信息
                    clientDoc.getDatabaseController().setTableLocation(origTable, newTable);
                }
            }
        }

        // Next loop through all the subreports and pass in the same information. 接下来循环所有子报表并且传递相同的信息。
        // You may consider creating a separate method which accepts.你可以考虑创建一个单独的的方法来
        if (reportName == null || !(reportName.equals(""))) {
            IStrings subNames = clientDoc.getSubreportController().getSubreportNames();
            for (int subNum=0;subNum -1) {
                os.write(data);
            }
        } finally {
            if (is != null) {
                is.close();
            }
        }

    }
    
    /**
     * Prints to the server printer.打印到服务器打印机。
     * 
     * @param clientDoc        The reportClientDocument representing the report being used
     * @param printerName    Name of printer used to print the report
     * @throws ReportSDKException 
     */
    public static void printToServer(ReportClientDocument clientDoc,String printerName)throws ReportSDKException {

        System.out.println("printToServer,Prints to the server printer.打印到服务器打印机。");

        PrintReportOptions printOptions = new PrintReportOptions();
        // Note: Printer with the  below must already be
        // configured.
        printOptions.setPrinterName(printerName);
        printOptions.setJobTitle("Sample Print Job from Crystal Reports.");
        printOptions.setPrinterDuplex(PrinterDuplex.useDefault);
        printOptions.setPaperSource(PaperSource.auto);
        printOptions.setPaperSize(PaperSize.paperLetter);
        printOptions.setNumberOfCopies(1);
        printOptions.setCollated(false);

        // Print report
        clientDoc.getPrintOutputController().printReport(printOptions);
    }
    
    /**
     * Prints a range of pages to the server printer.将一系列页面打印到服务器打印机。
     * 
     * @param clientDoc        The reportClientDocument representing the report being used
     * @param printerName    Name of printer used to print the report
     * @param startPage        Starting page
     * @param endPage        Ending page.
     * @throws ReportSDKException 
     */
    public static void printToServer(ReportClientDocument clientDoc,String printerName,int startPage, int endPage)throws ReportSDKException {

        System.out.println("printToServer,Prints a range of pages to the server printer.将一系列页面打印到服务器打印机。");

        PrintReportOptions printOptions = new PrintReportOptions();
        // Note: Printer with the  below must already be
        // configured.
        printOptions.setPrinterName(printerName);
        printOptions.setJobTitle("Sample Print Job from Crystal Reports.");
        printOptions.setPrinterDuplex(PrinterDuplex.useDefault);
        printOptions.setPaperSource(PaperSource.auto);
        printOptions.setPaperSize(PaperSize.paperLetter);
        printOptions.setNumberOfCopies(1);
        printOptions.setCollated(false);
        PrintReportOptions.PageRange printPageRange = new PrintReportOptions.PageRange(startPage,endPage);
        printOptions.addPrinterPageRange(printPageRange);

        // Print report
        clientDoc.getPrintOutputController().printReport(printOptions);
    }

}

你可能感兴趣的:(开发语言,java)