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
Conversation
@teleivo what do you think of this? :) |
@teleivo do you think we should merge this? :) |
@dkayiwa sorry but was covered in work, will test the PRs next week |
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]"; |
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 please address.
@@ -148,11 +149,11 @@ public void setDcTermsLicense(String dcTermsLicense) { | |||
this.dcTermsLicense = dcTermsLicense; | |||
} | |||
|
|||
public String getDcTermsDate() { | |||
public Date getDcTermsDate() { | |||
return dcTermsDate; |
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.
return new Date(dcTermsDate);
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.
dcTermsDate is already a date object. Why the need for the constructor again?
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.
Date is mutable. If you return it one can mutate the date bypassing our setter. Of course now the setter just sets the date, that's another point we might need to adapt.
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.
makes perfect sense. Am glad I asked. Let update now
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.
Hmm but there is no Date(Date) constructor though
@@ -62,6 +64,8 @@ | |||
|
|||
private static final String DCTERMS_CREATOR = "dcterms.creator"; | |||
|
|||
private static final String DATE_FORMAT = "yyyy-MM-dd"; |
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.
are you sure thats the only format we have to support? just asking :)
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.
Page 27 of the MRRT document says "This value shall be encoded in the XMLprimitive date format. Multiple instances of this parameter are not permitted."
A quick google search of "XMLprimitive date format" leads me here http://www.w3schools.com/xml/schema_dtypes_date.asp. Do you think I should modify it?
if (!elements.isEmpty()) { | ||
Element dcTermsDateElement = elements.get(0); | ||
try { | ||
new SimpleDateFormat("yyyy-MM-dd").parse(dcTermsDateElement.attr("content")); |
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.
you created a constant for the date format but you arent using it here. dangerous.
either extract the mrrt constants that are used in multiple places into a central class so it can be shared without introducing dependencies between classes which shouldnt have dependencies on each other or find a way to extract your rule of datevalidation so one could pass a dateformat to this rule. also make sure that the error message contains the dateformat so the user knows what format it needs to have. so the dateformat should be passed to the exception and used in the message with something like "date's need to be of format {0}"
@@ -53,4 +65,19 @@ public ValidationResult run(Elements subject) { | |||
} | |||
return validationResult; | |||
} | |||
|
|||
private final boolean validateDate(Elements elements) { | |||
boolean dateIsNotValid = false; |
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.
stick to calling the return variables "result" which we generally do
} | ||
catch (ParseException ex) { | ||
dateIsNotValid = true; | ||
log.debug(ex.getMessage(), ex); |
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.
wouldnt log that. make sure the violation is shown to the user with a proper message. thats the goal of this rule based system. give the user good information about whats wrong so he can correct his template. here I would say you just fill the log with info thats not worth to store there as the user can fix it easily.
@@ -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); |
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.
dont hard code the format of the date
@@ -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 |
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.
think about the user. would you know what to do with this message? or would you think "whats a valid date object"?
think: I am a radiologist importing a template. if I get a message like "date's need to be of format yyyy-MM-dd like for example 2016-01-24" I would know what to do
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.
ok. Will fix that
@ivange94 made some comments, please check them out |
@@ -53,4 +64,18 @@ public ValidationResult run(Elements subject) { | |||
} | |||
return validationResult; | |||
} | |||
|
|||
private final boolean validateDate(Elements elements) { |
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.
rename to isDateInvalid ; easier to read
1aa0f8d
to
59ec9d5
Compare
@@ -125,7 +132,7 @@ private final void initializeTemplate(MrrtReportTemplate template, Document doc) | |||
template.setDcTermsLicense(content); | |||
break; | |||
case DCTERMS_DATE: | |||
template.setDcTermsDate(content); | |||
template.setDcTermsDate(new SimpleDateFormat(MrrtReportTemplateConstants.DATE_FORMAT).parse(content)); |
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.
can you extract
new SimpleDateFormat(MrrtReportTemplateConstants.DATE_FORMAT).parse(content)
into a private method so its easier to read. For example
parseDcTermsDate(String content)
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.
fixed
@@ -148,11 +149,14 @@ public void setDcTermsLicense(String dcTermsLicense) { | |||
this.dcTermsLicense = dcTermsLicense; | |||
} | |||
|
|||
public String getDcTermsDate() { | |||
return dcTermsDate; | |||
public Date getDcTermsDate() { |
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 please address
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.
fixed
|
||
private final boolean isDateInvalid(Elements elements) { | ||
boolean result = false; | ||
if (!elements.isEmpty()) { |
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 can you turn this into a positive check/guard clause. so
if (elements.isEmpty()) {
return false;
}
this removes one level of nesting and makes it easier to read, since the reader see's immediately that if the elements is empty we return.
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.
fixed
*/ | ||
package org.openmrs.module.radiology.report.template; | ||
|
||
public final class MrrtReportTemplateConstants { |
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 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 comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -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 comment
The 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 comment
The 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.
initializeTemplate(result, doc); | ||
} | ||
catch (ParseException e) { | ||
throw new APIException("radiology.report.template.parser.error", null, e); |
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?
@@ -24,6 +29,10 @@ | |||
|
|||
static String SELECTOR_QUERY_META_ATTRIBUTE_NAME = "meta[name]"; | |||
|
|||
static String SELECTOR_QUERY_META_ATTRIBUTE_DATE = "meta[name=dcterms.date]"; | |||
|
|||
private static final Log log = LogFactory.getLog(MetaTagsValidationEngine.class); |
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 remove log, not used
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.
fixed
RAD-385 MrrtReportTemplate.dcTermsDate should be a Date object not a string
Description
Related Issue
see https://issues.openmrs.org/browse/RAD-385
Checklist:
git pull --rebase upstream master
.mvn clean package
right before creating this pull request andadded all formatting changes to my commit.