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.operation;
22  
23  import java.sql.CallableStatement;
24  import java.sql.ResultSet;
25  import java.sql.SQLException;
26  import java.util.List;
27  import java.util.Set;
28  
29  import javax.sql.DataSource;
30  
31  //import oracle.jdbc.OracleCallableStatement;
32  
33  import com.hrx.rasp.util.dao.ResultSetProcessor;
34  import com.hrx.rasp.util.dao.annotations.IN;
35  import com.hrx.rasp.util.dao.annotations.Secret;
36  import com.hrx.rasp.util.dao.exception.StoredProcedureException;
37  import com.hrx.rasp.util.dao.exception.StoredProcedurePrepareException;
38  import com.hrx.rasp.util.dao.exception.StoredProcedureProccessResultException;
39  import com.hrx.rasp.util.dao.exception.StoredProcedureReservedIndexException;
40  import com.hrx.rasp.util.dao.metadata.StoredProcField;
41  
42  /***
43   * 
44   * Search for a collection of records given the search criteria as input
45   * parameters and optional will sort the results.
46   * 
47   * The Select operation is not requiring a primary key. However if the DAO has
48   * an embedded primary key, the fields from PK can be used for a select
49   * operation criteria.
50   * 
51   * @author dan.stoica <dan.stoica@acslink.net.au>
52   */
53  public class Select extends AbstractDAOCommand
54  {
55  	public Select(DataSource ds)
56  	{
57  		super(ds);
58  	}
59  
60  	/***
61  	 * It transforms the ResultSet returned by the Oracle Function in a
62  	 * collection of records. The search criteria is using the DAO IN and
63  	 * Primary Key declared parameters
64  	 * 
65  	 * @throws StoredProcedureProccessResultException
66  	 */
67  	@SuppressWarnings("unchecked")
68  	protected Object run() throws StoredProcedureException, SQLException, StoredProcedurePrepareException, StoredProcedureProccessResultException
69  	{
70  		checkResultCode(2, 3);
71  		// create an ArrayList to contains the DAO objects
72  		List<Object> list;
73  
74  		try
75  		{
76  			// By DAO convention the result set must always be the return object
77  			// of the Oracle Function called by a Select operation
78  			ResultSet rs = (ResultSet) (getCallableStatement()).getObject(1);
79  
80  			Class<Object> returnType = (Class<Object>) getMetaData().getFunctionReturnClass();
81  			Class<ResultSetProcessor<Object>> resultSetProcessor = (Class<ResultSetProcessor<Object>>) getMetaData().getResultSetProcessorClass();
82  
83  			boolean isSecretValue = returnType.isAnnotationPresent(Secret.class);
84  
85  			/***
86  			 * Transform the ResultSet into a List of DAO objects parsing the
87  			 * result set and instantiating a new DAO for each database record
88  			 */
89  			list = ResponseProcessor.processResultSetParameter(rs, resultSetProcessor, returnType, getMetaData().getStartPosition(), getMetaData()
90  				.getMaxResult(), isSecretValue);
91  
92  		}
93  		catch (Exception e)
94  		{
95  			throw new StoredProcedureProccessResultException("Error while processing stored procedure: " + e.getMessage(), e);
96  		}
97  
98  		return list;
99  	}
100 
101 	/*
102 	 * Using PK and any number of IN parameters for query. The result will be a
103 	 * JDBC ResultSet. The requests calls a database function
104 	 * 
105 	 * (non-Javadoc)
106 	 * 
107 	 * @see com.hrx.rasp.util.dao.operation.AbstractDAOCommand#createStatement()
108 	 */
109 	@Override
110 	protected CallableStatement createStatement() throws StoredProcedurePrepareException, StoredProcedureReservedIndexException, SQLException
111 	{
112 		Set<StoredProcField<IN>> pkInParameters = null;
113 		if (isEmbeddedId())
114 		{
115 			pkInParameters = getEmbeddedIdMetaData().getInParameters();
116 		}
117 		CallableStatement st = StatementBuilder.getSelectStatement(getConnection(), getStoredProcName(), getInParameters(), pkInParameters,
118 			getSpBean(), getPkBean(), isEmbeddedId());
119 		return st;
120 	}
121 }