01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * 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.keygen;
23:
24: import javax.ejb.CreateException;
25: import javax.naming.InitialContext;
26: import javax.naming.NamingException;
27:
28: import org.jboss.deployment.DeploymentException;
29: import org.jboss.ejb.EntityEnterpriseContext;
30: import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
31: import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCEntityCommandMetaData;
32: import org.jboss.ejb.plugins.cmp.jdbc.JDBCInsertPKCreateCommand;
33: import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager;
34: import org.jboss.ejb.plugins.keygenerator.KeyGenerator;
35: import org.jboss.ejb.plugins.keygenerator.KeyGeneratorFactory;
36:
37: /**
38: * JDBCKeyGeneratorCreateCommand executes an INSERT INTO query.
39: * This command will ask the corresponding key generator for a
40: * value for the primary key before inserting the row.
41: *
42: * @author <a href="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
43: *
44: * @version $Revision: 57209 $
45: */
46: public class JDBCKeyGeneratorCreateCommand extends
47: JDBCInsertPKCreateCommand {
48: protected KeyGenerator keyGenerator;
49: protected JDBCCMPFieldBridge pkField;
50:
51: public void init(JDBCStoreManager manager)
52: throws DeploymentException {
53: super .init(manager);
54: pkField = getGeneratedPKField();
55: }
56:
57: protected void initEntityCommand(
58: JDBCEntityCommandMetaData entityCommand)
59: throws DeploymentException {
60: super .initEntityCommand(entityCommand);
61:
62: String factoryName = entityCommand
63: .getAttribute("key-generator-factory");
64: if (factoryName == null) {
65: throw new DeploymentException(
66: "key-generator-factory attribute must be set for entity "
67: + entity.getEntityName());
68: }
69:
70: try {
71: KeyGeneratorFactory keyGeneratorFactory = (KeyGeneratorFactory) new InitialContext()
72: .lookup(factoryName);
73: keyGenerator = keyGeneratorFactory.getKeyGenerator();
74: } catch (NamingException e) {
75: throw new DeploymentException(
76: "Error: can't find key generator factory: "
77: + factoryName, e);
78: } catch (Exception e) {
79: throw new DeploymentException(
80: "Error: can't create key generator instance; key generator factory: "
81: + factoryName, e);
82: }
83: }
84:
85: protected void generateFields(EntityEnterpriseContext ctx)
86: throws CreateException {
87: super .generateFields(ctx);
88:
89: Object pk = keyGenerator.generateKey();
90: log.debug("Generated new pk: " + pk);
91: pkField.setInstanceValue(ctx, pk);
92: }
93: }
|