KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > JDBCFindByPrimaryKeyQuery


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.jdbc;
23
24 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
25 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
26 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCAbstractCMPFieldBridge;
27 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCQueryMetaData;
28 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCReadAheadMetaData;
29 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCTypeMappingMetaData;
30 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCFunctionMappingMetaData;
31 import org.jboss.ejb.EntityEnterpriseContext;
32 import org.jboss.ejb.GenericEntityObjectFactory;
33 import org.jboss.deployment.DeploymentException;
34
35 import javax.ejb.FinderException JavaDoc;
36 import java.util.Collection JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.List JavaDoc;
39 import java.lang.reflect.Method JavaDoc;
40
41 /**
42  * JDBCBeanExistsCommand is a JDBC query that checks if an id exists
43  * in the database. This is used by the create and findByPrimaryKey
44  * code.
45  *
46  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
47  * @author <a HREF="mailto:marc.fleury@telkel.com">Marc Fleury</a>
48  * @author <a HREF="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
49  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
50  * @version $Revision: 43504 $
51  */

52 public final class JDBCFindByPrimaryKeyQuery extends JDBCAbstractQueryCommand
53 {
54    private JDBCStoreManager manager;
55    private boolean rowLocking;
56
57    public JDBCFindByPrimaryKeyQuery(JDBCStoreManager manager, JDBCQueryMetaData q)
58       throws DeploymentException
59    {
60       super(manager, q);
61       this.manager = manager;
62       rowLocking = manager.getMetaData().hasRowLocking();
63
64       JDBCEntityBridge entity = (JDBCEntityBridge) manager.getEntityBridge();
65
66       JDBCTypeMappingMetaData typeMapping = this.manager.getJDBCTypeFactory().getTypeMapping();
67       AliasManager aliasManager = new AliasManager(
68          typeMapping.getAliasHeaderPrefix(),
69          typeMapping.getAliasHeaderSuffix(),
70          typeMapping.getAliasMaxLength()
71       );
72
73       String JavaDoc alias = aliasManager.getAlias(entity.getEntityName());
74
75       StringBuffer JavaDoc select = new StringBuffer JavaDoc(200);
76       SQLUtil.getColumnNamesClause(entity.getPrimaryKeyFields(), alias, select);
77
78       StringBuffer JavaDoc from = new StringBuffer JavaDoc();
79       from.append(entity.getQualifiedTableName())
80          .append(' ')
81          .append(alias);
82
83       // set the preload fields
84
JDBCReadAheadMetaData readAhead = q.getReadAhead();
85       if(readAhead.isOnFind())
86       {
87          setEagerLoadGroup(readAhead.getEagerLoadGroup());
88          if(getEagerLoadMask() != null)
89          {
90             SQLUtil.appendColumnNamesClause(entity.getTableFields(), getEagerLoadMask(), alias, select);
91
92             List JavaDoc onFindCMRList = JDBCAbstractQueryCommand.getLeftJoinCMRNodes(
93                entity, entity.getQualifiedTableName(), readAhead.getLeftJoins(), null);
94
95             if(!onFindCMRList.isEmpty())
96             {
97                setOnFindCMRList(onFindCMRList);
98                JDBCAbstractQueryCommand.leftJoinCMRNodes(alias, onFindCMRList, aliasManager, from);
99                JDBCAbstractQueryCommand.appendLeftJoinCMRColumnNames(onFindCMRList, aliasManager, select);
100             }
101          }
102       }
103
104       StringBuffer JavaDoc where = new StringBuffer JavaDoc();
105       SQLUtil.getWhereClause(entity.getPrimaryKeyFields(), alias, where);
106
107       // generate the sql
108
StringBuffer JavaDoc sql = new StringBuffer JavaDoc(300);
109       if(rowLocking && readAhead.isOnFind() && getEagerLoadMask() != null)
110       {
111          JDBCFunctionMappingMetaData rowLockingTemplate = typeMapping.getRowLockingTemplate();
112          rowLockingTemplate.getFunctionSql(
113             new Object JavaDoc[]{
114                select,
115                from,
116                where.length() == 0 ? null : where,
117                null // order by
118
},
119             sql
120          );
121       }
122       else
123       {
124          sql.append(SQLUtil.SELECT)
125             .append(select)
126             .append(SQLUtil.FROM)
127             .append(from)
128             .append(SQLUtil.WHERE)
129             .append(where);
130       }
131
132       setSQL(sql.toString());
133       setParameterList(QueryParameter.createPrimaryKeyParameters(0, entity));
134    }
135
136    public Collection JavaDoc execute(Method JavaDoc finderMethod, Object JavaDoc[] args, EntityEnterpriseContext ctx, GenericEntityObjectFactory factory)
137       throws FinderException JavaDoc
138    {
139       // Check in readahead cache.
140
if(manager.getReadAheadCache().getPreloadDataMap(args[0], false) != null)
141       {
142          // copy pk [JBAS-1361]
143
Object JavaDoc pk = null;
144          JDBCFieldBridge[] pkFields = manager.getEntityBridge().getPrimaryKeyFields();
145          for(int i = 0; i < pkFields.length; ++i)
146          {
147             JDBCAbstractCMPFieldBridge pkField = ((JDBCAbstractCMPFieldBridge)pkFields[i]);
148             Object JavaDoc fieldValue = pkField.getPrimaryKeyValue(args[0]);
149             pk = pkField.setPrimaryKeyValue(pk, fieldValue);
150          }
151
152          final Object JavaDoc ejbObject = factory.getEntityEJBObject(pk);
153          return Collections.singletonList(ejbObject);
154       }
155       return super.execute(finderMethod, args, ctx, factory);
156    }
157 }
158
Popular Tags