KickJava   Java API By Example, From Geeks To Geeks.

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


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.EntityEnterpriseContext;
25 import org.jboss.ejb.plugins.cmp.jdbc2.bridge.JDBCEntityBridge2;
26 import org.jboss.deployment.DeploymentException;
27
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.DuplicateKeyException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.sql.SQLException JavaDoc;
32
33 /**
34  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
35  * @version <tt>$Revision: 37459 $</tt>
36  */

37 public class ApplicationPkCreateCommand
38    implements CreateCommand
39 {
40    private JDBCEntityBridge2 entityBridge;
41
42    public void init(JDBCStoreManager2 manager) throws DeploymentException
43    {
44       this.entityBridge = (JDBCEntityBridge2) manager.getEntityBridge();
45    }
46
47    public Object JavaDoc execute(Method JavaDoc m, Object JavaDoc[] args, EntityEnterpriseContext ctx) throws CreateException JavaDoc
48    {
49       Object JavaDoc pk;
50       PersistentContext pctx = (PersistentContext) ctx.getPersistenceContext();
51       if(ctx.getId() == null)
52       {
53          pk = entityBridge.extractPrimaryKeyFromInstance(ctx);
54
55          if(pk == null)
56          {
57             throw new CreateException JavaDoc("Primary key for created instance is null.");
58          }
59
60          pctx.setPk(pk);
61       }
62       else
63       {
64          // insert-after-ejb-post-create
65
try
66          {
67             pctx.flush();
68          }
69          catch(SQLException JavaDoc e)
70          {
71             if("23000".equals(e.getSQLState()))
72             {
73                throw new DuplicateKeyException JavaDoc("Unique key violation or invalid foreign key value: pk=" + ctx.getId());
74             }
75             else
76             {
77                throw new CreateException JavaDoc("Failed to create instance: pk=" + ctx.getId() +
78                   ", state=" + e.getSQLState() +
79                   ", msg=" + e.getMessage());
80             }
81          }
82          pk = ctx.getId();
83       }
84       return pk;
85    }
86 }
87
Popular Tags