001package ca.uhn.fhir.rest.param; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2017 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.defaultString; 023 024import org.apache.commons.lang3.StringUtils; 025import org.apache.commons.lang3.builder.EqualsBuilder; 026import org.apache.commons.lang3.builder.ToStringBuilder; 027import org.apache.commons.lang3.builder.ToStringStyle; 028 029import ca.uhn.fhir.context.FhirContext; 030import ca.uhn.fhir.model.api.IQueryParameterType; 031import ca.uhn.fhir.model.primitive.StringDt; 032import ca.uhn.fhir.rest.server.Constants; 033 034public class StringParam extends BaseParam implements IQueryParameterType { 035 036 private boolean myContains; 037 private boolean myExact; 038 private String myValue; 039 040 /** 041 * Constructor 042 */ 043 public StringParam() { 044 } 045 046 /** 047 * Constructor 048 */ 049 public StringParam(String theValue) { 050 setValue(theValue); 051 } 052 053 /** 054 * Constructor 055 */ 056 public StringParam(String theValue, boolean theExact) { 057 setValue(theValue); 058 setExact(theExact); 059 } 060 061 @Override 062 String doGetQueryParameterQualifier() { 063 if (isExact()) { 064 return Constants.PARAMQUALIFIER_STRING_EXACT; 065 } else if (isContains()) { 066 return Constants.PARAMQUALIFIER_STRING_CONTAINS; 067 } else { 068 return null; 069 } 070 } 071 072 @Override 073 String doGetValueAsQueryToken(FhirContext theContext) { 074 return ParameterUtil.escape(myValue); 075 } 076 077 @Override 078 void doSetValueAsQueryToken(FhirContext theContext, String theParamName, String theQualifier, String theValue) { 079 if (Constants.PARAMQUALIFIER_STRING_EXACT.equals(theQualifier)) { 080 setExact(true); 081 } else { 082 setExact(false); 083 } 084 if (Constants.PARAMQUALIFIER_STRING_CONTAINS.equals(theQualifier)) { 085 setContains(true); 086 } else { 087 setContains(false); 088 } 089 myValue = ParameterUtil.unescape(theValue); 090 } 091 092 @Override 093 public boolean equals(Object obj) { 094 if (this == obj) { 095 return true; 096 } 097 if (obj == null) { 098 return false; 099 } 100 if (!(obj instanceof StringParam)) { 101 return false; 102 } 103 104 StringParam other = (StringParam) obj; 105 106 EqualsBuilder eb = new EqualsBuilder(); 107 eb.append(myExact, other.myExact); 108 eb.append(myContains, other.myContains); 109 eb.append(myValue, other.myValue); 110 eb.append(getMissing(), other.getMissing()); 111 112 return eb.isEquals(); 113 } 114 115 public String getValue() { 116 return myValue; 117 } 118 119 public StringDt getValueAsStringDt() { 120 return new StringDt(myValue); 121 } 122 123 public String getValueNotNull() { 124 return defaultString(myValue); 125 } 126 127 /** 128 * String parameter modifier <code>:contains</code> 129 */ 130 public boolean isContains() { 131 return myContains; 132 } 133 134 public boolean isEmpty() { 135 return StringUtils.isEmpty(myValue); 136 } 137 138 public boolean isExact() { 139 return myExact; 140 } 141 142 /** 143 * String parameter modifier <code>:contains</code> 144 */ 145 public StringParam setContains(boolean theContains) { 146 myContains = theContains; 147 if (myContains) { 148 setExact(false); 149 setMissing(null); 150 } 151 return this; 152 } 153 154 public StringParam setExact(boolean theExact) { 155 myExact = theExact; 156 if (myExact) { 157 setContains(false); 158 setMissing(null); 159 } 160 return this; 161 } 162 163 public StringParam setValue(String theValue) { 164 myValue = theValue; 165 return this; 166 } 167 168 @Override 169 public String toString() { 170 ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); 171 builder.append("value", getValue()); 172 if (myExact) { 173 builder.append("exact", myExact); 174 } 175 if (myContains) { 176 builder.append("contains", myContains); 177 } 178 if (getMissing() != null) { 179 builder.append("missing", getMissing().booleanValue()); 180 } 181 return builder.toString(); 182 } 183 184}