001package ca.uhn.fhir.context; 002 003import ca.uhn.fhir.context.phonetic.IPhoneticEncoder; 004import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum; 005import org.apache.commons.lang3.builder.EqualsBuilder; 006import org.apache.commons.lang3.builder.HashCodeBuilder; 007import org.apache.commons.lang3.builder.ToStringBuilder; 008import org.apache.commons.lang3.builder.ToStringStyle; 009import org.hl7.fhir.instance.model.api.IBaseExtension; 010import org.hl7.fhir.instance.model.api.IIdType; 011 012import javax.annotation.Nonnull; 013import java.util.ArrayList; 014import java.util.Collection; 015import java.util.Collections; 016import java.util.HashMap; 017import java.util.HashSet; 018import java.util.List; 019import java.util.Map; 020import java.util.Set; 021import java.util.StringTokenizer; 022 023import static org.apache.commons.lang3.StringUtils.isNotBlank; 024import static org.apache.commons.lang3.StringUtils.trim; 025 026/* 027 * #%L 028 * HAPI FHIR - Core Library 029 * %% 030 * Copyright (C) 2014 - 2020 University Health Network 031 * %% 032 * Licensed under the Apache License, Version 2.0 (the "License"); 033 * you may not use this file except in compliance with the License. 034 * You may obtain a copy of the License at 035 * 036 * http://www.apache.org/licenses/LICENSE-2.0 037 * 038 * Unless required by applicable law or agreed to in writing, software 039 * distributed under the License is distributed on an "AS IS" BASIS, 040 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 041 * See the License for the specific language governing permissions and 042 * limitations under the License. 043 * #L% 044 */ 045 046public class RuntimeSearchParam { 047 private final IIdType myId; 048 private final Set<String> myBase; 049 private final List<RuntimeSearchParam> myCompositeOf; 050 private final String myDescription; 051 private final String myName; 052 private final RestSearchParameterTypeEnum myParamType; 053 private final String myPath; 054 private final Set<String> myTargets; 055 private final Set<String> myProvidesMembershipInCompartments; 056 private final RuntimeSearchParamStatusEnum myStatus; 057 private final String myUri; 058 private final Map<String, List<IBaseExtension<?, ?>>> myExtensions = new HashMap<>(); 059 private IPhoneticEncoder myPhoneticEncoder; 060 061 /** 062 * Constructor 063 */ 064 public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf, 065 Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) { 066 this(theId, theUri, theName, theDescription, thePath, theParamType, theCompositeOf, theProvidesMembershipInCompartments, theTargets, theStatus, null); 067 } 068 069 /** 070 * Constructor 071 */ 072 public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf, 073 Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus, Collection<String> theBase) { 074 super(); 075 076 myId = theId; 077 myUri = theUri; 078 myName = theName; 079 myDescription = theDescription; 080 myPath = thePath; 081 myParamType = theParamType; 082 myCompositeOf = theCompositeOf; 083 myStatus = theStatus; 084 if (theProvidesMembershipInCompartments != null && !theProvidesMembershipInCompartments.isEmpty()) { 085 myProvidesMembershipInCompartments = Collections.unmodifiableSet(theProvidesMembershipInCompartments); 086 } else { 087 myProvidesMembershipInCompartments = null; 088 } 089 if (theTargets != null && theTargets.isEmpty() == false) { 090 myTargets = Collections.unmodifiableSet(theTargets); 091 } else { 092 myTargets = Collections.emptySet(); 093 } 094 095 if (theBase == null || theBase.isEmpty()) { 096 HashSet<String> base = new HashSet<>(); 097 if (isNotBlank(thePath)) { 098 int indexOf = thePath.indexOf('.'); 099 if (indexOf != -1) { 100 base.add(trim(thePath.substring(0, indexOf))); 101 } 102 } 103 myBase = Collections.unmodifiableSet(base); 104 } else { 105 myBase = Collections.unmodifiableSet(new HashSet<>(theBase)); 106 } 107 } 108 109 /** 110 * Constructor 111 */ 112 public RuntimeSearchParam(String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) { 113 this(null, null, theName, theDescription, thePath, theParamType, null, theProvidesMembershipInCompartments, theTargets, theStatus); 114 } 115 116 /** 117 * Copy constructor 118 */ 119 public RuntimeSearchParam(RuntimeSearchParam theSp) { 120 this(theSp.getId(), theSp.getUri(), theSp.getName(), theSp.getDescription(), theSp.getPath(), theSp.getParamType(), theSp.getCompositeOf(), theSp.getProvidesMembershipInCompartments(), theSp.getTargets(), theSp.getStatus(), theSp.getBase()); 121 } 122 123 /** 124 * Retrieve user data - This can be used to store any application-specific data 125 */ 126 @Nonnull 127 public List<IBaseExtension<?, ?>> getExtensions(String theKey) { 128 List<IBaseExtension<?, ?>> retVal = myExtensions.get(theKey); 129 if (retVal != null) { 130 retVal = Collections.unmodifiableList(retVal); 131 } else { 132 retVal = Collections.emptyList(); 133 } 134 return retVal; 135 } 136 137 /** 138 * Sets user data - This can be used to store any application-specific data 139 */ 140 public RuntimeSearchParam addExtension(String theKey, IBaseExtension theValue) { 141 List<IBaseExtension<?, ?>> valuesList = myExtensions.computeIfAbsent(theKey, k -> new ArrayList<>()); 142 valuesList.add(theValue); 143 return this; 144 } 145 146 @Override 147 public String toString() { 148 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 149 .append("base", myBase) 150 .append("name", myName) 151 .append("path", myPath) 152 .append("id", myId) 153 .append("uri", myUri) 154 .toString(); 155 } 156 157 public IIdType getId() { 158 return myId; 159 } 160 161 public String getUri() { 162 return myUri; 163 } 164 165 @Override 166 public boolean equals(Object theO) { 167 if (this == theO) return true; 168 169 if (theO == null || getClass() != theO.getClass()) return false; 170 171 RuntimeSearchParam that = (RuntimeSearchParam) theO; 172 173 return new EqualsBuilder() 174 .append(getId(), that.getId()) 175 .append(getName(), that.getName()) 176 .append(getPath(), that.getPath()) 177 .append(getUri(), that.getUri()) 178 .isEquals(); 179 } 180 181 @Override 182 public int hashCode() { 183 return new HashCodeBuilder(17, 37) 184 .append(getId()) 185 .append(getName()) 186 .append(getPath()) 187 .append(getUri()) 188 .toHashCode(); 189 } 190 191 public Set<String> getBase() { 192 return myBase; 193 } 194 195 @Nonnull 196 public Set<String> getTargets() { 197 return myTargets; 198 } 199 200 public boolean hasTargets() { 201 return !myTargets.isEmpty(); 202 } 203 204 public RuntimeSearchParamStatusEnum getStatus() { 205 return myStatus; 206 } 207 208 public List<RuntimeSearchParam> getCompositeOf() { 209 return myCompositeOf; 210 } 211 212 public String getDescription() { 213 return myDescription; 214 } 215 216 public String getName() { 217 return myName; 218 } 219 220 public RestSearchParameterTypeEnum getParamType() { 221 return myParamType; 222 } 223 224 public String getPath() { 225 return myPath; 226 } 227 228 public List<String> getPathsSplit() { 229 String path = getPath(); 230 if (path.indexOf('|') == -1) { 231 return Collections.singletonList(path); 232 } 233 234 List<String> retVal = new ArrayList<>(); 235 StringTokenizer tok = new StringTokenizer(path, "|"); 236 while (tok.hasMoreElements()) { 237 String nextPath = tok.nextToken().trim(); 238 retVal.add(nextPath.trim()); 239 } 240 return retVal; 241 } 242 243 /** 244 * Can return null 245 */ 246 public Set<String> getProvidesMembershipInCompartments() { 247 return myProvidesMembershipInCompartments; 248 } 249 250 public enum RuntimeSearchParamStatusEnum { 251 ACTIVE, 252 DRAFT, 253 RETIRED, 254 UNKNOWN 255 } 256 257 public RuntimeSearchParam setPhoneticEncoder(IPhoneticEncoder thePhoneticEncoder) { 258 myPhoneticEncoder = thePhoneticEncoder; 259 return this; 260 } 261 262 public String encode(String theString) { 263 if (myPhoneticEncoder == null || theString == null) { 264 return theString; 265 } 266 return myPhoneticEncoder.encode(theString); 267 } 268}