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.SQLException;
25  import java.util.Set;
26  
27  import javax.sql.DataSource;
28  
29  import com.hrx.rasp.util.dao.exception.StoredProcedureException;
30  import com.hrx.rasp.util.dao.exception.StoredProcedurePrepareException;
31  import com.hrx.rasp.util.dao.exception.StoredProcedureProccessResultException;
32  import com.hrx.rasp.util.dao.exception.StoredProcedureReservedIndexException;
33  import com.hrx.rasp.util.dao.metadata.StoredProcField;
34  
35  /***
36   * 
37   * Create a record in the database
38   * 
39   * @author dan.stoica <dan.stoica@acslink.net.au>
40   * 
41   */
42  public class Create extends AbstractDAOCommand
43  {
44  	public Create(DataSource ds)
45  	{
46  		super(ds);
47  	}
48  
49  	/***
50  	 * Execute an insert query.
51  	 * 
52  	 * @return The bean containing the primary key generated by the database
53  	 * @throws StoredProcedureException
54  	 *             if database error occurred
55  	 * @throws SQLException
56  	 *             if stored procedure failed
57  	 * @throws StoredProcedurePrepareException
58  	 * @throws StoredProcedureProccessResultException
59  	 */
60  	protected Object run() throws StoredProcedureException, SQLException, StoredProcedurePrepareException, StoredProcedureProccessResultException
61  	{
62  		// process the results
63  		checkResultCode(1, 2);
64  		ResponseProcessor.processCallableStatement(getSpBean(), getPkBean(), getCallableStatement(), getMetaData());
65  
66  		Object pk = getSpBean();
67  		return pk;
68  	}
69  
70  	@SuppressWarnings("unchecked")
71  	@Override
72  	protected CallableStatement createStatement() throws StoredProcedurePrepareException, StoredProcedureReservedIndexException, SQLException
73  	{
74  		Object pkParameters = getPrimaryKeyParameters();
75  		CallableStatement st = StatementBuilder.getInsertStatement(getConnection(), getStoredProcName(), getInParameters(),
76  						(Set<StoredProcField<?>>) pkParameters, getSpBean(), getPkBean(), isEmbeddedId());
77  		return st;
78  	}
79  
80  }