List into other List jasper report - java

I have this object structure, to use like Java Bean in mi jasper report,
public class Person{
private String name;
private String lastname;
private List<Job> jobs;
}
public class Job{
private String jobName;
private String companyName;
private List<Reponsability> responsabilities;
}
All class with corresponding setters and getters
When I create a list into jasper report I define the "job list" JRDataSource expression this way:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{jobs})
Now I want to create another list, "responsibilities list" on the "job list", the question is:
How can I define the data source to get the corresponding job to pass into the JRDataSource Expression?
I imagine something like this:
new JRBeanCollectionDataSource(${jobs}.get($index).getResponsabilities())
but I can't get it work this

I found the solution to do this:
Into the list, I created a variable called jobInstance
<field name="jobInstance" class="com.mypackage.example.Job">
<fieldDescription><![CDATA[_THIS]]></fieldDescription>
</field>
Then when I set the datasource for responsibilities list did this:
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{jobInstance}.getResponsabilities())
References: https://community.jaspersoft.com/questions/508346/getting-current-object

Related

How to map a List<ObjectDTO> to Entity inside of a Bean with Model Mapper?

I have a 1:N relationship where a Victim might have lots of EmergencyContacts.
I've created a DTO called VictimDTO and inside of it there's a List and I'm using ModelMapper to convert my DTO into an Entity.
When I get rid of the list just for testing purposes, it works fine placing this code snippet inside of the insert "POST" method in the Controller layer in Java:
VictimEntity victimEntity = modelMapper.map(victimDTO, VictimEntity.class);
But now I've placed this List inside of my VictimDTO and I'm getting an Exception when trying to use this code.
My DTO actual code is this one as it follows below:
public class VictimDTO {
private String name;
private int age;
private String email;
private String phone;
private List<ContactDTO> contactDTOList;
//getters and setters
}
How can I use ModelMapper to convert a POJO with a Collection (List) as an instance variable inside into an Entity?
Thank you for your help.
Best regards,
Lucas AbrĂ£o

How to create Excel report with subtable with Jasper reports

I am using Jasper reports to create Excel file programmatically. I am using Java Data Source - a java class that implements JRDataSource interface. Up until now My Datasource returned a List of class instances that looked something like this:
public Class MyDataSource implements JRDataSource {
private Integer prop1;
private String prop2;
private String prop3;
...
// getters and setters omitted to save space
}
With this Data Source I was able to create a very nice excel table that looked:
prop1-Header prop2-Header prop3-Header...
----------------------------------------
prop1-value prop2-value prop3-value...
prop1-value prop2-value prop3-value...
...
But now MyDataSource class has additional property List<String>
public Class MyDataSource implements JRDataSource {
private Integer prop1;
private String prop2;
private String prop3;
private List<String> subvalues;
...
// getters and setters omitted to save space
}
So I need my excel to look like this
prop1-Header prop2-Header prop3-Header...
----------------------------------------
prop1-value prop2-value prop3-value...
Sub-header1 Sub-header2...
-----------------------
sub-value1 sub-value2....
....
prop1-value prop2-value prop3-value...
Sub-header1 Sub-header2...
-----------------------
sub-value1 sub-value2....
....
...
I managed to do that by concatenating The list into a single string, and it looks very similar to what I need. But I have no way of sorting and filtering on sub-value data. So, I need to actually make it as sub-list or sub-table. And this is my question - how to do this?
After searching for a while I found the solution. First of all, the is a very nice youtube video
How to fill Jasper Report Table using Collection of data in Java?
This video shows in greate detail how to pass from java code into report an additional collection besides your Datasource java class. This is done by doing something like this:
JRBeanCollectionDataSource detailBean = new JRBeanCollectionDataSource(getMySubvaluesINstancesList());
Map<String, Object> params = new HashMap<>();
params.put("DetailDataSource", detailBean);
jasperPrint = JasperFillManager.fillReport(jasperReport, params, ds);
Note the name "DetailDataSource". In your report, you will need to create a parameter with that name and declare it's type as net.sf.jasperreports.engine.data.JRBeanCollectionDataSource. After that, you build a table based on a dataset that you create based on that parameter. But this is a short description of the video referenced above.
However, that alone doesn't solve the problem yet. The remaining problem that collection passed as a parameter will only be rendered once and will not be rendered for each record of your datasource. So, what we need to do is to add our list property into our datasource class, the same way as described in the question:
public Class MyDataSource implements JRDataSource {
private Integer prop1;
private String prop2;
private String prop3;
private List<String> subvalues;
...
// getters and setters omitted to save space
}
After that in your report you will need to declare a field named "subvalues" and declare its type as java.util.List. And then change the definition of your Dataset that you created following the video referenced above. You will need to change JRDatasource expression from $P{yourParameterName} to new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{subvalues}) And bingo! your collection is part of a datasource and will be rendered for each record

How to search RealmList field of RealmObject in a single query

Say I have a class:
public class Company extends RealmObject {
private String companyId;
private RealmList<CompanyMember> companyMembers;
}
Where the class CompanyMember looks like:
public class CompanyMember extends RealmObject {
private String id;
private String name;
}
Is it possible, using a single query in realm, to search a specific Company object's 'companyMembers' list based on a CompanyMember objects 'name' field?
For example, search Company 123's CompanyMember list for all CompanyMember's with a name containing 'abc', and return a list of these matching CompanyMembers.
Currently I am retrieving the entire company from Realm and iteratively searching the member list for a match, but this does not seem to be the best way.
Yes, like so:
Company company = realm.where(Company.class).equalTo("companyId", "123" ).findFirst();
RealmList<CompanyMember> companyMembers = company.getCompanyMembers();
RealmResults<CompanyMember> filteredMembers = companyMembers.where().contains("name", "abc").findAll();

Setting list from drools decision table

I am using drools for implementing certain condtion.I don't know how to set the
arraylist of the bean in action from drools decision table.For example following is my class.
public class Dog implements Serializable{
private String id;
private List<String> names;
}
My decision table should set the list of names based on the id paseed.How to implement this?
Conditon Action
d:Dog
id ???(what needs to be done here)
1 "tom","jack","pearl"
This can be done in the usual way.
Condition Action
d:Dog d.getNames()
id addAll(Arrays.asList($param))
Match id! Add some names!
1 "tom","jack","pearl"
You'll have to import Arrays or use the full class name.
If you used Spreadsheet File (.xlsx or .xls) then you can try this:
pojo calss:
public class CategoryEntity {
private String type;
private String allAction;
private List<String> actionList; // list
// Getters and Setters
}
And Spreadsheet File like this:

Conditional object population

Hy guys, I've turning around a problem using Struts2.
Basically I've a form that the user can submit filling different fields. I would like to populate conditionally the right object with the properties insert by the user.
So for example, if the user fill the form in a intermediary way I would like to use the properties for to create an Intermediary object, instead if the user fill the request in entity way, i'll use the properties for to create the Entity object, ecc...
These object share the same interface Request.
Is there any way to use polimorphically properties for to create Intermediary or Entity object?
Suppose this is my two POJO:
public class Intermediary implements Request{
private String name;
private String surname;
private String code;
private String FiscalCode;
private String address;
...
/*Getters and setters */
....
}
public class Entity implements Request{
private String name;
private String surname;
private String code;
....
/*Getters and setters */
....
}
This could be my form into the jsp page:
<s:form action="fillRequest" id="formRichiesta" name="formRichiesta" method="post">
<s:radio id="tipoRichiedente" name="tipoRichiedente" list="richiedenti">
<label>Name</label><s:textfield name="name"/>
<label>Surname</label><s:textfield name="surname"/>
<label>code</label><s:textfield name="code"/>
<label>Adress</label><s:textfield name="adress"/>
<label>Fiscal Code</label><s:textfield name="fiscalCode"/>
</s:form>
<s:submit>
Basically my problem is that I can discover the kind of request the the user trying to fill only ofter submission and watching at the radiobutton state.
Is there any way to mapping the properties into the right object directly instead of creating one big object with all the form properties and then create the right implementation? for to map properties could I use the interface reference?
For example image my action:
public class RequestAction {
private Request request;
....
}
So in my jsp could I use:
<label>Name</label><s:textfield name="request.name"/>
Thanks in advance.

Categories

Resources