Wednesday, January 30, 2013

Crystal Reports: Suppress a subreport, if it has no data

 

A. Introduction

In this article, I would like to explain how to hide / suppress a subreport, if it contains no data. This could be the case when you would like to avoid scenarios like having the header of a subreport displayed announcing some details to follow, but then there is NOTHING there to show. Or another inconvenient output is – for instance - having the subreport reserving a certain space on the main report's sheet that is simply BLANK.

Well, don’t worry… The fix is very quick and simple :)
The example used in this article is based on a simple database table called BRANCHES consisting of three columns, namely Branch ID, Branch Name and Branch Address. Our subreport just displays the content of that table.

So, let us see what happens, when the DB table gets empty? How would the subreport as well as the main report (the parent) behave?

 

B. Database preparations

Create the example database table and fill it with data using the following script:

create table BRANCHES(
Branch_ID varchar(6) not null,
Branch_Name varchar(20) not null,
Branch_Address varchar(100) not null,
constraint pk_branches primary key (Branch_ID)
);

insert into BRANCHES values
('1111', 'Sydney', 'xxx - yyy, Sydney'),
('2222', 'Perth', 'aaa - bbb, Perth'),
('3333', 'Canberra', 'kkk - lll, Canberra');

 

C. Report Creation

  1. Create a new blank report and add 2 new Report Header sections.
  2. In Report Header a, add a Text Object and write any text that marks that you are BEFORE the subreport section.
  3. In Report Header b, add a Subreport and call it – for instance – Branches.
  4. In Report Header c, add a Text Object and write any text that marks that you are AFTER the subreport section.

    pic_01
  5. Now double-click the Branches subreport and edit it by writing any text in the Report Header section and adding the 3 columns of the Branches table to the Details section.

    pic_02
  6. The report is ready for testing. Click the Preview button and you should see the data of the three branches.

    pic_03
  7. So far so good. Now delete all the data of the Branches table from the database using this command: delete * from Branches;
  8. And now regenerate the report’s Preview. Can you see the problem?

    pic_04

 

D. Let’s fix it

  1. Double-click the Branches subreport.
  2. Go to File > Report Options.
  3. Select the option Suppress Printing if No Records.

    pic_05
  4. Now preview the report. As there are no records to display, the subreport has suppressed itself. BUT the main report is still reserving a space for the subreport.

    pic_06
  5. Right-click the subreport object and select the command Format Subreport.
  6. Go to the Subreport tab and select the option Suppress Blank Subreport.

    pic_07
  7. Now preview the report. The main report realizes that the subreport is empty and therefore it suppresses the whole subreport object. BUT still the section containing the subreport is showing on the screen!!!

    pic_08
  8. Go to Report > Section Expert.
  9. Select the section containing the subreport (in our example, it is Report Header b) and select the option Suppress Blank Section.

    pic_09
  10. Now preview the report. Finally all the traces of the subreport has been removed Smile

    pic_10

Thursday, July 21, 2011

Crystal Reports for Eclipse: How to set the Record Sorting at runtime?

A. Introduction

In this article, I would like to explain how to sort the data in a report based on the value of a parameter.
The example used in this article is based on a simple database table containing three columns, namely Product ID, Product Name and Product Description. A Command Table representing the data of this DB table will be added to a new report. And finally, it is required to define a parameter for choosing the sorting column at runtime.

If you are new to Crystal Reports for Eclipse, I recommend that you watch the following QuickStart demos:
Crystal Reports for Eclipse 2.0 - Command Table Part 1: Adding SQL to a Report
http://www.sdn.sap.com/irj/scn/elearn?rid=/library/uuid/00b07182-3ed3-2b10-6695-fb255b4eae01

Crystal Reports for Eclipse 2.0 - Command Table Part 2: Updating Command Table SQL
http://www.sdn.sap.com/irj/scn/elearn?rid=/library/uuid/d027948f-3fd3-2b10-e582-f19c3946490f

Crystal Reports for Eclipse 2.0 - Designing a Report
http://www.sdn.sap.com/irj/scn/elearn?rid=/library/uuid/d0ee1df6-41d3-2b10-de8d-a4fdc5077c68


B. Database preparations

Create the example database table and fill it with data using the following script:

CREATE TABLE REPORT_PARAM_FOR_SORTING(
Product_ID varchar(6) not null,
Product_Name varchar(20) not null,
Product_Description varchar(100) not null,
constraint pk_report primary key (Product_ID)
);

insert into REPORT_PARAM_FOR_SORTING values
('F50Z13', 'Optical mouse', 'This is an optical mouse.'),
('A04K02', 'Keyboard', 'This is a wireless keyboard.'),
('A00B18', 'Speakers', 'Perfect speakers.');



C. Report Creation
  1. Open Eclipse.
  2. Browse your Crystal Reports project.
  3. Create a new SQL file called "sqlReportParamForSorting".
  4. Open the SQL file and write the following SELECT query:
    select Product_ID, Product_Name, Product_Description
    from REPORT_PARAM_FOR_SORTING
  5. Right-click inside the SQL file and click Crystal Reports > Add to a New Report.
  6. Set the location and name for the new report and click OK.
  7. Open the newly created report.
  8. In the Crystal Reports perspective, open the Field Explorer view.
  9. Expand the command table, in order to see the selected DB fields.
  10. While the Layout page is open, drag and drop the three fields in the Body section of the report.
  11. Switch to the Preview page and check that you can see all three rows from the example DB table properly.


D. Sorting based on a parameter
  1. In the Field Explorer view, add a new parameter called "param_SortingColumn".
  2. In the Properties view for the new parameter, set the List of Values as shown in the following figure:


  3. In the Field Explorer view, add a new formula called "sort_Column".
  4. On the Formula page, write the following formula for "sort_Column":
    if ({?param_SortingColumn} = 'Name') then
    {sqlReportParamForSorting.PRODUCT_NAME}
    else
    {sqlReportParamForSorting.PRODUCT_ID}
  5. From the Crystal Reports menu, click Record Sorting.


  6. Select the formula "sort_Column" and click OK.


  7. Save the report.


E. Testing
  1. Switch to the Preview page.
  2. You would be prompted to select a sorting column.
  3. Select "Product Name" and the result should be as shown in the following figure:


  4. Refresh the report and select "Prompt for parameters before refresh".
  5. Select "Product ID" and the result should be as shown in the following figure:


Sunday, July 10, 2011

Java Tips: Why a PRIVATE constructor?

A. Introduction:
As a private constructor doesn't allow anyone outside the class to instantiate it, what is the use of that "isolated" class then?

Well, setting the access modifier of a constructor to private is a design decision to enforce noninstantiability of a class and therefore, prevent the misuse of an object's creation by the outside world.

If you don't define any constructors for your class, Java implicitly defines a default constructor, which is public.

Two common cases make use of defining a constructor as PRIVATE:
- The instantiation of a class is forbidden
- The instantiation of a class is done only via a dedicated manager / factory


B. Instantiation of a class is forbidden
In case the instantiation of a class is forbidden, it is recommended to make the private constructur throw an exception. This way, you make sure that even methods within the same class cannot call the constructor.

class DangerousClass {

private DangerousClass() {
throw new AssertionError();
}

}


Examples for this use case:
Utility classes having only static methods.
Click here for an interesting article about the Laws of Utility Classes.

Contsant classes holding only static constants.
Click here for more details about constant classes.


C. Instantiation of a class is done only via a dedicated manager / factory
In this case a public static method inside the class takes over the responsibility of creating the objects. This design practice is needed when certain criteria have to be checked before constructing the object, e.g. input validation, number of existing instances....etc

Examples for this use case:
Enum Types:

The constructor for an enum type must have package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

For more details about enum types, please refer to the following articles
Enum Types: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
Type-Safe Enumerations: http://www.javapractices.com/topic/TopicAction.do?Id=1


Singleton design pattern:
The Singleton design pattern ensures that a class has only one instance and provides a global point of access to that instance.
Click here for more details about the Singleton Design Pattern.

I also recommended reading this article "When is a Singleton not a Singleton?". It presents common mistakes when working with Singletons.


D. References and further readings
Private constructor:
This article provides a very nice and organized answer to the same question handled in this article.
http://www.javapractices.com/topic/TopicAction.do?Id=40

What's the point of having a private constructor?
This article belongs to a set of salient interview questions that cover a broad range of topics in Java.
http://www.programmerinterview.com/index.php/java-questions/why-have-a-private-constructor/

Sunday, July 3, 2011

Java Tips: Implicit super constructor ParentClassName() is undefined. Must explicitly invoke another constructor

Introduction

Have you ever encountered such a compilation error and wondered why your constructor is throwing such a weird error?
Well, in order to answer this question let us first go through a brief explanation of how constructors work. Then I shall suggest two alternatives for resolving this problem and finally, I would provide references to some very useful pages, where you can further explore the constructors' issues.

What is a default constructor?

Every class has at least one constructor. If a class does not explicitly declare any constructors, the Java compiler automatically provides a no-argument constructor, called the default constructor.


How does a constructor work?

A Java constructor performs the following functions:

1. It initializes the class variables to default values.
- Numeric types (Byte, short, int, long, float, and double) default to their respective zero values
- Boolean defaults to false
- Char defaults to the null character ('\u0000')- References of any objects default to null

2. It then calls another constructor in the same class "this(...)" or a constructor in the parent class "super(...)".- If an explicit call to another constructor is provided (it must be the first statement of a constructor), then this explicit call is performed.
- Otherwise, an implicit call to the parent class' no-argument constructor super() is performed.

3. It then initializes the class variables to the specified values. (For instance, int var = 10;)

4. Finally, it executes the body of the constructor.


How to fix the compilation error?

Here is an example problem from the many scenarios that can lead to the following complier error:
Implicit super constructor Parent() is undefined. Must explicitly invoke another constructor

Problem:

class Parent {

int parentX = 10;

public Parent(int x) {
parentX = x;
}

}

class Child extends Parent {

int childX;

public Child(int x) {
childX = x;
}

}


From the previous two sections, we now should be able to understand why the compiler is complaining, namely...
- The constructor of the Child class does not contain an explicit call to another constructor.
- Thus, it inserts an implicit call to the super class' no-argument constructor, i.e. Parent().
- The parent class DOES NOT have a default no-argument constructor, because it has defined an explicit constructor, namely Parent(int x).

Solution 1:
Explicitly, declare a no-argument constructor for the Parent class.

class Parent {

int parentX = 10;

public Parent() {
// do any initialization, if necessary
}

public Parent(int x) {
parentX = x;
}

}

class Child extends Parent {

int childX;

public Child(int x) {
childX = x;
}

}


Solution 2:
Add an explicit call to another constructor in the Child class'constructor.


class Parent {

int parentX = 10;

public Parent(int x) {
parentX = x;
}

}

class Child extends Parent {

int childX;

public Child(int x) {
super(x);childX = x;
}

}


Finally,...
Well, some developers (usually beginners) think that invisible code generated by the compiler is confusing, because they don't understand what's going on behind the scenes. But eventually, "almost" every developer appreciates this hidden code generation, because -amongst others- it allows one to write less code. And these are the two faces of the coin!

References
Wikipedia: Constructor (object-oriented programming)
This wikipedia article contains a simple definition of a constructor. It also provides examples for the use of constructors in different programming and scripting languages.
http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming)

Java Constructors | Java Beginner
This article illustrates via simple examples the use of constructors, constructor overloading and constructor chaining.
http://www.javabeginner.com/learn-java/java-constructors

Java Notes: Constructors
Java Notes contains a list of very useful Java programming notes by Fred Swartz. In this note, different aspects of a class constructor are covered.
Click here to browse the main index of the Java Notes.
http://leepoint.net/notes-java/oop/constructors/constructor.html

Java Basics: Commentary: Always write super()?
Java Basics is a very nice Java tutorial by Fred Swartz. In this commentary, Fred explains the explicit and implicit calls of super().
Click here to browse the main index of the Java Basics tutorial.
http://leepoint.net/JavaBasics/oop/oop-commentary/oopcom-super.html

Tuesday, June 28, 2011

Crystal Reports for Eclipse: Where to start?

What is Crystal Reports for Eclipse?

Crystal Reports for Eclipse is an Eclipse plug-in that integrates Crystal reports into Eclipse IDE. It makes it easy to develop and access Crystal Reports in your Eclipse projects.

According to the Crystal Reports for Eclipse Developer Guide, the plugin adds the following features to Eclipse:

  • An embedded report designer for creating Crystal reports.
  • Crystal reports viewers for viewing reports in Java applications and over the web.
  • SDKs to programmatically modify reports.
  • Java runtime libraries that implement reporting functionality.
  • Eclipse integration features, including project wizards, views, and perspectives for reporting.
  • A set of sample reports, sample database connections, and helper code.



Know How material

This section acts as an indexer for referencing pages which describe how to install and work with "Crystal Reports for Eclipse". It also contains links to very good demos, webinars and articles that can be of great help for beginners to find their first steps while using this tool.

SAP Crystal Reports, version for Eclipse
This is the official Home Page for "Crystal Reports for Eclipse" on the SAP Community Network.
http://www.sdn.sap.com/irj/sdn/crystalreports-java

Crystal Reports for Eclipse e-Book
The Crystal Reports for Eclipse e-Book contains articles, videos, and information on how to create reports and embed them in custom java applications.
http://wiki.sdn.sap.com/wiki/display/BOBJ/Crystal+Reports+for+Eclipse

Crystal Reports for Eclipse 2.0 demos
This page contains QuickStart demos that cover the topics of creating Crystal Reports projects, designing reports and adding them to the projects, configuring different types of datasources inside the reports, and finally viewing the report inside a sample Java application or a sample JSP page hosted in a web application.
http://www.sdn.sap.com/irj/sdn/crystalreports-java?rid=/webcontent/uuid/30e3a9b4-a7d3-2b10-678a-9ed48f648365

Crystal Reports Java SDK Samples
This page contains a set of code samples that cover a variety of topics, e.g. General, Viewing, Database, Exporting/Printing, Parameters and Report Modification
https://wiki.sdn.sap.com/wiki/display/BOBJ/Crystal+Reports+Java++SDK+Samples

Crystal Reports for Eclipse 2.0 - Sample Codes Package
This page contains a bundle of 12 sample codes for Crystal Reports for Eclipse 2.0. The package covers the topics "Web Application Viewing and Exporting", "Report Creation/Modification" and "Designer Extension Points".
http://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/13120

Crystal Reports for Java/Eclipse Articles
This page contains some good articles about Crystal Reports for Eclipse.
http://www.sdn.sap.com/irj/boc/index?rid=/webcontent/uuid/e038f87e-f576-2b10-1e92-baf4baf5f6f5