Skip to content

Commit 78a7e78

Browse files
Process subreports from generating endpoint, refactor resource bundle usage
1 parent 42c951f commit 78a7e78

File tree

5 files changed

+30
-47
lines changed

5 files changed

+30
-47
lines changed

src/main/java/org/openlmis/report/service/JasperTemplateService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,9 @@ public Map<String, BufferedImage> mapReportImagesToTemplate(JasperTemplate templ
212212
* @return the locale bundle parameters
213213
* @throws MalformedURLException the malformed url exception
214214
*/
215-
public Map<String, Object> getLocaleBundleParameters(JasperReport parentReport,
216-
String userLocaleString)
215+
public Map<String, Object> getLocaleBundleParameters(String userLocaleString)
217216
throws MalformedURLException {
218-
String resourceBundleName = parentReport != null ? parentReport.getResourceBundle() : null;
219-
if (resourceBundleName == null || resourceBundleName.trim().isEmpty()) {
217+
if (userLocaleString == null) {
220218
return Collections.emptyMap();
221219
}
222220

src/main/java/org/openlmis/report/web/GenerateReportController.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public ResponseEntity<byte[]> generateReport(@RequestBody GenerateReportDto requ
8787
JasperReport templateReport = jasperTemplateService.loadReport(request.getTemplate());
8888

8989
processDataSource(params);
90+
processSubreports(params);
9091
addTranslationsAndHeaders(templateReport, params, templateName);
9192
addFormattingParameters(params);
9293

@@ -116,11 +117,26 @@ private void processDataSource(Map<String, Object> params) {
116117
}
117118
}
118119

120+
private void processSubreports(Map<String, Object> params) {
121+
if (params.containsKey("subreport_bytes")) {
122+
try {
123+
String base64String = (String) params.get("subreport_bytes");
124+
byte[] subreportData = java.util.Base64.getDecoder().decode(base64String);
125+
126+
JasperReport compiledSubreport = jasperTemplateService.loadReport(subreportData);
127+
params.put("subreport", compiledSubreport);
128+
params.remove("subreport_bytes");
129+
} catch (Exception e) {
130+
LOGGER.error("Failed to decode and compile subreport", e);
131+
}
132+
}
133+
}
134+
119135
private void addTranslationsAndHeaders(JasperReport templateReport, Map<String, Object> params,
120136
String templateName) {
121137
try {
122-
String lang = params.containsKey("lang") ? String.valueOf(params.get("lang")) : "en";
123-
params.putAll(jasperTemplateService.getLocaleBundleParameters(templateReport, lang));
138+
String lang = params.containsKey("lang") ? String.valueOf(params.get("lang")) : null;
139+
params.putAll(jasperTemplateService.getLocaleBundleParameters(lang));
124140
params.putAll(jasperTemplateService.getMapSubreportGlobalHeaderParameters(templateReport));
125141
} catch (MalformedURLException e) {
126142
LOGGER.debug("Cannot load translation bundle for {}", templateName);

src/main/java/org/openlmis/report/web/JasperTemplateController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public ResponseEntity<byte[]> generateReport(
208208

209209
try {
210210
JasperReport templateReport = jasperTemplateService.loadReport(template);
211-
map.putAll(jasperTemplateService.getLocaleBundleParameters(templateReport, lang));
211+
map.putAll(jasperTemplateService.getLocaleBundleParameters(lang));
212212
map.putAll(jasperTemplateService.getMapSubreportGlobalHeaderParameters(templateReport));
213213
} catch (ReportingException e) {
214214
LOGGER.debug("Cannot compile template {}", template.getName());

src/test/java/org/openlmis/report/service/JasperTemplateServiceTest.java

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -622,28 +622,12 @@ public void mapReportImagesToTemplateShouldReturnMatchingImages()
622622

623623
@Test
624624
public void getLocaleBundleShouldReturnEmptyMapForInvalidResourceBundleNames() throws Exception {
625-
assertTrue(jasperTemplateService.getLocaleBundleParameters(null, "en").isEmpty());
626-
627-
JasperReport parentReport = mock(JasperReport.class);
628-
629-
when(parentReport.getResourceBundle()).thenReturn(null);
630-
assertTrue(jasperTemplateService.getLocaleBundleParameters(parentReport, "en").isEmpty());
631-
632-
// 3. Covers: resourceBundleName.isEmpty()
633-
when(parentReport.getResourceBundle()).thenReturn("");
634-
assertTrue(jasperTemplateService.getLocaleBundleParameters(parentReport, "en").isEmpty());
635-
636-
// 4. Covers: resourceBundleName.trim().isEmpty()
637-
when(parentReport.getResourceBundle()).thenReturn(" ");
638-
assertTrue(jasperTemplateService.getLocaleBundleParameters(parentReport, "en").isEmpty());
625+
assertTrue(jasperTemplateService.getLocaleBundleParameters(null).isEmpty());
639626
}
640627

641628
@Test
642629
public void getLocaleBundleShouldReturnEmptyMapIfResourceBundleDirectoryDoesNotExist()
643630
throws Exception {
644-
JasperReport parentReport = mock(JasperReport.class);
645-
when(parentReport.getResourceBundle()).thenReturn(RESOURCE_BUNDLE_NAME);
646-
647631
File mockDir = mock(File.class);
648632
whenNew(File.class).withArguments(RESOURCE_BUNDLE_PATH).thenReturn(mockDir);
649633

@@ -653,19 +637,16 @@ public void getLocaleBundleShouldReturnEmptyMapIfResourceBundleDirectoryDoesNotE
653637
when(ResourceBundle.getBundle(eq(RESOURCE_BUNDLE_NAME), any(Locale.class)))
654638
.thenThrow(new MissingResourceException("Test", RESOURCE_BUNDLE_NAME, "key"));
655639

656-
assertTrue(jasperTemplateService.getLocaleBundleParameters(parentReport, "en").isEmpty());
640+
assertTrue(jasperTemplateService.getLocaleBundleParameters("en").isEmpty());
657641

658642
when(mockDir.exists()).thenReturn(true);
659643
when(mockDir.isDirectory()).thenReturn(false);
660-
assertTrue(jasperTemplateService.getLocaleBundleParameters(parentReport, "en").isEmpty());
644+
assertTrue(jasperTemplateService.getLocaleBundleParameters("en").isEmpty());
661645
}
662646

663647
@Test
664648
public void getLocaleBundleShouldFallbackToInternalBundleWhenConfigDirectoryNotFound()
665649
throws Exception {
666-
JasperReport parentReport = mock(JasperReport.class);
667-
when(parentReport.getResourceBundle()).thenReturn(RESOURCE_BUNDLE_NAME);
668-
669650
File mockDir = mock(File.class);
670651
whenNew(File.class).withArguments(RESOURCE_BUNDLE_PATH).thenReturn(mockDir);
671652
when(mockDir.exists()).thenReturn(false);
@@ -677,7 +658,7 @@ public void getLocaleBundleShouldFallbackToInternalBundleWhenConfigDirectoryNotF
677658
.thenReturn(fallbackBundle);
678659

679660
Map<String, Object> result = jasperTemplateService
680-
.getLocaleBundleParameters(parentReport, "en");
661+
.getLocaleBundleParameters("en");
681662

682663
assertEquals(2, result.size());
683664
assertEquals(fallbackBundle, result.get(JRParameter.REPORT_RESOURCE_BUNDLE));
@@ -687,9 +668,6 @@ public void getLocaleBundleShouldFallbackToInternalBundleWhenConfigDirectoryNotF
687668
@Test
688669
public void getLocaleBundleShouldFallbackToInternalBundleWhenConfigBundleNotFound()
689670
throws Exception {
690-
JasperReport parentReport = mock(JasperReport.class);
691-
when(parentReport.getResourceBundle()).thenReturn(RESOURCE_BUNDLE_NAME);
692-
693671
File mockDir = mock(File.class);
694672
whenNew(File.class).withArguments(RESOURCE_BUNDLE_PATH).thenReturn(mockDir);
695673
when(mockDir.exists()).thenReturn(true);
@@ -709,7 +687,7 @@ public void getLocaleBundleShouldFallbackToInternalBundleWhenConfigBundleNotFoun
709687
.thenReturn(fallbackBundle);
710688

711689
Map<String, Object> result = jasperTemplateService
712-
.getLocaleBundleParameters(parentReport, "fr");
690+
.getLocaleBundleParameters("fr");
713691

714692
assertEquals(2, result.size());
715693
assertEquals(fallbackBundle, result.get(JRParameter.REPORT_RESOURCE_BUNDLE));
@@ -720,9 +698,6 @@ public void getLocaleBundleShouldFallbackToInternalBundleWhenConfigBundleNotFoun
720698
@Test
721699
public void getLocaleBundleShouldReturnEmptyMapIfTranslationBundleIsMissingFromDisk()
722700
throws Exception {
723-
JasperReport parentReport = mock(JasperReport.class);
724-
when(parentReport.getResourceBundle()).thenReturn(RESOURCE_BUNDLE_NAME);
725-
726701
File mockDir = mock(File.class);
727702
whenNew(File.class).withArguments(RESOURCE_BUNDLE_PATH).thenReturn(mockDir);
728703
when(mockDir.exists()).thenReturn(true);
@@ -734,14 +709,11 @@ public void getLocaleBundleShouldReturnEmptyMapIfTranslationBundleIsMissingFromD
734709
when(ResourceBundle.getBundle(anyString(), any(Locale.class), any(URLClassLoader.class)))
735710
.thenThrow(new MissingResourceException("Missing resource exception occurred",
736711
"ResourceBundle", "key"));
737-
assertTrue(jasperTemplateService.getLocaleBundleParameters(parentReport, "en").isEmpty());
712+
assertTrue(jasperTemplateService.getLocaleBundleParameters("en").isEmpty());
738713
}
739714

740715
@Test
741716
public void getLocaleBundleShouldReturnMapWithBundleAndLocale() throws Exception {
742-
JasperReport parentReport = mock(JasperReport.class);
743-
when(parentReport.getResourceBundle()).thenReturn(RESOURCE_BUNDLE_NAME);
744-
745717
// Mock the Config Directory
746718
File mockDir = mock(File.class);
747719
whenNew(File.class).withArguments(RESOURCE_BUNDLE_PATH).thenReturn(mockDir);
@@ -755,7 +727,7 @@ public void getLocaleBundleShouldReturnMapWithBundleAndLocale() throws Exception
755727
any(URLClassLoader.class))).thenReturn(mockBundle);
756728

757729
Map<String, Object> result = jasperTemplateService
758-
.getLocaleBundleParameters(parentReport, "fr");
730+
.getLocaleBundleParameters("fr");
759731

760732
assertEquals(2, result.size());
761733
assertEquals(mockBundle, result.get(JRParameter.REPORT_RESOURCE_BUNDLE));
@@ -765,9 +737,6 @@ public void getLocaleBundleShouldReturnMapWithBundleAndLocale() throws Exception
765737

766738
@Test
767739
public void getLocaleBundleShouldFallbackToEnglishForInvalidLocale() throws Exception {
768-
JasperReport parentReport = mock(JasperReport.class);
769-
when(parentReport.getResourceBundle()).thenReturn(RESOURCE_BUNDLE_NAME);
770-
771740
File mockDir = mock(File.class);
772741
whenNew(File.class).withArguments(RESOURCE_BUNDLE_PATH).thenReturn(mockDir);
773742
when(mockDir.exists()).thenReturn(true);
@@ -780,7 +749,7 @@ public void getLocaleBundleShouldFallbackToEnglishForInvalidLocale() throws Exce
780749
any(URLClassLoader.class))).thenReturn(mockBundle);
781750

782751
Map<String, Object> result = jasperTemplateService
783-
.getLocaleBundleParameters(parentReport, "invalid_locale");
752+
.getLocaleBundleParameters("invalid_locale");
784753

785754
assertEquals(Locale.ENGLISH, result.get(JRParameter.REPORT_LOCALE));
786755
}

src/test/java/org/openlmis/report/web/GenerateReportControllerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public void shouldInjectFormattingParameters() throws Exception {
173173

174174
@Test
175175
public void shouldHandleTranslationExceptionsGracefully() throws Exception {
176-
when(jasperTemplateService.getLocaleBundleParameters(any(), anyString()))
176+
when(jasperTemplateService.getLocaleBundleParameters(anyString()))
177177
.thenThrow(new MalformedURLException("Simulated failure"));
178178

179179
Map<String, Object> params = new HashMap<>();

0 commit comments

Comments
 (0)