View Javadoc
1   package ca.uhn.fhir.rest.gclient;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.Date;
24  
25  import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
26  import ca.uhn.fhir.model.primitive.DateTimeDt;
27  
28  /**
29   * Date parameter type for use in fluent client interfaces
30   */
31  public class DateClientParam implements IParam {
32  
33  	private String myParamName;
34  
35  	@Override
36  	public String getParamName() {
37  		return myParamName;
38  	}
39  
40  	public DateClientParam(String theParamName) {
41  		myParamName = theParamName;
42  	}
43  
44  	public IDateSpecifier after() {
45  		return new DateWithPrefix(">");
46  	}
47  
48  	public IDateSpecifier afterOrEquals() {
49  		return new DateWithPrefix(">=");
50  	}
51  
52  	public IDateSpecifier before() {
53  		return new DateWithPrefix("<=");
54  	}
55  
56  	public IDateSpecifier beforeOrEquals() {
57  		return new DateWithPrefix("<=");
58  	}
59  
60  	public IDateSpecifier exactly() {
61  		return new DateWithPrefix("");
62  	}
63  
64  	private class Criterion implements ICriterion<DateClientParam>, ICriterionInternal {
65  
66  		private String myValue;
67  
68  		public Criterion(String theValue) {
69  			myValue = theValue;
70  		}
71  
72  		@Override
73  		public String getParameterName() {
74  			return myParamName;
75  		}
76  
77  		@Override
78  		public String getParameterValue() {
79  			return myValue;
80  		}
81  
82  	}
83  
84  	private class DateWithPrefix implements IDateSpecifier {
85  		private String myPrefix;
86  
87  		public DateWithPrefix(String thePrefix) {
88  			myPrefix = thePrefix;
89  		}
90  
91  		@Override
92  		public ICriterion<DateClientParam> day(Date theValue) {
93  			DateTimeDt dt = new DateTimeDt(theValue);
94  			dt.setPrecision(TemporalPrecisionEnum.DAY);
95  			return new Criterion(myPrefix + dt.getValueAsString());
96  		}
97  
98  		@Override
99  		public ICriterion<DateClientParam> day(String theValue) {
100 			DateTimeDt dt = new DateTimeDt(theValue);
101 			dt.setPrecision(TemporalPrecisionEnum.DAY);
102 			return new Criterion(myPrefix + dt.getValueAsString());
103 		}
104 
105 		@Override
106 		public ICriterion<DateClientParam> now() {
107 			DateTimeDt dt = new DateTimeDt();
108 			dt.setPrecision(TemporalPrecisionEnum.DAY);
109 			return new Criterion(myPrefix + dt.getValueAsString());
110 		}
111 
112 		@Override
113 		public ICriterion<DateClientParam> second(Date theValue) {
114 			DateTimeDt dt = new DateTimeDt(theValue);
115 			dt.setPrecision(TemporalPrecisionEnum.DAY);
116 			return new Criterion(myPrefix + dt.getValueAsString());
117 		}
118 
119 		@Override
120 		public ICriterion<DateClientParam> second(String theValue) {
121 			DateTimeDt dt = new DateTimeDt(theValue);
122 			dt.setPrecision(TemporalPrecisionEnum.DAY);
123 			return new Criterion(myPrefix + dt.getValueAsString());
124 		}
125 
126 	}
127 
128 	public interface IDateSpecifier {
129 
130 		ICriterion<DateClientParam> day(Date theValue);
131 
132 		ICriterion<DateClientParam> day(String theValue);
133 
134 		ICriterion<DateClientParam> now();
135 
136 		ICriterion<DateClientParam> second(Date theValue);
137 
138 		ICriterion<DateClientParam> second(String theValue);
139 
140 	}
141 
142 }