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.server.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 String myOwningFieldName; 036 private String myParameterName; 037 private String myParameterValue; 038 private String myTargetResourceType; 039 040 public HasParam() { 041 super(); 042 } 043 044 045 public HasParam(String theTargetResourceType, String theOwningFieldName, String theParameterName, String theParameterValue) { 046 this(); 047 myTargetResourceType = theTargetResourceType; 048 myOwningFieldName = theOwningFieldName; 049 myParameterName = theParameterName; 050 myParameterValue = theParameterValue; 051 } 052 053 054 @Override 055 String doGetQueryParameterQualifier() { 056 return myTargetResourceType + ':' + myParameterName + ':' + myParameterValue; 057 } 058 059 @Override 060 String doGetValueAsQueryToken(FhirContext theContext) { 061 return myParameterValue; 062 } 063 064 @Override 065 void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { 066 String qualifier = defaultString(theQualifier); 067 if (!qualifier.startsWith(":")) { 068 throwInvalidSyntaxException(Constants.PARAM_HAS + qualifier); 069 } 070 int colonIndex0 = qualifier.indexOf(':', 1); 071 validateColon(qualifier, colonIndex0); 072 int colonIndex1 = qualifier.indexOf(':', colonIndex0 + 1); 073 validateColon(qualifier, colonIndex1); 074 075 myTargetResourceType = qualifier.substring(1, colonIndex0); 076 myOwningFieldName = qualifier.substring(colonIndex0 + 1, colonIndex1); 077 myParameterName = qualifier.substring(colonIndex1 + 1); 078 myParameterValue = theValue; 079 } 080 081 public String getOwningFieldName() { 082 return myOwningFieldName; 083 } 084 085 public String getParameterName() { 086 return myParameterName; 087 } 088 089 public String getParameterValue() { 090 return myParameterValue; 091 } 092 093 public String getTargetResourceType() { 094 return myTargetResourceType; 095 } 096 097 private static void validateColon(String theParameterName, int colonIndex) { 098 if (colonIndex == -1) { 099 throwInvalidSyntaxException(theParameterName); 100 } 101 } 102 103 104 private static void throwInvalidSyntaxException(String theParameterName) { 105 throw new InvalidRequestException("Invalid _has parameter syntax: " + theParameterName); 106 } 107 108}