View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jpa;
5   
6   import java.sql.Time;
7   import java.sql.Timestamp;
8   import java.util.Calendar;
9   import java.util.Date;
10  import java.util.HashMap;
11  import java.util.List;
12  import java.util.Map;
13  
14  import javax.persistence.FlushModeType;
15  import javax.persistence.Query;
16  import javax.persistence.TemporalType;
17  
18  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
19  import org.apache.commons.lang.builder.ToStringStyle;
20  
21  import com.github.smokestack.exception.NeedsMockDefinitionException;
22  import com.github.smokestack.exception.NotYetImplementedException;
23  
24  /**
25   * Common query functionality.
26   * 
27   * @author gliptak
28   */
29  public abstract class MockBaseQuery implements Query {
30  
31  	protected int startPosition;
32  	protected FlushModeType flushMode;
33  	protected Map<String, Object> hints=new HashMap<String, Object>();
34  	protected int maxResult;
35  
36  	/**
37  	 * 
38  	 */
39  	protected MockBaseQuery() {
40  	}
41  
42  	/* (non-Javadoc)
43  	 * @see javax.persistence.Query#executeUpdate()
44  	 */
45  	public int executeUpdate() {
46  		return _executeUpdate();
47  	}
48  
49  	public int _executeUpdate() {
50  		throw new NeedsMockDefinitionException();
51  	}
52  
53  	/* (non-Javadoc)
54  	 * @see javax.persistence.Query#getResultList()
55  	 */
56  	public List getResultList() {
57  		return _getResultList();
58  	}
59  
60  	public List _getResultList() {
61  		throw new NeedsMockDefinitionException();
62  	}
63  
64  	/* (non-Javadoc)
65  	 * @see javax.persistence.Query#getSingleResult()
66  	 */
67  	public Object getSingleResult() {
68  		return _getSingleResult();
69  	}
70  
71  	public Object _getSingleResult() {
72  		throw new NeedsMockDefinitionException();
73  	}
74  
75  	/* (non-Javadoc)
76  	 * @see javax.persistence.Query#setFirstResult(int)
77  	 */
78  	public Query setFirstResult(int startPosition) {
79  		_setFirstResult(startPosition);
80  		this.startPosition=startPosition;
81  		return this;
82  	}
83  
84  	public Query _setFirstResult(int startPosition2) {
85  		return null;
86  	}
87  
88  	/* (non-Javadoc)
89  	 * @see javax.persistence.Query#setFlushMode(javax.persistence.FlushModeType)
90  	 */
91  	public Query setFlushMode(FlushModeType flushMode) {
92  		_setFlushMode(flushMode);
93  		this.flushMode=flushMode;
94  		return this;
95  	}
96  
97  	public Query _setFlushMode(FlushModeType flushMode) {
98  		return null;
99  	}
100 
101 	/* (non-Javadoc)
102 	 * @see javax.persistence.Query#setHint(java.lang.String, java.lang.Object)
103 	 */
104 	public Query setHint(String hintName, Object value) {
105 		_setHint(hintName, value);
106 		hints.put(hintName, value);
107 		return this;
108 	}
109 
110 	public Query _setHint(String hintName, Object value) {
111 		return null;
112 	}
113 
114 	/* (non-Javadoc)
115 	 * @see javax.persistence.Query#setMaxResults(int)
116 	 */
117 	public Query setMaxResults(int maxResult) {
118 		_setMaxResults(maxResult);
119 		this.maxResult=maxResult;
120 		return this;
121 	}
122 
123 	public Query _setMaxResults(int maxResult) {
124 		return null;
125 	}
126 
127 	/* (non-Javadoc)
128 	 * @see javax.persistence.Query#setParameter(java.lang.String, java.lang.Object)
129 	 */
130 	public Query setParameter(String name, Object value) {
131 		_setParameter(name, value);
132 		throw new NotYetImplementedException();
133 	}
134 
135 	public Query _setParameter(String name, Object value) {
136 		return null;
137 	}
138 
139 	/* (non-Javadoc)
140 	 * @see javax.persistence.Query#setParameter(int, java.lang.Object)
141 	 */
142 	public Query setParameter(int position, Object value) {
143 		_setParameter(position, value);
144 		return this;
145 	}
146 
147 	public Query _setParameter(int position, Object value) {
148 		return null;
149 	}
150 
151 	/* (non-Javadoc)
152 	 * @see javax.persistence.Query#setParameter(java.lang.String, java.util.Date, javax.persistence.TemporalType)
153 	 */
154 	public Query setParameter(String name, Date value, TemporalType temporalType) {
155 		_setParameter(name, value, temporalType);
156 		return setParameter(name, getTemporalType(value, temporalType));
157 	}
158 
159 	public Query _setParameter(String name, Date value,
160 			TemporalType temporalType) {
161 		return null;
162 	}
163 
164 	/* (non-Javadoc)
165 	 * @see javax.persistence.Query#setParameter(java.lang.String, java.util.Calendar, javax.persistence.TemporalType)
166 	 */
167 	public Query setParameter(String name, Calendar value, TemporalType temporalType) {
168 		_setParameter(name, value, temporalType);
169 		return setParameter(name, getTemporalType(value, temporalType));
170 	}
171 
172 	public Query _setParameter(String name, Calendar value,
173 			TemporalType temporalType) {
174 		return null;
175 	}
176 
177 	/* (non-Javadoc)
178 	 * @see javax.persistence.Query#setParameter(int, java.util.Date, javax.persistence.TemporalType)
179 	 */
180 	public Query setParameter(int position, Date value, TemporalType temporalType) {
181 		throw new NotYetImplementedException();
182 	}
183 
184 	/* (non-Javadoc)
185 	 * @see javax.persistence.Query#setParameter(int, java.util.Calendar, javax.persistence.TemporalType)
186 	 */
187 	public Query setParameter(int position, Calendar value, TemporalType temporalType) {
188 		throw new NotYetImplementedException();
189 	}
190 	
191 	/**
192 	 * Convert TemporalType
193 	 * @param value
194 	 * @param type
195 	 * @return
196 	 */
197     protected Object getTemporalType(Date value, TemporalType type) {
198         switch (type) {
199         case DATE:
200             return value;
201         case TIME:
202             return new Time(value.getTime());
203         case TIMESTAMP:
204             return new Timestamp(value.getTime());
205         default:
206             return null;
207         }
208     }
209 
210     /**
211      * Convert TemporalType
212      * @param value
213      * @param type
214      * @return
215      */
216     protected Object getTemporalType(Calendar value, TemporalType type) {
217     	_getTemporalType(value, type);
218         return getTemporalType(value.getTime(), type);
219     }
220 
221     public Object _getTemporalType(Calendar value, TemporalType type) {
222     	return null;
223 	}
224 
225 	/* (non-Javadoc)
226 	 * @see java.lang.Object#toString()
227 	 */
228 	public String toString(){
229 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
230 	}
231 }