public static IBaseReference getSecurityContext(FhirContext theCtx, IBaseBinary theBinary) { RuntimeResourceDefinition def = theCtx.getResourceDefinition("Binary"); BaseRuntimeChildDefinition child = def.getChildByName("securityContext"); IBaseReference retVal = null; if (child != null) { List<IBase> values = child.getAccessor().getValues(theBinary); if (values.size() > 0) { retVal = (IBaseReference) values.get(0); } } return retVal; }
public static List<String> getBaseAsStrings(FhirContext theContext, IBaseResource theResource) { Validate.notNull(theContext, "theContext must not be null"); Validate.notNull(theResource, "theResource must not be null"); RuntimeResourceDefinition def = theContext.getResourceDefinition(theResource); BaseRuntimeChildDefinition base = def.getChildByName("base"); List<IBase> baseValues = base.getAccessor().getValues(theResource); List<String> retVal = new ArrayList<>(); for (IBase next : baseValues) { IPrimitiveType<?> nextPrimitive = (IPrimitiveType<?>) next; retVal.add(nextPrimitive.getValueAsString()); } return retVal; }
/** * Returns true if the given OperationOutcome has 1 or more Operation.issue repetitions */ public static boolean hasIssues(FhirContext theCtx, IBaseOperationOutcome theOutcome) { if (theOutcome == null) { return false; } RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome); BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue"); return issueChild.getAccessor().getValues(theOutcome).size() > 0; }
public static String getBundleType(FhirContext theContext, IBaseBundle theBundle) { RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle); BaseRuntimeChildDefinition entryChild = def.getChildByName("type"); List<IBase> entries = entryChild.getAccessor().getValues(theBundle); if (entries.size() > 0) { IPrimitiveType<?> typeElement = (IPrimitiveType<?>) entries.get(0); return typeElement.getValueAsString(); } return null; }
public static Integer getTotal(FhirContext theContext, IBaseBundle theBundle) { RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle); BaseRuntimeChildDefinition entryChild = def.getChildByName("total"); List<IBase> entries = entryChild.getAccessor().getValues(theBundle); if (entries.size() > 0) { IPrimitiveType<Number> typeElement = (IPrimitiveType<Number>) entries.get(0); if (typeElement != null && typeElement.getValue() != null) { return typeElement.getValue().intValue(); } } return null; }
/** * Add a paratemer value to a Parameters resource * * @param theContext The FhirContext * @param theParameters The Parameters resource * @param theName The parametr name * @param theValue The parameter value (can be a {@link IBaseResource resource} or a {@link IBaseDatatype datatype}) */ public static void addParameterToParameters(FhirContext theContext, IBaseParameters theParameters, String theName, Object theValue) { RuntimeResourceDefinition def = theContext.getResourceDefinition(theParameters); BaseRuntimeChildDefinition paramChild = def.getChildByName("parameter"); BaseRuntimeElementCompositeDefinition<?> paramChildElem = (BaseRuntimeElementCompositeDefinition<?>) paramChild.getChildByName("parameter"); addClientParameter(theContext, theValue, theParameters, paramChild, paramChildElem, theName); }
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 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; }
private static IBase createIssue(FhirContext theCtx, IBaseResource theOutcome) { RuntimeResourceDefinition ooDef = theCtx.getResourceDefinition(theOutcome); BaseRuntimeChildDefinition issueChild = ooDef.getChildByName("issue"); BaseRuntimeElementCompositeDefinition<?> issueElement = (BaseRuntimeElementCompositeDefinition<?>) issueChild.getChildByName("issue"); IBase issue = issueElement.newInstance(); issueChild.getMutator().addValue(theOutcome, issue); return issue; }
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)); }
if (def.getName().equals("Parameters")) { BaseRuntimeChildDefinition paramChild = def.getChildByName("parameter"); BaseRuntimeElementCompositeDefinition<?> paramChildElem = (BaseRuntimeElementCompositeDefinition<?>) paramChild.getChildByName("parameter");
private static void populatePrimitiveValue(FhirContext theContext, IBaseResource theSubscription, String theChildName, String theValue) { RuntimeResourceDefinition def = theContext.getResourceDefinition(theSubscription); Validate.isTrue(def.getName().equals("Subscription"), "theResource is not a subscription"); BaseRuntimeChildDefinition statusChild = def.getChildByName(theChildName); List<IBase> entries = statusChild.getAccessor().getValues(theSubscription); IPrimitiveType<?> instance; if (entries.size() == 0) { BaseRuntimeElementDefinition<?> statusElement = statusChild.getChildByName(theChildName); instance = (IPrimitiveType<?>) statusElement.newInstance(statusChild.getInstanceConstructorArguments()); statusChild.getMutator().addValue(theSubscription, instance); } else { instance = (IPrimitiveType<?>) entries.get(0); } instance.setValueAsString(theValue); }
public static void setSecurityContext(FhirContext theCtx, IBaseBinary theBinary, String theSecurityContext) { RuntimeResourceDefinition def = theCtx.getResourceDefinition("Binary"); BaseRuntimeChildDefinition child = def.getChildByName("securityContext"); BaseRuntimeElementDefinition<?> referenceDef = theCtx.getElementDefinition("reference"); IBaseReference reference = (IBaseReference) referenceDef.newInstance(); child.getMutator().addValue(theBinary, reference); reference.setReference(theSecurityContext); }
BaseRuntimeChildDefinition entryChild = def.getChildByName("link"); List<IBase> links = entryChild.getAccessor().getValues(theBundle); for (IBase nextLink : links) {
@SuppressWarnings("unchecked") public static List<Pair<String, IBaseResource>> getBundleEntryUrlsAndResources(FhirContext theContext, IBaseBundle theBundle) { RuntimeResourceDefinition def = theContext.getResourceDefinition(theBundle); BaseRuntimeChildDefinition entryChild = def.getChildByName("entry"); List<IBase> entries = entryChild.getAccessor().getValues(theBundle);
@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); } }
@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; }
@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()); } }
BaseRuntimeChildDefinition entryChild = def.getChildByName("entry"); List<IBase> entries = entryChild.getAccessor().getValues(theBundle);