001package ca.uhn.fhir.rest.param;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022import static org.apache.commons.lang3.StringUtils.isBlank;
023
024import java.util.List;
025
026import org.apache.commons.lang3.Validate;
027
028import ca.uhn.fhir.context.ConfigurationException;
029import ca.uhn.fhir.model.api.IQueryParameterType;
030import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
031
032public class CompositeParam<A extends IQueryParameterType, B extends IQueryParameterType> extends BaseParam implements IQueryParameterType {
033
034        private A myLeftType;
035        private B myRightType;
036
037        public CompositeParam(A theLeftInstance, B theRightInstance) {
038                myLeftType = theLeftInstance;
039                myRightType = theRightInstance;
040        }
041
042        public CompositeParam(Class<A> theLeftType, Class<B> theRightType) {
043                Validate.notNull(theLeftType);
044                Validate.notNull(theRightType);
045                try {
046                        myLeftType = theLeftType.newInstance();
047                } catch (InstantiationException e) {
048                        throw new ConfigurationException("Failed to instantiate type: " + myLeftType, e);
049                } catch (IllegalAccessException e) {
050                        throw new ConfigurationException("Failed to instantiate type: " + myLeftType, e);
051                }
052                try {
053                        myRightType = theRightType.newInstance();
054                } catch (InstantiationException e) {
055                        throw new ConfigurationException("Failed to instantiate type: " + myRightType, e);
056                } catch (IllegalAccessException e) {
057                        throw new ConfigurationException("Failed to instantiate type: " + myRightType, e);
058                }
059        }
060
061        @Override
062        String doGetQueryParameterQualifier() {
063                return null;
064        }
065
066        @Override
067        String doGetValueAsQueryToken() {
068                StringBuilder b = new StringBuilder();
069                if (myLeftType != null) {
070                        b.append(myLeftType.getValueAsQueryToken());
071                }
072                b.append('$');
073                if (myRightType != null) {
074                        b.append(myRightType.getValueAsQueryToken());
075                }
076                return b.toString();
077        }
078
079        @Override
080        void doSetValueAsQueryToken(String theQualifier, String theValue) {
081                if (isBlank(theValue)) {
082                        myLeftType.setValueAsQueryToken(theQualifier, "");
083                        myRightType.setValueAsQueryToken(theQualifier, "");
084                } else {
085                        List<String> parts = ParameterUtil.splitParameterString(theValue, '$', false);
086                        if (parts.size() > 2) {
087                                throw new InvalidRequestException("Invalid value for composite parameter (only one '$' is valid for this parameter, others must be escaped). Value was: " + theValue);
088                        }
089                        myLeftType.setValueAsQueryToken(theQualifier, parts.get(0));
090                        if (parts.size() > 1) {
091                                myRightType.setValueAsQueryToken(theQualifier, parts.get(1));
092                        }
093                }
094        }
095
096        /**
097         * @return Returns the left value for this parameter (the first of two parameters in this composite)
098         */
099        public A getLeftValue() {
100                return myLeftType;
101        }
102
103        /**
104         * @return Returns the right value for this parameter (the second of two parameters in this composite)
105         */
106        public B getRightValue() {
107                return myRightType;
108        }
109
110}