001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 University Health Network
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.getResourceDefinition(theOwningResource).getName();
049
050                myResource = theElement;
051                if (thePathToElement != null && !thePathToElement.isEmpty()) {
052                        StringBuilder sb = new StringBuilder();
053                        thePathToElement.iterator();
054                        for (Iterator<String> iterator = thePathToElement.iterator(); iterator.hasNext();) {
055                                sb.append(iterator.next());
056                                if (iterator.hasNext())
057                                        sb.append(".");
058                        }
059                        myName = sb.toString();
060                } else {
061                        myName = null;
062                }
063        }
064
065        @Override
066        public String toString() {
067                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
068                b.append("name", myName);
069                b.append("resource", myResource.getReferenceElement());
070                return b.build();
071        }
072
073        public String getName() {
074                return myName;
075        }
076
077        public IBaseReference getResourceReference() {
078                return myResource;
079        }
080
081        public boolean matchesIncludeSet(Set<Include> theIncludes) {
082                if (theIncludes == null)
083                        return false;
084                for (Include include : theIncludes) {
085                        if (matchesInclude(include))
086                                return true;
087                }
088                return false;
089        }
090
091        public boolean matchesInclude(Include theInclude) {
092                if (theInclude.getValue().equals("*")) {
093                        return true;
094                }
095                int colonIndex = theInclude.getValue().indexOf(':');
096                if (colonIndex != -1) {
097                        // DSTU2+ style
098                        String resourceName = theInclude.getValue().substring(0, colonIndex);
099                        String paramName = theInclude.getValue().substring(colonIndex + 1);
100                        RuntimeResourceDefinition resourceDef = myContext.getResourceDefinition(resourceName);
101                        if (resourceDef != null) {
102                                RuntimeSearchParam searchParamDef = resourceDef.getSearchParam(paramName);
103                                if (searchParamDef!=null) {
104                                        if (searchParamDef.getPathsSplit().contains(myOwningResource + "." + myName)) {
105                                                return true;
106                                        }
107                                }
108                        }
109                        return false;
110                }
111                // DSTU1 style
112                return (theInclude.getValue().equals(myOwningResource + '.' + myName));
113        }
114}