Skip to content

Commit

Permalink
fix: ReportController throws exception for jasperreport
Browse files Browse the repository at this point in the history
  • Loading branch information
vietnguyen committed May 16, 2024
1 parent 7f8f88e commit 66a2ea5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 13 deletions.
4 changes: 4 additions & 0 deletions dhis-2/dhis-services/dhis-service-reporting/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jcommon</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@
import org.jfree.chart.renderer.category.StackedAreaRenderer;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.RectangleInsets;
import org.jfree.chart.util.TableOrder;
import org.jfree.data.Range;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.RectangleInsets;
import org.jfree.util.TableOrder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static byte[] getChartAsPngByteArray(JFreeChart jFreeChart, int width, in
ByteArrayOutputStream out = new ByteArrayOutputStream();

try {
org.jfree.chart.ChartUtils.writeChartAsPNG(out, jFreeChart, width, height);
org.jfree.chart.ChartUtilities.writeChartAsPNG(out, jFreeChart, width, height);
out.flush();

return out.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,18 @@ void testThatGetEventReportsDoesNotContainNewEventVisualizations() {

assertThat(nodeMap.values(), is(empty()));
}

@Test
void testReportPdf() {
String body =
"""
{"name": "Name Test", "relativePeriods": {"last12Months": true},
"designContent": "<?xml version=\\"1.0\\" encoding=\\"UTF-8\\"?> <jasperReport xmlns=\\"http://jasperreports.sourceforge.net/jasperreports\\" xmlns:xsi=\\"http://www.w3.org/2001/XMLSchema-instance\\" xsi:schemaLocation=\\"http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd\\" name=\\"dpt\\" pageWidth=\\"595\\" pageHeight=\\"842\\" columnWidth=\\"555\\" leftMargin=\\"20\\" rightMargin=\\"20\\" topMargin=\\"20\\" bottomMargin=\\"20\\" uuid=\\"17839606-ae6d-42a7-8501-a4137adad2c1\\"> <property name=\\"ireport.zoom\\" value=\\"1.5\\"/> <property name=\\"ireport.x\\" value=\\"14\\"/> <property name=\\"ireport.y\\" value=\\"0\\"/> <field name=\\"organisationunitid\\" class=\\"java.lang.Integer\\"/> <field name=\\"organisationunitname\\" class=\\"java.lang.String\\"/> <field name=\\"reporting_month_name\\" class=\\"java.lang.String\\"/> <field name=\\"param_organisationunit_name\\" class=\\"java.lang.String\\"/> <field name=\\"organisation_unit_is_parent\\" class=\\"java.lang.String\\"/> <field name=\\"anc 1st visit_year\\" class=\\"java.lang.Double\\"/> <field name=\\"anc 2nd visit_year\\" class=\\"java.lang.Double\\"/> <field name=\\"anc 3rd visit_year\\" class=\\"java.lang.Double\\"/> <field name=\\"anc 4th or more_year\\" class=\\"java.lang.Double\\"/> <background> <band splitType=\\"Stretch\\"/> </background> <title> <band height=\\"326\\" splitType=\\"Stretch\\"> <staticText> <reportElement uuid=\\"1876010e-28c0-499a-bf70-aea9f6040d7b\\" x=\\"12\\" y=\\"15\\" width=\\"532\\" height=\\"41\\" forecolor=\\"#184F73\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"24\\"/> </textElement> <text><![CDATA[ANC Visits]]></text> </staticText> <bar3DChart> <chart evaluationTime=\\"Report\\"> <reportElement uuid=\\"fe511602-c6ba-4e97-a653-8acd9b61d305\\" x=\\"12\\" y=\\"114\\" width=\\"532\\" height=\\"210\\"/> <chartTitle/> <chartSubtitle/> <chartLegend/> </chart> <categoryDataset> <dataset> <incrementWhenExpression><![CDATA[$F{organisation_unit_is_parent}.equals( \\"Yes\\" )]]></incrementWhenExpression> </dataset> <categorySeries> <seriesExpression><![CDATA[\\"ANC 1st visit\\"]]></seriesExpression> <categoryExpression><![CDATA[\\"\\"]]></categoryExpression> <valueExpression><![CDATA[$F{anc 1st visit_year}]]></valueExpression> </categorySeries> <categorySeries> <seriesExpression><![CDATA[\\"ANC 2nd visit\\"]]></seriesExpression> <categoryExpression><![CDATA[\\"\\"]]></categoryExpression> <valueExpression><![CDATA[$F{anc 2nd visit_year}]]></valueExpression> </categorySeries> <categorySeries> <seriesExpression><![CDATA[\\"ANC 3rd visit\\"]]></seriesExpression> <categoryExpression><![CDATA[\\"\\"]]></categoryExpression> <valueExpression><![CDATA[$F{anc 3rd visit_year}]]></valueExpression> </categorySeries> <categorySeries> <seriesExpression><![CDATA[\\"ANC >=4 visits\\"]]></seriesExpression> <categoryExpression><![CDATA[\\"\\"]]></categoryExpression> <valueExpression><![CDATA[$F{anc 4th or more_year}]]></valueExpression> </categorySeries> </categoryDataset> <bar3DPlot isShowLabels=\\"true\\"> <plot/> <itemLabel color=\\"#000000\\" backgroundColor=\\"#FFFFFF\\"/> </bar3DPlot> </bar3DChart> <textField> <reportElement uuid=\\"0aeb7c9b-04ad-43d7-82e9-ca53dc04b3fc\\" x=\\"10\\" y=\\"68\\" width=\\"253\\" height=\\"20\\"/> <textElement textAlignment=\\"Right\\"> <font size=\\"13\\"/> </textElement> <textFieldExpression><![CDATA[$F{reporting_month_name}]]></textFieldExpression> </textField> <textField> <reportElement uuid=\\"a25c78a8-61a7-4003-a10f-1dbfbbdc43ac\\" x=\\"287\\" y=\\"68\\" width=\\"255\\" height=\\"20\\"/> <textElement> <font size=\\"13\\"/> </textElement> <textFieldExpression><![CDATA[$F{param_organisationunit_name}]]></textFieldExpression> </textField> <staticText> <reportElement uuid=\\"fb9533bc-db90-43bb-aad2-722abae6b51d\\" x=\\"263\\" y=\\"68\\" width=\\"24\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"13\\"/> </textElement> <text><![CDATA[-]]></text> </staticText> </band> </title> <pageHeader> <band height=\\"15\\" splitType=\\"Stretch\\"/> </pageHeader> <columnHeader> <band height=\\"51\\" splitType=\\"Stretch\\"> <staticText> <reportElement uuid=\\"6e085c2a-f2f0-4b51-951c-3d0f0a117a92\\" x=\\"89\\" y=\\"26\\" width=\\"129\\" height=\\"20\\"/> <textElement> <font size=\\"11\\" isBold=\\"true\\"/> </textElement> <text><![CDATA[Organisation unit]]></text> </staticText> <staticText> <reportElement uuid=\\"a575c888-3f49-4840-9aa3-0a84e6d35a78\\" x=\\"218\\" y=\\"26\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"11\\" isBold=\\"true\\"/> </textElement> <text><![CDATA[ANC 1st visit]]></text> </staticText> <staticText> <reportElement uuid=\\"6f1d357d-5f36-4b1f-9a3a-0c8a51b318ec\\" x=\\"273\\" y=\\"26\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"11\\" isBold=\\"true\\"/> </textElement> <text><![CDATA[ANC 2nd visit]]></text> </staticText> <staticText> <reportElement uuid=\\"a21c2500-87f5-4509-a766-dbdc9dcec98f\\" x=\\"328\\" y=\\"26\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"11\\" isBold=\\"true\\"/> </textElement> <text><![CDATA[ANC 3rd visit]]></text> </staticText> <staticText> <reportElement uuid=\\"50047dbb-126f-4099-b49a-960e1cd09e33\\" x=\\"383\\" y=\\"26\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"11\\" isBold=\\"true\\"/> </textElement> <text><![CDATA[ANC >=4 visits]]></text> </staticText> <line> <reportElement uuid=\\"43f67873-03e8-4b94-bca3-8882afbbad7e\\" x=\\"73\\" y=\\"45\\" width=\\"381\\" height=\\"1\\"/> </line> </band> </columnHeader> <detail> <band height=\\"21\\" splitType=\\"Stretch\\"> <textField isBlankWhenNull=\\"true\\"> <reportElement uuid=\\"8088fed7-0677-4ffd-8f04-0dbf5d39ad95\\" x=\\"89\\" y=\\"0\\" width=\\"129\\" height=\\"20\\"/> <textElement> <font size=\\"9\\"/> </textElement> <textFieldExpression><![CDATA[$F{organisationunitname}]]></textFieldExpression> </textField> <textField pattern=\\"###0\\" isBlankWhenNull=\\"true\\"> <reportElement uuid=\\"ee6ce247-f961-4b3f-ba8e-c98c9e6e33ab\\" x=\\"218\\" y=\\"0\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"9\\"/> </textElement> <textFieldExpression><![CDATA[$F{anc 1st visit_year}]]></textFieldExpression> </textField> <textField pattern=\\"###0\\" isBlankWhenNull=\\"true\\"> <reportElement uuid=\\"ab1f1ec1-e403-48bf-a2fd-70b5fd73b3b3\\" x=\\"273\\" y=\\"0\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"9\\"/> </textElement> <textFieldExpression><![CDATA[$F{anc 2nd visit_year}]]></textFieldExpression> </textField> <textField pattern=\\"###0\\" isBlankWhenNull=\\"true\\"> <reportElement uuid=\\"5ec61f4a-c211-4d92-b585-ed7310d33acc\\" x=\\"328\\" y=\\"0\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"9\\"/> </textElement> <textFieldExpression><![CDATA[$F{anc 3rd visit_year}]]></textFieldExpression> </textField> <textField pattern=\\"###0\\" isBlankWhenNull=\\"true\\"> <reportElement uuid=\\"3ba05985-9b10-4ba0-9ef5-ab3aa00910cc\\" x=\\"383\\" y=\\"0\\" width=\\"55\\" height=\\"20\\"/> <textElement textAlignment=\\"Center\\"> <font size=\\"9\\"/> </textElement> <textFieldExpression><![CDATA[$F{anc 4th or more_year}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band height=\\"22\\" splitType=\\"Stretch\\"/> </columnFooter> <pageFooter> <band height=\\"20\\" splitType=\\"Stretch\\"/> </pageFooter> <summary> <band height=\\"28\\" splitType=\\"Stretch\\"/> </summary> </jasperReport>"
}""";
String uid = assertStatus(CREATED, POST("/reports/", body));
assertFalse(
GET("/reports/" + uid + "/data.pdf?t=1715330660314&ou=ImspTQPwCqd&pe=2023&date=2023-01-01")
.content("application/pdf")
.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import org.hisp.dhis.visualization.VisualizationService;
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.utils.ContextUtils;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -247,7 +247,7 @@ public void getVisualizationData(
filename,
attachment);

ChartUtils.writeChartAsPNG(response.getOutputStream(), jFreeChart, width, height);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, width, height);
} else {
response.setContentType(CONTENT_TYPE_JSON);
renderService.toJson(response.getOutputStream(), getVisualizationGrid(uid, ou, date));
Expand Down Expand Up @@ -287,7 +287,7 @@ public void getVisualizationChartData(
"chart.png",
attachment);

ChartUtils.writeChartAsPNG(response.getOutputStream(), chart, width, height);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, width, height);
}

@GetMapping(value = {"/visualizations/history/data", "/visualizations/history/data.png"})
Expand Down Expand Up @@ -348,7 +348,7 @@ public void getVisualizationChartHistory(
13,
i18nManager.getI18nFormat());

ChartUtils.writeChartAsPNG(response.getOutputStream(), chart, width, height);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), chart, width, height);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import org.hisp.dhis.webapi.controller.AbstractCrudController;
import org.hisp.dhis.webapi.utils.ContextUtils;
import org.hisp.dhis.webapi.webdomain.WebOptions;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -152,7 +152,7 @@ public void getChart(
filename,
attachment);

ChartUtils.writeChartAsPNG(response.getOutputStream(), jFreeChart, width, height);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, width, height);
}

// --------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import static org.hisp.dhis.schema.descriptors.EventVisualizationSchemaDescriptor.API_ENDPOINT;
import static org.hisp.dhis.system.util.CodecUtils.filenameEncode;
import static org.hisp.dhis.webapi.utils.ContextUtils.CONTENT_TYPE_PNG;
import static org.jfree.chart.ChartUtils.writeChartAsPNG;

import java.io.IOException;
import java.util.Date;
Expand All @@ -63,6 +62,7 @@
import org.hisp.dhis.webapi.mvc.annotation.ApiVersion;
import org.hisp.dhis.webapi.utils.ContextUtils;
import org.hisp.dhis.webapi.webdomain.WebOptions;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -126,7 +126,7 @@ void generateChart(
contextUtils.configureResponse(
response, CONTENT_TYPE_PNG, RESPECT_SYSTEM_SETTING, filename, attachment);

writeChartAsPNG(response.getOutputStream(), jFreeChart, width, height);
ChartUtilities.writeChartAsPNG(response.getOutputStream(), jFreeChart, width, height);
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions dhis-2/dhis-web-embedded-jetty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.hisp.dhis.util.ContextUtils;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;

Expand Down Expand Up @@ -141,7 +142,7 @@ public void execute(ActionInvocation invocation) throws Exception {
ContextUtils.configureResponse(response, ContextUtils.CONTENT_TYPE_PNG, true, filename, false);

OutputStream os = response.getOutputStream();
ChartUtils.writeChartAsPNG(os, chart, width, height);
ChartUtilities.writeChartAsPNG(os, chart, width, height);
os.flush();
}
}
8 changes: 7 additions & 1 deletion dhis-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@

<!-- Reporting -->
<jasperreports.version>6.20.0</jasperreports.version>
<jfreechart.version>1.5.4</jfreechart.version>
<jfreechart.version>1.0.19</jfreechart.version>
<jfreechart-common.version>1.0.23</jfreechart-common.version>
<htmlparser.version>2.1</htmlparser.version>
<htmllexer.version>2.1</htmllexer.version>
<poi.version>5.2.4</poi.version>
Expand Down Expand Up @@ -1161,6 +1162,11 @@
<artifactId>jasperreports-fonts</artifactId>
<version>${jasperreports.version}</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jcommon</artifactId>
<version>${jfreechart-common.version}</version>
</dependency>
<dependency>
<groupId>org.jfree</groupId>
<artifactId>jfreechart</artifactId>
Expand Down

0 comments on commit 66a2ea5

Please sign in to comment.