001package ca.uhn.fhir.rest.param; 002 003import static org.apache.commons.lang3.StringUtils.defaultString; 004 005/* 006 * #%L 007 * HAPI FHIR - Core Library 008 * %% 009 * Copyright (C) 2014 - 2017 University Health Network 010 * %% 011 * Licensed under the Apache License, Version 2.0 (the "License"); 012 * you may not use this file except in compliance with the License. 013 * You may obtain a copy of the License at 014 * 015 * http://www.apache.org/licenses/LICENSE-2.0 016 * 017 * Unless required by applicable law or agreed to in writing, software 018 * distributed under the License is distributed on an "AS IS" BASIS, 019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 020 * See the License for the specific language governing permissions and 021 * limitations under the License. 022 * #L% 023 */ 024 025import ca.uhn.fhir.context.FhirContext; 026import ca.uhn.fhir.model.api.IQueryParameterType; 027import ca.uhn.fhir.rest.api.Constants; 028import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; 029 030/** 031 * Implementation of the _has method parameter 032 */ 033public class HasParam extends BaseParam implements IQueryParameterType { 034 035 private static final long serialVersionUID = 1L; 036 037 private String myOwningFieldName; 038 private String myParameterName; 039 private String myParameterValue; 040 private String myTargetResourceType; 041 042 public HasParam() { 043 super(); 044 } 045 046 047 public HasParam(String theTargetResourceType, String theOwningFieldName, String theParameterName, String theParameterValue) { 048 this(); 049 myTargetResourceType = theTargetResourceType; 050 myOwningFieldName = theOwningFieldName; 051 myParameterName = theParameterName; 052 myParameterValue = theParameterValue; 053 } 054 055 056 @Override 057 String doGetQueryParameterQualifier() { 058 return myTargetResourceType + ':' + myParameterName + ':' + myParameterValue; 059 } 060 061 @Override 062 String doGetValueAsQueryToken(FhirContext theContext) { 063 return myParameterValue; 064 } 065 066 @Override 067 void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { 068 String qualifier = defaultString(theQualifier); 069 if (!qualifier.startsWith(":")) { 070 throwInvalidSyntaxException(Constants.PARAM_HAS + qualifier); 071 } 072 int colonIndex0 = qualifier.indexOf(':', 1); 073 validateColon(qualifier, colonIndex0); 074 int colonIndex1 = qualifier.indexOf(':', colonIndex0 + 1); 075 validateColon(qualifier, colonIndex1); 076 077 myTargetResourceType = qualifier.substring(1, colonIndex0); 078 myOwningFieldName = qualifier.substring(colonIndex0 + 1, colonIndex1); 079 myParameterName = qualifier.substring(colonIndex1 + 1); 080 myParameterValue = theValue; 081 } 082 083 public String getOwningFieldName() { 084 return myOwningFieldName; 085 } 086 087 public String getParameterName() { 088 return myParameterName; 089 } 090 091 public String getParameterValue() { 092 return myParameterValue; 093 } 094 095 public String getTargetResourceType() { 096 return myTargetResourceType; 097 } 098 099 private static void validateColon(String theParameterName, int colonIndex) { 100 if (colonIndex == -1) { 101 throwInvalidSyntaxException(theParameterName); 102 } 103 } 104 105 106 private static void throwInvalidSyntaxException(String theParameterName) { 107 throw new InvalidRequestException("Invalid _has parameter syntax: " + theParameterName); 108 } 109 110}