Common Viewer Properties


You can control various features of a viewer from within your JSP application by calling the appropriate method. Some of the commonly used features are covered in this section.

Printing and exporting are two features that users may frequently request from a JSP application that displays a Crystal report. The print feature gives the user the capability to print the report, and the export feature enables the user to export the report into either an RTF or PDF file (as covered earlier in this chapter).

The print feature is enabled or disabled by calling the setHasPrintButton() method and passing it either a Boolean true or false value, depending on whether you want to enable or disable printing. You ll also need to call the setPrintMode() method and pass it a constant that indicates the printing mode. The setPrintMode() method can be passed the constant CrPrintMode .ACTIVEX to associate the Print button with the new Crystal Reports 10 ActiveX print plug-in. If ACTIVEX is specified, then the ActiveX plug-in will be downloaded without having to display the report in the PDF format before printing. If ACTIVEX is not specified, then the report is displayed in Adobe PDF format. The user then uses the Print option in the Acrobat Reader to print the report.

Here are the statements that you ll need to enable printing:

 viewer.setHasPrintButton(true); 
viewer.setPrintMode(CrPrintMode.ACTIVEX);

Enabling and disabling the export feature is similar to printing; it is accomplished by calling the setHasExportButton() and passing it either a Boolean true or false value, as shown here:

 viewer.setHasExportButton(true); 

If you plan to enable printing and/or exporting, then you ll also need to set ownership of the web page and the form used to display the report. You do this by passing a Boolean value of true to the following methods . The owner provides the opening and closing HTML tags and can retrieve and set values for the entire page.

 viewer.setOwnPage(true); 
viewer.setOwnForm(true);

The Refresh button is another feature that you may want to provide. If a viewer clicks the Refresh button, the report will be refreshed to present a current set of data from the database. The setHasRefreshButton() method is used for the Refresh button. Passing the setHasRefreshButton() method a Boolean value of true causes the Refresh button to appear in the viewer. Passing it a false value disables the Refresh button.

If you enable the Refresh button, then you should be prepared to deal with information the report may require, such as database credentials or report parameter fields. Depending on how the report is designed, the viewer may be required to supply these values before the report is re-displayed. You may prefer to set these values in your JSP application, which you ll learn about later in this section. Call the setHasRefreshButton() method to enable or disable sending parameter values each time the report is refreshed, as shown here:

 viewer.setHasRefreshButton(true); 
viewer.setReuseParameterValuesOnRefresh(true);

Drill-down is another feature that you may need to control. For example, there may be times you do want users to have the ability to drill down, and other times you don t want this capability to be available. Typically, you ll write Java code to determine the security level of the current user and then set the drill-down access to the report by calling the setEnableDrillDown() method, passing it either a Boolean true or false value to enable or disable this feature, as shown here:

 viewer.setEnableDrillDown(true); 

Parameter Fields

Parameter fields are special fields added to a Crystal report that prompt the end user to input a value. Generally these values will be used in a selection formula to limit the result set returned to the report (parameter fields are covered in detail in Chapter 14). Parameter fields can take several types of values, including discrete values and range values. Discrete values consist of one value, whereas a range value will have a beginning value and an ending value. This example demonstrates how to pass a discrete value.

In order to use parameter fields in your JSP application, first create an instance of the Fields class. The Fields class is a collection of fields that will ultimately be passed to the report viewer. Here s how to create the Fields object:

 Fields fields = new Fields(); 

With the Fields object created, you need to create parameter fields and values. Do this by declaring an instance of the ParameterField class and one of the ParameterFieldDiscreteValue class (if the source report uses a single-value parameter field). The ParameterField class has methods used to manage a parameter field, and the ParameterFieldDiscreteValue class has methods to manage the value of a parameter field. Here s how to create these objects:

 ParameterField field = new ParameterField(); 
ParameterFieldDiscreteValue value = new ParameterFieldDiscreteValue();

You must associate each parameter field with a parameter in the Crystal report and with the report itself. This is accomplished by calling the setName() method and passing it the name of the parameter in the report. The parameter field is associated with a report by calling the setReportName() method, which requires one argument that identifies the report. If the parameter field is associated with the main report, then you need only pass a pair of double quotations as the argument, as is shown in the second statement here. If the parameter field is associated with a subreport, then pass the name of the subreport as the argument.

 field.setName("Region"); 
field.setReportName("");

Next, you need to set the value of the parameter field by calling the setValue() method. The setValue() method requires one argument, which is the value that will be assigned to the parameter field. You then must add the value to the parameter field by calling the add() method and the getCurrentValues() method. These are shown here:

 value.setValue("CA"); 
field.getCurrentValues().add(value);

After associating a value with a parameter field, you need to include the parameter field in the Fields collection by calling the add() method, as illustrated here:

 fields.add(field); 

The final step is to associate the Fields collection with the report viewer by calling the setParameterFields() method and passing it the Fields collection, as shown here:

 viewer.setParameterFields(fields); 

Database Login

One of the most common routines that you ll perform is to have your JSP application log into a database used by a Crystal report. For this, you ll need to make use of the Connection Info class.

Begin by creating two objects. The first object is an instance of the ConnectionInfos class. The ConnectionInfos class is similar to the Fields class that you learned about in the preceding section of this chapter in that each is a collection. The Fields class is a collection of Field objects, and the ConnectionInfos class is a collection of ConnectionInfo objects (one for each table in a report). Notice that the only difference in the names of these classes is the letter s at the end of the name, so try not to confuse them. Here s how to create these objects:

 ConnectionInfos connInfos = new ConnectionInfos(); 
ConnectionInfo connInfo = new ConnectionInfo();

Next, you need to set the user name and password for the connection by calling the setUserName() method and the setPassword() method. The ConnectionInfo object is then added to the ConnectionInfos object, as shown here:

 connInfo.setUserName("user"); 
connInfo.setPassword("password");
connInfos.add(connInfo);

The ConnectionInfos object is then associated with the report viewer by calling the setDatabaseLogonInfos() method, as illustrated here:

 viewer.setDatabaseLogonInfos(connInfos); 

The following JSP application reiterates all the features of the Crystal report that were introduced in this section:

 <%@ page import="com.crystaldecisions.report.web.viewer.CrystalReportViewer, 
com.crystaldecisions.report.web.viewer.CrPrintMode,
com.crystaldecisions.sdk.occa.report.data.*"%>
<%
CrystalReportViewer viewer = new CrystalReportViewer();
viewer.setName("Crystal Report Viewer");
viewer.setOwnPage(true);
viewer.setOwnForm(true);
viewer.setHasExportButton(true);
viewer.setHasPrintButton(true);
viewer.setPrintMode(CrPrintMode.ACTIVEX);
viewer.setEnableDrillDown(true);
viewer.setHasRefreshButton(true);
viewer.setReuseParameterValuesOnRefresh(true);
viewer.setViewTimeSelectionFormula("{Customer.Country} = \"USA\"");
Fields fields = new Fields();
ParameterField field = new ParameterField();
ParameterFieldDiscreteValue value = new ParameterFieldDiscreteValue();
field.setName("Region");
field.setReportName("");
value.setValue("CA");
field.getCurrentValues().add(value);
fields.add(field);
viewer.setParameterFields(fields);
ConnectionInfos connInfos = new ConnectionInfos();
ConnectionInfo connInfo = new ConnectionInfo();
connInfo.setUserName("user");
connInfo.setPassword("password");
connInfos.add(connInfo);
viewer.setDatabaseLogonInfos(connInfos);
viewer.setReportSource(reportSource);
viewer.processHttpRequest(request, response, getServletConfig().
getServletContext(),null);
%>
Note  

You ll notice use of the setViewTimeSelectionFormula method in the preceding code. This method allows the report s record selection to be changed at view time by the report viewer. Pass a valid Crystal Reports record selection formula (discussed in Chapter 8) to this method.




Crystal Reports 10
Crystal Reports 10: The Complete Reference
ISBN: B005DI80VA
EAN: N/A
Year: 2004
Pages: 223
Authors: George Peck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net