Thank you, but that article describes how to apply format separately to each field. Defining format on multiple fields makes template hard to read because it becomes filled with tags. If I follow the article, template would contain a lot of repetitive formatting
Created: <<[item.created]:"yyyy.MM.dd hh:mm a">> Updated: <<[item.updated]:"yyyy.MM.dd hh:mm a">> Closed: <<[item.closed]:"yyyy.MM.dd hh:mm a">> Due Date: <<[item.dueDate]:"yyyy.MM.dd hh:mm a">>
if there was some kind of IDatasourceResultFormatter for datasource values (like there is IFieldResultFormatter for MS Word fields), template would be more readable, and formatting could be done in java code
Created: <<[item.created]>> Updated: <<[item.updated]>> Closed: <<[item.closed]>> Due Date: <<[item.dueDate]>>
Moreover, if you want to show some default value like N/A
instead of null
, readability becomes even worse:
Created: <<if [item.created == null]>>N/A<<else>><<[item.created]:"yyyy.MM.dd hh:mm a">><</if>> Updated: <<if [item.updated == null]>>N/A<<else>><<[item.updated]:"yyyy.MM.dd hh:mm a">><</if>> Closed: <<if [item.closed == null]>>N/A<<else>><<[item.closed]:"yyyy.MM.dd hh:mm a">><</if>> Due Date: <<if [item.dueDate == null]>>N/A<<else>><<[item.dueDate]:"yyyy.MM.dd hh:mm a">><</if>>
if there was some kind of IDatasourceResultFormatter for datasource values, template would be just
Created: <<[item.created]>> Updated: <<[item.updated]>> Closed: <<[item.closed]>> Due Date: <<[item.dueDate]>>
and IDatasourceResultFormatter could handle replacing nulls with desired default value, as well as provide default formatting:
private class DatasourceResultFormatter implements IDatasourceResultFormatter {
private String formatValue(Object value, String format) {
if (value == null) return "N/A";
return String.format(format == null ? "yyyy.MM.dd hh:mm a" : format, value);
}
public String formatNumeric(Double value, String format) {
formatValue(value, format);
}
public String formatDateTime(Date value, String format) {
formatValue(value, format);
}
public String formatString(String value, String format) {
formatValue(value, format);
}
}
In case of Time zone or locale issues, such a formatter would allow to workaround it easily in formatDateTime method for all existing templates by applying timezone, that is why i had referenced WORDSJAVA-2097 in this context before.
To summarize, this is more about flexibility and readability of template. I understand that this all can be done by adding more tags and methods calls to templates. But having such a formatter would allow to apply changes without any changes in templates, and would provide one entry point for default data formatting and workarounds.
Not sure if that is possible, just a proposal for feature.