JasperReports - How to deal with nested object collection - jasper-reports

I'm Java developer for about 2 years but I'm new with Jasper so I need some help, I know how to deal with a simple report with fields coming from a class or entity but now I have to do something harder.
I have these 3 entities:
Group
Function
Privilege
The group may have a list of function, and a function has a list oc privileges, that is the simplest scenario but a group may have a list of other groups instead of a list of function.
simple example: Group1 -> (Function1 [Privilege1, Privilege2], Function2 [Privilege3, Privilege4] - Group1 got a list with 2 functions each one with 2 privileges.
complex example: Group2 -> (Group1 -> [Function1 {Privilege1, Privilege2}, Function2 {Privilege3, Privilege4}], Group3 -> [FunctionX {Privilege1, Privilege2}, FunctionY... - Group 2 got a list with 2 sub-groups, each sub-group got functions and so on.
To build the report I'm used to send to Jasper just one object, in this case the parent group so I believe I need a way to iterate through its lists but I don't know what is the best approach.
I made some research about groups and sub reports but I couldn't find any situation like that and I can't figure out what is the best for what I need to do.
Some help would be appreciated!

I'm create jasper report as pass one single object as parameter or field.
assume one file structure
com.project.MyClass, we pass object of MyClass
this object is passed as parameter or as datasource whatever you want.
assume myClass is object
if you pass one single object than you must change expression class java.lang.String to com.project.MyClass in jasper.
now you can access your object as $F{myClass }.getMethodName()
you can also use use $F{myClass }.getMethodName().getSubmethodName()
this is 100% working. I use it for my all reports. If you want any help then ask.

Related

Crystal Reports 2016: using "startswith" function for multiple parameters

Per my supervisor's request, I have to modify a report in a way that two parameters that allows multiple selections have to be moved to the select expert section. Since the parameters are allowing the multiple selections, I am trying to use the "startswith" function like this.
({Command.incident_type} startswith {?IncidentType})
and
({Command.property_use} startswith {?PropertyUse})
I've used the startswith function before, but not for more than one parameter. The report is showing data, but it's not showing all the data. It is omitting one or two fields and I am not sure why?
You can try something as follows. I have not tested it so not sure if it will work but I would possibly do it this way for a similar request.
{Command.incident_type} like "{?IncidentType}*" and
{Command.property_use} like "{?PropertyUse}*"
Example

How to make my subreport access a different database than the main one?

I am evaluating JasperReports and Jaspersoft Studio for using in the company I work for and I am trying to build a report that contains information from 2 different databases.
I have 2 data adapters configured.
I've my research and found out that the only way to do so is to have either a list, table or subreport in a main report, but I still can't make it work.
I took the subreport approach, I have two reports that work separately, the main one executes this query in the sample DB
select 1 from ORDERS limit 10
The subreport runs this one in a Vertica database I own:
select 1 from my_schema.my_table limit 10
Both of them, as said, run separately.
So I go to the mainreport and add a SubReport element to the ColumnHeader band, then the wizard opens:
In the step 1 I select the subreport from the project
Second step is connection. I have these options:
Same JDBC conection
Use another connection
Use an empty data source
Use a JRDatasourceExpression
Don't use any connection.
The first and third ones are not what I want.
I tried to set the second one, but I could not find a way to select the data adapter that I have configured.
The last one doesn't work ( I get a message saying that my_schema doesn't exists, so I think that it is still trying to access the MainReport database).
Is there anyway I can make a subreport run a query in a different database/datasource from the main report?
I can summarize what I did if anyone has the same issue:
I made a proof of concept (therefore I do not have the code anymore), but I created a JRDataSource class using this tutorial and there I manually access the database and returned the rows. Then I use this as the datasource of my subreport/table.
Not as nice as I wanted, but it is possible
I guess this iReport article is also applicable to reports designed in jaspersoft studio; you just need to define the subreport connection parameter with the given expression.
Perhaps, you can ask for help from two parameters which defined in the master report by yourself. Such as:
$P{MySubreport} with type net.sf.jasperreport.engine.JasperReport;
$P{MyDatasource} with type net.sf.jasperreport.engine.JRDataSource;
Associate these two parameters to your subreport element respectively in attributes 'Expression' and 'Data Source Expression';
Please write your DataSource class that implements interface JRDataSource.
Then, in your servlet class, put your real parameter values(JasperReport for your subreport .jasper file, and your DataSource object) into a parameters Map object, and call JasperFillManager.fillReport().

What is the purpose of the <group> tag in jrxml files

can anyone explain in very simple words what the <group> tag stands for. I already read this: enter link description here
But I can't get it, I played with reports over three hours but can't get any results.
__
I have a csv datasource looking like that:
Name | Value
-------------
Ab | 123
Ba | wow
Cb | got
De | it
For instance I would like to get only the entry in the value column where the entry in the Name column is "Cb" (would be "got").
Hence, I created a group with: following expression: $F{Name}.compareToIgnoreCase("Cb")
Unfortunately I always get the entire column although I only want one entry.
Do I use the group tag in the right way? Maybe I have misunderstood the intended purpose of this tag.
Any help would be appreciated.
2014-March-22:
I made a simple example PastBin:minimalcode-jrxml-file using following csv file as datasource:
Name;Tom
Birthday;01-May-1980
Country;Germany
sex;male
Here is a screenshot of the design view (ireports 5.5.0):
And here is what I get when I click on preview (in ireports), unfortunately this is not what i want:
Maybe anyone can help.
Thanks
The purpose of this tag is to "group/unite data based on a specific column value. Let's take for example the classic HR schema. Every Employee is linked with a specific Department. While getting all the employees inside a report, you can "divide" them in groups based on the department on which they are connected (i.e: by creating a group with for the employee.department_id attribute). A simple usage might be creating different tables with employees records for every department. Basically, is the same function as the Group clause in SQL (only that Jasper is aware of these groups and allows you to customize them).
In your case, i'm not quite sure that the expression used is creating a jasper-group. Anyway, what you're trying to do (i mean, getting only that record) can be easily accomplished by a "WHERE" clause inside the report query: WHERE name='Cb'
You don't need group use filter expression:
<filterExpression><![CDATA[$F{Name}.equalsIgnoreCase("Cb")]]></filterExpression>

Dynamically change parameters based on the value of another parameter?

I have a crystal 2008 report that allows users to group to two levels.
I have two parameters, using which users can select which attribute they want to group on.
The two params are of type string and the list of values is static. Both levels have the same options to group on.
Is there someway to filter the list based on what they selected for param 1. So that they cannot group on the same thing twice.
I don't think it's possible. You can cascade dynamic parameters link Country, State but not how you want to.
How many options do they have to choose from? Do they normally choose anything other than a few different combinations from the maximum?
I think you're options are to either leave it as is (allowing them to pick the same thing for both groups). Or create a single parameters which looks like- Group by:
Date, Country
Type, Country
Date, Type
etc
Short answer: not possible.
Options:
create a custom UI that does what you want to do
if the list of grouping fields aren't too long, you could combine the two parameters into a single one, then list the options that you want to support. For example, if you have 3 grouping fields (A,B,C), your single, combined parameter would be A->B, B->A, B->C, C->B, A->C, C->A.

Crystal Reports formula method, GroupName

I am fixing some old reports and have come across the method GroupName. I cannot find exactly what it does and am coming for support. For that matter, is there an online reference that contains a list of the built it methods in CR, parameters, etc.?
GroupName just returns a string of... well, the current Group Name. This is either just the field you were grouping on, or is a value customized via the Group Expert -> Options (Change Group Options) -> "Customize Group Name Field". It's whatever displays in the group tree.
If it's not listed in the Crystal Reports Online Help, which is oddly the case with GroupName, then your best bet is some good, old-fashioned Googling. I'm not sure of any resource other than commercial products that lists descriptions and usage instructions for all the functions, properties, etc.
EDIT: You have to pass the field you're grouping on. So, for a random example, GroupName(Patient.Patient_Type_Code) might return strings "Therapy", "Rehab", etc. from a numeric type code you're grouping on.