Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OLMIS-3963: Allow Specification of Report Formats #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/org/openlmis/report/domain/JasperTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public class JasperTemplate extends BaseEntity {
@Setter
private String description;

@ElementCollection
@Getter
@Setter
private List<String> supportedFormats;

@ElementCollection
@CollectionTable
@Getter
Expand Down Expand Up @@ -110,6 +115,7 @@ public void export(Exporter exporter) {
exporter.setId(id);
exporter.setName(name);
exporter.setType(type);
exporter.setSupportedFormats(supportedFormats);
}

@PrePersist
Expand All @@ -136,5 +142,7 @@ public interface Exporter {
void setDescription(String description);

void setRequiredRights(List<String> rights);

void setSupportedFormats(List<String> formats);
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/openlmis/report/dto/JasperTemplateDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public class JasperTemplateDto implements JasperTemplate.Exporter {
@Setter
private List<String> requiredRights;

@Getter
@Setter
private List<String> supportedFormats;

@Getter
@Setter
private List<JasperTemplateParameter.Exporter> templateParameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public class JasperTemplateService {
static final String REPORT_TYPE_PROPERTY = "reportType";
private static final String DEFAULT_REPORT_TYPE = "Consistency Report";
private static final String[] ALLOWED_FILETYPES = {"jrxml"};

protected static final String SUPPORTED_FORMATS_PROPERTY = "supportedFormats";

@Autowired
private JasperTemplateRepository jasperTemplateRepository;

Expand Down Expand Up @@ -231,6 +232,11 @@ private void validateFileAndSetData(JasperTemplate jasperTemplate, MultipartFile
jasperTemplate.setType(reportType);
}

String formats = report.getProperty(SUPPORTED_FORMATS_PROPERTY);
if (formats != null) {
jasperTemplate.setSupportedFormats(extractListProperties(formats));
}

JRParameter[] jrParameters = report.getParameters();

if (jrParameters != null && jrParameters.length > 0) {
Expand Down Expand Up @@ -347,12 +353,15 @@ private List<JasperTemplateParameterDependency> extractDependencies(JRParameter
}

private List<String> extractListProperties(JRParameter parameter, String property) {
String dependencyProperty = parameter.getPropertiesMap().getProperty(property);
return extractListProperties(
parameter.getPropertiesMap().getProperty(property));
}

if (dependencyProperty != null) {
private List<String> extractListProperties(String property) {
if (property != null) {
// split by unescaped commas
return Arrays
.stream(dependencyProperty.split("(?<!\\\\),"))
.stream(property.split("(?<!\\\\),"))
.map(option -> option.replace("\\,", ","))
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--
-- Name: jaspertemplate_supportedformats; Type: TABLE; Schema: reports; Owner: postgres
--

CREATE TABLE jaspertemplate_supportedformats (
jaspertemplateid uuid NOT NULL,
supportedformats character varying(255)
);

--
-- Name: jaspertemplate_supportedformats fkwnf16ufb6p8t2b6fasz39fgse; Type: FK CONSTRAINT; Schema: reports; Owner: postgres
--

ALTER TABLE ONLY jaspertemplate_supportedformats
ADD CONSTRAINT fkwnf16ufb6p8t2b6fasz39fgse FOREIGN KEY (jaspertemplateid) REFERENCES jasper_templates(id);
9 changes: 8 additions & 1 deletion src/main/resources/schemas/jasperTemplateDto.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@
"description": {
"type": ["string", "null"],
"title": "description"
},
"supportedFormats": {
"type": ["array", "null"],
"title": "supportedFormats",
"items": {
"type": "string"
}
}
} ,
},
"required": [
"id",
"name"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
Expand All @@ -88,6 +90,7 @@
import static org.openlmis.report.i18n.ReportingMessageKeys.ERROR_REPORTING_PARAMETER_MISSING;
import static org.openlmis.report.i18n.ReportingMessageKeys.ERROR_REPORTING_TEMPLATE_EXIST;
import static org.openlmis.report.service.JasperTemplateService.REPORT_TYPE_PROPERTY;
import static org.openlmis.report.service.JasperTemplateService.SUPPORTED_FORMATS_PROPERTY;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
Expand Down Expand Up @@ -330,10 +333,13 @@ public void shouldValidateFileAndSetData() throws Exception {
JRPropertiesMap propertiesMap = mock(JRPropertiesMap.class);
JRExpression jrExpression = mock(JRExpression.class);

String[] propertyNames = {DISPLAY_NAME};
when(report.getParameters()).thenReturn(new JRParameter[]{param1, param2, param3});
when(report.getProperty(REPORT_TYPE_PROPERTY)).thenReturn("test type");
when(report.getProperty(SUPPORTED_FORMATS_PROPERTY)).thenReturn("csv,xls");

when(report.getParameters()).thenReturn(new JRParameter[]{param1, param2, param3});
when(JasperCompileManager.compileReport(inputStream)).thenReturn(report);

String[] propertyNames = {DISPLAY_NAME};
when(propertiesMap.getPropertyNames()).thenReturn(propertyNames);
when(propertiesMap.getProperty(DISPLAY_NAME)).thenReturn(PARAM_DISPLAY_NAME);
when(propertiesMap.getProperty(REQUIRED)).thenReturn("true");
Expand Down Expand Up @@ -376,6 +382,9 @@ public void shouldValidateFileAndSetData() throws Exception {
verify(jasperTemplateRepository).save(jasperTemplate);

assertEquals("test type", jasperTemplate.getType());
assertThat(jasperTemplate.getSupportedFormats(), hasSize(2));
assertThat(jasperTemplate.getSupportedFormats(), hasItems("csv", "xls"));

assertThat(jasperTemplate.getTemplateParameters().get(0).getDisplayName(),
is(PARAM_DISPLAY_NAME));
assertThat(jasperTemplate.getTemplateParameters().get(0).getDescription(), is("desc"));
Expand Down