001package ca.uhn.fhir.util;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 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.model.api.Include;
034
035/**
036 * Created by Bill de Beaubien on 2/26/2015.
037 */
038public class ResourceReferenceInfo {
039        private String myOwningResource;
040        private String myName;
041        private IBaseReference myResource;
042
043        public ResourceReferenceInfo(FhirContext theContext, IBaseResource theOwningResource, List<String> thePathToElement, IBaseReference theElement) {
044
045                myOwningResource = theContext.getResourceDefinition(theOwningResource).getName();
046
047                myResource = theElement;
048                if (thePathToElement != null && !thePathToElement.isEmpty()) {
049                        StringBuilder sb = new StringBuilder();
050                        thePathToElement.iterator();
051                        for (Iterator<String> iterator = thePathToElement.iterator(); iterator.hasNext();) {
052                                sb.append(iterator.next());
053                                if (iterator.hasNext())
054                                        sb.append(".");
055                        }
056                        myName = sb.toString();
057                } else {
058                        myName = null;
059                }
060        }
061
062        @Override
063        public String toString() {
064                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
065                b.append("name", myName);
066                b.append("resource", myResource.getReferenceElement());
067                return b.build();
068        }
069
070        public String getName() {
071                return myName;
072        }
073
074        public IBaseReference getResourceReference() {
075                return myResource;
076        }
077
078        public boolean matchesIncludeSet(Set<Include> theIncludes) {
079                if (theIncludes == null)
080                        return false;
081                for (Include include : theIncludes) {
082                        if (matchesInclude(include))
083                                return true;
084                }
085                return false;
086        }
087
088        public boolean matchesInclude(Include theInclude) {
089                if (theInclude.getValue().equals("*")) {
090                        return true;
091                }
092                if (theInclude.getValue().indexOf(':') != -1) {
093                        // DSTU2 style
094                        return (theInclude.getValue().equals(myOwningResource + ':' + myName));
095                } else {
096                        // DSTU1 style
097                        return (theInclude.getValue().equals(myOwningResource + '.' + myName));
098                }
099        }
100}