View Javadoc
1   /***
2    * License Agreement.
3    * 
4    * JSPA (POJO-SP)
5    * 
6    * Copyright (C) 2009 HRX Pty Ltd
7    * 
8    * This file is part of JSPA.
9    * 
10   * JSPA is free software: you can redistribute it and/or modify it under the
11   * terms of the GNU Lesser General Public License version 3 as published by the
12   * Free Software Foundation.
13   * 
14   * JSPA is distributed in the hope that it will be useful, but WITHOUT ANY
15   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16   * A PARTICULAR PURPOSE. See the Lesser General Public License for more details.
17   * 
18   * You should have received a copy of the GNU Lesser General Public License
19   * along with JSPA. If not, see <http://www.gnu.org/licenses/>.
20   */
21  package com.hrx.rasp.util.dao.metadata;
22  
23  import java.lang.reflect.Field;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import com.hrx.rasp.util.dao.ResultSetProcessor;
28  import com.hrx.rasp.util.dao.annotations.IN;
29  import com.hrx.rasp.util.dao.annotations.INOUT;
30  import com.hrx.rasp.util.dao.annotations.OUT;
31  import com.hrx.rasp.util.dao.annotations.Operation;
32  import com.hrx.rasp.util.dao.operation.OperationType;
33  
34  /***
35   * @author dan.stoica <dan.stoica@acslink.net.au>
36   * 
37   */
38  public class StoredProcMetaDataImpl implements StoredProcMetaData
39  {
40  	private long startTime = 0;
41  	private OperationType operation;
42  	private String procName;
43  
44  	private Class<?> functionReturnClass;
45  	private Class<ResultSetProcessor<Object>> resultSetProcessorClass;
46  
47  	private StoredProcPkMetaData embeddedIdMetaData;
48  	private Field embeddedIdField;
49  
50  	private int errorCodeIndex;
51  	private int errorMessageIndex;
52  
53  	/***
54  	 * The maximum number of results to retrieve from result set.
55  	 * 
56  	 */
57  	private int maxResult = 0;
58  
59  	/***
60  	 * The start position of the first result, numbered from 0 for ResutlSet
61  	 * parameters
62  	 */
63  	private int startPosition = 0;
64  
65  	private boolean defaultErrorIndex;
66  
67  	private boolean embeddedId = false;
68  	/***
69  	 * <parameter index, Oracle SQL Type>
70  	 */
71  	private final Set<StoredProcField<IN>> paramsIn = new HashSet<StoredProcField<IN>>();
72  	private final Set<StoredProcField<OUT>> paramsOut = new HashSet<StoredProcField<OUT>>();
73  	private final Set<StoredProcField<INOUT>> paramsInOUT = new HashSet<StoredProcField<INOUT>>();
74  	// primary key can be any type (IN, OUT or INOUT)
75  	private final Set<StoredProcField<?>> primaryKeyParameters = new HashSet<StoredProcField<?>>();
76  	private int argCount;
77  
78  	public void clear()
79  	{
80  		this.argCount = 0;
81  		this.procName = null;
82  		this.embeddedIdField = null;
83  		this.embeddedIdMetaData = null;
84  		this.functionReturnClass = null;
85  		this.resultSetProcessorClass = null;
86  		paramsIn.clear();
87  		paramsOut.clear();
88  		paramsInOUT.clear();
89  		embeddedId = false;
90  		primaryKeyParameters.clear();
91  	}
92  
93  	public void putInParameter(StoredProcField<IN> spField)
94  	{
95  		paramsIn.add(spField);
96  		putPkParameter(spField);
97  	}
98  
99  	public void putOutParameter(StoredProcField<OUT> spField)
100 	{
101 		paramsOut.add(spField);
102 		putPkParameter(spField);
103 	}
104 
105 	public void putInOutParameter(StoredProcField<INOUT> spField)
106 	{
107 		paramsInOUT.add(spField);
108 		putPkParameter(spField);
109 	}
110 
111 	public void putPkParameter(StoredProcField<?> spField)
112 	{
113 		if (spField.isPrimaryKey() && !isEmbeddedId())
114 		{
115 			this.primaryKeyParameters.add(spField);
116 		}
117 	}
118 
119 	/***
120 	 * @return the paramsIn
121 	 */
122 	public Set<StoredProcField<IN>> getInParameters()
123 	{
124 		return paramsIn;
125 	}
126 
127 	/***
128 	 * @return the paramsOut
129 	 */
130 	public Set<StoredProcField<OUT>> getOutParameters()
131 	{
132 		return paramsOut;
133 	}
134 
135 	/***
136 	 * @return
137 	 */
138 	public Set<StoredProcField<INOUT>> getInOutParameters()
139 	{
140 		return paramsInOUT;
141 	}
142 
143 	public Set<StoredProcField<?>> getPrimaryKeyParameters()
144 	{
145 		return primaryKeyParameters;
146 	}
147 
148 	/***
149 	 * @return the procName
150 	 */
151 	public String getStoredProcName()
152 	{
153 		return procName;
154 	}
155 
156 	/***
157 	 * @param procName
158 	 *            the procName to set
159 	 */
160 	public void setStoredProcName(String procName)
161 	{
162 		this.procName = procName;
163 	}
164 
165 	public int getArgCount()
166 	{
167 		if (argCount == 0)
168 		{
169 			argCount = this.paramsIn.size() + paramsOut.size() + paramsInOUT.size() + primaryKeyParameters.size();
170 			if (embeddedIdMetaData != null)
171 			{
172 				argCount = argCount + this.embeddedIdMetaData.getArgCount();
173 			}
174 		}
175 		return argCount;
176 	}
177 
178 	public void setEmbeddedId(Field embeddedIdField, StoredProcPkMetaData embeddedIdMetaData)
179 	{
180 		this.embeddedId = true;
181 		this.embeddedIdField = embeddedIdField;
182 		this.embeddedIdMetaData = embeddedIdMetaData;
183 		this.primaryKeyParameters.clear();
184 	}
185 
186 	public Field getEmbeddedIdField()
187 	{
188 		return embeddedIdField;
189 	}
190 
191 	public StoredProcPkMetaData getEmbeddedIdMetaData()
192 	{
193 		return embeddedIdMetaData;
194 	}
195 
196 	public OperationType getOperationType()
197 	{
198 		return operation;
199 	}
200 
201 	public void prepare(Operation operation, int errorCodeIndex, int errorMessageIndex)
202 	{
203 		clear();
204 		setOperationType(operation.type());
205 		setStoredProcName(operation.name());
206 		setErrorCodeIndex(errorCodeIndex);
207 		setErrorMessageIndex(errorMessageIndex);
208 	}
209 
210 	public void setOperationType(OperationType operation)
211 	{
212 		this.operation = operation;
213 	}
214 
215 	/***
216 	 * @return the resultSetProcessorClass
217 	 */
218 	public Class<ResultSetProcessor<Object>> getResultSetProcessorClass()
219 	{
220 		return (Class<ResultSetProcessor<Object>>) resultSetProcessorClass;
221 	}
222 
223 	/***
224 	 * @param resultSetProcessorClass
225 	 *            the resultSetProcessorClass to set
226 	 */
227 	public void setResultSetProcessorClass(Class<ResultSetProcessor<Object>> resultSetProcessorClass)
228 	{
229 		this.resultSetProcessorClass = resultSetProcessorClass;
230 	}
231 
232 	/***
233 	 * @return the functionReturnClass
234 	 */
235 	public Class<?> getFunctionReturnClass()
236 	{
237 		return functionReturnClass;
238 	}
239 
240 	/***
241 	 * @param functionReturnClass
242 	 *            the functionReturnClass to set
243 	 */
244 	public void setFunctionReturnClass(Class<?> functionReturnClass)
245 	{
246 		this.functionReturnClass = functionReturnClass;
247 	}
248 
249 	/***
250 	 * @return the embeddedId
251 	 */
252 	public boolean isEmbeddedId()
253 	{
254 		return embeddedId;
255 	}
256 
257 	/***
258 	 * @return the errorCodeIndex
259 	 */
260 	public int getErrorCodeIndex()
261 	{
262 		return errorCodeIndex;
263 	}
264 
265 	/***
266 	 * @param errorCodeIndex
267 	 *            the errorCodeIndex to set
268 	 */
269 	public void setErrorCodeIndex(int errorCodeIndex)
270 	{
271 		this.errorCodeIndex = errorCodeIndex;
272 	}
273 
274 	/***
275 	 * @return the errorMessageIndex
276 	 */
277 	public int getErrorMessageIndex()
278 	{
279 		return errorMessageIndex;
280 	}
281 
282 	/***
283 	 * @param errorMessageIndex
284 	 *            the errorMessageIndex to set
285 	 */
286 	public void setErrorMessageIndex(int errorMessageIndex)
287 	{
288 		this.errorMessageIndex = errorMessageIndex;
289 	}
290 
291 	/***
292 	 * @return the defaultErrorIndex
293 	 */
294 	public boolean isDefaultErrorIndex()
295 	{
296 		return defaultErrorIndex;
297 	}
298 
299 	/***
300 	 * @param defaultErrorIndex
301 	 *            the defaultErrorIndex to set
302 	 */
303 	public void setDefaultErrorIndex(boolean defaultErrorIndex)
304 	{
305 		this.defaultErrorIndex = defaultErrorIndex;
306 	}
307 
308 	/***
309 	 * @return the maxResult
310 	 */
311 	public int getMaxResult()
312 	{
313 		return maxResult;
314 	}
315 
316 	/***
317 	 * @param maxResult
318 	 *            the maxResult to set
319 	 */
320 	public void setMaxResult(int maxResult)
321 	{
322 		this.maxResult = maxResult;
323 	}
324 
325 	/***
326 	 * @return the startPosition
327 	 */
328 	public int getStartPosition()
329 	{
330 		return startPosition;
331 	}
332 
333 	/***
334 	 * @param startPosition
335 	 *            the startPosition to set
336 	 */
337 	public void setStartPosition(int startPosition)
338 	{
339 		this.startPosition = startPosition;
340 	}
341 
342 	/***
343 	 * @return the startTime
344 	 */
345 	public long getStartTime()
346 	{
347 		return startTime;
348 	}
349 
350 	/***
351 	 * @param startTime
352 	 *            the startTime to set
353 	 */
354 	public void startTimer()
355 	{
356 		this.startTime = System.currentTimeMillis();
357 	}
358 
359 }