Generate jasper report using singe java bean - java

i have requirement to create order declaration report, i am using jasper studio to create the jasper template. in that template i have order id, customer details and his address, with these details i have to create a report.
i have below jasper report code
JasperCompileManager.compileReportToFile("src/main/resource/orderDeclarationForm.jrxml");
JasperPrint jasperPrint = JasperFillManager.fillReport("src/main/resource/orderDeclarationForm.jasper", new HashMap<String, Object>(), new JRTableModelDataSource(getTableModelData()));
// JasperExportManager.exportReportToPdfFile("resource/orderDeclarationForm.jrprint");
JasperExportManager.exportReportToPdfFile(jasperPrint, "src/main/resource/orderDeclarationForm.pdf");
but instead of JRTableModelDataSource i have to pass java bean class so the jasper engine has to take the data from one single java bean, i have gone through javabean as datasource where it takes list of beans, but my requirement is only one bean which has order details. please advice me on this

If you only need one instance of a bean to be passed to JasperFillManager.fillReport method, then it makes sense to pass them as parameters as long as their count is feasible (in your case its only 3).
Map<String,Object> params = new HashMap<String,Object>();
params.put("orderId", xxx);
params.put("customerDetails", xxx);
params.put("address", xxx);
Afterwards, pass this params Map object:
JasperPrint jasperPrint = JasperFillManager.fillReport("src/main/resource/orderDeclarationForm.jasper", params, new JRTableModelDataSource(getTableModelData()));
Please check this link for more info on how to read Parameters from within your .jrxml file.
Thanks.

Related

Java QueryDSL Building a Path through a Map dynamically

I'm looking to form a QueryDSL Path where there is a Map somewhere in the object hierarchy. If I just have simple object hierarchies, I can do the following, which works:
BooleanBuilder builder = new BooleanBuilder();
Path path = Expressions.path(Outer.class, "root");
path = Expressions.path(Inner.class, path, "inner");
builder.and(Expressions.path(String.class, path, "field").eq("test"));
However, I'm a bit confused when coming to a Map. There doesn't seem to be method parameter to pass in the root path, so I'm just at a loss on what to do. Passing the Path metadata doesn't seem to work, but I really didn't expect it to. And there is also no place to pass in the property name that is the map.
BooleanBuilder builder = new BooleanBuilder();
Path path = Expressions.path(Outer.class, "root");
path = Expressions.mapPath(String.class, String.class, StringPath.class, path.getMetadata()); // What would be the proper way to dive down into the map?
builder.and(Expressions.path(String.class, path, "field").eq("test"));
Clearly I do not understand the proper use of this method, but I am unable to glean what the proper use would be.
Also of note, I'm trying to do this dynamically, so even though I could use Q-generated classes in these examples, that isn't my final goal.
So if I had an object structure of Outer.map[key].field, how could I build a Path to select field?
The closest question I found was QueryDsl web query on the key of a Map field, but that assumes directly accessing a dictionary, not having a dictionary somewhere in the path.

MarkLogic: pass Map-Object from Java to XQuery

I would like to pass a simple Java Map<String, String> through the MarkLogic Java API to an XQuery script. The script is already deployed to the /ext directory on the server and starts like this:
xquery version "1.0-ml";
declare variable $dr as xs:string external;
declare variable $en as xs:string external;
declare variable $fi as map:map external;
...
I'm using the pattern described on the MarkLogic Site: https://docs.marklogic.com/guide/java/resourceservices#id_70532 ("Basic Steps for Module Invocation").
My Java code looks like this:
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
ServerEvaluationCall invoker = client.newServerEval();
invoker.addVariable("dr", "foo");
invoker.addVariable("en", "bar");
invoker.addVariableAs("fi", map);
String response = invoker.evalAs(String.class);
This fails with this exception:
java.lang.IllegalArgumentException: No factory for class java.util.HashMap
at com.marklogic.client.impl.HandleFactoryRegistryImpl.makeHandle(HandleFactoryRegistryImpl.java:98) ~[marklogic-client-api-4.0.3.jar:?]
at com.marklogic.client.impl.ServerEvaluationCallImpl.addVariableAs(ServerEvaluationCallImpl.java:123) ~[marklogic-client-api-4.0.3.jar:?]
I think I need to convert the map before sending it to ML or maybe use one of the handler classes, but I was not able to solve it or find any examples of this. Has anyone done this before?
It should work to pass a Jackson JSON object to an XQuery external variable. The value in XQuery should be either an XQuery map or a JSON node. If it's a JSON node, the xdmp:from-json() function can convert the value to an XQuery map.
For the Java part, look for the example introduced as "the following code uses a Jackson object mapper to set an external variable value to a JSON object":
https://docs.marklogic.com/guide/java/resourceservices#id_21827
Hoping that helps,

How to get the map values from "yml" file as a Map?

I have a "details.yml" file, considering all the setting for getting all values from "yml.file" is done. But I am unable to store Map values into
"Map"
Here is my "details.yml" file below
details:
company:XYZ
values:
name: Manish
last: Raut
And in my class file i am able to get the values of "company" from yml file using #Value("${company}")
#Component
#EnableConfigurationProperties
#ConfigurationProperties(prefix = "details")
public class abcd() {
#Value("${company}")
String company;
#Value("${values}")
Map<String, String> values =new HashMap<String, String>();
...............................
}
i am not able to get those values in Mao which i created in this class, but i am getting values for "Company".
Help me with this?
Im the past, I added getter/setter for MAP type and it was work.
Did you try it? (getter/setter for 'values')
I'm not really sure what marshalling framework Spring uses behind the scenes and how it configures it (you could probably find out with some debugging and maybe make it work for your case), but you could always add an extra layer to your application and configure your own.
For instance you could use Jackson with yaml dataformat - https://dzone.com/articles/read-yaml-in-java-with-jackson.

How to create dynamic Bean?

I have a text file which contain comma separated data which is the attribute of our bean.
e.g. name,age,gender,city,zipcode
We read the text file and we have a list which contain all the attribute. Here we need to create a dynamic Bean which contain the attribute based on that list which we get after reading text file, but we have different text files with different fields. So how should I create a dynamic bean which can contain the attributes according to the list which we will get after reading test file? Please give me some solution on this issue.
It isn't a dynamic Bean,
but I would use a HashMap:
HashMap<String, String> values = new HashMap<String, String>();
values.put("name", "Sebastian Blablabla");
values.put("city", "MyTown");
System.out.println(values.get("name"));
System.out.println(values.containsKey("city"));
System.out.println(values.containsKey("zipcode"));
Dynamic beans by Oracle use Maps too, look here:
http://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0210dynamicbeans01.html
I would just Use a Super- Class; Just like, i dont know..
public class Item

Using a POJO as an iReport data source

I have a POJO that compiles data from various sources into a single object. The object is instantiated with a single parameter. Example:
Invoice inv=new Invoice(1239);
This will bring back a complete invoice containing other POJOs populated with data from various sources (such as the billing and shipping addresses as Address objects).
Can I use this as a data source within iReport?
You could try use a JRMapCollectionDataSource from which you can build a DataSource from a collection.
You could take the values from the POJO object and place them into a collection if possible.
Here is some sample code for constructing a DataSource.
Collection<Map<String, Object>> myColl = new ArrayList<Map<String,Object>>();
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("Field1","Value1");
map1.put("Field2","Value2");
map1.put("Field3", someObject);
myColl.add(map1);
JRMapCollectionDataSource source = new JRMapCollectionDataSource(myColl);
Another option would be to create a custom datasource by implementing JRRewindableDataSource or JRDataSource.

Categories

Resources