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.hl7.fhir.instance.model.api.IIdType;
009
010import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
011
012/*
013 * #%L
014 * HAPI FHIR - Core Library
015 * %%
016 * Copyright (C) 2014 - 2017 University Health Network
017 * %%
018 * Licensed under the Apache License, Version 2.0 (the "License");
019 * you may not use this file except in compliance with the License.
020 * You may obtain a copy of the License at
021 * 
022 *      http://www.apache.org/licenses/LICENSE-2.0
023 * 
024 * Unless required by applicable law or agreed to in writing, software
025 * distributed under the License is distributed on an "AS IS" BASIS,
026 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
027 * See the License for the specific language governing permissions and
028 * limitations under the License.
029 * #L%
030 */
031
032public class RuntimeSearchParam {
033        private final IIdType myId;
034        private final Set<String> myBase;
035        private final List<RuntimeSearchParam> myCompositeOf;
036        private final String myDescription;
037        private final String myName;
038        private final RestSearchParameterTypeEnum myParamType;
039        private final String myPath;
040        private final Set<String> myTargets;
041        private final Set<String> myProvidesMembershipInCompartments;
042        private final RuntimeSearchParamStatusEnum myStatus;
043        private final String myUri;
044
045        public IIdType getId() {
046                return myId;
047        }
048
049        public String getUri() {
050                return myUri;
051        }
052
053        public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf,
054                                                                          Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) {
055                this(theId, theUri, theName, theDescription, thePath, theParamType, theCompositeOf, theProvidesMembershipInCompartments, theTargets, theStatus, null);
056        }
057
058        public RuntimeSearchParam(IIdType theId, String theUri, String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, List<RuntimeSearchParam> theCompositeOf,
059                        Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus, Collection<String> theBase) {
060                super();
061                myId = theId;
062                myUri = theUri;
063                myName = theName;
064                myDescription = theDescription;
065                myPath = thePath;
066                myParamType = theParamType;
067                myCompositeOf = theCompositeOf;
068                myStatus = theStatus;
069                if (theProvidesMembershipInCompartments != null && !theProvidesMembershipInCompartments.isEmpty()) {
070                        myProvidesMembershipInCompartments = Collections.unmodifiableSet(theProvidesMembershipInCompartments);
071                } else {
072                        myProvidesMembershipInCompartments = null;
073                }
074                if (theTargets != null && theTargets.isEmpty() == false) {
075                        myTargets = Collections.unmodifiableSet(theTargets);
076                } else {
077                        myTargets = null;
078                }
079
080                if (theBase == null || theBase.isEmpty()) {
081                        HashSet<String> base = new HashSet<>();
082                        if (isNotBlank(thePath)) {
083                                int indexOf = thePath.indexOf('.');
084                                if (indexOf != -1) {
085                                        base.add(trim(thePath.substring(0, indexOf)));
086                                }
087                        }
088                        myBase = Collections.unmodifiableSet(base);
089                } else {
090                        myBase = Collections.unmodifiableSet(new HashSet<>(theBase));
091                }
092        }
093
094        public Set<String> getBase() {
095                return myBase;
096        }
097
098        public Set<String> getTargets() {
099                return myTargets;
100        }
101
102        public RuntimeSearchParamStatusEnum getStatus() {
103                return myStatus;
104        }
105
106        public RuntimeSearchParam(String theName, String theDescription, String thePath, RestSearchParameterTypeEnum theParamType, Set<String> theProvidesMembershipInCompartments, Set<String> theTargets, RuntimeSearchParamStatusEnum theStatus) {
107                this(null, null, theName, theDescription, thePath, theParamType, null, theProvidesMembershipInCompartments, theTargets, theStatus);
108        }
109
110        public List<RuntimeSearchParam> getCompositeOf() {
111                return myCompositeOf;
112        }
113
114        public String getDescription() {
115                return myDescription;
116        }
117
118        public String getName() {
119                return myName;
120        }
121
122        public RestSearchParameterTypeEnum getParamType() {
123                return myParamType;
124        }
125
126        public String getPath() {
127                return myPath;
128        }
129
130        public List<String> getPathsSplit() {
131                String path = getPath();
132                if (path.indexOf('|') == -1) {
133                        return Collections.singletonList(path);
134                }
135
136                List<String> retVal = new ArrayList<String>();
137                StringTokenizer tok = new StringTokenizer(path, "|");
138                while (tok.hasMoreElements()) {
139                        String nextPath = tok.nextToken().trim();
140                        retVal.add(nextPath.trim());
141                }
142                return retVal;
143        }
144
145        /**
146         * Can return null
147         */
148        public Set<String> getProvidesMembershipInCompartments() {
149                return myProvidesMembershipInCompartments;
150        }
151
152        public enum RuntimeSearchParamStatusEnum {
153                ACTIVE,
154                DRAFT,
155                RETIRED
156        }
157        
158}