Don't include an mpty text element in Bundles being returned by the server

This commit is contained in:
jamesagnew 2015-04-23 08:04:05 -04:00
parent e18433d709
commit 5bcd6a4751
8 changed files with 85 additions and 8 deletions

View File

@ -28,6 +28,7 @@ import static ca.uhn.fhir.model.api.TemporalPrecisionEnum.YEAR;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
@ -64,7 +65,9 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
private static final FastDateFormat ourYearMonthDayTimeZoneFormat = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");
private static final FastDateFormat ourYearMonthFormat = FastDateFormat.getInstance("yyyy-MM");
private static final FastDateFormat ourYearMonthNoDashesFormat = FastDateFormat.getInstance("yyyyMM");
private static final Pattern ourYearMonthPattern = Pattern.compile("[0-9]{4}[0-9]{2}");
private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM);
private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
private static final Pattern ourYearMonthPattern = Pattern.compile("[0-9]{4}[0-9]{2}");
private static final Pattern ourYearPattern = Pattern.compile("[0-9]{4}");
static {
@ -89,7 +92,55 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
private boolean myTimeZoneZulu = false;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseDateTimeDt.class);
/**
/**
* Returns a human readable version of this date/time using the system local format.
* <p>
* <b>Note on time zones:</b> This method renders the value using the time zone
* that is contained within the value. For example, if this date object contains the
* value "2012-01-05T12:00:00-08:00", the human display will be rendered as "12:00:00"
* even if the application is being executed on a system in a different time zone. If
* this behaviour is not what you want, use {@link #toHumanDisplayLocalTimezone()}
* instead.
* </p>
*/
public String toHumanDisplay() {
TimeZone tz = getTimeZone();
Calendar value = tz != null ? Calendar.getInstance(tz) : Calendar.getInstance();
value.setTime(getValue());
switch (getPrecision()) {
case YEAR:
case MONTH:
case DAY:
return ourHumanDateFormat.format(value);
case MILLI:
case SECOND:
default:
return ourHumanDateTimeFormat.format(value);
}
}
/**
* Returns a human readable version of this date/time using the system local format,
* converted to the local timezone if neccesary.
*
* @see #toHumanDisplay() for a method which does not convert the time to the local
* timezone before rendering it.
*/
public String toHumanDisplayLocalTimezone() {
switch (getPrecision()) {
case YEAR:
case MONTH:
case DAY:
return ourHumanDateFormat.format(getValue());
case MILLI:
case SECOND:
default:
return ourHumanDateTimeFormat.format(getValue());
}
}
/**
* Constructor
*/
public BaseDateTimeDt() {

View File

@ -24,7 +24,6 @@ import static org.apache.commons.lang3.StringUtils.*;
import ca.uhn.fhir.model.api.BasePrimitive;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.model.api.annotation.SimpleSetter;
import ca.uhn.fhir.parser.DataFormatException;
@DatatypeDef(name = "code")
public class CodeDt extends BasePrimitive<String> implements ICodedDatatype, Comparable<CodeDt> {
@ -46,7 +45,7 @@ public class CodeDt extends BasePrimitive<String> implements ICodedDatatype, Com
@Override
public boolean isEmpty() {
return isBlank(getValueAsString());
return super.isBaseEmpty() && isBlank(getValueAsString());
}
@Override

View File

@ -50,7 +50,7 @@ public class XhtmlDt extends BasePrimitive<List<XMLEvent>> {
@Override
public boolean isEmpty() {
return super.isEmpty() && (getValue() == null || getValue().isEmpty());
return super.isBaseEmpty() && (getValue() == null || getValue().isEmpty());
}
/**

View File

@ -479,7 +479,7 @@ public class JsonParser extends BaseParser implements IParser {
if (gen != null) {
BaseNarrativeDt<?> narr = ((IResource) theResource).getText();
gen.generateNarrative(theResDef.getResourceProfile(), theResource, narr);
if (narr != null) {
if (narr != null && !narr.isEmpty()) {
RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
String childName = nextChild.getChildNameByDatatype(child.getDatatype());
BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);

View File

@ -273,6 +273,20 @@ public class ResourceProviderDstu2Test {
}
}
@Test
public void testSearchBundleDoesntIncludeTextElement() throws Exception {
HttpGet read = new HttpGet(ourServerBase + "/Patient?_format=json");
CloseableHttpResponse response = ourHttpClient.execute(read);
try {
String text = IOUtils.toString(response.getEntity().getContent());
ourLog.info(text);
assertEquals(Constants.STATUS_HTTP_200_OK, response.getStatusLine().getStatusCode());
assertThat(text, not(containsString("\"text\",\"type\"")));
} finally {
response.close();
}
}
@Test
public void testSearchWithInclude() throws Exception {
Organization org = new Organization();

View File

@ -15,12 +15,21 @@ import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
public class BaseDateTimeDtTest {
private SimpleDateFormat myDateInstantParser;
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseDateTimeDtTest.class);
@Before
public void before() {
myDateInstantParser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
}
@Test
public void testToHumanDisplay() {
DateTimeDt dt = new DateTimeDt("2012-01-05T12:00:00-08:00");
String human = dt.toHumanDisplay();
ourLog.info(human);
assertEquals("Jan 5, 2012 12:00:00 PM", human);
}
/**
* See HAPI #101 - https://github.com/jamesagnew/hapi-fhir/issues/101
*/

View File

@ -108,7 +108,7 @@ public class JsonParserDstu2Test {
public void testEncodeBundleNewBundleNoText() {
ca.uhn.fhir.model.dstu2.resource.Bundle b = new ca.uhn.fhir.model.dstu2.resource.Bundle();
// b.getText().setDiv("<div>aaa</div>");
b.getText().setDiv("");
b.getText().getStatus().setValueAsString("");;
Entry e = b.addEntry();

View File

@ -140,6 +140,10 @@
Prevent server from returning a Content-Location header for search
response when using the DSTU2 bundle format
</action>
<action type="fix">
JPA server (uhnfhirtest.uhn.ca) sometimes included an empty
"text" element in Bundles being returned.
</action>
</release>
<release version="0.9" date="2015-Mar-14">
<action type="add">