我工作的是同樣的要求。我正在開發一個工具,通過使用swing和BO SDK來更新批量Crystal報表連接。
下面是我將用於更新連接的虛擬代碼。
例子: -
package org.bukhantsov.javatool;
import com.businessobjects.rebean.wi.DocumentInstance;
import com.crystaldecisions.sdk.exception.SDKException;
import com.crystaldecisions.sdk.framework.CrystalEnterprise;
import com.crystaldecisions.sdk.framework.IEnterpriseSession;
import com.crystaldecisions.sdk.framework.ISessionMgr;
import com.crystaldecisions.sdk.occa.infostore.IInfoObject;
import com.crystaldecisions.sdk.occa.infostore.IInfoObjects;
import com.crystaldecisions.sdk.occa.infostore.IInfoStore;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.managedreports.reportAppFactory;
import com.crystaldecisions.sdk.occa.report.application.databaseController;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.document.*;
import com.crystaldecisions.sdk.occa.report.definition.*;
import com.crystaldecisions.sdk.occa.report.lib.propertyBag;
class ChangeDataSource {
public static void main(String args[]){
String cms = "134.X.X.X:6400";
String username = "Administrator";
String password = "";
String auth = "secEnterprise";
IEnterpriseSession enterpriseSession = null;
ISessionMgr sessionMgr = null;//CrystalEnterprise.getSessionMgr();
Exception failure = null;
boolean loggedIn = true;
ReportClientDocument clientDoc = null;
if (enterpriseSession == null)
{
try
{
sessionMgr = CrystalEnterprise.getSessionMgr();
enterpriseSession = sessionMgr.logon(username, password, cms, auth);
System.out.println("\nLOGIN SUCCESSFUL\n");
}
catch (Exception error)
{
loggedIn = false;
failure = error;
}
if (!loggedIn)
{
System.out.println("\nLOGIN FAILED\n");
}
else
{
// Query for the sample report from the Enterprise CMS.
try {
IInfoStore iStore = (IInfoStore) enterpriseSession.getService("InfoStore");
IInfoObjects infoObjects = iStore.query("Select SI_ID From CI_INFOOBJECTS Where SI_Type='CrystalReport'");
System.out.println("\ninfoObjects size = "+infoObjects.getResultSize());
IReportAppFactory reportAppFactory = (IReportAppFactory)enterpriseSession.getService("RASReportFactory");
IInfoObject infoObject = (IInfoObject)infoObjects.get(0);
/* if (infoObjects != null) {
Iterator lReportListIterator = lReportList.iterator();
while (lReportListIterator.hasNext()) {
IInfoObject infoObject = (IInfoObject) lReportListIterator.next();*/
clientDoc = new ReportClientDocument();
clientDoc = reportAppFactory.openDocument(infoObject,0, java.util.Locale.US);
System.out.println("Report "+ infoObject.getTitle() +" Opened");
switch_tables(clientDoc.getDatabaseController());
}catch(ReportSDKException re){
re.printStackTrace();
}catch(SDKException re){
re.printStackTrace();
}
}
}
}
private static void switch_tables(DatabaseController databaseController) throws ReportSDKException {
//Report can be opened from the relative location specified in the CRConfig.xml, or the report location
//tag can be removed to open the reports as Java resources or using an absolute path (absolute path not recommended
//for Web applications).
final String REPORT_NAME = "change_db_location/simplereport.rpt";
final String DBUSERNAME = "vantech";
final String DBPASSWORD = "vantech";
final String CONNECTION_STRING = "!com.microsoft.jdbc.sqlserver.SQLServerDriver!jdbc:microsoft:sqlserver://10.50.212.103:1433;DatabaseName={database};user={userid};password={password}";
final String TRUSTEDCON = "false";
final String PREQESERVERNAME = "jdbc:microsoft:sqlserver://10.50.212.103:1433";
final String SERVERTYPE = "JDBC (JNDI)";
final String DATABASE_DLL = "crdb_jdbc.dll";
final String DATABASE = "Xtreme";
final String DBCLASSNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
final String USEJDBC = "true";
final String DATABASE_NAME = "Xtreme";
final String SERVERNAME = "jdbc:microsoft:sqlserver://10.50.212.103:1433";
final String CONNECTIONURL = "jdbc:microsoft:sqlserver://10.50.212.103:1433";
final String SERVER = "10.50.212.103";
//Open report.
ReportClientDocument boReportClientDocument = new ReportClientDocument();
boReportClientDocument.open(REPORT_NAME, 0);
// Set DB Username and Password
boReportClientDocument.getDatabaseController().logon(DBUSERNAME, DBPASSWORD);
// Create the two connectioninfo objects to use
IConnectionInfo oldConnectionInfo = new ConnectionInfo();
IConnectionInfo newConnectionInfo = new ConnectionInfo();
// Assign the old Connection info to the reports current info
DatabaseController dbController = boReportClientDocument.getDatabaseController();
oldConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);
// If this connection needed parameters, we would use this field.
Fields pFields = null;
// Create a new propertybag for the new location
PropertyBag boPropertyBag1 = new PropertyBag();
// Set new table logon properties
boPropertyBag1.put("JDBC Connection String", CONNECTION_STRING);
boPropertyBag1.put("Trusted_Connection", TRUSTEDCON);
boPropertyBag1.put("PreQEServerName", PREQESERVERNAME);
boPropertyBag1.put("Server Type", SERVERTYPE);
boPropertyBag1.put("Database DLL", DATABASE_DLL);
boPropertyBag1.put("Database", DATABASE);
boPropertyBag1.put("Database Class Name", DBCLASSNAME);
boPropertyBag1.put("Use JDBC", USEJDBC);
boPropertyBag1.put("Database Name", DATABASE_NAME);
boPropertyBag1.put("Server Name", SERVERNAME);
boPropertyBag1.put("Connection URL", CONNECTIONURL);
boPropertyBag1.put("Server", SERVER);
// Assign the properties to the connection info
newConnectionInfo.setAttributes(boPropertyBag1);
// Set the DB Username and Pwd
newConnectionInfo.setUserName(DBUSERNAME);
newConnectionInfo.setPassword(DBPASSWORD);
// The Kind of connectionInfos is SQL
newConnectionInfo.setKind(ConnectionInfoKind.SQL);
// set the parameters to replace.
// The 4 options are:
// _doNotVerifyDB
// _ignoreCurrentTableQualifiers
// _mapFieldByRowsetPosition
// _useDefault
int replaceParams = DBOptions._ignoreCurrentTableQualifiers + DBOptions._doNotVerifyDB;
// Now replace the connections
dbController.replaceConnection(oldConnectionInfo, newConnectionInfo, pFields, replaceParams);
//Store the report source in session, will be used by the CrystalReportViewer.
//session.setAttribute("reportSource", boReportClientDocument.getReportSource());
//Launch CrystalReportViewer page that contains the report viewer.
//response.sendRedirect("CrystalReportViewer.jsp");
}
}
希望這會幫助你。
謝謝, Sachin