001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins.cmp.jdbc2;
023:
024: import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
025: import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCCMPFieldBridge2;
026: import org.jboss.ejb.plugins.cmp.jdbc2.schema.Schema;
027: import org.jboss.ejb.plugins.cmp.jdbc.QueryParameter;
028: import org.jboss.ejb.plugins.cmp.jdbc.JDBCEntityPersistenceStore;
029: import org.jboss.ejb.plugins.cmp.jdbc.JDBCTypeFactory;
030: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCTypeMappingMetaData;
031: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCFunctionMappingMetaData;
032: import org.jboss.ejb.GenericEntityObjectFactory;
033: import org.jboss.logging.Logger;
034: import org.jboss.deployment.DeploymentException;
035:
036: import javax.ejb.FinderException;
037: import javax.ejb.ObjectNotFoundException;
038:
039: /**
040: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
041: * @version <tt>$Revision: 61754 $</tt>
042: */
043: public class FindByPrimaryKeyCommand extends AbstractQueryCommand {
044: public FindByPrimaryKeyCommand(JDBCEntityBridge2 entity)
045: throws DeploymentException {
046: this .entity = entity;
047:
048: JDBCCMPFieldBridge2[] fields = (JDBCCMPFieldBridge2[]) entity
049: .getTableFields();
050: String selectColumns = fields[0].getColumnName();
051: for (int i = 1; i < fields.length; ++i) {
052: selectColumns += ", " + fields[i].getColumnName();
053: }
054:
055: JDBCCMPFieldBridge2[] pkFields = (JDBCCMPFieldBridge2[]) entity
056: .getPrimaryKeyFields();
057: String whereColumns = pkFields[0].getColumnName() + "=?";
058: for (int i = 1; i < pkFields.length; ++i) {
059: whereColumns += " and " + pkFields[i].getColumnName()
060: + "=?";
061: }
062:
063: if (entity.getMetaData().hasRowLocking()) {
064: JDBCEntityPersistenceStore manager = entity.getManager();
065: JDBCTypeFactory typeFactory = manager.getJDBCTypeFactory();
066: JDBCTypeMappingMetaData typeMapping = typeFactory
067: .getTypeMapping();
068: JDBCFunctionMappingMetaData rowLockingTemplate = typeMapping
069: .getRowLockingTemplate();
070:
071: if (rowLockingTemplate == null) {
072: throw new DeploymentException(
073: "Row locking template is not defined for mapping: "
074: + typeMapping.getName());
075: }
076:
077: sql = rowLockingTemplate.getFunctionSql(
078: new Object[] { selectColumns,
079: entity.getQualifiedTableName(),
080: whereColumns, null }, new StringBuffer())
081: .toString();
082: } else {
083: sql = "select ";
084: sql += selectColumns;
085: sql += " from " + entity.getQualifiedTableName()
086: + " where ";
087: sql += whereColumns;
088: }
089:
090: log = Logger.getLogger(getClass().getName() + "."
091: + entity.getEntityName() + "#findByPrimaryKey");
092:
093: log.debug("sql: " + sql);
094:
095: setParameters(QueryParameter.createPrimaryKeyParameters(0,
096: entity));
097: setEntityReader(entity, false);
098: }
099:
100: public Object fetchOne(Schema schema,
101: GenericEntityObjectFactory factory, Object[] args)
102: throws FinderException {
103: Object pk = args[0];
104: if (pk == null) {
105: throw new IllegalArgumentException(
106: "Null argument for findByPrimaryKey");
107: }
108:
109: Object instance;
110: boolean cached = entity.getTable().hasRow(pk);
111: if (!cached) {
112: instance = super .executeFetchOne(args, factory);
113: if (instance == null) {
114: throw new ObjectNotFoundException(
115: "Instance not find: entity="
116: + entity.getEntityName() + ", pk=" + pk);
117: }
118: } else {
119: instance = factory.getEntityEJBObject(pk);
120: }
121:
122: return instance;
123: }
124: }
|