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