diff --git a/api/src/main/java/org/openmrs/module/radiology/report/template/DefaultMrrtReportTemplateFileParser.java b/api/src/main/java/org/openmrs/module/radiology/report/template/DefaultMrrtReportTemplateFileParser.java index d84ab51e6..e7c2c2cba 100644 --- a/api/src/main/java/org/openmrs/module/radiology/report/template/DefaultMrrtReportTemplateFileParser.java +++ b/api/src/main/java/org/openmrs/module/radiology/report/template/DefaultMrrtReportTemplateFileParser.java @@ -12,6 +12,8 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -62,6 +64,8 @@ class DefaultMrrtReportTemplateFileParser implements MrrtReportTemplateFileParse private static final String DCTERMS_CREATOR = "dcterms.creator"; + private static final String DATE_FORMAT = "yyyy-MM-dd"; + private MrrtReportTemplateValidator validator; public void setValidator(MrrtReportTemplateValidator validator) { @@ -78,7 +82,12 @@ public MrrtReportTemplate parse(String mrrtTemplate) throws IOException { final Document doc = Jsoup.parse(mrrtTemplate, ""); final MrrtReportTemplate result = new MrrtReportTemplate(); - initializeTemplate(result, doc); + try { + initializeTemplate(result, doc); + } + catch (ParseException e) { + throw new APIException("radiology.report.template.parser.error", null, e); + } try { addTermsToTemplate(result, doc.getElementsByTag("script") .get(0) @@ -90,7 +99,7 @@ public MrrtReportTemplate parse(String mrrtTemplate) throws IOException { return result; } - private final void initializeTemplate(MrrtReportTemplate template, Document doc) { + private final void initializeTemplate(MrrtReportTemplate template, Document doc) throws ParseException { final Elements metaTags = doc.getElementsByTag("meta"); template.setPath(doc.baseUri()); @@ -125,7 +134,7 @@ private final void initializeTemplate(MrrtReportTemplate template, Document doc) template.setDcTermsLicense(content); break; case DCTERMS_DATE: - template.setDcTermsDate(content); + template.setDcTermsDate(new SimpleDateFormat(DATE_FORMAT).parse(content)); break; case DCTERMS_CREATOR: template.setDcTermsCreator(content); diff --git a/api/src/main/java/org/openmrs/module/radiology/report/template/MetaTagsValidationEngine.java b/api/src/main/java/org/openmrs/module/radiology/report/template/MetaTagsValidationEngine.java index b481e4267..f9685c63d 100644 --- a/api/src/main/java/org/openmrs/module/radiology/report/template/MetaTagsValidationEngine.java +++ b/api/src/main/java/org/openmrs/module/radiology/report/template/MetaTagsValidationEngine.java @@ -9,21 +9,30 @@ */ package org.openmrs.module.radiology.report.template; -import org.jsoup.select.Elements; - +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; + /** * Validates tags of an Mrrt Report Template. */ class MetaTagsValidationEngine implements ValidationEngine { + private static final Log log = LogFactory.getLog(MetaTagsValidationEngine.class); + static String SELECTOR_QUERY_META_ATTRIBUTE_CHARSET = "meta[charset]"; static String SELECTOR_QUERY_META_ATTRIBUTE_NAME = "meta[name]"; + static String SELECTOR_QUERY_META_ATTRIBUTE_DATE = "meta[name=dcterms.date]"; + private List> rules; public MetaTagsValidationEngine() { @@ -35,6 +44,9 @@ public MetaTagsValidationEngine() { new ElementsExpressionValidationRule("At least one 'meta' element encoding dublin core attributes expected", "radiology.MrrtReportTemplate.validation.error.meta.dublinCore.missing", SELECTOR_QUERY_META_ATTRIBUTE_NAME, subject -> subject.isEmpty())); + rules.add(new ElementsExpressionValidationRule("dcterms.date element should be a valid date", + "radiology.MrrtReportTemplate.validation.error.date.invalid", SELECTOR_QUERY_META_ATTRIBUTE_DATE, + subject -> validateDate(subject))); } /** @@ -53,4 +65,19 @@ public ValidationResult run(Elements subject) { } return validationResult; } + + private final boolean validateDate(Elements elements) { + boolean dateIsNotValid = false; + if (!elements.isEmpty()) { + Element dcTermsDateElement = elements.get(0); + try { + new SimpleDateFormat("yyyy-MM-dd").parse(dcTermsDateElement.attr("content")); + } + catch (ParseException ex) { + dateIsNotValid = true; + log.debug(ex.getMessage(), ex); + } + } + return dateIsNotValid; + } } diff --git a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplate.java b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplate.java index 31505ce0f..4759bc49b 100644 --- a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplate.java +++ b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplate.java @@ -9,6 +9,7 @@ */ package org.openmrs.module.radiology.report.template; +import java.util.Date; import java.util.Set; import org.openmrs.BaseOpenmrsData; @@ -44,7 +45,7 @@ public class MrrtReportTemplate extends BaseOpenmrsData { private String dcTermsLicense; - private String dcTermsDate; + private Date dcTermsDate; private String dcTermsCreator; @@ -148,11 +149,11 @@ public void setDcTermsLicense(String dcTermsLicense) { this.dcTermsLicense = dcTermsLicense; } - public String getDcTermsDate() { + public Date getDcTermsDate() { return dcTermsDate; } - public void setDcTermsDate(String dcTermsDate) { + public void setDcTermsDate(Date dcTermsDate) { this.dcTermsDate = dcTermsDate; } diff --git a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidator.java b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidator.java index 10b47a8cf..8a8ca6982 100644 --- a/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidator.java +++ b/api/src/main/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidator.java @@ -50,6 +50,7 @@ public interface MrrtReportTemplateValidator { * @should throw api exception if html element does not have a body element * @should throw api exception if html element has more than one body element * @should catch all violation errors and throw an mrrt report template exception + * @should throw an mrrt report template exception if dcterms date of template file is not valid */ public void validate(String mrrtTemplate) throws IOException; } diff --git a/api/src/main/resources/MrrtReportTemplate.hbm.xml b/api/src/main/resources/MrrtReportTemplate.hbm.xml index 4b4885c80..cc5276304 100644 --- a/api/src/main/resources/MrrtReportTemplate.hbm.xml +++ b/api/src/main/resources/MrrtReportTemplate.hbm.xml @@ -33,7 +33,7 @@ - + diff --git a/api/src/main/resources/liquibase.xml b/api/src/main/resources/liquibase.xml index 8228e793e..4e9387d56 100644 --- a/api/src/main/resources/liquibase.xml +++ b/api/src/main/resources/liquibase.xml @@ -568,4 +568,8 @@ + + Change the datatype of dcterms_date from varchar(32) to date + + diff --git a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateFileParserComponentTest.java b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateFileParserComponentTest.java index bc26925da..7a73515b1 100644 --- a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateFileParserComponentTest.java +++ b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateFileParserComponentTest.java @@ -18,6 +18,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; import org.apache.commons.io.IOUtils; import org.junit.Before; @@ -120,6 +122,7 @@ public void parse_shouldReturnAnMrrtTemplateObjectIfFileIsValid() throws Excepti String templateContent = getFileContent("mrrttemplates/ihe/connectathon/2015/CTChestAbdomen.html"); MrrtReportTemplate template = parser.parse(templateContent); + Date dcTermsDate = new SimpleDateFormat("yyyy-MM-dd").parse(TEST_DCTERMS_DATE); assertNotNull(template); assertThat(template.getCharset(), is(CHARSET)); @@ -132,7 +135,7 @@ public void parse_shouldReturnAnMrrtTemplateObjectIfFileIsValid() throws Excepti assertThat(template.getDcTermsPublisher(), is(TEST_DCTERMS_PUBLISHER)); assertThat(template.getDcTermsRights(), is(TEST_DCTERMS_RIGHTS)); assertThat(template.getDcTermsLicense(), is(TEST_DCTERMS_LICENSE)); - assertThat(template.getDcTermsDate(), is(TEST_DCTERMS_DATE)); + assertThat(template.getDcTermsDate(), is(dcTermsDate)); assertThat(template.getDcTermsCreator(), is(TEST_DCTERMS_CREATOR)); } diff --git a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidatorComponentTest.java b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidatorComponentTest.java index 386f9a8df..cd8b67fd2 100644 --- a/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidatorComponentTest.java +++ b/api/src/test/java/org/openmrs/module/radiology/report/template/MrrtReportTemplateValidatorComponentTest.java @@ -407,4 +407,17 @@ public void validate_catchAllViolationErrorsAndThrowAnMrrtReportTemplateExceptio is(4)); } } + + /** + * @see MrrtReportTemplateValidator#validate(String) + * @verifies throw an mrrt report template exception if dcterms date of template file is not valid + */ + @Test + public void validate_shouldThrowAnMrrtReportTemplateExceptionIfDctermsDateOfTemplateFileIsNotValid() throws Exception { + + String templateContent = getFileContent( + "mrrttemplates/ihe/connectathon/2015/invalidMrrtReportTemplate-dcTermsDateIsNotAValidDateObject.html"); + expectedException.expect(MrrtReportTemplateValidationException.class); + validator.validate(templateContent); + } } diff --git a/api/src/test/resources/mrrttemplates/ihe/connectathon/2015/invalidMrrtReportTemplate-dcTermsDateIsNotAValidDateObject.html b/api/src/test/resources/mrrttemplates/ihe/connectathon/2015/invalidMrrtReportTemplate-dcTermsDateIsNotAValidDateObject.html new file mode 100644 index 000000000..1f2f7a4a4 --- /dev/null +++ b/api/src/test/resources/mrrttemplates/ihe/connectathon/2015/invalidMrrtReportTemplate-dcTermsDateIsNotAValidDateObject.html @@ -0,0 +1,41 @@ + + + + CT Chest-Abdomen + + + + + + + + + + + + + + + +
+
Section Header
+

This is the CT Chest-Abdomen report template

+
+ + \ No newline at end of file diff --git a/omod/src/main/resources/messages.properties b/omod/src/main/resources/messages.properties index 9095c1585..b0dc2162e 100644 --- a/omod/src/main/resources/messages.properties +++ b/omod/src/main/resources/messages.properties @@ -181,6 +181,7 @@ @MODULE_ID@.MrrtReportTemplate.not.imported.empty=Failed to import report template because it was empty @MODULE_ID@.MrrtReportTemplate.validation.error.meta.charset.occurence=Template file should have exactly one 'meta' element with attribute 'charset' @MODULE_ID@.MrrtReportTemplate.validation.error.meta.dublinCore.missing=Template file should have at least one 'meta' element encoding dublin core attributes +@MODULE_ID@.MrrtReportTemplate.validation.error.date.invalid=dcterms.date of Template file should be a valid date object @MODULE_ID@.patientId=Patient Id @MODULE_ID@.patientFullName=Patient Full Name