001package ca.uhn.fhir.rest.gclient; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2016 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 */ 022 023import java.util.Date; 024 025import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 026import ca.uhn.fhir.model.primitive.DateTimeDt; 027 028/** 029 * Date parameter type for use in fluent client interfaces 030 */ 031public class DateClientParam extends BaseClientParam implements IParam { 032 033 private String myParamName; 034 035 @Override 036 public String getParamName() { 037 return myParamName; 038 } 039 040 public DateClientParam(String theParamName) { 041 myParamName = theParamName; 042 } 043 044 public IDateSpecifier after() { 045 return new DateWithPrefix(">"); 046 } 047 048 public IDateSpecifier afterOrEquals() { 049 return new DateWithPrefix(">="); 050 } 051 052 public IDateSpecifier before() { 053 return new DateWithPrefix("<"); 054 } 055 056 public IDateSpecifier beforeOrEquals() { 057 return new DateWithPrefix("<="); 058 } 059 060 public IDateSpecifier exactly() { 061 return new DateWithPrefix(""); 062 } 063 064 private class Criterion implements ICriterion<DateClientParam>, ICriterionInternal { 065 066 private String myValue; 067 068 public Criterion(String theValue) { 069 myValue = theValue; 070 } 071 072 @Override 073 public String getParameterName() { 074 return myParamName; 075 } 076 077 @Override 078 public String getParameterValue() { 079 return myValue; 080 } 081 082 } 083 084 private class DateWithPrefix implements IDateSpecifier { 085 private String myPrefix; 086 087 public DateWithPrefix(String thePrefix) { 088 myPrefix = thePrefix; 089 } 090 091 @Override 092 public ICriterion<DateClientParam> day(Date theValue) { 093 DateTimeDt dt = new DateTimeDt(theValue); 094 dt.setPrecision(TemporalPrecisionEnum.DAY); 095 return new Criterion(myPrefix + dt.getValueAsString()); 096 } 097 098 @Override 099 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.SECOND); 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.SECOND); 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}