Rendering improvements: partial dates, questionnaire links, and patient renderer

This commit is contained in:
Grahame Grieve 2022-02-12 08:27:29 +11:00
parent 76ed4abba7
commit fb1a08dca8
7 changed files with 101 additions and 24 deletions

View File

@ -1,5 +1,9 @@
package org.hl7.fhir.r5.renderers; package org.hl7.fhir.r5.renderers;
import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -9,8 +13,12 @@ import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.time.chrono.IsoChronology;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle; import java.time.format.FormatStyle;
import java.time.format.ResolverStyle;
import java.time.format.SignStyle;
import java.util.Currency; import java.util.Currency;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
@ -39,6 +47,7 @@ import org.hl7.fhir.r5.model.DataRequirement.SortDirection;
import org.hl7.fhir.r5.model.ContactPoint.ContactPointSystem; import org.hl7.fhir.r5.model.ContactPoint.ContactPointSystem;
import org.hl7.fhir.r5.model.DataType; import org.hl7.fhir.r5.model.DataType;
import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.DateTimeType;
import org.hl7.fhir.r5.model.DateType;
import org.hl7.fhir.r5.model.Enumeration; import org.hl7.fhir.r5.model.Enumeration;
import org.hl7.fhir.r5.model.Expression; import org.hl7.fhir.r5.model.Expression;
import org.hl7.fhir.r5.model.Extension; import org.hl7.fhir.r5.model.Extension;
@ -413,15 +422,8 @@ public class DataRenderer extends Renderer {
// mode - if rendering mode is technical, format defaults to XML format // mode - if rendering mode is technical, format defaults to XML format
// locale - otherwise, format defaults to SHORT for the Locale (which defaults to default Locale) // locale - otherwise, format defaults to SHORT for the Locale (which defaults to default Locale)
if (isOnlyDate(type.getPrecision())) { if (isOnlyDate(type.getPrecision())) {
DateTimeFormatter fmt = context.getDateFormat();
if (fmt == null) {
if (context.isTechnicalMode()) {
fmt = DateTimeFormatter.ISO_DATE;
} else {
fmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(context.getLocale());
}
}
DateTimeFormatter fmt = getDateFormatForPrecision(type);
LocalDate date = LocalDate.of(type.getYear(), type.getMonth()+1, type.getDay()); LocalDate date = LocalDate.of(type.getYear(), type.getMonth()+1, type.getDay());
return fmt.format(date); return fmt.format(date);
} }
@ -442,6 +444,43 @@ public class DataRenderer extends Renderer {
return fmt.format(zdt); return fmt.format(zdt);
} }
private DateTimeFormatter getDateFormatForPrecision(BaseDateTimeType type) {
DateTimeFormatter fmt = getContextDateFormat(type);
if (fmt != null) {
return fmt;
}
if (context.isTechnicalMode()) {
switch (type.getPrecision()) {
case YEAR:
return new DateTimeFormatterBuilder().appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD).toFormatter();
case MONTH:
return new DateTimeFormatterBuilder().appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD).appendLiteral('-').appendValue(MONTH_OF_YEAR, 2).toFormatter();
default:
return DateTimeFormatter.ISO_DATE;
}
} else {
switch (type.getPrecision()) {
case YEAR:
return DateTimeFormatter.ofPattern("uuuu");
case MONTH:
return DateTimeFormatter.ofPattern("MMM uuuu");
default:
return DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).withLocale(context.getLocale());
}
}
}
private DateTimeFormatter getContextDateFormat(BaseDateTimeType type) {
switch (type.getPrecision()) {
case YEAR:
return context.getDateYearFormat();
case MONTH:
return context.getDateYearMonthFormat();
default:
return context.getDateFormat();
}
}
private boolean isOnlyDate(TemporalPrecisionEnum temporalPrecisionEnum) { private boolean isOnlyDate(TemporalPrecisionEnum temporalPrecisionEnum) {
return temporalPrecisionEnum == TemporalPrecisionEnum.YEAR || temporalPrecisionEnum == TemporalPrecisionEnum.MONTH || temporalPrecisionEnum == TemporalPrecisionEnum.DAY; return temporalPrecisionEnum == TemporalPrecisionEnum.YEAR || temporalPrecisionEnum == TemporalPrecisionEnum.MONTH || temporalPrecisionEnum == TemporalPrecisionEnum.DAY;
} }
@ -533,6 +572,12 @@ public class DataRenderer extends Renderer {
} }
} }
public void renderDate(XhtmlNode x, Base e) {
if (e.hasPrimitiveValue()) {
x.addText(displayDateTime((DateType) e));
}
}
public void renderDateTime(XhtmlNode x, String s) { public void renderDateTime(XhtmlNode x, String s) {
if (s != null) { if (s != null) {
DateTimeType dt = new DateTimeType(s); DateTimeType dt = new DateTimeType(s);

View File

@ -4,6 +4,8 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import org.apache.poi.hssf.record.chart.DatRecord; import org.apache.poi.hssf.record.chart.DatRecord;
import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRFormatError;
import org.hl7.fhir.r5.model.DateTimeType; import org.hl7.fhir.r5.model.DateTimeType;
import org.hl7.fhir.r5.model.DateType; import org.hl7.fhir.r5.model.DateType;
import org.hl7.fhir.r5.model.DomainResource; import org.hl7.fhir.r5.model.DomainResource;
@ -219,5 +221,7 @@ public class PatientRenderer extends ResourceRenderer {
} }
} }
// @Override
//// public boolean render(XhtmlNode x, ResourceWrapper r) throws FHIRFormatError, DefinitionException, IOException {
// }
} }

View File

@ -312,9 +312,7 @@ public class ProfileDrivenRenderer extends ResourceRenderer {
x.addText(new Base64().encodeAsString(((Base64BinaryType) e).getValue())); x.addText(new Base64().encodeAsString(((Base64BinaryType) e).getValue()));
else if (e instanceof org.hl7.fhir.r5.model.DateType) { else if (e instanceof org.hl7.fhir.r5.model.DateType) {
org.hl7.fhir.r5.model.DateType dt = ((org.hl7.fhir.r5.model.DateType) e); org.hl7.fhir.r5.model.DateType dt = ((org.hl7.fhir.r5.model.DateType) e);
if (((org.hl7.fhir.r5.model.DateType) e).hasValue()) { renderDate(x, dt);
x.addText(((org.hl7.fhir.r5.model.DateType) e).toHumanDisplay());
}
} else if (e instanceof Enumeration) { } else if (e instanceof Enumeration) {
Object ev = ((Enumeration<?>) e).getValue(); Object ev = ((Enumeration<?>) e).getValue();
x.addText(ev == null ? "" : ev.toString()); // todo: look up a display name if there is one x.addText(ev == null ? "" : ev.toString()); // todo: look up a display name if there is one

View File

@ -712,7 +712,7 @@ public class QuestionnaireRenderer extends TerminologyRenderer {
XhtmlNode ans = item(ul, "Answers"); XhtmlNode ans = item(ul, "Answers");
if (!Utilities.noString(i.getAnswerValueSet()) && i.getAnswerValueSet().startsWith("#")) { if (!Utilities.noString(i.getAnswerValueSet()) && i.getAnswerValueSet().startsWith("#")) {
ValueSet vs = (ValueSet) q.getContained(i.getAnswerValueSet().substring(1)); ValueSet vs = (ValueSet) q.getContained(i.getAnswerValueSet().substring(1));
if (vs == null) { if (vs == null || !vs.hasUserData("path")) {
ans.tx(i.getAnswerValueSet()); ans.tx(i.getAnswerValueSet());
} else { } else {
ans.ah(vs.getUserString("path")).tx(vs.present()); ans.ah(vs.getUserString("path")).tx(vs.present());

View File

@ -114,6 +114,9 @@ public class RendererFactory {
if ("Library".equals(resource.getName())) { if ("Library".equals(resource.getName())) {
return new LibraryRenderer(context); return new LibraryRenderer(context);
} }
if ("Patient".equals(resource.getName())) {
return new PatientRenderer(context);
}
if ("DiagnosticReport".equals(resource.getName())) { if ("DiagnosticReport".equals(resource.getName())) {
return new DiagnosticReportRenderer(context); return new DiagnosticReportRenderer(context);
} }

View File

@ -127,6 +127,8 @@ public class RenderingContext {
private ZoneId timeZoneId; private ZoneId timeZoneId;
private DateTimeFormatter dateTimeFormat; private DateTimeFormatter dateTimeFormat;
private DateTimeFormatter dateFormat; private DateTimeFormatter dateFormat;
private DateTimeFormatter dateYearFormat;
private DateTimeFormatter dateYearMonthFormat;
/** /**
* *
@ -359,6 +361,15 @@ public class RenderingContext {
res.definitionsTarget = definitionsTarget; res.definitionsTarget = definitionsTarget;
res.destDir = destDir; res.destDir = destDir;
res.addGeneratedNarrativeHeader = addGeneratedNarrativeHeader; res.addGeneratedNarrativeHeader = addGeneratedNarrativeHeader;
res.questionnaireMode = questionnaireMode;
res.header = header;
res.selfLink = selfLink;
res.inlineGraphics = inlineGraphics;
res.timeZoneId = timeZoneId;
res.dateTimeFormat = dateTimeFormat;
res.dateFormat = dateFormat;
res.dateYearFormat = dateYearFormat;
res.dateYearMonthFormat = dateYearMonthFormat;
return res; return res;
} }
@ -514,6 +525,30 @@ public class RenderingContext {
this.dateFormat = DateTimeFormatter.ofPattern(dateFormat); this.dateFormat = DateTimeFormatter.ofPattern(dateFormat);
} }
public DateTimeFormatter getDateYearFormat() {
return dateYearFormat;
}
public void setDateYearFormat(DateTimeFormatter dateYearFormat) {
this.dateYearFormat = dateYearFormat;
}
public void setDateYearFormatString(String dateYearFormat) {
this.dateYearFormat = DateTimeFormatter.ofPattern(dateYearFormat);
}
public DateTimeFormatter getDateYearMonthFormat() {
return dateYearMonthFormat;
}
public void setDateYearMonthFormat(DateTimeFormatter dateYearMonthFormat) {
this.dateYearMonthFormat = dateYearMonthFormat;
}
public void setDateYearMonthFormatString(String dateYearMonthFormat) {
this.dateYearMonthFormat = DateTimeFormatter.ofPattern(dateYearMonthFormat);
}
public ResourceRendererMode getMode() { public ResourceRendererMode getMode() {
return mode; return mode;
} }

View File

@ -111,15 +111,7 @@ public class NarrativeGenerationTests {
List<Arguments> objects = new ArrayList<>(); List<Arguments> objects = new ArrayList<>();
while (test != null && test.getNodeName().equals("test")) { while (test != null && test.getNodeName().equals("test")) {
TestDetails t = new TestDetails(test); TestDetails t = new TestDetails(test);
if (t.getId().equals("sdc")) {
if (SystemUtils.OS_NAME.contains(WINDOWS)) {
objects.add(Arguments.of(t.getId(), t)); objects.add(Arguments.of(t.getId(), t));
} else {
System.out.println("sdc test not being adding because the current OS will not pass the test...");
}
} else {
objects.add(Arguments.of(t.getId(), t));
}
test = XMLUtil.getNextSibling(test); test = XMLUtil.getNextSibling(test);
} }
return objects.stream(); return objects.stream();