View Javadoc
1   package ca.uhn.fhir.context;
2   
3   import static org.apache.commons.lang3.StringUtils.isNotBlank;
4   import static org.apache.commons.lang3.StringUtils.trim;
5   
6   import java.util.*;
7   
8   import org.apache.commons.lang3.builder.EqualsBuilder;
9   import org.apache.commons.lang3.builder.HashCodeBuilder;
10  import org.apache.commons.lang3.builder.ToStringBuilder;
11  import org.apache.commons.lang3.builder.ToStringStyle;
12  import org.hl7.fhir.instance.model.api.IIdType;
13  
14  import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
15  
16  /*
17   * #%L
18   * HAPI FHIR - Core Library
19   * %%
20   * Copyright (C) 2014 - 2018 University Health Network
21   * %%
22   * Licensed under the Apache License, Version 2.0 (the "License");
23   * you may not use this file except in compliance with the License.
24   * You may obtain a copy of the License at
25   * 
26   *      http://www.apache.org/licenses/LICENSE-2.0
27   * 
28   * Unless required by applicable law or agreed to in writing, software
29   * distributed under the License is distributed on an "AS IS" BASIS,
30   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31   * See the License for the specific language governing permissions and
32   * limitations under the License.
33   * #L%
34   */
35  
36  public class RuntimeSearchParam {
37  	private final IIdType myId;
38  	private final Set<String> myBase;
39  	private final List<RuntimeSearchParam> myCompositeOf;
40  	private final String myDescription;
41  	private final String myName;
42  	private final RestSearchParameterTypeEnum myParamType;
43  	private final String myPath;
44  	private final Set<String> myTargets;
45  
46  	@Override
47  	public String toString() {
48  		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
49  			.append("base", myBase)
50  			.append("name", myName)
51  			.append("path", myPath)
52  			.append("id", myId)
53  			.append("uri", myUri)
54  			.toString();
55  	}
56  
57  	private final Set<String> myProvidesMembershipInCompartments;
58  	private final RuntimeSearchParamStatusEnum myStatus;
59  	private final String myUri;
60  
61  	public IIdType getId() {
62  		return myId;
63  	}
64  
65  	public String getUri() {
66  		return myUri;
67  	}
68  
69  	public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf,
70  									  Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) {
71  		this(theId, theUri, theName, theDescription, thePath, theParamType, theCompositeOf, theProvidesMembershipInCompartments, theTargets, theStatus, null);
72  	}
73  
74  	@Override
75  	public boolean equals(Object theO) {
76  		if (this == theO) return true;
77  
78  		if (theO == null || getClass() != theO.getClass()) return false;
79  
80  		RuntimeSearchParam that = (RuntimeSearchParam) theO;
81  
82  		return new EqualsBuilder()
83  			.append(getId(), that.getId())
84  			.append(getName(), that.getName())
85  			.append(getPath(), that.getPath())
86  			.append(getUri(), that.getUri())
87  			.isEquals();
88  	}
89  
90  	@Override
91  	public int hashCode() {
92  		return new HashCodeBuilder(17, 37)
93  			.append(getId())
94  			.append(getName())
95  			.append(getPath())
96  			.append(getUri())
97  			.toHashCode();
98  	}
99  
100 	public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf,
101 									  Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus, Collection<String> theBase) {
102 		super();
103 
104 		myId = theId;
105 		myUri = theUri;
106 		myName = theName;
107 		myDescription = theDescription;
108 		myPath = thePath;
109 		myParamType = theParamType;
110 		myCompositeOf = theCompositeOf;
111 		myStatus = theStatus;
112 		if (theProvidesMembershipInCompartments != null && !theProvidesMembershipInCompartments.isEmpty()) {
113 			myProvidesMembershipInCompartments = Collections.unmodifiableSet(theProvidesMembershipInCompartments);
114 		} else {
115 			myProvidesMembershipInCompartments = null;
116 		}
117 		if (theTargets != null && theTargets.isEmpty() == false) {
118 			myTargets = Collections.unmodifiableSet(theTargets);
119 		} else {
120 			myTargets = null;
121 		}
122 
123 		if (theBase == null || theBase.isEmpty()) {
124 			HashSet<String> base = new HashSet<>();
125 			if (isNotBlank(thePath)) {
126 				int indexOf = thePath.indexOf('.');
127 				if (indexOf != -1) {
128 					base.add(trim(thePath.substring(0, indexOf)));
129 				}
130 			}
131 			myBase = Collections.unmodifiableSet(base);
132 		} else {
133 			myBase = Collections.unmodifiableSet(new HashSet<>(theBase));
134 		}
135 	}
136 
137 	public Set<String> getBase() {
138 		return myBase;
139 	}
140 
141 	public Set<String> getTargets() {
142 		return myTargets;
143 	}
144 
145 	public RuntimeSearchParamStatusEnum getStatus() {
146 		return myStatus;
147 	}
148 
149 	public RuntimeSearchParam(String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) {
150 		this(null, null, theName, theDescription, thePath, theParamType, null, theProvidesMembershipInCompartments, theTargets, theStatus);
151 	}
152 
153 	public List<RuntimeSearchParam> getCompositeOf() {
154 		return myCompositeOf;
155 	}
156 
157 	public String getDescription() {
158 		return myDescription;
159 	}
160 
161 	public String getName() {
162 		return myName;
163 	}
164 
165 	public RestSearchParameterTypeEnum getParamType() {
166 		return myParamType;
167 	}
168 
169 	public String getPath() {
170 		return myPath;
171 	}
172 
173 	public List<String> getPathsSplit() {
174 		String path = getPath();
175 		if (path.indexOf('|') == -1) {
176 			return Collections.singletonList(path);
177 		}
178 
179 		List<String> retVal = new ArrayList<String>();
180 		StringTokenizer tok = new StringTokenizer(path, "|");
181 		while (tok.hasMoreElements()) {
182 			String nextPath = tok.nextToken().trim();
183 			retVal.add(nextPath.trim());
184 		}
185 		return retVal;
186 	}
187 
188 	/**
189 	 * Can return null
190 	 */
191 	public Set<String> getProvidesMembershipInCompartments() {
192 		return myProvidesMembershipInCompartments;
193 	}
194 
195 	public enum RuntimeSearchParamStatusEnum {
196 		ACTIVE,
197 		DRAFT,
198 		RETIRED,
199 		UNKNOWN
200 	}
201 	
202 }