From a05e1e4a648ad496d000e8eb8db2d36f90b52400 Mon Sep 17 00:00:00 2001 From: shilpa-iplit Date: Wed, 3 Feb 2021 22:09:03 +0530 Subject: [PATCH] internationalization rtl changes --- .../filter/JasperResponseConverter.java | 10 ++++- .../reports/template/BaseReportTemplate.java | 44 ++++++++++++++++++- .../template/IpdPatientsReportTemplate.java | 4 +- .../template/ObsCannedReportTemplate.java | 3 +- .../template/PatientReportTemplate.java | 2 +- .../template/PatientsWithLabtestResults.java | 2 +- .../reports/template/SqlReportTemplate.java | 2 +- .../bahmni/reports/template/Templates.java | 6 +-- .../reports/template/VisitReportTemplate.java | 4 +- .../bahmni/reports/util/BahmniReportUtil.java | 29 +++++++++++- .../bahmni/reports/web/ReportGenerator.java | 15 +++++++ .../org/bahmni/reports/web/ReportParams.java | 9 ++++ src/main/resources/ReportLabels.properties | 39 ++++++++++++++++ src/main/resources/ReportLabels_ar.properties | 38 ++++++++++++++++ 14 files changed, 192 insertions(+), 15 deletions(-) create mode 100644 src/main/resources/ReportLabels.properties create mode 100644 src/main/resources/ReportLabels_ar.properties diff --git a/src/main/java/org/bahmni/reports/filter/JasperResponseConverter.java b/src/main/java/org/bahmni/reports/filter/JasperResponseConverter.java index 87947fc7..ec467447 100644 --- a/src/main/java/org/bahmni/reports/filter/JasperResponseConverter.java +++ b/src/main/java/org/bahmni/reports/filter/JasperResponseConverter.java @@ -3,8 +3,10 @@ import net.sf.dynamicreports.jasper.builder.JasperConcatenatedReportBuilder; import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; import net.sf.dynamicreports.jasper.builder.export.Exporters; +import net.sf.dynamicreports.jasper.builder.export.JasperHtmlExporterBuilder; import net.sf.dynamicreports.jasper.builder.export.JasperXlsExporterBuilder; import net.sf.dynamicreports.report.exception.DRException; + import org.apache.log4j.Logger; import org.bahmni.reports.template.Templates; import org.bahmni.reports.web.ReportParams; @@ -31,7 +33,13 @@ public void convertToResponseType(ReportParams reportParams, JasperConcatenatedReportBuilder concatenatedReportBuilder) throws DRException { switch (reportParams.getResponseType()) { case TEXT_HTML: - concatenatedReportBuilder.toHtml(outputStream); + if ("ar".equalsIgnoreCase(reportParams.getLanguageTag())) { + JasperHtmlExporterBuilder htmlBuilder = Exporters.htmlExporter(outputStream); + htmlBuilder.setHtmlHeader(""); + concatenatedReportBuilder.toHtml(htmlBuilder); + } else { + concatenatedReportBuilder.toHtml(outputStream); + } break; case APPLICATION_VND_MS_EXCEL: concatenatedReportBuilder.toXlsx(outputStream); diff --git a/src/main/java/org/bahmni/reports/template/BaseReportTemplate.java b/src/main/java/org/bahmni/reports/template/BaseReportTemplate.java index 20e3f950..b09ea724 100644 --- a/src/main/java/org/bahmni/reports/template/BaseReportTemplate.java +++ b/src/main/java/org/bahmni/reports/template/BaseReportTemplate.java @@ -1,15 +1,57 @@ package org.bahmni.reports.template; import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; +import net.sf.dynamicreports.report.builder.column.Columns; +import net.sf.dynamicreports.report.builder.column.TextColumnBuilder; import net.sf.dynamicreports.report.constant.PageType; +import net.sf.dynamicreports.report.definition.datatype.DRIDataType; + +import org.apache.commons.lang3.StringUtils; import org.bahmni.reports.model.Config; import org.bahmni.reports.model.Report; import org.bahmni.reports.report.BahmniReportBuilder; import java.sql.Connection; import java.util.List; +import java.util.ResourceBundle; public abstract class BaseReportTemplate { + private static ResourceBundle localeBundle = null; + + public static class col { + public static TextColumnBuilder column(String title, String fieldName, Class valueClass) { + return net.sf.dynamicreports.report.builder.DynamicReports.col.column(title, fieldName, valueClass); + } + + public static TextColumnBuilder column(String title, String fieldName, DRIDataType dataType) { + return Columns.column(getResourceBundleLabel(title), fieldName, dataType); + } + } + + public void setLocaleBundle(ResourceBundle lBundle) { + localeBundle = lBundle; + } + + public ResourceBundle getLocaleBundle() { + return localeBundle; + } + + public static String getResourceBundleLabel(String key) { + try { + String newKey = ""; + if (!StringUtils.isEmpty(key)) { + newKey = key.trim(); + newKey = newKey.replaceAll(" ", "_"); + } + if (localeBundle!=null) { + return new String(localeBundle.getString(newKey).getBytes("8859_1"), "UTF-8"); + } + } catch (Exception e) { + } + return key; + } + public abstract BahmniReportBuilder build(Connection connection, JasperReportBuilder jasperReport, Report report, - String startDate, String endDate, List resources, PageType pageType) throws Exception; + String startDate, String endDate, List resources, + PageType pageType) throws Exception; } \ No newline at end of file diff --git a/src/main/java/org/bahmni/reports/template/IpdPatientsReportTemplate.java b/src/main/java/org/bahmni/reports/template/IpdPatientsReportTemplate.java index b9278f79..e1a1042a 100644 --- a/src/main/java/org/bahmni/reports/template/IpdPatientsReportTemplate.java +++ b/src/main/java/org/bahmni/reports/template/IpdPatientsReportTemplate.java @@ -20,7 +20,7 @@ import java.util.Date; import java.util.List; -import static net.sf.dynamicreports.report.builder.DynamicReports.col; +//import static net.sf.dynamicreports.report.builder.DynamicReports.col; import static net.sf.dynamicreports.report.builder.DynamicReports.type; import static org.bahmni.reports.template.Templates.minimalColumnStyle; import static org.bahmni.reports.util.FileReaderUtil.getFileContent; @@ -30,7 +30,7 @@ public class IpdPatientsReportTemplate extends BaseReportTemplate report, String startDate, String endDate, List resources, PageType pageType) throws SQLException { - CommonComponents.addTo(jasperReport, report, pageType); + CommonComponents.addTo(jasperReport, report, pageType); PatientAttributesHelper patientAttributesHelper = new PatientAttributesHelper(report.getConfig().getPatientAttributes()); diff --git a/src/main/java/org/bahmni/reports/template/ObsCannedReportTemplate.java b/src/main/java/org/bahmni/reports/template/ObsCannedReportTemplate.java index ac77e0bb..388a96a8 100644 --- a/src/main/java/org/bahmni/reports/template/ObsCannedReportTemplate.java +++ b/src/main/java/org/bahmni/reports/template/ObsCannedReportTemplate.java @@ -5,7 +5,6 @@ import net.sf.dynamicreports.report.constant.PageType; import net.sf.dynamicreports.report.constant.WhenNoDataType; import org.apache.commons.lang3.StringUtils; -import org.bahmni.reports.BahmniReportsProperties; import org.bahmni.reports.model.ObsCannedReportTemplateConfig; import org.bahmni.reports.model.Report; import org.bahmni.reports.model.UsingDatasource; @@ -19,7 +18,7 @@ import java.util.ArrayList; import java.util.List; -import static net.sf.dynamicreports.report.builder.DynamicReports.col; +//import static net.sf.dynamicreports.report.builder.DynamicReports.col; import static net.sf.dynamicreports.report.builder.DynamicReports.type; import static org.bahmni.reports.util.FileReaderUtil.getFileContent; diff --git a/src/main/java/org/bahmni/reports/template/PatientReportTemplate.java b/src/main/java/org/bahmni/reports/template/PatientReportTemplate.java index 72752fc4..d684773e 100644 --- a/src/main/java/org/bahmni/reports/template/PatientReportTemplate.java +++ b/src/main/java/org/bahmni/reports/template/PatientReportTemplate.java @@ -13,7 +13,7 @@ import java.sql.*; import java.util.List; -import static net.sf.dynamicreports.report.builder.DynamicReports.col; +//import static net.sf.dynamicreports.report.builder.DynamicReports.col; import static net.sf.dynamicreports.report.builder.DynamicReports.type; import static org.bahmni.reports.util.FileReaderUtil.getFileContent; diff --git a/src/main/java/org/bahmni/reports/template/PatientsWithLabtestResults.java b/src/main/java/org/bahmni/reports/template/PatientsWithLabtestResults.java index 9cebbd75..b8b9bbb6 100644 --- a/src/main/java/org/bahmni/reports/template/PatientsWithLabtestResults.java +++ b/src/main/java/org/bahmni/reports/template/PatientsWithLabtestResults.java @@ -17,7 +17,7 @@ import java.util.Date; import java.util.List; -import static net.sf.dynamicreports.report.builder.DynamicReports.col; +//import static net.sf.dynamicreports.report.builder.DynamicReports.col; import static net.sf.dynamicreports.report.builder.DynamicReports.type; import static org.bahmni.reports.template.Templates.minimalColumnStyle; import static org.bahmni.reports.util.FileReaderUtil.getFileContent; diff --git a/src/main/java/org/bahmni/reports/template/SqlReportTemplate.java b/src/main/java/org/bahmni/reports/template/SqlReportTemplate.java index 918ecb35..0eede305 100644 --- a/src/main/java/org/bahmni/reports/template/SqlReportTemplate.java +++ b/src/main/java/org/bahmni/reports/template/SqlReportTemplate.java @@ -19,7 +19,7 @@ import java.sql.Types; import java.util.List; -import static net.sf.dynamicreports.report.builder.DynamicReports.col; +//import static net.sf.dynamicreports.report.builder.DynamicReports.col; import static org.bahmni.reports.util.FileReaderUtil.getFileContent; public class SqlReportTemplate extends BaseReportTemplate { diff --git a/src/main/java/org/bahmni/reports/template/Templates.java b/src/main/java/org/bahmni/reports/template/Templates.java index 918d950a..4cc92721 100644 --- a/src/main/java/org/bahmni/reports/template/Templates.java +++ b/src/main/java/org/bahmni/reports/template/Templates.java @@ -34,7 +34,7 @@ import net.sf.dynamicreports.report.definition.ReportParameters; import java.awt.*; -import java.util.Locale; +//import java.util.Locale; import static net.sf.dynamicreports.report.builder.DynamicReports.*; @@ -100,7 +100,7 @@ public class Templates { .setHeadingStyle(0, stl.style(rootStyle).bold()); reportTemplate = template() - .setLocale(Locale.ENGLISH) + //.setLocale(Locale.ENGLISH) .setColumnStyle(columnStyle) .setColumnTitleStyle(columnTitleStyle) .setGroupStyle(groupStyle) @@ -115,7 +115,7 @@ public class Templates { .setTableOfContentsCustomizer(tableOfContentsCustomizer); excelReportTemplate = template() - .setLocale(Locale.ENGLISH) + //.setLocale(Locale.ENGLISH) .setColumnStyle(columnStyle) .setColumnTitleStyle(columnTitleStyle) .setGroupStyle(groupStyle) diff --git a/src/main/java/org/bahmni/reports/template/VisitReportTemplate.java b/src/main/java/org/bahmni/reports/template/VisitReportTemplate.java index 8fd85f2c..f0fd11a7 100644 --- a/src/main/java/org/bahmni/reports/template/VisitReportTemplate.java +++ b/src/main/java/org/bahmni/reports/template/VisitReportTemplate.java @@ -17,7 +17,7 @@ import java.sql.SQLException; import java.util.List; -import static net.sf.dynamicreports.report.builder.DynamicReports.col; +//import static net.sf.dynamicreports.report.builder.DynamicReports.col; import static net.sf.dynamicreports.report.builder.DynamicReports.type; import static org.bahmni.reports.template.Templates.minimalColumnStyle; import static org.bahmni.reports.util.FileReaderUtil.getFileContent; @@ -61,6 +61,7 @@ public BahmniReportBuilder build(Connection connection, JasperReportBuilder jasp TextColumnBuilder genderColumn = col.column("Gender", "Gender", type.stringType()).setStyle(minimalColumnStyle); jasperReport.setShowColumnTitle(true) + .setResourceBundle(getLocaleBundle()) .setWhenNoDataType(WhenNoDataType.ALL_SECTIONS_NO_DETAIL) .columns(patientIdColumn, patientNameColumn, genderColumn); @@ -68,7 +69,6 @@ public BahmniReportBuilder build(Connection connection, JasperReportBuilder jasp addColumns(jasperReport, report.getConfig().getPersonAttributes(), minimalColumnStyle); String sqlString = getSqlString(personAttributes, visitAttributes, startDate, endDate); - JasperReportBuilder jasperReportBuilder = SqlUtil.executeReportWithStoredProc(jasperReport, connection, sqlString); return new BahmniReportBuilder(jasperReportBuilder); } diff --git a/src/main/java/org/bahmni/reports/util/BahmniReportUtil.java b/src/main/java/org/bahmni/reports/util/BahmniReportUtil.java index b204eee4..a63ec156 100644 --- a/src/main/java/org/bahmni/reports/util/BahmniReportUtil.java +++ b/src/main/java/org/bahmni/reports/util/BahmniReportUtil.java @@ -4,8 +4,11 @@ import net.sf.dynamicreports.report.base.DRReport; import net.sf.dynamicreports.report.base.column.DRColumn; import net.sf.dynamicreports.report.constant.PageType; +import net.sf.jasperreports.engine.JRParameter; + import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.Predicate; +import org.apache.commons.lang.StringUtils; import org.bahmni.reports.BahmniReportsProperties; import org.bahmni.reports.model.Config; import org.bahmni.reports.model.GenericReportsConfig; @@ -19,17 +22,41 @@ import java.sql.Connection; import java.util.*; +//import static net.sf.dynamicreports.report.builder.DynamicReports.col; import static net.sf.dynamicreports.report.builder.DynamicReports.report; +//import static net.sf.dynamicreports.report.builder.DynamicReports.type; +//import static org.bahmni.reports.template.Templates.minimalColumnStyle; + public class BahmniReportUtil { + public static String getResourceBundleLabel(String key, ResourceBundle reportLocaleBundle) { + try { + String newKey = ""; + if (!StringUtils.isEmpty(key)) { + newKey = key.trim(); + newKey = newKey.replaceAll(" ", "_"); + } + if (reportLocaleBundle!=null) { + return new String(reportLocaleBundle.getString(newKey).getBytes("8859_1"), "UTF-8"); + } + } catch (Exception e) { + } + return key; + } + public static BahmniReportBuilder build(Report report, Connection connection, String startDate, String endDate, List resources, PageType pageType, BahmniReportsProperties bahmniReportsProperties) throws Exception { BaseReportTemplate reportTemplate = report.getTemplate(bahmniReportsProperties); + ResourceBundle reportLocaleBundle = reportTemplate.getLocaleBundle(); JasperReportBuilder reportBuilder = report(); - reportBuilder = new ReportHeader().add(reportBuilder, report.getName(), startDate, endDate); + reportBuilder = new ReportHeader().add(reportBuilder, getResourceBundleLabel(report.getName(), reportLocaleBundle), startDate, endDate); + reportBuilder = reportBuilder.setResourceBundle(reportLocaleBundle); + reportBuilder = reportBuilder.setLocale(reportLocaleBundle.getLocale()); + //reportBuilder = reportBuilder.setParameter(JRParameter.REPORT_RESOURCE_BUNDLE, reportLocaleBundle); + //reportBuilder = reportBuilder.setParameter(JRParameter.REPORT_LOCALE, reportLocaleBundle.getLocale()); BahmniReportBuilder build = reportTemplate.build(connection, reportBuilder, report, startDate, endDate, resources, pageType); excludeColumns(report.getConfig(), reportBuilder); orderColumns(report.getConfig(), reportBuilder); diff --git a/src/main/java/org/bahmni/reports/web/ReportGenerator.java b/src/main/java/org/bahmni/reports/web/ReportGenerator.java index 76c53f8e..ff040252 100644 --- a/src/main/java/org/bahmni/reports/web/ReportGenerator.java +++ b/src/main/java/org/bahmni/reports/web/ReportGenerator.java @@ -2,6 +2,8 @@ import net.sf.dynamicreports.jasper.builder.JasperConcatenatedReportBuilder; import net.sf.dynamicreports.jasper.builder.JasperReportBuilder; + +import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.bahmni.reports.BahmniReportsProperties; import org.bahmni.reports.filter.JasperResponseConverter; @@ -17,6 +19,8 @@ import java.sql.Connection; import java.util.ArrayList; import java.util.List; +import java.util.Locale; +import java.util.ResourceBundle; import static net.sf.dynamicreports.report.builder.DynamicReports.concatenatedReport; @@ -41,10 +45,21 @@ public ReportGenerator(ReportParams reportParams, OutputStream outputStream, All public void invoke() throws Exception { ArrayList resources = new ArrayList<>(); try { + Locale currentLoc = Locale.ENGLISH; + if (!StringUtils.isEmpty(reportParams.getLanguageTag())) { + currentLoc = Locale.forLanguageTag(reportParams.getLanguageTag()); + } + ResourceBundle reportLocaleBundle = null; + if (currentLoc!=null) { + reportLocaleBundle = ResourceBundle.getBundle("ReportLabels", currentLoc); + } Report report = Reports.find(reportParams.getName(), bahmniReportsProperties.getConfigFileUrl(),httpClient); report.setHttpClient(httpClient); validateResponseTypeSupportedFor(report, reportParams.getResponseType()); BaseReportTemplate reportTemplate = report.getTemplate(bahmniReportsProperties); + if (reportLocaleBundle!=null) { + reportTemplate.setLocaleBundle(reportLocaleBundle); + } Connection connection = allDatasources.getConnectionFromDatasource(reportTemplate); BahmniReportBuilder reportBuilder = BahmniReportUtil.build(report, connection, reportParams.getStartDate(), reportParams.getEndDate(), resources, reportParams.getPaperSize(), bahmniReportsProperties); diff --git a/src/main/java/org/bahmni/reports/web/ReportParams.java b/src/main/java/org/bahmni/reports/web/ReportParams.java index 26f67eb5..c78c610a 100644 --- a/src/main/java/org/bahmni/reports/web/ReportParams.java +++ b/src/main/java/org/bahmni/reports/web/ReportParams.java @@ -16,6 +16,7 @@ public class ReportParams implements Serializable { private String paperSize; private String appName; private String userName; + private String languageTag; public String getStartDate() { return startDate; @@ -80,4 +81,12 @@ public String getUserName() { public void setUserName(String userName) { this.userName = userName; } + + public String getLanguageTag() { + return languageTag; + } + + public void setLanguageTag(String languageTag) { + this.languageTag = languageTag; + } } diff --git a/src/main/resources/ReportLabels.properties b/src/main/resources/ReportLabels.properties new file mode 100644 index 00000000..24e29e30 --- /dev/null +++ b/src/main/resources/ReportLabels.properties @@ -0,0 +1,39 @@ +Visit_Report=Visit Report +IPD_Patients_Report=IPD Patients Report +identifier=Patient Identifier +Patient_ID=Patient ID +Patient_Name=Patient Name +Gender=Gender +Age=Age +Patient_Id=Patient Id +Patient_Name=Patient Name +Birth_Date=Birth Date +First_Name=First Name +Family_Name=Family Name +Death_Date=Death Date +Cause_Of_Death=Cause Of Death +Address1=Address1 +Address2=Address2 +Address3=Address3 +City/Village=City/Village +State/Province=State/Province +County/District=County/District +Registration_Date=Registration Date + +New_OPD=New OPD +Old_OPD=Old OPD +Total_OPD=Total OPD +New_IPD=New IPD +Old_IPD=Old IPD +Total_IPD=Total IPD + +Department=Department +Test=Test +Total_Count=Total Count +Positive_Count=Positive Count +Negative_Count=Negative Count + +Diagnosis=Diagnosis +Date_of_Admission=Date of Admission +Date_of_Discharge=Date of Discharge +Visit_Type=Visit Type diff --git a/src/main/resources/ReportLabels_ar.properties b/src/main/resources/ReportLabels_ar.properties new file mode 100644 index 00000000..1f49421c --- /dev/null +++ b/src/main/resources/ReportLabels_ar.properties @@ -0,0 +1,38 @@ +Visit_Report=جوجل +IPD_Patients_Report=جوج +identifier=جوجل +Patient_ID=جوج +Patient_Name=جوجل +Gender=شيلبا +Age=شيلبا +Birthdate=جوج +Birth_Date=جوج +First_Name=جوج +Family_Name=جوج +Death_Date=جوج +Cause_Of_Death=جوج +Address1=جوج +Address2=جوج +Address3=جوج +City/Village=جوج +State/Province=جوج +County/District=جوج +Registration_Date=جوج + +New_OPD=شيلبا +Old_OPD=شيلبا +Total_OPD=شيلبا +New_IPD=شيلبا +Old_IPD=شيلبا +Total_IPD=شيلبا + +Department=شيلبا +Test=شيلبا +Total_Count=شيلبا +Positive_Count=شيلبا +Negative_Count=شيلبا + +Diagnosis=شيلبا +Date_of_Admission=شيلبا +Date_of_Discharge=شيلبا +Visit_Type=شيلبا