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
016/*
017 * #%L
018 * HAPI FHIR - Core Library
019 * %%
020 * Copyright (C) 2014 - 2019 University Health Network
021 * %%
022 * Licensed under the Apache License, Version 2.0 (the "License");
023 * you may not use this file except in compliance with the License.
024 * You may obtain a copy of the License at
025 *
026 *      http://www.apache.org/licenses/LICENSE-2.0
027 *
028 * Unless required by applicable law or agreed to in writing, software
029 * distributed under the License is distributed on an "AS IS" BASIS,
030 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
031 * See the License for the specific language governing permissions and
032 * limitations under the License.
033 * #L%
034 */
035
036public class RuntimeSearchParam {
037        private final IIdType myId;
038        private final Set<String> myBase;
039        private final List<RuntimeSearchParam> myCompositeOf;
040        private final String myDescription;
041        private final String myName;
042        private final RestSearchParameterTypeEnum myParamType;
043        private final String myPath;
044        private final Set<String> myTargets;
045
046        @Override
047        public String toString() {
048                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
049                        .append("base", myBase)
050                        .append("name", myName)
051                        .append("path", myPath)
052                        .append("id", myId)
053                        .append("uri", myUri)
054                        .toString();
055        }
056
057        private final Set<String> myProvidesMembershipInCompartments;
058        private final RuntimeSearchParamStatusEnum myStatus;
059        private final String myUri;
060
061        public IIdType getId() {
062                return myId;
063        }
064
065        public String getUri() {
066                return myUri;
067        }
068
069        public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf,
070                                                                          Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) {
071                this(theId, theUri, theName, theDescription, thePath, theParamType, theCompositeOf, theProvidesMembershipInCompartments, theTargets, theStatus, null);
072        }
073
074        @Override
075        public boolean equals(Object theO) {
076                if (this == theO) return true;
077
078                if (theO == null || getClass() != theO.getClass()) return false;
079
080                RuntimeSearchParam that = (RuntimeSearchParam) theO;
081
082                return new EqualsBuilder()
083                        .append(getId(), that.getId())
084                        .append(getName(), that.getName())
085                        .append(getPath(), that.getPath())
086                        .append(getUri(), that.getUri())
087                        .isEquals();
088        }
089
090        @Override
091        public int hashCode() {
092                return new HashCodeBuilder(17, 37)
093                        .append(getId())
094                        .append(getName())
095                        .append(getPath())
096                        .append(getUri())
097                        .toHashCode();
098        }
099
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}