Unit tests all pass!

This commit is contained in:
jamesagnew 2014-12-10 22:02:31 -05:00
parent d8e14e4213
commit e1aa3f335e
33 changed files with 370 additions and 271 deletions

View File

@ -30,6 +30,7 @@ import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.text.WordUtils;
import org.hl7.fhir.instance.model.IBase;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.annotation.Child;
@ -185,7 +186,7 @@ public abstract class BaseRuntimeDeclaredChildDefinition extends BaseRuntimeChil
private final class FieldPlainMutator implements IMutator {
@Override
public void addValue(Object theTarget, IElement theValue) {
public void addValue(Object theTarget, IBase theValue) {
try {
myField.set(theTarget, theValue);
} catch (IllegalArgumentException e) {
@ -216,12 +217,12 @@ public abstract class BaseRuntimeDeclaredChildDefinition extends BaseRuntimeChil
private final class FieldListMutator implements IMutator {
@Override
public void addValue(Object theTarget, IElement theValue) {
public void addValue(Object theTarget, IBase theValue) {
try {
@SuppressWarnings("unchecked")
List<IElement> existingList = (List<IElement>) myField.get(theTarget);
List<IBase> existingList = (List<IBase>) myField.get(theTarget);
if (existingList == null) {
existingList = new ArrayList<IElement>(2);
existingList = new ArrayList<IBase>(2);
myField.set(theTarget, existingList);
}
existingList.add(theValue);
@ -282,11 +283,11 @@ public abstract class BaseRuntimeDeclaredChildDefinition extends BaseRuntimeChil
}
@Override
public void addValue(Object theTarget, IElement theValue) {
public void addValue(Object theTarget, IBase theValue) {
@SuppressWarnings("unchecked")
List<IElement> existingList = (List<IElement>) myAccessor.getValues(theTarget);
List<IBase> existingList = (List<IBase>) myAccessor.getValues(theTarget);
if (existingList == null) {
existingList = new ArrayList<IElement>();
existingList = new ArrayList<IBase>();
try {
myMutatorMethod.invoke(theTarget, existingList);
} catch (IllegalAccessException e) {
@ -335,7 +336,7 @@ public abstract class BaseRuntimeDeclaredChildDefinition extends BaseRuntimeChil
}
@Override
public void addValue(Object theTarget, IElement theValue) {
public void addValue(Object theTarget, IBase theValue) {
try {
if (theValue != null && !myTargetReturnType.isAssignableFrom(theValue.getClass())) {
throw new ConfigurationException("Value for field " + myElementName + " expects type " + myTargetReturnType + " but got " + theValue.getClass());

View File

@ -33,7 +33,6 @@ import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.i18n.HapiLocalizer;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.IFhirVersion;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.resource.Binary;
@ -107,7 +106,7 @@ public class FhirContext {
/**
* Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.
*/
public BaseRuntimeElementDefinition<?> getElementDefinition(Class<? extends IElement> theElementType) {
public BaseRuntimeElementDefinition<?> getElementDefinition(Class<? extends IBase> theElementType) {
return myClassToElementDefinition.get(theElementType);
}
@ -144,7 +143,7 @@ public class FhirContext {
/**
* Returns the scanned runtime model for the given type. This is an advanced feature which is generally only needed for extending the core library.
*/
public RuntimeResourceDefinition getResourceDefinition(IResource theResource) {
public RuntimeResourceDefinition getResourceDefinition(IBaseResource theResource) {
return getResourceDefinition(theResource.getClass());
}

View File

@ -30,8 +30,8 @@ import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
@ -88,11 +88,11 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
String elementName;
String alternateElementName = null;
BaseRuntimeElementDefinition<?> nextDef;
if (IResource.class.isAssignableFrom(next)) {
if (IBaseResource.class.isAssignableFrom(next)) {
elementName = getElementName() + StringUtils.capitalize(next.getSimpleName());
alternateElementName = getElementName() + "Resource";
List<Class<? extends IResource>> types = new ArrayList<Class<? extends IResource>>();
types.add((Class<? extends IResource>) next);
List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>();
types.add((Class<? extends IBaseResource>) next);
nextDef = new RuntimeResourceReferenceDefinition(elementName, types);
nextDef.sealAndInitialize(theClassToElementDefinitions);
} else {
@ -105,7 +105,7 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
myNameToChildDefinition.put(alternateElementName, nextDef);
}
if (IResource.class.isAssignableFrom(next)) {
if (IBaseResource.class.isAssignableFrom(next)) {
myDatatypeToElementDefinition.put(ResourceReferenceDt.class, nextDef);
alternateElementName = getElementName() + "Resource";
myDatatypeToElementName.put(ResourceReferenceDt.class, alternateElementName);

View File

@ -20,7 +20,7 @@ package ca.uhn.fhir.context;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
@ -31,6 +31,7 @@ import java.util.Map;
import java.util.Set;
import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.annotation.Child;
@ -140,7 +141,7 @@ public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclared
if ("valueResourceReference".equals(myDatatypeChildName)) {
// Per one of the examples here: http://hl7.org/implement/standards/fhir/extensibility.html#extension
myDatatypeChildName = "valueResource";
List<Class<? extends IResource>> types = new ArrayList<Class<? extends IResource>>();
List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>();
types.add(IResource.class);
myChildDef = new RuntimeResourceReferenceDefinition("valueResource", types);
}else {

View File

@ -30,10 +30,10 @@ import java.util.Set;
import org.apache.commons.lang3.text.WordUtils;
import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.IDatatype;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.base.composite.BaseResourceReferenceDt;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
@ -99,8 +99,8 @@ public class RuntimeChildUndeclaredExtensionDefinition extends BaseRuntimeChildD
// Resource Reference
myDatatypeToAttributeName.put(ResourceReferenceDt.class, "valueResource");
List<Class<? extends IResource>> types = new ArrayList<Class<? extends IResource>>();
types.add(IResource.class);
List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>();
types.add(IBaseResource.class);
RuntimeResourceReferenceDefinition def = new RuntimeResourceReferenceDefinition("valueResource", types);
def.sealAndInitialize(theClassToElementDefinitions);
myAttributeNameToDefinition.put("valueResource", def);

View File

@ -52,7 +52,7 @@ public class RuntimeResourceReferenceDefinition extends BaseRuntimeElementDefini
void sealAndInitialize(Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
myResourceTypeToDefinition = new HashMap<Class<? extends IBaseResource>, RuntimeResourceDefinition>();
for (Class<? extends IBaseResource> next : myResourceTypes) {
if (next.equals(IResource.class) || next.equals(Resource.class)) {
if (next.equals(IResource.class) || next.equals(Resource.class) || next.equals(IBaseResource.class)) {
continue;
}
RuntimeResourceDefinition definition = (RuntimeResourceDefinition) theClassToElementDefinitions.get(next);

View File

@ -82,7 +82,7 @@ public abstract class BaseIdentifiableElement extends BaseElement implements IId
}
@Override
public void setValue(String theValue) throws DataFormatException {
public IdDt setValue(String theValue) throws DataFormatException {
throw new UnsupportedOperationException("Use IElement#setElementSpecificId(String) to set the element ID for an element");
}

View File

@ -22,6 +22,7 @@ package ca.uhn.fhir.model.api;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hl7.fhir.instance.model.IPrimitiveType;
import ca.uhn.fhir.parser.DataFormatException;
@ -67,9 +68,10 @@ public abstract class BasePrimitive<T> extends BaseIdentifiableElement implement
}
@Override
public void setValue(T theValue) throws DataFormatException {
public IPrimitiveType<T> setValue(T theValue) throws DataFormatException {
myCoercedValue = theValue;
updateStringValue();
return this;
}
protected void updateStringValue() {

View File

@ -25,8 +25,5 @@ import org.hl7.fhir.instance.model.IBase;
public interface IElement extends IBase {
boolean isEmpty();
}

View File

@ -24,7 +24,7 @@ import org.hl7.fhir.instance.model.IPrimitiveType;
import ca.uhn.fhir.parser.DataFormatException;
public interface IPrimitiveDatatype<T> extends IDatatype, IPrimitiveType {
public interface IPrimitiveDatatype<T> extends IDatatype, IPrimitiveType<T> {
void setValueAsString(String theValue) throws DataFormatException;
@ -32,5 +32,5 @@ public interface IPrimitiveDatatype<T> extends IDatatype, IPrimitiveType {
T getValue();
void setValue(T theValue) throws DataFormatException;
IPrimitiveType<T> setValue(T theValue) throws DataFormatException;
}

View File

@ -373,9 +373,10 @@ public abstract class BaseDateTimeDt extends BasePrimitive<Date> {
}
@Override
public void setValue(Date theValue) {
public BaseDateTimeDt setValue(Date theValue) {
clearTimeZone();
super.setValue(theValue);
return this;
}
/**

View File

@ -20,8 +20,7 @@ package ca.uhn.fhir.model.primitive;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.*;
import java.math.BigDecimal;
@ -29,8 +28,11 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hl7.fhir.instance.model.IBaseResource;
import org.hl7.fhir.instance.model.Resource;
import ca.uhn.fhir.model.api.IPrimitiveDatatype;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.model.api.annotation.SimpleSetter;
import ca.uhn.fhir.parser.DataFormatException;
@ -346,9 +348,10 @@ public class IdDt implements IPrimitiveDatatype<String> {
* <p>
* regex: [a-z0-9\-\.]{1,36}
* </p>
* @return
*/
@Override
public void setValue(String theValue) throws DataFormatException {
public IdDt setValue(String theValue) throws DataFormatException {
// TODO: add validation
myValue = theValue;
myHaveComponentParts = false;
@ -388,6 +391,7 @@ public class IdDt implements IPrimitiveDatatype<String> {
}
}
return this;
}
/**
@ -505,4 +509,16 @@ public class IdDt implements IPrimitiveDatatype<String> {
return isBlank(getValue());
}
public void applyTo(IBaseResource theResouce) {
if (theResouce == null) {
throw new NullPointerException("theResource can not be null");
} else if (theResouce instanceof IResource) {
((IResource) theResouce).setId(new IdDt(getValue()));
} else if (theResouce instanceof Resource) {
((Resource) theResouce).setId(getIdPart());
} else {
throw new IllegalArgumentException("Unknown resource class type, does not implement IResource or extend Resource");
}
}
}

View File

@ -22,6 +22,8 @@ package ca.uhn.fhir.model.view;
import java.util.List;
import org.hl7.fhir.instance.model.IBase;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
import ca.uhn.fhir.context.ConfigurationException;
@ -81,8 +83,8 @@ public class ViewGenerator {
continue;
}
List<? extends IElement> sourceValues = sourceChildEquivalent.getAccessor().getValues(theSource);
for (IElement nextElement : sourceValues) {
List<? extends IBase> sourceValues = sourceChildEquivalent.getAccessor().getValues(theSource);
for (IBase nextElement : sourceValues) {
nextChild.getMutator().addValue(theTarget, nextElement);
}
}
@ -102,8 +104,8 @@ public class ViewGenerator {
} else {
List<? extends IElement> values = sourceDeclaredExt.getAccessor().getValues(theSource);
for (IElement nextElement : values) {
List<? extends IBase> values = sourceDeclaredExt.getAccessor().getValues(theSource);
for (IBase nextElement : values) {
nextExt.getMutator().addValue(theTarget, nextElement);
}

View File

@ -36,6 +36,7 @@ import java.util.Properties;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.IBaseResource;
import org.thymeleaf.Arguments;
import org.thymeleaf.Configuration;
import org.thymeleaf.TemplateEngine;
@ -60,7 +61,6 @@ import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.thymeleaf.templateresolver.TemplateResolver;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.dstu.composite.NarrativeDt;
import ca.uhn.fhir.model.dstu.valueset.NarrativeStatusEnum;
import ca.uhn.fhir.model.primitive.XhtmlDt;
@ -92,12 +92,12 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
}
@Override
public NarrativeDt generateNarrative(IResource theResource) {
public NarrativeDt generateNarrative(IBaseResource theResource) {
return generateNarrative(null, theResource);
}
@Override
public NarrativeDt generateNarrative(String theProfile, IResource theResource) {
public NarrativeDt generateNarrative(String theProfile, IBaseResource theResource) {
if (!myInitialized) {
initialize();
}
@ -144,12 +144,12 @@ public abstract class BaseThymeleafNarrativeGenerator implements INarrativeGener
}
@Override
public String generateTitle(IResource theResource) {
public String generateTitle(IBaseResource theResource) {
return generateTitle(null, theResource);
}
@Override
public String generateTitle(String theProfile, IResource theResource) {
public String generateTitle(String theProfile, IBaseResource theResource) {
if (!myInitialized) {
initialize();
}

View File

@ -20,18 +20,19 @@ package ca.uhn.fhir.narrative;
* #L%
*/
import ca.uhn.fhir.model.api.IResource;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.model.dstu.composite.NarrativeDt;
import ca.uhn.fhir.parser.DataFormatException;
public interface INarrativeGenerator {
NarrativeDt generateNarrative(String theProfile, IResource theResource) throws DataFormatException;
NarrativeDt generateNarrative(String theProfile, IBaseResource theResource) throws DataFormatException;
NarrativeDt generateNarrative(IResource theResource);
NarrativeDt generateNarrative(IBaseResource theResource);
String generateTitle(IResource theResource);
String generateTitle(IBaseResource theResource);
String generateTitle(String theProfile, IResource theResource);
String generateTitle(String theProfile, IBaseResource theResource);
}

View File

@ -20,7 +20,7 @@ package ca.uhn.fhir.parser;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.*;
import java.io.IOException;
import java.io.Reader;
@ -34,7 +34,11 @@ import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.DomainResource;
import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import org.hl7.fhir.instance.model.Reference;
import org.hl7.fhir.instance.model.Resource;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeDeclaredChildDefinition;
@ -42,7 +46,6 @@ import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeChildChoiceDefinition;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.TagList;
import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt;
@ -59,35 +62,64 @@ public abstract class BaseParser implements IParser {
}
protected String fixContainedResourceId(String theValue) {
if (StringUtils.isNotBlank(theValue)&&theValue.charAt(0)=='#') {
if (StringUtils.isNotBlank(theValue) && theValue.charAt(0) == '#') {
return theValue.substring(1);
}
return theValue;
}
private void containResourcesForEncoding(ContainedResources theContained, IBaseResource theResource, IBaseResource theTarget) {
List<ResourceReferenceDt> allElements = myContext.newTerser().getAllPopulatedChildElementsOfType(theResource, ResourceReferenceDt.class);
Set<String> allIds = new HashSet<String>();
if (theTarget instanceof IResource) {
List<? extends IResource> containedResources = ((IResource) theTarget).getContained().getContainedResources();
for (IResource next : containedResources) {
String nextId = next.getId().getValue();
if (StringUtils.isNotBlank(nextId)) {
allIds.add(nextId);
}
}
} else if (theTarget instanceof DomainResource) {
List<Resource> containedResources = ((DomainResource) theTarget).getContained();
for (Resource next : containedResources) {
String nextId = next.getId();
if (StringUtils.isNotBlank(nextId)) {
allIds.add(nextId);
}
}
} else {
// no resources to contain
}
for (IResource next : theTarget.getContained().getContainedResources()) {
String nextId = next.getId().getValue();
if (StringUtils.isNotBlank(nextId)) {
allIds.add(nextId);
{
List<ResourceReferenceDt> allElements = myContext.newTerser().getAllPopulatedChildElementsOfType(theResource, ResourceReferenceDt.class);
for (ResourceReferenceDt next : allElements) {
IResource resource = next.getResource();
if (resource != null) {
if (resource.getId().isEmpty() || resource.getId().isLocal()) {
theContained.addContained(resource);
} else {
continue;
}
containResourcesForEncoding(theContained, resource, theTarget);
}
}
}
for (ResourceReferenceDt next : allElements) {
IResource resource = next.getResource();
if (resource != null) {
if (resource.getId().isEmpty() || resource.getId().isLocal()) {
theContained.addContained(resource);
} else {
continue;
}
{
List<Reference> allElements = myContext.newTerser().getAllPopulatedChildElementsOfType(theResource, Reference.class);
for (Reference next : allElements) {
Resource resource = next.getResource();
if (resource != null) {
if (resource.getIdElement().isEmpty() || resource.getId().startsWith("#")) {
theContained.addContained(resource);
} else {
continue;
}
containResourcesForEncoding(theContained, resource, theTarget);
containResourcesForEncoding(theContained, resource, theTarget);
}
}
}
@ -141,7 +173,8 @@ public abstract class BaseParser implements IParser {
}
/**
* If set to <code>true</code> (default is <code>false</code>), narratives will not be included in the encoded values.
* If set to <code>true</code> (default is <code>false</code>), narratives will not be included in the encoded
* values.
*/
public boolean getSuppressNarratives() {
return mySuppressNarratives;
@ -186,12 +219,12 @@ public abstract class BaseParser implements IParser {
return this;
}
protected void throwExceptionForUnknownChildType(BaseRuntimeChildDefinition nextChild, Class<? extends IElement> type) {
protected void throwExceptionForUnknownChildType(BaseRuntimeChildDefinition nextChild, Class<? extends IBase> theType) {
if (nextChild instanceof BaseRuntimeDeclaredChildDefinition) {
StringBuilder b = new StringBuilder();
b.append(((BaseRuntimeDeclaredChildDefinition) nextChild).getElementName());
b.append(" has type ");
b.append(type);
b.append(theType.getName());
b.append(" but this is not a valid type for this element");
if (nextChild instanceof RuntimeChildChoiceDefinition) {
RuntimeChildChoiceDefinition choice = (RuntimeChildChoiceDefinition) nextChild;
@ -199,7 +232,7 @@ public abstract class BaseParser implements IParser {
}
throw new DataFormatException(b.toString());
}
throw new DataFormatException(nextChild + " has no child of type " + type);
throw new DataFormatException(nextChild + " has no child of type " + theType);
}
protected String determineReferenceText(ResourceReferenceDt theRef) {
@ -221,37 +254,38 @@ public abstract class BaseParser implements IParser {
return reference;
}
static class ContainedResources {
private long myNextContainedId = 1;
private IdentityHashMap<IResource, IdDt> myResourceToId = new IdentityHashMap<IResource, IdDt>();
private List<IResource> myResources = new ArrayList<IResource>();
private IdentityHashMap<IBaseResource, IdDt> myResourceToId = new IdentityHashMap<IBaseResource, IdDt>();
private List<IBaseResource> myResources = new ArrayList<IBaseResource>();
public void addContained(IResource theResource) {
public void addContained(IBaseResource theResource) {
if (myResourceToId.containsKey(theResource)) {
return;
}
IdDt newId;
if (theResource.getId().isLocal()) {
newId = theResource.getId();
if (theResource instanceof IResource && ((IResource) theResource).getId().isLocal()) {
newId = ((IResource) theResource).getId();
} else if (theResource instanceof Resource && ((Resource)theResource).getId() != null && ((Resource)theResource).getId().startsWith("#")) {
newId = new IdDt(((Resource)theResource).getId());
} else {
// TODO: make this configurable between the two below (and something else?)
// newId = new IdDt(UUID.randomUUID().toString());
newId = new IdDt(myNextContainedId++);
}
myResourceToId.put(theResource, newId);
myResources.add(theResource);
}
public List<IResource> getContainedResources() {
public List<IBaseResource> getContainedResources() {
return myResources;
}
public IdDt getResourceId(IResource theResource) {
return myResourceToId.get(theResource);
public IdDt getResourceId(IBaseResource theNext) {
return myResourceToId.get(theNext);
}
public boolean isEmpty() {

View File

@ -25,7 +25,6 @@ import java.io.Reader;
import java.io.Writer;
import org.hl7.fhir.instance.model.IBaseResource;
import org.hl7.fhir.instance.model.Resource;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.model.api.Bundle;
@ -67,7 +66,7 @@ public interface IParser {
*/
void encodeTagListToWriter(TagList theTagList, Writer theWriter) throws IOException;
<T extends IResource> Bundle parseBundle(Class<T> theResourceType, Reader theReader);
<T extends IBaseResource> Bundle parseBundle(Class<T> theResourceType, Reader theReader);
Bundle parseBundle(Reader theReader);
@ -153,13 +152,4 @@ public interface IParser {
*/
IParser setSuppressNarratives(boolean theSuppressNarratives);
/**
* Parses a resource from the HL7.org structure library
*
* @param theClass The resource type
* @param theResourceBody The body of the resource to parse
* @return A parsed resource
*/
<T extends Resource> T parse(Class<T> theClass, String theResourceBody);
}

View File

@ -20,9 +20,7 @@ package ca.uhn.fhir.parser;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.*;
import java.io.IOException;
import java.io.Reader;
@ -51,7 +49,9 @@ import javax.json.stream.JsonParsingException;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import org.hl7.fhir.instance.model.Resource;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
@ -129,7 +129,7 @@ public class JsonParser extends BaseParser implements IParser {
if (theResourceTypeObj == null) {
throw new DataFormatException("Invalid JSON content detected, missing required element: '" + thePosition + "'");
}
if (theResourceTypeObj.getValueType() != theValueType) {
throw new DataFormatException("Invalid content of element " + thePosition + ", expected " + theValueType);
}
@ -167,7 +167,7 @@ public class JsonParser extends BaseParser implements IParser {
if (linkStarted) {
eventWriter.writeEnd();
}
writeCategories(eventWriter, theBundle.getCategories());
writeOptionalTagWithTextNode(eventWriter, "totalResults", theBundle.getTotalResults());
@ -206,10 +206,10 @@ public class JsonParser extends BaseParser implements IParser {
encodeResourceToJsonStreamWriter(resDef, resource, eventWriter, "content", false);
}
if (nextEntry.getSummary().isEmpty()==false) {
if (nextEntry.getSummary().isEmpty() == false) {
eventWriter.write("summary", nextEntry.getSummary().getValueAsString());
}
eventWriter.writeEnd(); // entry object
}
eventWriter.writeEnd(); // entry array
@ -232,16 +232,15 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, JsonGenerator theWriter, IElement theValue, BaseRuntimeElementDefinition<?> theChildDef,
String theChildName, boolean theIsSubElementWithinResource) throws IOException {
private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, JsonGenerator theWriter, IBase theNextValue, BaseRuntimeElementDefinition<?> theChildDef, String theChildName, boolean theIsSubElementWithinResource) throws IOException {
switch (theChildDef.getChildType()) {
case PRIMITIVE_DATATYPE: {
IPrimitiveDatatype<?> value = (IPrimitiveDatatype<?>) theValue;
IPrimitiveDatatype<?> value = (IPrimitiveDatatype<?>) theNextValue;
if (isBlank(value.getValueAsString())) {
break;
}
if (value instanceof IntegerDt) {
if (theChildName != null) {
theWriter.write(theChildName, ((IntegerDt) value).getValue());
@ -278,23 +277,23 @@ public class JsonParser extends BaseParser implements IParser {
} else {
theWriter.writeStartObject();
}
if (theValue instanceof ExtensionDt) {
theWriter.write("url", ((ExtensionDt) theValue).getUrlAsString());
if (theNextValue instanceof ExtensionDt) {
theWriter.write("url", ((ExtensionDt) theNextValue).getUrlAsString());
}
encodeCompositeElementToStreamWriter(theResDef, theResource, theValue, theWriter, childCompositeDef, theIsSubElementWithinResource);
encodeCompositeElementToStreamWriter(theResDef, theResource, theNextValue, theWriter, childCompositeDef, theIsSubElementWithinResource);
theWriter.writeEnd();
break;
}
case RESOURCE_REF: {
ResourceReferenceDt referenceDt = (ResourceReferenceDt) theValue;
ResourceReferenceDt referenceDt = (ResourceReferenceDt) theNextValue;
if (theChildName != null) {
theWriter.writeStartObject(theChildName);
} else {
theWriter.writeStartObject();
}
String reference = determineReferenceText(referenceDt);
String reference = determineReferenceText(referenceDt);
if (StringUtils.isNotBlank(reference)) {
theWriter.write(XmlParser.RESREF_REFERENCE, reference);
}
@ -306,14 +305,14 @@ public class JsonParser extends BaseParser implements IParser {
}
case CONTAINED_RESOURCES: {
theWriter.writeStartArray(theChildName);
ContainedDt value = (ContainedDt) theValue;
ContainedDt value = (ContainedDt) theNextValue;
for (IResource next : value.getContainedResources()) {
if (getContainedResources().getResourceId(next)!=null) {
if (getContainedResources().getResourceId(next) != null) {
continue;
}
encodeResourceToJsonStreamWriter(theResDef, next, theWriter, null, true, fixContainedResourceId(next.getId().getValue()));
}
for (IResource next : getContainedResources().getContainedResources()) {
for (IBaseResource next : getContainedResources().getContainedResources()) {
IdDt resourceId = getContainedResources().getResourceId(next);
encodeResourceToJsonStreamWriter(theResDef, next, theWriter, null, true, fixContainedResourceId(resourceId.getValue()));
}
@ -322,7 +321,7 @@ public class JsonParser extends BaseParser implements IParser {
}
case PRIMITIVE_XHTML: {
if (!getSuppressNarratives()) {
XhtmlDt dt = (XhtmlDt) theValue;
XhtmlDt dt = (XhtmlDt) theNextValue;
if (theChildName != null) {
theWriter.write(theChildName, dt.getValueAsString());
} else {
@ -344,10 +343,10 @@ public class JsonParser extends BaseParser implements IParser {
}
private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, JsonGenerator theEventWriter,
List<? extends BaseRuntimeChildDefinition> theChildren, boolean theIsSubElementWithinResource) throws IOException {
private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theNextValue, JsonGenerator theEventWriter, List<? extends BaseRuntimeChildDefinition> theChildren, boolean theIsSubElementWithinResource) throws IOException {
for (BaseRuntimeChildDefinition nextChild : theChildren) {
if (nextChild instanceof RuntimeChildNarrativeDefinition) {
INarrativeGenerator gen = myContext.getNarrativeGenerator();
if (gen != null) {
NarrativeDt narr = gen.generateNarrative(theResDef.getResourceProfile(), theResource);
@ -361,7 +360,7 @@ public class JsonParser extends BaseParser implements IParser {
}
}
List<? extends IElement> values = nextChild.getAccessor().getValues(theElement);
List<? extends IBase> values = nextChild.getAccessor().getValues(theNextValue);
if (values == null || values.isEmpty()) {
continue;
}
@ -373,7 +372,7 @@ public class JsonParser extends BaseParser implements IParser {
ArrayList<ArrayList<HeldExtension>> modifierExtensions = new ArrayList<ArrayList<HeldExtension>>(0);
int valueIdx = 0;
for (IElement nextValue : values) {
for (IBase nextValue : values) {
if (nextValue == null || nextValue.isEmpty()) {
if (nextValue instanceof ContainedDt) {
if (theIsSubElementWithinResource || getContainedResources().isEmpty()) {
@ -384,26 +383,27 @@ public class JsonParser extends BaseParser implements IParser {
}
}
Class<? extends IElement> type = nextValue.getClass();
Class<? extends IBase> type = nextValue.getClass();
String childName = nextChild.getChildNameByDatatype(type);
BaseRuntimeElementDefinition<?> childDef = nextChild.getChildElementDefinitionByDatatype(type);
if (childDef == null) {
super.throwExceptionForUnknownChildType(nextChild, type);
}
boolean primitive = childDef.getChildType() == ChildTypeEnum.PRIMITIVE_DATATYPE;
if (childDef.getChildType() == ChildTypeEnum.CONTAINED_RESOURCES && theIsSubElementWithinResource) {
continue;
}
if (nextChild instanceof RuntimeChildDeclaredExtensionDefinition) {
// Don't encode extensions
// RuntimeChildDeclaredExtensionDefinition extDef = (RuntimeChildDeclaredExtensionDefinition) nextChild;
// if (extDef.isModifier()) {
// addToHeldExtensions(valueIdx, modifierExtensions, extDef, nextValue);
// } else {
// addToHeldExtensions(valueIdx, extensions, extDef, nextValue);
// }
// RuntimeChildDeclaredExtensionDefinition extDef = (RuntimeChildDeclaredExtensionDefinition)
// nextChild;
// if (extDef.isModifier()) {
// addToHeldExtensions(valueIdx, modifierExtensions, extDef, nextValue);
// } else {
// addToHeldExtensions(valueIdx, extensions, extDef, nextValue);
// }
} else {
if (currentChildName == null || !currentChildName.equals(childName)) {
@ -468,25 +468,30 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, JsonGenerator theEventWriter,
BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIsSubElementWithinResource) throws IOException, DataFormatException {
extractAndWriteExtensionsAsDirectChild(theElement, theEventWriter, resDef, theResDef, theResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getExtensions(), theIsSubElementWithinResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getChildren(),theIsSubElementWithinResource);
private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theNextValue, JsonGenerator theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIsSubElementWithinResource) throws IOException, DataFormatException {
extractAndWriteExtensionsAsDirectChild(theNextValue, theEventWriter, resDef, theResDef, theResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theNextValue, theEventWriter, resDef.getExtensions(), theIsSubElementWithinResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theNextValue, theEventWriter, resDef.getChildren(), theIsSubElementWithinResource);
}
private void encodeResourceToJsonStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, JsonGenerator theEventWriter, String theObjectNameOrNull,
boolean theIsSubElementWithinResource) throws IOException {
private void encodeResourceToJsonStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, JsonGenerator theEventWriter, String theObjectNameOrNull, boolean theIsSubElementWithinResource) throws IOException {
String resourceId = null;
if (theIsSubElementWithinResource && StringUtils.isNotBlank(theResource.getId().getValue())) {
resourceId = theResource.getId().getValue();
if (theResource instanceof IResource) {
IResource res = (IResource) theResource;
if (theIsSubElementWithinResource && StringUtils.isNotBlank(res.getId().getValue())) {
resourceId = res.getId().getValue();
}
} else if (theResource instanceof Resource) {
Resource res = (Resource) theResource;
if (theIsSubElementWithinResource && StringUtils.isNotBlank(res.getId())) {
resourceId = res.getId();
}
}
encodeResourceToJsonStreamWriter(theResDef, theResource, theEventWriter, theObjectNameOrNull, theIsSubElementWithinResource, resourceId);
}
private void encodeResourceToJsonStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, JsonGenerator theEventWriter, String theObjectNameOrNull, boolean theIsSubElementWithinResource,
String theResourceId) throws IOException {
private void encodeResourceToJsonStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, JsonGenerator theEventWriter, String theObjectNameOrNull, boolean theIsSubElementWithinResource, String theResourceId) throws IOException {
if (!theIsSubElementWithinResource) {
super.containResourcesForEncoding(theResource);
}
@ -515,7 +520,7 @@ public class JsonParser extends BaseParser implements IParser {
}
@Override
public void encodeResourceToWriter(IResource theResource, Writer theWriter) throws IOException {
public void encodeResourceToWriter(IBaseResource theResource, Writer theWriter) throws IOException {
Validate.notNull(theResource, "Resource can not be null");
JsonGenerator eventWriter = createJsonGenerator(theWriter);
@ -556,10 +561,10 @@ public class JsonParser extends BaseParser implements IParser {
}
/**
* This is useful only for the two cases where extensions are encoded as direct children (e.g. not in some object called _name): resource extensions, and extension extensions
* This is useful only for the two cases where extensions are encoded as direct children (e.g. not in some object
* called _name): resource extensions, and extension extensions
*/
private void extractAndWriteExtensionsAsDirectChild(IElement theElement, JsonGenerator theEventWriter, BaseRuntimeElementDefinition<?> theElementDef, RuntimeResourceDefinition theResDef,
IResource theResource) throws IOException {
private void extractAndWriteExtensionsAsDirectChild(IBase theElement, JsonGenerator theEventWriter, BaseRuntimeElementDefinition<?> theElementDef, RuntimeResourceDefinition theResDef, IBaseResource theResource) throws IOException {
List<HeldExtension> extensions = new ArrayList<HeldExtension>(0);
List<HeldExtension> modifierExtensions = new ArrayList<HeldExtension>(0);
@ -573,9 +578,9 @@ public class JsonParser extends BaseParser implements IParser {
writeExtensionsAsDirectChild(theResource, theEventWriter, theResDef, extensions, modifierExtensions);
}
private void extractDeclaredExtensions(IElement theResource, BaseRuntimeElementDefinition<?> resDef, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions) {
private void extractDeclaredExtensions(IBase theResource, BaseRuntimeElementDefinition<?> resDef, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions) {
for (RuntimeChildDeclaredExtensionDefinition nextDef : resDef.getExtensionsNonModifier()) {
for (IElement nextValue : nextDef.getAccessor().getValues(theResource)) {
for (IBase nextValue : nextDef.getAccessor().getValues(theResource)) {
if (nextValue != null) {
if (nextValue == null || nextValue.isEmpty()) {
continue;
@ -585,7 +590,7 @@ public class JsonParser extends BaseParser implements IParser {
}
}
for (RuntimeChildDeclaredExtensionDefinition nextDef : resDef.getExtensionsModifier()) {
for (IElement nextValue : nextDef.getAccessor().getValues(theResource)) {
for (IBase nextValue : nextDef.getAccessor().getValues(theResource)) {
if (nextValue != null) {
if (nextValue == null || nextValue.isEmpty()) {
continue;
@ -596,7 +601,7 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void extractUndeclaredExtensions(IElement theResource, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions) {
private void extractUndeclaredExtensions(IBase theResource, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions) {
if (theResource instanceof ISupportsUndeclaredExtensions) {
List<ExtensionDt> ext = ((ISupportsUndeclaredExtensions) theResource).getUndeclaredExtensions();
for (ExtensionDt next : ext) {
@ -647,10 +652,10 @@ public class JsonParser extends BaseParser implements IParser {
}
@Override
public <T extends IResource> Bundle parseBundle(Class<T> theResourceType, Reader theReader) {
JsonReader reader;
public <T extends IBaseResource> Bundle parseBundle(Class<T> theResourceType, Reader theReader) {
JsonReader reader;
JsonObject object;
try {
reader = Json.createReader(theReader);
object = reader.readObject();
@ -853,7 +858,7 @@ public class JsonParser extends BaseParser implements IParser {
def = myContext.getResourceDefinition(resourceType);
}
ParserState<? extends IResource> state = ParserState.getPreResourceInstance(def.getImplementingClass(), myContext, true);
ParserState<? extends IResource> state = (ParserState<? extends IResource>) ParserState.getPreResourceInstance(def.getImplementingClass(), myContext, true);
state.enteringNewElement(null, def.getName());
parseChildren(object, state);
@ -867,7 +872,7 @@ public class JsonParser extends BaseParser implements IParser {
}
@Override
public <T extends IResource> T parseResource(Class<T> theResourceType, String theMessageString) {
public <T extends IBaseResource> T parseResource(Class<T> theResourceType, String theMessageString) {
return parseResource(theResourceType, new StringReader(theMessageString));
}
@ -923,8 +928,7 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void writeExtensionsAsDirectChild(IResource theResource, JsonGenerator theEventWriter, RuntimeResourceDefinition resDef, List<HeldExtension> extensions,
List<HeldExtension> modifierExtensions) throws IOException {
private void writeExtensionsAsDirectChild(IBaseResource theResource, JsonGenerator theEventWriter, RuntimeResourceDefinition resDef, List<HeldExtension> extensions, List<HeldExtension> modifierExtensions) throws IOException {
if (extensions.isEmpty() == false) {
theEventWriter.writeStartArray("extension");
for (HeldExtension next : extensions) {
@ -969,21 +973,21 @@ public class JsonParser extends BaseParser implements IParser {
private RuntimeChildDeclaredExtensionDefinition myDef;
private ExtensionDt myUndeclaredExtension;
private IElement myValue;
private IBase myValue;
public HeldExtension(ExtensionDt theUndeclaredExtension) {
assert theUndeclaredExtension != null;
myUndeclaredExtension = theUndeclaredExtension;
}
public HeldExtension(RuntimeChildDeclaredExtensionDefinition theDef, IElement theValue) {
public HeldExtension(RuntimeChildDeclaredExtensionDefinition theDef, IBase theValue) {
assert theDef != null;
assert theValue != null;
myDef = theDef;
myValue = theValue;
}
public void write(RuntimeResourceDefinition theResDef, IResource theResource, JsonGenerator theEventWriter) throws IOException {
public void write(RuntimeResourceDefinition theResDef, IBaseResource theResource, JsonGenerator theEventWriter) throws IOException {
if (myUndeclaredExtension != null) {
writeUndeclaredExt(theResDef, theResource, theEventWriter, myUndeclaredExtension);
} else {
@ -998,7 +1002,7 @@ public class JsonParser extends BaseParser implements IParser {
// theEventWriter, myValue, def, "value" +
// WordUtils.capitalize(def.getName()));
String childName = myDef.getChildNameByDatatype(myValue.getClass());
encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, myValue, def, childName,false);
encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, myValue, def, childName, false);
}
// theEventWriter.name(myUndeclaredExtension.get);
@ -1007,7 +1011,7 @@ public class JsonParser extends BaseParser implements IParser {
}
}
private void writeUndeclaredExt(RuntimeResourceDefinition theResDef, IResource theResource, JsonGenerator theEventWriter, ExtensionDt ext) throws IOException {
private void writeUndeclaredExt(RuntimeResourceDefinition theResDef, IBaseResource theResource, JsonGenerator theEventWriter, ExtensionDt ext) throws IOException {
IElement value = ext.getValue();
theEventWriter.writeStartObject();
@ -1029,7 +1033,7 @@ public class JsonParser extends BaseParser implements IParser {
throw new ConfigurationException("Unable to encode extension, unregognized child element type: " + value.getClass().getCanonicalName());
}
BaseRuntimeElementDefinition<?> childDef = extDef.getChildElementDefinitionByDatatype(value.getClass());
encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, value, childDef, childName,true);
encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, value, childDef, childName, true);
}
// theEventWriter.name(myUndeclaredExtension.get);

View File

@ -20,9 +20,7 @@ package ca.uhn.fhir.parser;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.apache.commons.lang3.StringUtils.*;
import java.util.ArrayList;
import java.util.HashMap;
@ -34,7 +32,10 @@ import javax.xml.stream.events.XMLEvent;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.hl7.fhir.instance.model.Element;
import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import org.hl7.fhir.instance.model.IPrimitiveType;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementCompositeDefinition;
@ -52,11 +53,9 @@ import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.ICompositeDatatype;
import ca.uhn.fhir.model.api.ICompositeElement;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.IIdentifiableElement;
import ca.uhn.fhir.model.api.IPrimitiveDatatype;
import ca.uhn.fhir.model.api.IBaseResource;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.IResourceBlock;
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
@ -755,7 +754,7 @@ class ParserState<T> {
if (myInstance instanceof IIdentifiableElement) {
((IIdentifiableElement) myInstance).setElementSpecificId((theValue));
} else if (myInstance instanceof IBaseResource) {
((IBaseResource) myInstance).setId(new IdDt(theValue));
(myInstance).setId(new IdDt(theValue));
}
} else if ("contentType".equals(theName)) {
myInstance.setContentType(theValue);
@ -826,7 +825,7 @@ class ParserState<T> {
@Override
public void wereBack() {
IBaseResource res = getCurrentElement();
IResource res = (IResource) getCurrentElement();
assert res != null;
if (res.getId() == null || res.getId().isEmpty()) {
ourLog.debug("Discarding contained resource with no ID!");
@ -836,7 +835,8 @@ class ParserState<T> {
res.setId(new IdDt('#' + res.getId().getIdPart()));
}
}
getPreResourceState().getCurrentElement().getContained().getContainedResources().add(res);
IResource preResCurrentElement = (IResource) getPreResourceState().getCurrentElement();
preResCurrentElement.getContained().getContainedResources().add(res);
}
@ -844,12 +844,12 @@ class ParserState<T> {
private class DeclaredExtensionState extends BaseState {
private IElement myChildInstance;
private IBase myChildInstance;
private RuntimeChildDeclaredExtensionDefinition myDefinition;
private IElement myParentInstance;
private IBase myParentInstance;
private PreResourceState myPreResourceState;
public DeclaredExtensionState(PreResourceState thePreResourceState, RuntimeChildDeclaredExtensionDefinition theDefinition, IElement theParentInstance) {
public DeclaredExtensionState(PreResourceState thePreResourceState, RuntimeChildDeclaredExtensionDefinition theDefinition, IBase theParentInstance) {
super(thePreResourceState);
myPreResourceState = thePreResourceState;
myDefinition = theDefinition;
@ -879,7 +879,7 @@ class ParserState<T> {
}
case PRIMITIVE_DATATYPE: {
RuntimePrimitiveDatatypeDefinition primitiveTarget = (RuntimePrimitiveDatatypeDefinition) target;
IPrimitiveDatatype<?> newChildInstance = primitiveTarget.newInstance();
IPrimitiveType<?> newChildInstance = primitiveTarget.newInstance();
myDefinition.getMutator().addValue(myParentInstance, newChildInstance);
PrimitiveState newState = new PrimitiveState(getPreResourceState(), newChildInstance);
push(newState);
@ -918,7 +918,7 @@ class ParserState<T> {
}
@Override
protected IElement getCurrentElement() {
protected IBase getCurrentElement() {
return myParentInstance;
}
@ -927,9 +927,9 @@ class ParserState<T> {
private class ElementCompositeState extends BaseState {
private BaseRuntimeElementCompositeDefinition<?> myDefinition;
private ICompositeElement myInstance;
private IBase myInstance;
public ElementCompositeState(PreResourceState thePreResourceState, BaseRuntimeElementCompositeDefinition<?> theDef, ICompositeElement theInstance) {
public ElementCompositeState(PreResourceState thePreResourceState, BaseRuntimeElementCompositeDefinition<?> theDef, IBase theInstance) {
super(thePreResourceState);
myDefinition = theDef;
myInstance = theInstance;
@ -940,8 +940,10 @@ class ParserState<T> {
if ("id".equals(theName)) {
if (myInstance instanceof IIdentifiableElement) {
((IIdentifiableElement) myInstance).setElementSpecificId((theValue));
} else if (myInstance instanceof Element) {
((Element) myInstance).setId(theValue);
} else if (myInstance instanceof IBaseResource) {
((IBaseResource) myInstance).setId(new IdDt(theValue));
new IdDt(theValue).applyTo((IBaseResource) myInstance);
}
} else if ("url".equals(theName) && myInstance instanceof ExtensionDt) {
((ExtensionDt) myInstance).setUrl(theValue);
@ -986,7 +988,7 @@ class ParserState<T> {
}
case PRIMITIVE_DATATYPE: {
RuntimePrimitiveDatatypeDefinition primitiveTarget = (RuntimePrimitiveDatatypeDefinition) target;
IPrimitiveDatatype<?> newChildInstance;
IPrimitiveType<?> newChildInstance;
newChildInstance = primitiveTarget.newInstance(child.getInstanceConstructorArguments());
child.getMutator().addValue(myInstance, newChildInstance);
PrimitiveState newState = new PrimitiveState(getPreResourceState(), newChildInstance);
@ -1020,7 +1022,7 @@ class ParserState<T> {
}
case CONTAINED_RESOURCES: {
RuntimeElemContainedResources targetElem = (RuntimeElemContainedResources) target;
List<? extends IElement> values = child.getAccessor().getValues(myInstance);
List<? extends IBase> values = child.getAccessor().getValues(myInstance);
ContainedDt newDt;
if (values == null || values.isEmpty() || values.get(0) == null) {
newDt = targetElem.newInstance();
@ -1055,7 +1057,7 @@ class ParserState<T> {
}
@Override
protected IElement getCurrentElement() {
protected IBase getCurrentElement() {
return myInstance;
}
@ -1096,8 +1098,8 @@ class ParserState<T> {
}
case PRIMITIVE_DATATYPE: {
RuntimePrimitiveDatatypeDefinition primitiveTarget = (RuntimePrimitiveDatatypeDefinition) target;
IPrimitiveDatatype<?> newChildInstance = primitiveTarget.newInstance();
myExtension.setValue(newChildInstance);
IPrimitiveType<?> newChildInstance = primitiveTarget.newInstance();
myExtension.setValue((IElement) newChildInstance);
PrimitiveState newState = new PrimitiveState(getPreResourceState(), newChildInstance);
push(newState);
return;
@ -1166,9 +1168,9 @@ class ParserState<T> {
* Stitch together resource references
*/
Map<String, IBaseResource> idToResource = new HashMap<String, IBaseResource>();
List<IBaseResource> resources = myInstance.toListOfResources();
for (IBaseResource next : resources) {
Map<String, IResource> idToResource = new HashMap<String, IResource>();
List<IResource> resources = myInstance.toListOfResources();
for (IResource next : resources) {
if (next.getId() != null && next.getId().isEmpty() == false) {
idToResource.put(next.getId().toUnqualifiedVersionless().getValue(), next);
}
@ -1178,7 +1180,7 @@ class ParserState<T> {
List<ResourceReferenceDt> refs = myContext.newTerser().getAllPopulatedChildElementsOfType(next, ResourceReferenceDt.class);
for (ResourceReferenceDt nextRef : refs) {
if (nextRef.isEmpty() == false && nextRef.getReference() != null) {
IBaseResource target = idToResource.get(nextRef.getReference().getValue());
IResource target = idToResource.get(nextRef.getReference().getValue());
if (target != null) {
nextRef.setResource(target);
}
@ -1194,7 +1196,7 @@ class ParserState<T> {
private Map<String, IBaseResource> myContainedResources = new HashMap<String, IBaseResource>();
private BundleEntry myEntry;
private IBaseResource myInstance;
private IResource myInstance;
private List<ResourceReferenceDt> myResourceReferences = new ArrayList<ResourceReferenceDt>();
private Class<? extends IBaseResource> myResourceType;
@ -1241,7 +1243,7 @@ class ParserState<T> {
}
RuntimeResourceDefinition def = (RuntimeResourceDefinition) definition;
myInstance = def.newInstance();
myInstance = (IResource) def.newInstance();
if (myEntry != null) {
myEntry.setResource(myInstance);
}
@ -1295,13 +1297,13 @@ class ParserState<T> {
}
@Override
public void acceptElement(IElement theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition) {
public void acceptElement(IBase theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition) {
if (theElement instanceof ResourceReferenceDt) {
ResourceReferenceDt nextRef = (ResourceReferenceDt) theElement;
String ref = nextRef.getReference().getValue();
if (isNotBlank(ref)) {
if (ref.startsWith("#")) {
IBaseResource target = myContainedResources.get(ref.substring(1));
IResource target = (IResource) myContainedResources.get(ref.substring(1));
if (target != null) {
nextRef.setResource(target);
} else {
@ -1359,9 +1361,9 @@ class ParserState<T> {
}
private class PrimitiveState extends BaseState {
private IPrimitiveDatatype<?> myInstance;
private IPrimitiveType<?> myInstance;
public PrimitiveState(PreResourceState thePreResourceState, IPrimitiveDatatype<?> theInstance) {
public PrimitiveState(PreResourceState thePreResourceState, IPrimitiveType<?> theInstance) {
super(thePreResourceState);
myInstance = theInstance;
}
@ -1373,8 +1375,10 @@ class ParserState<T> {
} else if ("id".equals(theName)) {
if (myInstance instanceof IIdentifiableElement) {
((IIdentifiableElement) myInstance).setElementSpecificId(theValue);
} else if (myInstance instanceof Element) {
((Element) myInstance).setId(theValue);
} else if (myInstance instanceof IBaseResource) {
((IBaseResource) myInstance).setId(new IdDt(theValue));
new IdDt(theValue).applyTo((org.hl7.fhir.instance.model.IBaseResource) myInstance);
}
}
}
@ -1404,7 +1408,7 @@ class ParserState<T> {
}
@Override
protected IElement getCurrentElement() {
protected IBase getCurrentElement() {
return myInstance;
}

View File

@ -20,9 +20,7 @@ package ca.uhn.fhir.parser;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.defaultString;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.*;
import java.io.IOException;
import java.io.Reader;
@ -46,7 +44,10 @@ import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.DomainResource;
import org.hl7.fhir.instance.model.IBase;
import org.hl7.fhir.instance.model.IBaseResource;
import org.hl7.fhir.instance.model.Narrative;
import org.hl7.fhir.instance.model.Resource;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
@ -300,14 +301,14 @@ public class XmlParser extends BaseParser implements IParser {
}
@Override
public <T extends IResource> Bundle parseBundle(Class<T> theResourceType, Reader theReader) {
public <T extends IBaseResource> Bundle parseBundle(Class<T> theResourceType, Reader theReader) {
XMLEventReader streamReader = createStreamReader(theReader);
return parseBundle(streamReader, theResourceType);
}
@Override
public <T extends IResource> T parseResource(Class<T> theResourceType, Reader theReader) {
public <T extends IBaseResource> T parseResource(Class<T> theResourceType, Reader theReader) {
XMLEventReader streamReader = createStreamReader(theReader);
return parseResource(theResourceType, streamReader);
@ -423,8 +424,8 @@ public class XmlParser extends BaseParser implements IParser {
}
}
private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, XMLStreamWriter theEventWriter, IElement nextValue, String childName,
BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
private void encodeChildElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theEventWriter, IBase nextValue, String childName, BaseRuntimeElementDefinition<?> childDef, String theExtensionUrl, boolean theIncludedResource)
throws XMLStreamException, DataFormatException {
if (nextValue.isEmpty()) {
if (childDef.getChildType() == ChildTypeEnum.CONTAINED_RESOURCES && getContainedResources().isEmpty() == false && theIncludedResource == false) {
// We still want to go in..
@ -474,7 +475,7 @@ public class XmlParser extends BaseParser implements IParser {
}
encodeResourceToXmlStreamWriter(next, theEventWriter, true, fixContainedResourceId(next.getId().getValue()));
}
for (IResource next : getContainedResources().getContainedResources()) {
for (IBaseResource next : getContainedResources().getContainedResources()) {
IdDt resourceId = getContainedResources().getResourceId(next);
encodeResourceToXmlStreamWriter(next, theEventWriter, true, fixContainedResourceId(resourceId.getValue()));
}
@ -499,34 +500,49 @@ public class XmlParser extends BaseParser implements IParser {
}
private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, XMLStreamWriter theEventWriter,
List<? extends BaseRuntimeChildDefinition> children, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
private void encodeCompositeElementChildrenToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, List<? extends BaseRuntimeChildDefinition> children, boolean theIncludedResource) throws XMLStreamException,
DataFormatException {
for (BaseRuntimeChildDefinition nextChild : children) {
if (nextChild instanceof RuntimeChildNarrativeDefinition && !theIncludedResource) {
INarrativeGenerator gen = myContext.getNarrativeGenerator();
NarrativeDt narr = theResource.getText();
if (gen != null && narr.isEmpty()) {
narr = gen.generateNarrative(theResDef.getResourceProfile(), theResource);
}
if (narr != null) {
RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
String childName = nextChild.getChildNameByDatatype(child.getDatatype());
BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, narr, childName, type, null, theIncludedResource);
continue;
if (theResource instanceof IResource) {
NarrativeDt narr = ((IResource)theResource).getText();
if (gen != null && narr.isEmpty()) {
narr = gen.generateNarrative(theResDef.getResourceProfile(), theResource);
}
if (narr != null) {
RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
String childName = nextChild.getChildNameByDatatype(child.getDatatype());
BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, narr, childName, type, null, theIncludedResource);
continue;
}
} else {
Narrative narr1 = ((DomainResource)theResource).getText();
NarrativeDt narr2 = null;
if (gen != null && narr1.isEmpty()) {
narr2 = gen.generateNarrative(theResDef.getResourceProfile(), theResource);
}
if (narr2 != null) {
RuntimeChildNarrativeDefinition child = (RuntimeChildNarrativeDefinition) nextChild;
String childName = nextChild.getChildNameByDatatype(child.getDatatype());
BaseRuntimeElementDefinition<?> type = child.getChildByName(childName);
encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, narr2, childName, type, null, theIncludedResource);
continue;
}
}
}
List<? extends IElement> values = nextChild.getAccessor().getValues(theElement);
List<? extends IBase> values = nextChild.getAccessor().getValues(theElement);
if (values == null || values.isEmpty()) {
continue;
}
for (IElement nextValue : values) {
for (IBase nextValue : values) {
if ((nextValue == null || nextValue.isEmpty()) && !(nextValue instanceof ContainedDt)) {
continue;
}
Class<? extends IElement> type = nextValue.getClass();
Class<? extends IBase> type = nextValue.getClass();
String childName = nextChild.getChildNameByDatatype(type);
String extensionUrl = nextChild.getExtensionUrl();
BaseRuntimeElementDefinition<?> childDef = nextChild.getChildElementDefinitionByDatatype(type);
@ -559,15 +575,13 @@ public class XmlParser extends BaseParser implements IParser {
}
}
private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IResource theResource, IElement theElement, XMLStreamWriter theEventWriter,
BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theElement, XMLStreamWriter theEventWriter, BaseRuntimeElementCompositeDefinition<?> resDef, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
encodeExtensionsIfPresent(theResDef, theResource, theEventWriter, theElement, theIncludedResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getExtensions(), theIncludedResource);
encodeCompositeElementChildrenToStreamWriter(theResDef, theResource, theElement, theEventWriter, resDef.getChildren(), theIncludedResource);
}
private void encodeExtensionsIfPresent(RuntimeResourceDefinition theResDef, IResource theResource, XMLStreamWriter theWriter, IElement theElement, boolean theIncludedResource)
throws XMLStreamException, DataFormatException {
private void encodeExtensionsIfPresent(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theWriter, IBase theElement, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
if (theElement instanceof ISupportsUndeclaredExtensions) {
ISupportsUndeclaredExtensions res = (ISupportsUndeclaredExtensions) theElement;
encodeUndeclaredExtensions(theResDef, theResource, theWriter, res.getUndeclaredExtensions(), "extension", theIncludedResource);
@ -638,8 +652,7 @@ public class XmlParser extends BaseParser implements IParser {
theEventWriter.writeEndElement();
}
private void encodeUndeclaredExtensions(RuntimeResourceDefinition theResDef, IResource theResource, XMLStreamWriter theWriter, List<ExtensionDt> extensions, String tagName,
boolean theIncludedResource) throws XMLStreamException, DataFormatException {
private void encodeUndeclaredExtensions(RuntimeResourceDefinition theResDef, IBaseResource theResource, XMLStreamWriter theWriter, List<ExtensionDt> extensions, String tagName, boolean theIncludedResource) throws XMLStreamException, DataFormatException {
for (ExtensionDt next : extensions) {
theWriter.writeStartElement(tagName);
theWriter.writeAttribute("url", next.getUrl().getValue());
@ -755,12 +768,12 @@ public class XmlParser extends BaseParser implements IParser {
}
}
private Bundle parseBundle(XMLEventReader theStreamReader, Class<? extends IResource> theResourceType) {
private Bundle parseBundle(XMLEventReader theStreamReader, Class<? extends IBaseResource> theResourceType) {
ParserState<Bundle> parserState = ParserState.getPreAtomInstance(myContext, theResourceType, false);
return doXmlLoop(theStreamReader, parserState);
}
private <T extends IResource> T parseResource(Class<T> theResourceType, XMLEventReader theStreamReader) {
private <T extends IBaseResource> T parseResource(Class<T> theResourceType, XMLEventReader theStreamReader) {
ParserState<T> parserState = ParserState.getPreResourceInstance(theResourceType, myContext, false);
return doXmlLoop(theStreamReader, parserState);
}

View File

@ -20,8 +20,7 @@ package ca.uhn.fhir.rest.client;
* #L%
*/
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.*;
import java.io.IOException;
import java.io.Reader;
@ -36,6 +35,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.context.RuntimeResourceDefinition;
@ -233,7 +233,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public <T extends IResource> T read(final Class<T> theType, IdDt theId) {
public <T extends IBaseResource> T read(final Class<T> theType, IdDt theId) {
if (theId == null || theId.hasIdPart() == false) {
throw new IllegalArgumentException("theId does not contain a valid ID, is: " + theId);
}
@ -254,7 +254,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public <T extends IResource> T read(Class<T> theType, String theId) {
public <T extends IBaseResource> T read(Class<T> theType, String theId) {
return read(theType, new IdDt(theId));
}
@ -274,7 +274,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
if (def == null) {
throw new IllegalArgumentException(myContext.getLocalizer().getMessage(I18N_CANNOT_DETEMINE_RESOURCE_TYPE, theUrl.getValueAsString()));
}
return read(def.getImplementingClass(), id);
return (IResource) read(def.getImplementingClass(), id);
}
@Override
@ -283,7 +283,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public <T extends IResource> Bundle search(final Class<T> theType, Map<String, List<IQueryParameterType>> theParams) {
public <T extends IBaseResource> Bundle search(final Class<T> theType, Map<String, List<IQueryParameterType>> theParams) {
LinkedHashMap<String, List<String>> params = new LinkedHashMap<String, List<String>>();
for (Entry<String, List<IQueryParameterType>> nextEntry : theParams.entrySet()) {
ArrayList<String> valueList = new ArrayList<String>();
@ -307,7 +307,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
@Override
public <T extends IResource> Bundle search(final Class<T> theType, UriDt theUrl) {
public <T extends IBaseResource> Bundle search(final Class<T> theType, UriDt theUrl) {
BaseHttpClientInvocation invocation = new HttpGetClientInvocation(theUrl.getValueAsString());
return invokeClient(myContext, new BundleResponseHandler(theType), invocation);
}
@ -317,7 +317,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
return search(inferResourceClass(theUrl), theUrl);
}
private Class<? extends IResource> inferResourceClass(UriDt theUrl) {
private Class<? extends IBaseResource> inferResourceClass(UriDt theUrl) {
String urlString = theUrl.getValueAsString();
int i = urlString.indexOf('?');
@ -360,7 +360,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
myLogRequestAndResponse = theLogRequestAndResponse;
}
private String toResourceName(Class<? extends IResource> theType) {
private String toResourceName(Class<? extends IBaseResource> theType) {
return myContext.getResourceDefinition(theType).getName();
}
@ -544,9 +544,9 @@ public class GenericClient extends BaseClient implements IGenericClient {
private final class BundleResponseHandler implements IClientResponseHandler<Bundle> {
private Class<? extends IResource> myType;
private Class<? extends IBaseResource> myType;
public BundleResponseHandler(Class<? extends IResource> theType) {
public BundleResponseHandler(Class<? extends IBaseResource> theType) {
myType = theType;
}
@ -832,7 +832,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
}
}
private final class ResourceResponseHandler<T extends IResource> implements IClientResponseHandler<T> {
private final class ResourceResponseHandler<T extends IBaseResource> implements IClientResponseHandler<T> {
private IdDt myId;
private Class<T> myType;
@ -852,7 +852,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
T retVal = parser.parseResource(myType, theResponseReader);
if (myId != null) {
retVal.setId(myId);
myId.applyTo(retVal);
}
MethodUtil.parseClientRequestResourceHeaders(theHeaders, retVal);
@ -869,7 +869,7 @@ public class GenericClient extends BaseClient implements IGenericClient {
private Integer myParamLimit;
private String myResourceId;
private String myResourceName;
private Class<? extends IResource> myResourceType;
private Class<? extends IBaseResource> myResourceType;
private SearchStyleEnum mySearchStyle;
private List<SortInternal> mySort = new ArrayList<SortInternal>();

View File

@ -23,6 +23,8 @@ package ca.uhn.fhir.rest.client;
import java.util.List;
import java.util.Map;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.IResource;
@ -152,7 +154,7 @@ public interface IGenericClient {
* The ID to load, including the resource ID and the resource version ID. Valid values include "Patient/123/_history/222", or "http://example.com/fhir/Patient/123/_history/222"
* @return The resource
*/
<T extends IResource> T read(Class<T> theType, IdDt theId);
<T extends IBaseResource> T read(Class<T> theType, IdDt theId);
/**
* Implementation of the "instance read" method.
@ -163,7 +165,7 @@ public interface IGenericClient {
* The ID to load
* @return The resource
*/
<T extends IResource> T read(Class<T> theType, String theId);
<T extends IBaseResource> T read(Class<T> theType, String theId);
/**
* Perform the "read" operation (retrieve the latest version of a resource instance by ID) using an absolute URL.
@ -200,7 +202,7 @@ public interface IGenericClient {
* @param theParams
* @return
*/
<T extends IResource> Bundle search(Class<T> theType, Map<String, List<IQueryParameterType>> theParams);
<T extends IBaseResource> Bundle search(Class<T> theType, Map<String, List<IQueryParameterType>> theParams);
/**
* Perform the "search" operation using an absolute URL.
@ -211,7 +213,7 @@ public interface IGenericClient {
* The absolute URL, e.g. "http://example.com/fhir/Patient/123"
* @return The returned bundle from the server
*/
<T extends IResource> Bundle search(Class<T> theType, UriDt theUrl);
<T extends IBaseResource> Bundle search(Class<T> theType, UriDt theUrl);
Bundle search(UriDt theUrl);

View File

@ -1,7 +1,6 @@
package ca.uhn.fhir.rest.method;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.*;
import java.io.IOException;
import java.io.PushbackReader;
@ -22,6 +21,9 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.hl7.fhir.instance.model.IBaseResource;
import org.hl7.fhir.instance.model.Resource;
import org.hl7.fhir.instance.model.Resource.ResourceMetaComponent;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.context.FhirContext;
@ -115,20 +117,24 @@ public class MethodUtil {
}
addTagsToPostOrPut(theResource, retVal);
// addContentTypeHeaderBasedOnDetectedType(retVal, theResourceBody);
// addContentTypeHeaderBasedOnDetectedType(retVal, theResourceBody);
return retVal;
}
public static void parseClientRequestResourceHeaders(Map<String, List<String>> theHeaders, IResource resource) {
public static void parseClientRequestResourceHeaders(Map<String, List<String>> theHeaders, IBaseResource resource) {
List<String> lmHeaders = theHeaders.get(Constants.HEADER_LAST_MODIFIED_LOWERCASE);
if (lmHeaders != null && lmHeaders.size() > 0 && StringUtils.isNotBlank(lmHeaders.get(0))) {
String headerValue = lmHeaders.get(0);
Date headerDateValue;
try {
headerDateValue = DateUtils.parseDate(headerValue);
InstantDt lmValue = new InstantDt(headerDateValue);
resource.getResourceMetadata().put(ResourceMetadataKeyEnum.UPDATED, lmValue);
if (resource instanceof IResource) {
InstantDt lmValue = new InstantDt(headerDateValue);
((IResource) resource).getResourceMetadata().put(ResourceMetadataKeyEnum.UPDATED, lmValue);
} else if (resource instanceof Resource){
((Resource) resource).getMeta().setLastUpdated(headerDateValue);
}
} catch (Exception e) {
ourLog.warn("Unable to parse date string '{}'. Error is: {}", headerValue, e.toString());
}
@ -137,7 +143,9 @@ public class MethodUtil {
List<String> clHeaders = theHeaders.get(Constants.HEADER_CONTENT_LOCATION_LC);
if (clHeaders != null && clHeaders.size() > 0 && StringUtils.isNotBlank(clHeaders.get(0))) {
String headerValue = clHeaders.get(0);
resource.getId().setValue(headerValue);
if (isNotBlank(headerValue)) {
new IdDt(headerValue).applyTo(resource);
}
}
List<String> categoryHeaders = theHeaders.get(Constants.HEADER_CATEGORY_LC);
@ -146,7 +154,14 @@ public class MethodUtil {
for (String header : categoryHeaders) {
parseTagValue(tagList, header);
}
ResourceMetadataKeyEnum.TAG_LIST.put(resource, tagList);
if (resource instanceof IResource) {
ResourceMetadataKeyEnum.TAG_LIST.put((IResource) resource, tagList);
} else if (resource instanceof Resource) {
ResourceMetaComponent meta = ((Resource) resource).getMeta();
for (Tag next : tagList) {
meta.addTag().setSystem(next.getScheme()).setCode(next.getTerm()).setDisplay(next.getLabel());
}
}
}
}
@ -261,8 +276,8 @@ public class MethodUtil {
}
addTagsToPostOrPut(theResource, retVal);
// addContentTypeHeaderBasedOnDetectedType(retVal, theResourceBody);
// addContentTypeHeaderBasedOnDetectedType(retVal, theResourceBody);
return retVal;
}

View File

@ -21,6 +21,9 @@ package ca.uhn.fhir.rest.param;
*/
import static org.apache.commons.lang3.StringUtils.*;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IQueryParameterType;
import ca.uhn.fhir.model.api.IResource;
@ -89,7 +92,7 @@ public class ReferenceParam extends IdDt implements IQueryParameterType {
myChain = theChain;
}
public Class<? extends IResource> getResourceType(FhirContext theCtx) {
public Class<? extends IBaseResource> getResourceType(FhirContext theCtx) {
if (isBlank(getResourceType())) {
return null;
}

View File

@ -53,7 +53,7 @@ public class FhirTerser {
myContext = theContext;
}
private <T extends IElement> void addUndeclaredExtensions(IElement theElement, BaseRuntimeElementDefinition<?> theDefinition, BaseRuntimeChildDefinition theChildDefinition,
private <T extends IBase> void addUndeclaredExtensions(IBase theElement, BaseRuntimeElementDefinition<?> theDefinition, BaseRuntimeChildDefinition theChildDefinition,
IModelVisitor theCallback) {
if (theElement instanceof ISupportsUndeclaredExtensions) {
ISupportsUndeclaredExtensions containingElement = (ISupportsUndeclaredExtensions) theElement;
@ -87,7 +87,7 @@ public class FhirTerser {
visit(theResource, null, def, new IModelVisitor() {
@SuppressWarnings("unchecked")
@Override
public void acceptElement(IElement theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition) {
public void acceptElement(IBase theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition) {
if (theElement == null || theElement.isEmpty()) {
return;
}
@ -140,13 +140,13 @@ public class FhirTerser {
private List<Object> getValues(BaseRuntimeElementCompositeDefinition<?> theCurrentDef, Object theCurrentObj, List<String> theSubList) {
String name = theSubList.get(0);
BaseRuntimeChildDefinition nextDef = theCurrentDef.getChildByNameOrThrowDataFormatException(name);
List<? extends IElement> values = nextDef.getAccessor().getValues(theCurrentObj);
List<? extends IBase> values = nextDef.getAccessor().getValues(theCurrentObj);
List<Object> retVal = new ArrayList<Object>();
if (theSubList.size() == 1) {
retVal.addAll(values);
} else {
for (IElement nextElement : values) {
for (IBase nextElement : values) {
BaseRuntimeElementCompositeDefinition<?> nextChildDef = (BaseRuntimeElementCompositeDefinition<?>) myContext.getElementDefinition(nextElement.getClass());
List<?> foundValues = getValues(nextChildDef, nextElement, theSubList.subList(1, theSubList.size()));
retVal.addAll(foundValues);
@ -170,7 +170,7 @@ public class FhirTerser {
}
private <T extends IElement> void visit(IElement theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition, IModelVisitor theCallback) {
private <T extends IElement> void visit(IBase theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition, IModelVisitor theCallback) {
theCallback.acceptElement(theElement, theChildDefinition, theDefinition);
addUndeclaredExtensions(theElement, theDefinition, theChildDefinition, theCallback);
@ -198,9 +198,9 @@ public class FhirTerser {
case RESOURCE: {
BaseRuntimeElementCompositeDefinition<?> childDef = (BaseRuntimeElementCompositeDefinition<?>) theDefinition;
for (BaseRuntimeChildDefinition nextChild : childDef.getChildrenAndExtension()) {
List<? extends IElement> values = nextChild.getAccessor().getValues(theElement);
List<? extends IBase> values = nextChild.getAccessor().getValues(theElement);
if (values != null) {
for (IElement nextValue : values) {
for (IBase nextValue : values) {
if (nextValue == null) {
continue;
}

View File

@ -20,10 +20,11 @@ package ca.uhn.fhir.util;
* #L%
*/
import org.hl7.fhir.instance.model.IBase;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition;
import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.ISupportsUndeclaredExtensions;
/**
@ -37,7 +38,7 @@ public interface IModelVisitor {
* @param theChildDefinition May be null if this is a root element
* @param theDefinition
*/
void acceptElement(IElement theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition);
void acceptElement(IBase theElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition);
/**
*

View File

@ -34,7 +34,6 @@ import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.BundleEntry;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.base.resource.BaseOperationOutcome.BaseIssue;
import ca.uhn.fhir.model.dstu.valueset.IssueSeverityEnum;
import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
import com.phloc.commons.error.IResourceError;
@ -88,7 +87,7 @@ public class SchematronBaseValidator implements IValidator {
private ISchematronResource getSchematron(ValidationContext<IResource> theCtx) {
Class<? extends IResource> resource = theCtx.getResource().getClass();
Class<? extends IResource> baseResourceClass = theCtx.getFhirContext().getResourceDefinition(resource).getBaseDefinition().getImplementingClass();
Class<? extends IResource> baseResourceClass = (Class<? extends IResource>) theCtx.getFhirContext().getResourceDefinition(resource).getBaseDefinition().getImplementingClass();
return getSchematronAndCache(theCtx, "dstu", baseResourceClass);
}

View File

@ -7,4 +7,6 @@ package org.hl7.fhir.instance.model;
*/
public interface IBase {
boolean isEmpty();
}

View File

@ -1,5 +1,14 @@
package org.hl7.fhir.instance.model;
public interface IPrimitiveType extends IBase {
public interface IPrimitiveType<T> extends IBase {
void setValueAsString(String theValue) throws IllegalArgumentException;
String getValueAsString();
T getValue();
IPrimitiveType<T> setValue(T theValue) throws IllegalArgumentException;
}

View File

@ -3,7 +3,7 @@ package org.hl7.fhir.instance.model;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
public abstract class PrimitiveType<T> extends Type implements IPrimitiveType {
public abstract class PrimitiveType<T> extends Type implements IPrimitiveType<T> {
private static final long serialVersionUID = 2L;

View File

@ -36,6 +36,7 @@ import ca.uhn.fhir.model.api.ICompositeDatatype;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.WordUtils;
import org.hl7.fhir.instance.model.IBaseResource;
import ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum;
import ca.uhn.fhir.model.api.IFhirVersion;
@ -131,12 +132,13 @@ public class FhirDstu1 implements IFhirVersion {
private void fillName(StructureElement elem, BaseRuntimeElementDefinition<?> nextDef) {
if (nextDef instanceof RuntimeResourceReferenceDefinition) {
RuntimeResourceReferenceDefinition rr = (RuntimeResourceReferenceDefinition) nextDef;
for (Class<? extends IResource> next : rr.getResourceTypes()) {
for (Class<? extends IBaseResource> next : rr.getResourceTypes()) {
StructureElementDefinitionType type = elem.getDefinition().addType();
type.getCode().setValue("ResourceReference");
if (next != IResource.class) {
RuntimeResourceDefinition resDef = rr.getDefinitionForResourceType(next);
@SuppressWarnings("unchecked")
RuntimeResourceDefinition resDef = rr.getDefinitionForResourceType((Class<? extends IResource>) next);
type.getProfile().setValueAsString(resDef.getResourceProfile());
}
}

View File

@ -13,6 +13,7 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
@ -28,7 +29,6 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import sun.misc.BASE64Decoder;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.base.composite.BaseCodingDt;
import ca.uhn.fhir.model.base.resource.BaseSecurityEvent;
@ -114,7 +114,7 @@ public class AuditingInterceptorTest {
assertEquals("Patient: PatientOne Test", object.getName().getValue());
assertEquals(SecurityEventObjectLifecycleEnum.ACCESS_OR_USE, object.getLifecycle().getValueAsEnum());
assertEquals(SecurityEventObjectTypeEnum.PERSON, object.getType().getValueAsEnum());
assertEquals(requestURL, new String(new BASE64Decoder().decodeBuffer(object.getQuery().getValueAsString())));
assertEquals(requestURL, new String(Base64.decodeBase64(object.getQuery().getValueAsString())));
}
@ -162,7 +162,7 @@ public class AuditingInterceptorTest {
}else{
fail("Unexpected patient identifier being audited: " + object.getIdentifier().getValue().getValue());
}
assertEquals(requestURL, new String(new BASE64Decoder().decodeBuffer(object.getQuery().getValueAsString())));
assertEquals(requestURL, new String(Base64.decodeBase64(object.getQuery().getValueAsString())));
assertEquals(SecurityEventObjectTypeEnum.PERSON, object.getType().getValueAsEnum());
}

View File

@ -31,6 +31,7 @@ import org.hamcrest.core.IsNot;
import org.hamcrest.core.StringContains;
import org.hamcrest.text.StringContainsInOrder;
import org.hl7.fhir.instance.model.List_;
import org.hl7.fhir.instance.model.Narrative.NarrativeStatus;
import org.hl7.fhir.instance.model.Organization;
import org.hl7.fhir.instance.model.Patient;
import org.hl7.fhir.instance.model.Profile;
@ -710,7 +711,7 @@ public class XmlParserTest {
Patient patient = ourCtx.newXmlParser().parse(Patient.class, msg);
assertEquals(NarrativeStatusEnum.GENERATED, patient.getText().getStatus().getValueAsEnum());
assertEquals(NarrativeStatus.GENERATED, patient.getText().getStatus());
assertEquals("<div xmlns=\"http://www.w3.org/1999/xhtml\">John Cardinal: 444333333 </div>", patient.getText().getDiv().getValueAsString());
assertEquals("PRP1660", patient.getIdentifier().get(0).getValue().getValueAsString());