View Javadoc
1   package ca.uhn.fhir.rest.gclient;
2   
3   import static org.apache.commons.lang3.StringUtils.isNotBlank;
4   
5   /*
6    * #%L
7    * HAPI FHIR - Core Library
8    * %%
9    * Copyright (C) 2014 - 2018 University Health Network
10   * %%
11   * Licensed under the Apache License, Version 2.0 (the "License");
12   * you may not use this file except in compliance with the License.
13   * You may obtain a copy of the License at
14   * 
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   * 
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the License for the specific language governing permissions and
21   * limitations under the License.
22   * #L%
23   */
24  
25  import java.util.Date;
26  
27  import ca.uhn.fhir.context.FhirContext;
28  import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
29  import ca.uhn.fhir.model.primitive.DateTimeDt;
30  import ca.uhn.fhir.rest.param.ParamPrefixEnum;
31  
32  /**
33   * Date parameter type for use in fluent client interfaces
34   */
35  public class DateClientParam  extends BaseClientParam implements IParam {
36  
37  	private String myParamName;
38  
39  	@Override
40  	public String getParamName() {
41  		return myParamName;
42  	}
43  
44  	public DateClientParam(String theParamName) {
45  		myParamName = theParamName;
46  	}
47  
48  	public IDateSpecifier after() {
49  		return new DateWithPrefix(ParamPrefixEnum.GREATERTHAN);
50  	}
51  
52  	public IDateSpecifier afterOrEquals() {
53  		return new DateWithPrefix(ParamPrefixEnum.GREATERTHAN_OR_EQUALS);
54  	}
55  
56  	public IDateSpecifier before() {
57  		return new DateWithPrefix(ParamPrefixEnum.LESSTHAN);
58  	}
59  
60  	public IDateSpecifier beforeOrEquals() {
61  		return new DateWithPrefix(ParamPrefixEnum.LESSTHAN_OR_EQUALS);
62  	}
63  
64  	public IDateSpecifier exactly() {
65  		return new DateWithPrefix(ParamPrefixEnum.EQUAL);
66  	}
67  
68  	private class Criterion implements IDateCriterion, ICriterionInternal {
69  
70  		private String myValue;
71  		private ParamPrefixEnum myPrefix;
72  		private Criterion orCriterion;
73  
74  		public Criterion(ParamPrefixEnum thePrefix, String theValue) {
75  			myPrefix = thePrefix;
76  			myValue = theValue;
77  		}
78  
79  		@Override
80  		public String getParameterName() {
81  			return myParamName;
82  		}
83  
84  		@Override
85  		public String getParameterValue(FhirContext theContext) {
86  			StringBuilder b = new StringBuilder();
87  			if (orCriterion != null) {
88  				String orValue = orCriterion.getParameterValue(theContext);
89  				if (isNotBlank(orValue)) {
90  					b.append(orValue);
91  				}
92  			}
93  			if (isNotBlank(myValue)) {
94  				if (b.length() > 0) {
95  					b.append(',');
96  				}
97  				if (myPrefix != null && myPrefix != ParamPrefixEnum.EQUAL) {
98  					b.append(myPrefix.getValue());
99  				}
100 				b.append(myValue);
101 			}
102 			return b.toString();
103 		}
104 
105 		@Override
106 		public IDateSpecifier orAfter() {
107 			return new DateWithPrefix(ParamPrefixEnum.GREATERTHAN, this);
108 		}
109 
110 		@Override
111 		public IDateSpecifier orAfterOrEquals() {
112 			return new DateWithPrefix(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, this);
113 		}
114 
115 		@Override
116 		public IDateSpecifier orBefore() {
117 			return new DateWithPrefix(ParamPrefixEnum.LESSTHAN, this);
118 		}
119 
120 		@Override
121 		public IDateSpecifier orBeforeOrEquals() {
122 			return new DateWithPrefix(ParamPrefixEnum.LESSTHAN_OR_EQUALS, this);
123 		}
124 
125 		@Override
126 		public IDateSpecifier orExactly() {
127 			return new DateWithPrefix(ParamPrefixEnum.EQUAL, this);
128 		}
129 
130 	}
131 
132 	private class DateWithPrefix implements IDateSpecifier {
133 		private ParamPrefixEnum myPrefix;
134 		private Criterion previous = null;
135 
136 		public DateWithPrefix(ParamPrefixEnum thePrefix, Criterion previous) {
137 			myPrefix = thePrefix;
138 			this.previous = previous;
139 		}
140 		
141 		public DateWithPrefix(ParamPrefixEnum thePrefix) {
142 			myPrefix = thePrefix;
143 		}
144 
145 		@Override
146 		public IDateCriterion day(Date theValue) {
147 			DateTimeDt dt = new DateTimeDt(theValue);
148 			dt.setPrecision(TemporalPrecisionEnum.DAY);
149 			return constructCriterion(dt);
150 		}
151 
152 		@Override
153 		public IDateCriterion day(String theValue) {
154 			DateTimeDt dt = new DateTimeDt(theValue);
155 			dt.setPrecision(TemporalPrecisionEnum.DAY);
156 			return constructCriterion(dt);
157 		}
158 
159 		@Override
160 		public IDateCriterion now() {
161 			DateTimeDt dt = DateTimeDt.withCurrentTime();
162 			dt.setPrecision(TemporalPrecisionEnum.SECOND);
163 			return constructCriterion(dt);
164 		}
165 
166 		@Override
167 		public IDateCriterion second(Date theValue) {
168 			DateTimeDt dt = new DateTimeDt(theValue);
169 			dt.setPrecision(TemporalPrecisionEnum.SECOND);
170 			return constructCriterion(dt);
171 		}
172 
173 		@Override
174 		public IDateCriterion second(String theValue) {
175 			DateTimeDt dt = new DateTimeDt(theValue);
176 			dt.setPrecision(TemporalPrecisionEnum.SECOND);
177 			return constructCriterion(dt);
178 		}
179 		
180 		private IDateCriterion constructCriterion(DateTimeDt dt) {
181 			String valueAsString = dt.getValueAsString();
182 			Criterion criterion = new Criterion(myPrefix, valueAsString);
183 			if (previous != null) {
184 				criterion.orCriterion = previous;
185 			}	
186 			return criterion;
187 		}
188 	}
189 
190 	public interface IDateSpecifier {
191 
192 		IDateCriterion day(Date theValue);
193 
194 		IDateCriterion day(String theValue);
195 
196 		IDateCriterion now();
197 
198 		IDateCriterion second(Date theValue);
199 
200 		IDateCriterion second(String theValue);
201 
202 	}
203 	
204 	public interface IDateCriterion extends ICriterion<DateClientParam> {
205 		IDateSpecifier orAfter();
206 
207 		IDateSpecifier orAfterOrEquals();
208 
209 		IDateSpecifier orBefore();
210 
211 		IDateSpecifier orBeforeOrEquals();
212 
213 		IDateSpecifier orExactly();
214 	}
215 
216 }