-
Notifications
You must be signed in to change notification settings - Fork 65
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
RAD-385 MrrtReportTemplate.dcTermsDate should be a Date object not a string #499
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,16 @@ | |
*/ | ||
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 <meta> tags of an Mrrt Report Template. | ||
*/ | ||
|
@@ -24,6 +29,8 @@ class MetaTagsValidationEngine implements ValidationEngine<Elements> { | |
|
||
static String SELECTOR_QUERY_META_ATTRIBUTE_NAME = "meta[name]"; | ||
|
||
static String SELECTOR_QUERY_META_ATTRIBUTE_DATE = "meta[name=dcterms.date]"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ivange94 please address. |
||
|
||
private List<ValidationRule<Elements>> rules; | ||
|
||
public MetaTagsValidationEngine() { | ||
|
@@ -35,6 +42,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 -> isDateInvalid(subject))); | ||
} | ||
|
||
/** | ||
|
@@ -53,4 +63,19 @@ public ValidationResult run(Elements subject) { | |
} | ||
return validationResult; | ||
} | ||
|
||
private final boolean isDateInvalid(Elements elements) { | ||
boolean result = false; | ||
if (elements.isEmpty()) { | ||
return result; | ||
} | ||
Element dcTermsDateElement = elements.get(0); | ||
try { | ||
new SimpleDateFormat(MrrtReportTemplateConstants.DATE_FORMAT).parse(dcTermsDateElement.attr("content")); | ||
} | ||
catch (ParseException ex) { | ||
result = true; | ||
} | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,19 @@ public void setDcTermsLicense(String dcTermsLicense) { | |
this.dcTermsLicense = dcTermsLicense; | ||
} | ||
|
||
public String getDcTermsDate() { | ||
return dcTermsDate; | ||
/** | ||
* Get dcterms date property of an MRRT report template. | ||
* | ||
* @return Date date property of the MRRT report template or null if no date was set | ||
*/ | ||
public Date getDcTermsDate() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ivange94 please address There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
if (dcTermsDate == null) { | ||
return dcTermsDate; | ||
} | ||
return new Date(dcTermsDate.getTime()); | ||
} | ||
|
||
public void setDcTermsDate(String dcTermsDate) { | ||
public void setDcTermsDate(Date dcTermsDate) { | ||
this.dcTermsDate = dcTermsDate; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under | ||
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license. | ||
* | ||
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS | ||
* graphic logo is a trademark of OpenMRS Inc. | ||
*/ | ||
package org.openmrs.module.radiology.report.template; | ||
|
||
/** | ||
* Utility class that contains constants which are used within this module. | ||
*/ | ||
public final class MrrtReportTemplateConstants { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ivange94 please also add a private constructor like so as this class should only serve as a container for constants. also add javadocs to the class and the constant (refer to the section in the IHE standard where you got the format from). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
|
||
/** | ||
* Constant for dcterms.date format for an {@code MrrtReportTemplate}. | ||
* | ||
* See table "Table 4.105.4.1.2-1: HTTP Query Parameters" on page 27 of | ||
* the MRRT Standards document. | ||
*/ | ||
public static final String DATE_FORMAT = "yyyy-MM-dd"; | ||
|
||
private MrrtReportTemplateConstants() { | ||
// Utility class not meant to be instantiated. | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(MrrtReportTemplateConstants.DATE_FORMAT).parse(TEST_DCTERMS_DATE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ivange94 this seems bit lost here. simply change the type of TEST_DCTERMS_DATE from String to Date. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no Date constructor that can construct a Date instance from a String. So even if I change type of TEST_DCTERMS_DATE, i still need to put the logic(which I am doing here by using the SimpleDateFormat.parse()) that will convert the string to a date. So I'll end up with something like this private static final Date TEST_DCTERMS_DATE = new SimpleDateFormat(format).parse("2013-06-01"); And since the parse method throws and exception I will now have to declare it the class to throw that exception. I thought it would be better to throw the exception at a method level since all our tests throw exceptions anyways. There is actually a couple constructors I can call without using the formatter but they are all deprecated. |
||
|
||
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)); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ivange94 shouldnt this exception be the same as in the validation?
since the exception comes from your change which is the parsing of the date.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understand this point. The code that validates the template is different from the code that parses and they both throw this exception. Did you mean I should not have caught it here too?