KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc2 > FindByPrimaryKeyCommand


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc2;
23
24 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
25 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMPFieldBridge2;
26 import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema;
27 import org.jboss.ejb.plugins.cmp.jdbc.QueryParameter;
28 import org.jboss.ejb.plugins.cmp.jdbc.JDBCEntityPersistenceStore;
29 import org.jboss.ejb.plugins.cmp.jdbc.JDBCTypeFactory;
30 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCTypeMappingMetaData;
31 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCFunctionMappingMetaData;
32 import org.jboss.ejb.GenericEntityObjectFactory;
33 import org.jboss.logging.Logger;
34 import org.jboss.deployment.DeploymentException;
35
36 import javax.ejb.FinderException JavaDoc;
37 import javax.ejb.ObjectNotFoundException JavaDoc;
38
39
40 /**
41  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
42  * @version <tt>$Revision: 58402 $</tt>
43  */

44 public class FindByPrimaryKeyCommand
45    extends AbstractQueryCommand
46 {
47    public FindByPrimaryKeyCommand(JDBCEntityBridge2 entity) throws DeploymentException
48    {
49       this.entity = entity;
50
51       JDBCCMPFieldBridge2[] fields = (JDBCCMPFieldBridge2[]) entity.getTableFields();
52       String JavaDoc selectColumns = fields[0].getColumnName();
53       for(int i = 1; i < fields.length; ++i)
54       {
55          selectColumns += ", " + fields[i].getColumnName();
56       }
57
58       JDBCCMPFieldBridge2[] pkFields = (JDBCCMPFieldBridge2[]) entity.getPrimaryKeyFields();
59       String JavaDoc whereColumns = pkFields[0].getColumnName() + "=?";
60       for(int i = 1; i < pkFields.length; ++i)
61       {
62          whereColumns += " and " + pkFields[i].getColumnName() + "=?";
63       }
64
65       if(entity.getMetaData().hasRowLocking())
66       {
67          JDBCEntityPersistenceStore manager = entity.getManager();
68          JDBCTypeFactory typeFactory = manager.getJDBCTypeFactory();
69          JDBCTypeMappingMetaData typeMapping = typeFactory.getTypeMapping();
70          JDBCFunctionMappingMetaData rowLockingTemplate = typeMapping.getRowLockingTemplate();
71
72          if(rowLockingTemplate == null)
73          {
74             throw new DeploymentException("Row locking template is not defined for mapping: " + typeMapping.getName());
75          }
76
77          sql = rowLockingTemplate.getFunctionSql(
78             new Object JavaDoc[]{selectColumns, entity.getQualifiedTableName(), whereColumns, null}, new StringBuffer JavaDoc()
79          ).toString();
80       }
81       else
82       {
83          sql = "select ";
84          sql += selectColumns;
85          sql += " from " + entity.getQualifiedTableName() + " where ";
86          sql += whereColumns;
87       }
88
89       log = Logger.getLogger(getClass().getName() + "." + entity.getEntityName() + "#findByPrimaryKey");
90
91       log.debug("sql: " + sql);
92
93       setParameters(QueryParameter.createPrimaryKeyParameters(0, entity));
94       setEntityReader(entity, false);
95    }
96
97    public Object JavaDoc fetchOne(Schema schema, GenericEntityObjectFactory factory, Object JavaDoc[] args) throws FinderException JavaDoc
98    {
99       Object JavaDoc pk = args[0];
100       if(pk == null)
101       {
102          throw new IllegalArgumentException JavaDoc("Null argument for findByPrimaryKey");
103       }
104
105       Object JavaDoc instance;
106       boolean cached = entity.getTable().hasRow(pk);
107       if(!cached)
108       {
109          instance = super.executeFetchOne(args, factory);
110          if(instance == null)
111          {
112             throw new ObjectNotFoundException JavaDoc("Instance not find: entity=" + entity.getEntityName() + ", pk=" + pk);
113          }
114       }
115       else
116       {
117          instance = factory.getEntityEJBObject(pk);
118       }
119
120       return instance;
121    }
122 }
123
Popular Tags