KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27
28 import org.jboss.ejb.EntityEnterpriseContext;
29 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge;
30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCEntityBridge;
31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCFieldBridge;
32
33 /**
34  * This command establishes relationships for CMR fields that have
35  * foreign keys mapped to primary keys.
36  *
37  * @author <a HREF="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
38  *
39  * @version $Revision: 37459 $
40  */

41 public final class JDBCPostCreateEntityCommand
42 {
43    // Attributes ------------------------------------
44
private final JDBCEntityBridge entity;
45    private final JDBCCMRFieldBridge[] cmrWithFKMappedToCMP;
46
47    // Constructors ----------------------------------
48
public JDBCPostCreateEntityCommand(JDBCStoreManager manager)
49    {
50       entity = (JDBCEntityBridge) manager.getEntityBridge();
51       JDBCFieldBridge[] cmrFields = entity.getCMRFields();
52       List JavaDoc fkToCMPList = new ArrayList JavaDoc(4);
53       for(int i = 0; i < cmrFields.length; ++i)
54       {
55          JDBCCMRFieldBridge cmrField = (JDBCCMRFieldBridge)cmrFields[i];
56          JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge)cmrField.getRelatedCMRField();
57          if(cmrField.hasFKFieldsMappedToCMPFields()
58             || relatedCMRField.hasFKFieldsMappedToCMPFields())
59          {
60             fkToCMPList.add(cmrField);
61          }
62       }
63       if(fkToCMPList.isEmpty())
64          cmrWithFKMappedToCMP = null;
65       else
66          cmrWithFKMappedToCMP = (JDBCCMRFieldBridge[])fkToCMPList
67             .toArray(new JDBCCMRFieldBridge[fkToCMPList.size()]);
68    }
69
70    // Public ----------------------------------------
71
public Object JavaDoc execute(Method JavaDoc m, Object JavaDoc[] args, EntityEnterpriseContext ctx)
72    {
73       if(cmrWithFKMappedToCMP == null)
74          return null;
75
76       for(int i = 0; i < cmrWithFKMappedToCMP.length; ++i)
77       {
78          JDBCCMRFieldBridge cmrField = cmrWithFKMappedToCMP[i];
79          JDBCCMRFieldBridge relatedCMRField = (JDBCCMRFieldBridge)cmrField.getRelatedCMRField();
80          if(cmrField.hasFKFieldsMappedToCMPFields())
81          {
82             Object JavaDoc relatedId = cmrField.getRelatedIdFromContext(ctx);
83             if(relatedId != null)
84             {
85                try
86                {
87                   if(cmrField.isForeignKeyValid(relatedId))
88                   {
89                      cmrField.createRelationLinks(ctx, relatedId);
90                   }
91                   else
92                   {
93                      relatedCMRField.addRelatedPKWaitingForMyPK(relatedId, ctx.getId());
94                   }
95                }
96                catch(Exception JavaDoc e)
97                {
98                   // no such object
99
}
100             }
101          }
102          else if(relatedCMRField.hasFKFieldsMappedToCMPFields())
103          {
104             cmrField.addRelatedPKsWaitedForMe(ctx);
105          }
106       }
107       return null;
108    }
109 }
110
Popular Tags