public static List<String> getNamedParameterValuesAsString(FhirContext theCtx, IBaseParameters theParameters, String theParameterName) { Validate.notNull(theParameters, "theParameters must not be null"); RuntimeResourceDefinition resDef = theCtx.getResourceDefinition(theParameters.getClass()); BaseRuntimeChildDefinition parameterChild = resDef.getChildByName("parameter"); List<IBase> parameterReps = parameterChild.getAccessor().getValues(theParameters); List<String> retVal = new ArrayList<>(); for (IBase nextParameter : parameterReps) { BaseRuntimeElementCompositeDefinition<?> nextParameterDef = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(nextParameter.getClass()); BaseRuntimeChildDefinition nameChild = nextParameterDef.getChildByName("name"); List<IBase> nameValues = nameChild.getAccessor().getValues(nextParameter); Optional<? extends IPrimitiveType<?>> nameValue = nameValues .stream() .filter(t -> t instanceof IPrimitiveType<?>) .map(t -> ((IPrimitiveType<?>) t)) .findFirst(); if (!nameValue.isPresent() || !theParameterName.equals(nameValue.get().getValueAsString())) { continue; } BaseRuntimeChildDefinition valueChild = nextParameterDef.getChildByName("value[x]"); List<IBase> valueValues = valueChild.getAccessor().getValues(nextParameter); valueValues .stream() .filter(t->t instanceof IPrimitiveType<?>) .map(t->((IPrimitiveType<?>)t).getValueAsString()) .filter(StringUtils::isNotBlank) .forEach(retVal::add); } return retVal; }
private static void addClientParameter(FhirContext theContext, Object theValue, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) { if (theValue instanceof IBaseResource) { IBase parameter = createParameterRepetition(theContext, theTargetResource, paramChild, paramChildElem, theName); paramChildElem.getChildByName("resource").getMutator().addValue(parameter, (IBaseResource) theValue); } else if (theValue instanceof IBaseDatatype) { IBase parameter = createParameterRepetition(theContext, theTargetResource, paramChild, paramChildElem, theName); paramChildElem.getChildByName("value[x]").getMutator().addValue(parameter, (IBaseDatatype) theValue); } else if (theValue instanceof Collection) { Collection<?> collection = (Collection<?>) theValue; for (Object next : collection) { addClientParameter(theContext, next, theTargetResource, paramChild, paramChildElem, theName); } } else { throw new IllegalArgumentException("Don't know how to handle value of type " + theValue.getClass() + " for parameter " + theName); } }
private void minimizeResource(IBaseResource theInput) { if (theInput instanceof IBaseBundle) { for (IBaseResource next : BundleUtil.toListOfResources(myCtx, (IBaseBundle) theInput)) { minimizeResource(next); } } BaseRuntimeElementCompositeDefinition<?> element = (BaseRuntimeElementCompositeDefinition) myCtx.getElementDefinition(theInput.getClass()); BaseRuntimeChildDefinition textElement = element.getChildByName("text"); if (textElement != null) { textElement.getMutator().setValue(theInput, null); } }
private static String getFirstIssueStringPart(FhirContext theCtx, IBaseOperationOutcome theOutcome, String name) { if (theOutcome == null) { return null; } RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome); BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue"); List<IBase> issues = issueChild.getAccessor().getValues(theOutcome); if (issues.isEmpty()) { return null; } IBase issue = issues.get(0); BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(issue.getClass()); BaseRuntimeChildDefinition detailsChild = issueElement.getChildByName(name); List<IBase> details = detailsChild.getAccessor().getValues(issue); if (details.isEmpty()) { return null; } return ((IPrimitiveType<?>) details.get(0)).getValueAsString(); }
/** * Extract all of the resources of a given type from a given bundle */ @SuppressWarnings("unchecked") public static <T extends IBaseResource> List<T> toListOfResourcesOfType(FhirContext theContext, IBaseBundle theBundle, Class<T> theTypeToInclude) { List<T> retVal = new ArrayList<>(); RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle); BaseRuntimeChildDefinition entryChild = def.getChildByName("entry"); List<IBase> entries = entryChild.getAccessor().getValues(theBundle); BaseRuntimeElementCompositeDefinition<?> entryChildElem = (BaseRuntimeElementCompositeDefinition<?>) entryChild.getChildByName("entry"); BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); for (IBase nextEntry : entries) { for (IBase next : resourceChild.getAccessor().getValues(nextEntry)) { if (theTypeToInclude != null && !theTypeToInclude.isAssignableFrom(next.getClass())) { continue; } retVal.add((T) next); } } return retVal; }
BaseRuntimeElementCompositeDefinition<?> paramChildElem = (BaseRuntimeElementCompositeDefinition<?>) paramChild.getChildByName("parameter"); RuntimeChildPrimitiveDatatypeDefinition nameChild = (RuntimeChildPrimitiveDatatypeDefinition) paramChildElem.getChildByName("name"); BaseRuntimeChildDefinition valueChild = paramChildElem.getChildByName("value[x]"); BaseRuntimeChildDefinition resourceChild = paramChildElem.getChildByName("resource");
private <T extends IBaseBundle> IGetPageTyped<T> nextOrPrevious(String theWantRel, T theBundle) { RuntimeResourceDefinition def = myContext.getResourceDefinition(theBundle); List<IBase> links = def.getChildByName("link").getAccessor().getValues(theBundle); if (links == null || links.isEmpty()) { throw new IllegalArgumentException(myContext.getLocalizer().getMessage(GenericClient.class, "noPagingLinkFoundInBundle", theWantRel)); } for (IBase nextLink : links) { BaseRuntimeElementCompositeDefinition linkDef = (BaseRuntimeElementCompositeDefinition) myContext.getElementDefinition(nextLink.getClass()); List<IBase> rel = linkDef.getChildByName("relation").getAccessor().getValues(nextLink); if (rel == null || rel.isEmpty()) { continue; } String relation = ((IPrimitiveType<?>) rel.get(0)).getValueAsString(); if (theWantRel.equals(relation) || (theWantRel == PREVIOUS && PREV.equals(relation))) { List<IBase> urls = linkDef.getChildByName("url").getAccessor().getValues(nextLink); if (urls == null || urls.isEmpty()) { continue; } String url = ((IPrimitiveType<?>) urls.get(0)).getValueAsString(); if (isBlank(url)) { continue; } return (IGetPageTyped<T>) byUrl(url).andReturnBundle(theBundle.getClass()); } } throw new IllegalArgumentException(myContext.getLocalizer().getMessage(GenericClient.class, "noPagingLinkFoundInBundle", theWantRel)); }
BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); BaseRuntimeChildDefinition requestChild = entryChildElem.getChildByName("request"); BaseRuntimeElementCompositeDefinition<?> requestDef = (BaseRuntimeElementCompositeDefinition<?>) requestChild.getChildByName("request"); BaseRuntimeChildDefinition urlChild = requestDef.getChildByName("url");
for (IBase nextValue : nextChild.getAccessor().getValues(theSource)) { String elementName = nextChild.getChildNameByDatatype(nextValue.getClass()); BaseRuntimeChildDefinition targetChild = targetDef.getChildByName(elementName); if (targetChild == null) { if (theIgnoreMissingFields) {
@SuppressWarnings("unchecked") private void addParam(String theName, IBase theValue) { BaseRuntimeChildDefinition parameterChild = myParametersDef.getChildByName("parameter"); BaseRuntimeElementCompositeDefinition<?> parameterElem = (BaseRuntimeElementCompositeDefinition<?>) parameterChild.getChildByName("parameter"); IBase parameter = parameterElem.newInstance(); parameterChild.getMutator().addValue(myParameters, parameter); IPrimitiveType<String> name = (IPrimitiveType<String>) myContext.getElementDefinition("string").newInstance(); name.setValue(theName); parameterElem.getChildByName("name").getMutator().setValue(parameter, name); if (theValue instanceof IBaseDatatype) { BaseRuntimeElementDefinition<?> datatypeDef = myContext.getElementDefinition(theValue.getClass()); if (datatypeDef instanceof IRuntimeDatatypeDefinition) { Class<? extends IBaseDatatype> profileOf = ((IRuntimeDatatypeDefinition) datatypeDef).getProfileOf(); if (profileOf != null) { datatypeDef = myContext.getElementDefinition(profileOf); } } String childElementName = "value" + StringUtils.capitalize(datatypeDef.getName()); BaseRuntimeChildDefinition childByName = parameterElem.getChildByName(childElementName); childByName.getMutator().setValue(parameter, theValue); } else if (theValue instanceof IBaseResource) { parameterElem.getChildByName("resource").getMutator().setValue(parameter, theValue); } else { throw new IllegalArgumentException("Don't know how to handle parameter of type " + theValue.getClass()); } }
IBase meta = metaValues.get(0); BaseRuntimeElementCompositeDefinition<?> metaDef = (BaseRuntimeElementCompositeDefinition<?>) myFhirContext.getElementDefinition(meta.getClass()); BaseRuntimeChildDefinition versionIdDef = metaDef.getChildByName("versionId"); List<IBase> versionIdValues = versionIdDef.getAccessor().getValues(meta); if (versionIdValues.size() > 0) {
BaseRuntimeChildDefinition relChild = relDef.getChildByName("relation"); List<IBase> relValues = relChild.getAccessor().getValues(nextLink); for (IBase next : relValues) { BaseRuntimeChildDefinition urlChild = linkDef.getChildByName("url"); List<IBase> values = urlChild.getAccessor().getValues(nextLink); for (IBase nextUrl : values) {
BaseRuntimeChildDefinition resourceChild = entryChildElem.getChildByName("resource"); BaseRuntimeChildDefinition requestChild = entryChildElem.getChildByName("request"); BaseRuntimeElementCompositeDefinition<?> requestElem = (BaseRuntimeElementCompositeDefinition<?>) requestChild.getChildByName("request"); BaseRuntimeChildDefinition urlChild = requestElem.getChildByName("url"); BaseRuntimeChildDefinition methodChild = requestElem.getChildByName("method");
private static IBase createParameterRepetition(FhirContext theContext, IBaseResource theTargetResource, BaseRuntimeChildDefinition paramChild, BaseRuntimeElementCompositeDefinition<?> paramChildElem, String theName) { IBase parameter = paramChildElem.newInstance(); paramChild.getMutator().addValue(theTargetResource, parameter); IPrimitiveType<?> value; value = createString(theContext, theName); paramChildElem.getChildByName("name").getMutator().addValue(parameter, value); return parameter; }
List<IBase> resources = entryDef.getChildByName("resource").getAccessor().getValues(nextEntry1); if (resources == null) { continue;
private static void populateDetails(FhirContext theCtx, IBase theIssue, String theSeverity, String theDetails, String theLocation, String theCode) { BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) theCtx.getElementDefinition(theIssue.getClass()); BaseRuntimeChildDefinition detailsChild; detailsChild = issueElement.getChildByName("diagnostics"); BaseRuntimeChildDefinition codeChild = issueElement.getChildByName("code"); IPrimitiveType<?> codeElem = (IPrimitiveType<?>) codeChild.getChildByName("code").newInstance(codeChild.getInstanceConstructorArguments()); codeElem.setValueAsString(theCode); codeChild.getMutator().addValue(theIssue, codeElem); BaseRuntimeElementDefinition<?> stringDef = detailsChild.getChildByName(detailsChild.getElementName()); BaseRuntimeChildDefinition severityChild = issueElement.getChildByName("severity"); BaseRuntimeChildDefinition locationChild = issueElement.getChildByName("location"); IPrimitiveType<?> severityElem = (IPrimitiveType<?>) severityChild.getChildByName("severity").newInstance(severityChild.getInstanceConstructorArguments()); severityElem.setValueAsString(theSeverity); severityChild.getMutator().addValue(theIssue, severityElem); IPrimitiveType<?> string = (IPrimitiveType<?>) stringDef.newInstance(); string.setValueAsString(theDetails); detailsChild.getMutator().setValue(theIssue, string); if (isNotBlank(theLocation)) { IPrimitiveType<?> locationElem = (IPrimitiveType<?>) locationChild.getChildByName("location").newInstance(locationChild.getInstanceConstructorArguments()); locationElem.setValueAsString(theLocation); locationChild.getMutator().addValue(theIssue, locationElem); } }
@SuppressWarnings("unchecked") @Override public T invokeClient(String theResponseMimeType, InputStream theResponseInputStream, int theResponseStatusCode, Map<String, List<String>> theHeaders) throws BaseServerResponseException { EncodingEnum respType = EncodingEnum.forContentType(theResponseMimeType); if (respType == null) { throw NonFhirResponseException.newInstance(theResponseStatusCode, theResponseMimeType, theResponseInputStream); } IParser parser = respType.newParser(myContext); RuntimeResourceDefinition type = myContext.getResourceDefinition("Parameters"); IBaseResource retVal = parser.parseResource(type.getImplementingClass(), theResponseInputStream); BaseRuntimeChildDefinition paramChild = type.getChildByName("parameter"); BaseRuntimeElementCompositeDefinition<?> paramChildElem = (BaseRuntimeElementCompositeDefinition<?>) paramChild.getChildByName("parameter"); List<IBase> parameter = paramChild.getAccessor().getValues(retVal); if (parameter == null || parameter.isEmpty()) { return (T) myContext.getElementDefinition(myType).newInstance(); } IBase param = parameter.get(0); List<IBase> meta = paramChildElem.getChildByName("value[x]").getAccessor().getValues(param); if (meta.isEmpty()) { return (T) myContext.getElementDefinition(myType).newInstance(); } return (T) meta.get(0); } }
BaseRuntimeChildDefinition fullUrlChild = entryDef.getChildByName("fullUrl"); if (fullUrlChild == null) { continue; // TODO: remove this once the data model in tinder plugin catches up to 1.2 IPrimitiveType<?> value = (IPrimitiveType<?>) fullUrl.get(0); if (value.isEmpty() == false) { List<IBase> entryResources = entryDef.getChildByName("resource").getAccessor().getValues(nextEntry); if (entryResources != null && entryResources.size() > 0) { IBaseResource res = (IBaseResource) entryResources.get(0);
@SuppressWarnings("unchecked") private T readHtmlResponse(InputStream theResponseInputStream) { RuntimeResourceDefinition resDef = getFhirContext().getResourceDefinition(myReturnType); IBaseResource instance = resDef.newInstance(); BaseRuntimeChildDefinition textChild = resDef.getChildByName("text"); BaseRuntimeElementCompositeDefinition<?> textElement = (BaseRuntimeElementCompositeDefinition<?>) textChild.getChildByName("text"); IBase textInstance = textElement.newInstance(); textChild.getMutator().addValue(instance, textInstance); BaseRuntimeChildDefinition divChild = textElement.getChildByName("div"); BaseRuntimeElementDefinition<?> divElement = divChild.getChildByName("div"); IPrimitiveType<?> divInstance = (IPrimitiveType<?>) divElement.newInstance(); try { divInstance.setValueAsString(IOUtils.toString(theResponseInputStream, Charsets.UTF_8)); } catch (Exception e) { throw new InvalidResponseException(400, "Failed to process HTML response from server: " + e.getMessage(), e); } divChild.getMutator().addValue(textInstance, divInstance); return (T) instance; }
paramChild.getMutator().addValue(parameters, parameter); BaseRuntimeChildDefinition resourceElem = paramChildElem.getChildByName("resource"); resourceElem.getMutator().addValue(parameter, (IBase) retVal);