
001package ca.uhn.fhir.util; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import java.util.Iterator; 024import java.util.List; 025import java.util.Set; 026 027import org.apache.commons.lang3.builder.ToStringBuilder; 028import org.apache.commons.lang3.builder.ToStringStyle; 029import org.hl7.fhir.instance.model.api.IBaseReference; 030import org.hl7.fhir.instance.model.api.IBaseResource; 031 032import ca.uhn.fhir.context.FhirContext; 033import ca.uhn.fhir.context.RuntimeResourceDefinition; 034import ca.uhn.fhir.context.RuntimeSearchParam; 035import ca.uhn.fhir.model.api.Include; 036 037/** 038 * Created by Bill de Beaubien on 2/26/2015. 039 */ 040public class ResourceReferenceInfo { 041 private String myOwningResource; 042 private String myName; 043 private IBaseReference myResource; 044 private FhirContext myContext; 045 046 public ResourceReferenceInfo(FhirContext theContext, IBaseResource theOwningResource, List<String> thePathToElement, IBaseReference theElement) { 047 myContext = theContext; 048 myOwningResource = theContext.getResourceType(theOwningResource); 049 050 myResource = theElement; 051 if (thePathToElement != null && !thePathToElement.isEmpty()) { 052 StringBuilder sb = new StringBuilder(); 053 for (Iterator<String> iterator = thePathToElement.iterator(); iterator.hasNext();) { 054 sb.append(iterator.next()); 055 if (iterator.hasNext()) 056 sb.append("."); 057 } 058 myName = sb.toString(); 059 } else { 060 myName = null; 061 } 062 } 063 064 @Override 065 public String toString() { 066 ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 067 b.append("name", myName); 068 b.append("resource", myResource.getReferenceElement()); 069 return b.build(); 070 } 071 072 public String getName() { 073 return myName; 074 } 075 076 public IBaseReference getResourceReference() { 077 return myResource; 078 } 079 080 public boolean matchesIncludeSet(Set<Include> theIncludes) { 081 if (theIncludes == null) 082 return false; 083 for (Include include : theIncludes) { 084 if (matchesInclude(include)) 085 return true; 086 } 087 return false; 088 } 089 090 public boolean matchesInclude(Include theInclude) { 091 if (theInclude.getValue().equals("*")) { 092 return true; 093 } 094 int colonIndex = theInclude.getValue().indexOf(':'); 095 if (colonIndex != -1) { 096 // DSTU2+ style 097 String resourceName = theInclude.getValue().substring(0, colonIndex); 098 String paramName = theInclude.getValue().substring(colonIndex + 1); 099 RuntimeResourceDefinition resourceDef = myContext.getResourceDefinition(resourceName); 100 if (resourceDef != null) { 101 RuntimeSearchParam searchParamDef = resourceDef.getSearchParam(paramName); 102 if (searchParamDef!=null) { 103 final String completeName = myOwningResource + "." + myName; 104 boolean matched = false; 105 for (String s : searchParamDef.getPathsSplit()) { 106 if (s.equals(completeName) || 107 s.startsWith(completeName + ".")) { 108 matched = true; break; 109 } 110 } 111 return matched; 112 } 113 } 114 return false; 115 } 116 // DSTU1 style 117 return (theInclude.getValue().equals(myOwningResource + '.' + myName)); 118 } 119}