001package ca.uhn.fhir.rest.gclient;
002
003import java.util.Collection;
004
005import ca.uhn.fhir.context.FhirContext;
006import ca.uhn.fhir.model.primitive.IdDt;
007
008/*
009 * #%L
010 * HAPI FHIR - Core Library
011 * %%
012 * Copyright (C) 2014 - 2017 University Health Network
013 * %%
014 * Licensed under the Apache License, Version 2.0 (the "License");
015 * you may not use this file except in compliance with the License.
016 * You may obtain a copy of the License at
017 * 
018 *      http://www.apache.org/licenses/LICENSE-2.0
019 * 
020 * Unless required by applicable law or agreed to in writing, software
021 * distributed under the License is distributed on an "AS IS" BASIS,
022 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
023 * See the License for the specific language governing permissions and
024 * limitations under the License.
025 * #L%
026 */
027
028
029public class ReferenceClientParam extends BaseClientParam  implements IParam {
030
031        private String myName;
032
033        public ReferenceClientParam(String theName) {
034                myName = theName;
035        }
036
037        @Override
038        public String getParamName() {
039                return myName;
040        }
041        
042        public ICriterion<ReferenceClientParam> hasChainedProperty(ICriterion<?> theCriterion) {
043                return new ReferenceChainCriterion(getParamName(), theCriterion);
044        }
045
046        /**
047         * Match the referenced resource if the resource has the given ID (this can be
048         * the logical ID or the absolute URL of the resource)
049         */
050        public ICriterion<ReferenceClientParam> hasId(IdDt theId) {
051                return new StringCriterion<ReferenceClientParam>(getParamName(), theId.getValue());
052        }
053
054        /**
055         * Match the referenced resource if the resource has the given ID (this can be
056         * the logical ID or the absolute URL of the resource)
057         */
058        public ICriterion<ReferenceClientParam> hasId(String theId) {
059                return new StringCriterion<ReferenceClientParam>(getParamName(), theId);
060        }
061
062        /**
063         * Match the referenced resource if the resource has ANY of the given IDs
064         * (this is an OR search, not an AND search), (this can be the logical ID or
065         * the absolute URL of the resource). Note that to specify an AND search,
066         * simply add a subsequent {@link IQuery#where(ICriterion) where} criteria
067         * with the same parameter.
068         */
069        public ICriterion<ReferenceClientParam> hasAnyOfIds(Collection<String> theIds) {
070                return new StringCriterion<ReferenceClientParam>(getParamName(), theIds);
071        }
072        
073        private static class ReferenceChainCriterion implements ICriterion<ReferenceClientParam>, ICriterionInternal {
074
075                private String myParamName;
076                private ICriterionInternal myWrappedCriterion;
077
078                public ReferenceChainCriterion(String theParamName, ICriterion<?> theWrappedCriterion) {
079                        myParamName = theParamName;
080                        myWrappedCriterion = (ICriterionInternal) theWrappedCriterion;
081                }
082
083                @Override
084                public String getParameterName() {
085                        return myParamName + "." + myWrappedCriterion.getParameterName();
086                }
087
088                @Override
089                public String getParameterValue(FhirContext theContext) {
090                        return myWrappedCriterion.getParameterValue(theContext);
091                }
092
093        }
094
095}