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