DynamicJasper - subreport not displaying - java

i'm new to DynamicJasper.
I'm building a report that contain a subreport, i can run my original report successfully without any error, but the subreport is not displaying.
I'm using DynamicReportBuilder(drb) for building my main report, i'm wondering i'm adding my subreport in the wrong way.
Below is how i build my subreport
private DynamicReport subReportTesting() throws Exception
{
DynamicReportBuilder drb = new DynamicReportBuilder();
DynamicReport dr = drb.addColumn(subAbstractColumn1)
.addColumn(subAbstractColumn2)
.addColumn(subAbstractColumn3)
.setPrintColumnNames(true)
.setIgnorePagination(true)
.setMargins(0, 0, 0, 0)
.setTitle("Sales Report")
.setSubtitle("This report was generated at " + new Date())
.setUseFullPageWidth(true)
.build();
return dr;
}
and here is how i add my main report and subreport.
public DynamicReport buildReport() throws Exception {
{
DynamicReportBuilder drb = new DynamicReportBuilder();
drb.setTitle("This is original report")
.setSubtitle("This is subtitle")
.setMargins(15, 15, 15, 15)
.setUseFullPageWidth(true);
.setDetailHeight(30);
.setAllowDetailSplit(true);
.addColumn(mainAbstractColumn1);
.addColumn(mainAbstractColumn2);
.addColumn(mainAbstractColumn3);
DynamicReport myDynamicReport = new DynamicReport();
try {
myDynamicReport = subReportTesting();
} catch (Exception e) {
e.printStackTrace();
}
SubReportBuilder srb = new SubReportBuilder();
srb.setDynamicReport(myDynamicReport, new ClassicLayoutManager());
try {
mySubReport = srb.build();
**drb.addConcatenatedReport(mySubReport);**
} catch (DJBuilderException e) {
e.printStackTrace();
}
DynamicReport myMainReport = drb.build();
return myMainReport;
}
My contents is add by using preparedStatement and HashMap.
I expect the subreport will display it's title, subtitle and column header even there is no data to be show in the subreport's content, but currently it show nothing about the subreport.
Please correct me if i make any mistake.
Thank you so much.

I recommend you this http://dynamicjasper.com/docs/current/xref-test/ar/com/fdvs/dj/test/MultiReportConcatenated.html

Related

Error in creating PDF using jaspersoft ireport

I am using reactjs, spring-boot, SQL and ireport to generate the PDF reports. So the file is printed. But the data passing to the clientInvoice.jrxml is null. How I am fix this. I have attached the spring-boot value passing(the is referred to the invoiceID here and it is passing from the frontend).
public ResponseEntity generateClientInvoiceByClientInvoiceId(int id) {
try{
File file = ResourceUtils.getFile("classpath:report/clientInvoice.jrxml");
InputStream input = new FileInputStream(file);
// Compile the Jasper report from .jrxml to .japser
JasperReport jasperReport = JasperCompileManager.compileReport(input);
// Get the parameter
Map<String, Object> parameters = new HashMap<>();
parameters.put("invoiceId",id);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());
// Export the report to a PDF file
JasperExportManager.exportReportToPdfFile(jasperPrint, "D://" + id + ".pdf");
return new ResponseEntity<Object>(null, HttpStatus.OK);
}catch (CustomException e){
e.printStackTrace();
CustomErrorResponse errors = new CustomErrorResponse();
errors.setError(e.getMessage());
return new ResponseEntity(errors, HttpStatus.SERVICE_UNAVAILABLE);
}catch (Exception e){
e.printStackTrace();
CustomErrorResponse errors = new CustomErrorResponse();
errors.setError("Error in creating client invoice ");
return new ResponseEntity<>(errors, HttpStatus.SERVICE_UNAVAILABLE);
}
}
Only I have attached the query which I am using in the JRXML.(The query is attached to the JRXML file).
<queryString>
<![CDATA[SELECT cv.`id`,cv.`client_id`,cv.`no_of_boxes` as total_boxes,cv.`sub_total`,cv.`total_weight`, cvi.`selling_price`, SUM(cvi.`weight`),sp.`selling_prawn_category_type`,cv.`shipping_address`,c.company,sc.`category_name`,count(cvi.selling_category_id) as noOfBoxes, cvi.`selling_price`*SUM(cvi.`weight`) as total_price_per_category,cv.date FROM client_invoice cv INNER JOIN client_invoice_item cvi ON cvi.`client_invoice_id` = cv.`id`INNER JOIN `selling_prawn_category_type` sp ON cvi.`selling_category_id` = sp.`id`INNER JOIN `selling_prawn_category` sc ON sc.selling_prawn_category_id = sp.idINNER JOIN `CLIENT` c ON c.id = cv.client_idWHERE cv.`id` =$P{invoiceId} GROUP BY cvi.`selling_category_id`]]>
</queryString>
Please help me quickly as possible to resolve this.
I think that report is empty beacause of:
new JREmptyDataSource()
You can inject DataSource and try something like this:
try (Connection connection = dataSource.getConnection()){
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, prepareParameters(command), connection);
}

Add CSS to JasperReports' HTML Export

I've created a report with JasperReports 6.4.3 which is normally exported to PDF. Now, I'm trying to export this report to HTML as well.
I don't want to create a HTML file via JasperExportManager, so I'm using JasperReport's HtmlExporter to wirte the report directly into an outputstream.
Her is my code:
public void exportToHtml(OutputStream outputStream, JRDataSource jrDataSource, Map<String, Object> parameter, File reportFile)
throws IOException {
try {
JasperPrint jasperprint = JasperFillManager.fillReport(reportFile.getAbsolutePath(), parameter, jrDataSource);
HtmlExporter exporter = new HtmlExporter();
SimpleHtmlExporterOutput exporterOutput = new SimpleHtmlExporterOutput(outputStream);
Map<String, String> images = Maps.newHashMap();
exporterOutput.setImageHandler(new HtmlResourceHandler() {
#Override
public void handleResource(String id, byte[] data) {
System.err.println("id" + id);
images.put(id, "data:image/jpg;base64," + Base64.encodeBytes(data));
}
#Override
public String getResourcePath(String id) {
return images.get(id);
}
});
exporter.setExporterOutput(exporterOutput);
exporter.setExporterInput(new SimpleExporterInput(jasperprint));
SimpleHtmlExporterConfiguration exporterConfiguration = new SimpleHtmlExporterConfiguration();
exporterConfiguration.setBetweenPagesHtml("<div style='page-break-after:always'></div>");
exporter.setConfiguration(exporterConfiguration);
exporter.exportReport();
} catch (JRException jrException) {
throw new IOException(jrException);
}
}
The output looks good, but I need to add some styles to the HTML output. So I wonder if it is possible to add a reference to a css file to the exported html report.
Similarly to how you are setting the between pages HTML, you could do the same for the header/footer with:
exporterConfiguration.setHtmlHeader("...");
exporterConfiguration.setHtmlFooter("...");
The default HTML code for header is set here and the one for footer is set here.
You need to match the opening/closing tags when modifying any of them.

Programatic java ATL launch not working

I have a huge problem:
I have an ATL transformation that works flawlessly when I use the normal way to do it, using the atl plugin.
But when I try to java launch, it cannot find the classes of a model (org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'operationalTemplateGroup' is not found or is abstract)
Exemple:
I have a "operationalTemplateGroup" class on my model, but the metamodel describes it as :
<eClassifiers xsi:type="ecore:EClass" name="OPERATIONALTEMPLATEGROUP">
<eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
<details key="name" value="OPERATIONALTEMPLATEGROUP"/>
<details key="kind" value="elementOnly"/>
</eAnnotations>
See, OPERATIONALTEMPLATEGROUP and not operationalTemplateGroup, why the plugin can parse it but the java launcher dont?
I have this problem with only 1 model, the others are working fine, and if I change it to operationalTemplateGroup it will work, until the next class that has the same problem, ( About 6000 lines of code ).
Method:
public static void main(String[] args) {
try {
System.out.println("Running...");
/*
* Paths
*/
String inModelPath = "models/architectures/AToMS.acmehealth";
String inModelPath2 = "models/openehr/TemplatesAToMS.openehr2008v4";
String outModelPath = "models/richubi/TemplatesAToMS22.rich_interface_model";
String archMMPath = "metamodels/architectures/ACMEHealthv2.ecore";
String openEHRMMPath = "metamodels/openehr/openehr2008v4.ecore";
String richUbiMMPath = "metamodels/richubi/rich_interface_components.ecore";
System.out.println("inModelPath: " inModelPath);
System.out.println("inModelPath2: " inModelPath2);
System.out.println("outModelPath: " outModelPath);
System.out.println("archMMPath: " archMMPath);
System.out.println("openEHRMMPath: " openEHRMMPath);
System.out.println("richUbiMMPath: " richUbiMMPath);
/*
* Initializations
*/
ILauncher transformationLauncher = new EMFVMLauncher();
ModelFactory modelFactory = new EMFModelFactory();
IInjector injector = new EMFInjector();
IExtractor extractor = new EMFExtractor();
/*
* Load metamodels
*/
IReferenceModel archMetamodel = modelFactory.newReferenceModel();
injector.inject(archMetamodel, archMMPath);
IReferenceModel openEHRMetamodel = modelFactory.newReferenceModel();
injector.inject(openEHRMetamodel, openEHRMMPath);
IReferenceModel richUbiMetamodel = modelFactory.newReferenceModel();
injector.inject(richUbiMetamodel, richUbiMMPath);
System.out.println("Metamodels loaded.");
/*
* Load models and run transformation
*/
IModel inModel2 = modelFactory.newModel(openEHRMetamodel);
injector.inject(inModel2, inModelPath2); // ERROR HERE!!!!
IModel inModel = modelFactory.newModel(archMetamodel);
injector.inject(inModel, inModelPath);
IModel outModel = modelFactory.newModel(richUbiMetamodel);
System.out.println("IN, OUT models loaded.");
System.out.print("Running ATL trasformation...");
transformationLauncher.initialize(new HashMap<String, Object>());
transformationLauncher.addLibrary("LibHelpers",
new FileInputStream("transformations/LibHelpers.asm"));
transformationLauncher.addInModel(inModel, "archMM", "openEHRMM");
transformationLauncher.addInModel(inModel2, null, "openEHRMM");
transformationLauncher.addOutModel(outModel, "archM", "openEHRM");
transformationLauncher
.launch(ILauncher.RUN_MODE,
new NullProgressMonitor(),
new HashMap<String, Object>(),
new FileInputStream(
"/home/operador/workspace/Transformation/transformations/OpenEHRTemplates2RichUbi.asm"));
System.out.println("Done.");
System.out.print("Extracting OUT model...");
extractor.extract(outModel, outModelPath);
System.out.println("Done.");
/*
* Unload all models and metamodels (EMF-specific)
*/
EMFModelFactory emfModelFactory = (EMFModelFactory) modelFactory;
emfModelFactory.unload((EMFModel) inModel);
emfModelFactory.unload((EMFModel) outModel);
emfModelFactory.unload((EMFReferenceModel) archMetamodel);
emfModelFactory.unload((EMFReferenceModel) openEHRMetamodel);
} catch (ATLCoreException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

NoClassDefFoundError when creating dynamic jasper report

I am trying to create a dynamic jasper report. So I tried creating one using an example in the internet. But when I run the program I get an exception:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/digester/Digester at
net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:143)
at com.tfc.cheque.handle.ui.ReportPrint.main(ReportPrint.java:31)
given below is the code I used:
public class ReportPrint {
public static void main(String[] args) throws JRException, IOException {
JasperReport jasperReport = JasperCompileManager.compileReport("data.xml");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap(), new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");
}
public ReportPrint(){
try {
DynamicReportBuilder dynamicReportBuilder = new DynamicReportBuilder();
// configure report-level settings
dynamicReportBuilder.setReportName("Some");
dynamicReportBuilder.setPageSizeAndOrientation(Page.Page_Letter_Landscape());
// add id column to the report
ColumnBuilder columnBuilderID = ColumnBuilder.getNew();
columnBuilderID.setTitle("ID");
columnBuilderID.setWidth(180);
columnBuilderID.setFixedWidth(true);
columnBuilderID.setColumnProperty("ID", Integer.class.getName(), "#id");
dynamicReportBuilder.addColumn(columnBuilderID.build());
// add name column to report
ColumnBuilder columnBuilderName = ColumnBuilder.getNew();
columnBuilderName.setTitle("Name");
columnBuilderName.setWidth(180);
columnBuilderName.setFixedWidth(true);
columnBuilderName.setColumnProperty("Name", String.class.getName(), "#name");
dynamicReportBuilder.addColumn(columnBuilderName.build());
// add email column to report
ColumnBuilder columnBuilderEmail = ColumnBuilder.getNew();
columnBuilderEmail.setTitle("Email");
columnBuilderEmail.setWidth(180);
columnBuilderEmail.setFixedWidth(true);
columnBuilderEmail.setColumnProperty("Email", String.class.getName(), "#email");
dynamicReportBuilder.addColumn(columnBuilderEmail.build());
// add salary column to report
ColumnBuilder columnBuilderSalary = ColumnBuilder.getNew();
columnBuilderSalary.setTitle("Salary");
columnBuilderSalary.setWidth(180);
columnBuilderSalary.setFixedWidth(true);
columnBuilderSalary.setColumnProperty("Salary", Integer.class.getName(), "#salary");
dynamicReportBuilder.addColumn(columnBuilderSalary.build());
DynamicReport dynamicReport = dynamicReportBuilder.build();
// build a datasource representing the XML file
JRDataSource dataSource = new JRXmlDataSource(new File("data.xml"), "//employee");
// build JasperPrint instance, filling the report with data from datasource created above
JasperPrint jasperPrint = DynamicJasperHelper.generateJasperPrint(
dynamicReport, new ClassicLayoutManager(), dataSource, new HashMap<String, Object>());
// export to the pdf
String pdfFile = Math.round(Math.random() * 100000) + ".pdf";
JRExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, pdfFile);
exporter.exportReport();
} catch(JRException e) {
e.printStackTrace();
}
}
}
I cannot Figure out the problem here. Please help. Thank you.
Here's a screenshot of my class path settings:
Looks like the file commons-digester.jar is missing in your classpath. Download it and add it to the classpath.

Calling Existing PipeLine in GATE

I am new to Java and I want to call my saved pipeline using GATE JAVA API through Eclipse
I am not sure how I could do this although I know how to create new documents etc
FeatureMap params = Factory.newFeatureMap();
params.put(Document.DOCUMENT_URL_PARAMETER_NAME, new URL("http://www.gate.ac.uk"));
params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, "UTF-8");
// document features
FeatureMap feats = Factory.newFeatureMap();
feats.put("date", new Date());
Factory.createResource("gate.corpora.DocumentImpl", params, feats, "This is home");
//End Solution 2
// obtain a map of all named annotation sets
Document doc = Factory.newDocument("Document text");
Map <String, AnnotationSet> namedASes = doc.getNamedAnnotationSets();
System.out.println("No. of named Annotation Sets:" + namedASes.size());
// no of annotations each set contains
for (String setName : namedASes.keySet()) {
// annotation set
AnnotationSet aSet = namedASes.get(setName);
// no of annotations
System.out.println("No. of Annotations for " +setName + ":" + aSet.size());
There is a good example of GATE usage from java. Probably it does exactly what you want. BatchProcessApp.java.
In particular:
loading pipeline is done with lines
// load the saved application
CorpusController application =
(CorpusController)PersistenceManager.loadObjectFromFile(gappFile);
pipeli executed with
// run the application
application.execute();
Code is informative, clear and could be easy changed for your particular needs. The oxygen of open source project :)
Something like this could be used(do not forget to init GATE: set GATE home and etc):
private void getProcessedText(String textToProcess) {
Document gateDocument = null;
try {
// you can use your method from above to build document
gateDocument = createGATEDocument(textToProcess);
corpusController.getCorpus().add(gateDocument);
corpusController.execute();
// put here your annotations processing
} catch (Throwable ex) {
ex.printStackTrace();
} finally {
if (corpusController.getCorpus() != null) {
corpusController.getCorpus().remove(gateDocument);
}
if (gateDocument != null) {
Factory.deleteResource(gateDocument);
}
}
}
private CorpusController initPersistentGateResources() {
try {
Corpus corpus = Factory.newCorpus("New Corpus");
corpusController = (CorpusController) PersistenceManager.loadObjectFromFile(new File("PATH-TO-YOUR-GAPP-FILE"));
corpusController.setCorpus(corpus);
} catch (Exception ex) {
ex.printStackTrace();
}
return corpusController;
}

Categories