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.util.HashMap;
24  import java.util.Map;
25  
26  import javax.sql.DataSource;
27  
28  import com.hrx.rasp.util.dao.metadata.StoredProcMetaDataImpl;
29  
30  /***
31   * @author dan.stoica <dan.stoica@acslink.net.au>
32   * 
33   */
34  public class CommandFactory
35  {
36  	private Map<OperationType, DAOCommand> cache = new HashMap<OperationType, DAOCommand>();
37  
38  	private final DataSource ds;
39  
40  	/***
41  	 * @param ds
42  	 */
43  	public CommandFactory(DataSource ds)
44  	{
45  		super();
46  		this.ds = ds;
47  	}
48  
49  	public DAOCommand getCommand(OperationType operation, StoredProcMetaDataImpl meta, Object bean)
50  	{
51  		DAOCommand cmd;
52  		// reuse the command if it is already in the cache. Otherway if the
53  		// command is not found in the cache we instantiate a new command
54  
55  		if (cache.containsKey(operation))
56  		{
57  			cmd = cache.get(operation);
58  		}
59  		else
60  		{
61  			cmd = operation.createCommand(ds);
62  			cache.put(operation, cmd);
63  		}
64  
65  		prepareCommand(cmd, bean, meta);
66  
67  		return cmd;
68  	}
69  
70  	/***
71  	 * @param meta
72  	 * @param bean
73  	 * @param cmd
74  	 */
75  	private void prepareCommand(DAOCommand cmd, Object bean, StoredProcMetaDataImpl meta)
76  	{
77  		cmd.setMetaData(meta);
78  		cmd.setSpBean(bean);
79  	}
80  }