KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.sql.DataSource JavaDoc;
29
30 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMPFieldBridge;
31 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge;
32 import org.jboss.logging.Logger;
33
34 /**
35  * Inserts relations into a relation table.
36  *
37  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
38  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
39  * @version $Revision: 37459 $
40  */

41 public final class JDBCInsertRelationsCommand {
42    private final Logger log;
43     
44    public JDBCInsertRelationsCommand(JDBCStoreManager manager) {
45       this.log = Logger.getLogger(
46             this.getClass().getName() +
47             "." +
48             manager.getMetaData().getName());
49    }
50    
51    public void execute(RelationData relationData) {
52       if(relationData.addedRelations.size() == 0) {
53          return;
54       }
55       
56       Connection JavaDoc con = null;
57       PreparedStatement JavaDoc ps = null;
58       
59       JDBCCMRFieldBridge cmrField = relationData.getLeftCMRField();
60       try {
61          // get the sql
62
String JavaDoc sql = getSQL(relationData);
63          boolean debug = log.isDebugEnabled();
64          if(debug)
65             log.debug("Executing SQL: " + sql);
66
67          // get the connection
68
DataSource JavaDoc dataSource = cmrField.getDataSource();
69          con = dataSource.getConnection();
70          
71          // get a prepared statement
72
ps = con.prepareStatement(sql);
73          
74          Iterator JavaDoc pairs = relationData.addedRelations.iterator();
75          while(pairs.hasNext()) {
76             RelationPair pair = (RelationPair)pairs.next();
77             
78             // set the parameters
79
setParameters(ps, relationData, pair);
80          
81             int rowsAffected = ps.executeUpdate();
82          }
83       } catch(Exception JavaDoc e) {
84          throw new EJBException JavaDoc("Could insert relations into " +
85                cmrField.getQualifiedTableName(), e);
86       } finally {
87          JDBCUtil.safeClose(ps);
88          JDBCUtil.safeClose(con);
89       }
90    }
91    
92    protected static String JavaDoc getSQL(RelationData relationData) {
93       JDBCCMRFieldBridge left = relationData.getLeftCMRField();
94       JDBCCMRFieldBridge right = relationData.getRightCMRField();
95       
96       StringBuffer JavaDoc sql = new StringBuffer JavaDoc(200);
97       sql.append(SQLUtil.INSERT_INTO).append(left.getQualifiedTableName());
98
99       sql.append('(');
100          SQLUtil.getColumnNamesClause(left.getTableKeyFields(), sql);
101       sql.append(SQLUtil.COMMA);
102          SQLUtil.getColumnNamesClause(right.getTableKeyFields(), sql);
103       sql.append(')');
104
105       sql.append(SQLUtil.VALUES).append('(');
106             SQLUtil.getValuesClause(left.getTableKeyFields(), sql);
107             sql.append(SQLUtil.COMMA);
108             SQLUtil.getValuesClause(right.getTableKeyFields(), sql);
109       sql.append(')');
110       return sql.toString();
111    }
112       
113    protected static void setParameters(PreparedStatement JavaDoc ps,
114                                 RelationData relationData,
115                                 RelationPair pair)
116    {
117       int index = 1;
118
119       // left keys
120
Object JavaDoc leftId = pair.getLeftId();
121       JDBCCMPFieldBridge[] leftFields = (JDBCCMPFieldBridge[]) relationData.getLeftCMRField().getTableKeyFields();
122       for(int i = 0; i < leftFields.length; ++i)
123          index = leftFields[i].setPrimaryKeyParameters(ps, index, leftId);
124
125       // right keys
126
Object JavaDoc rightId = pair.getRightId();
127       JDBCCMPFieldBridge[] rightFields = (JDBCCMPFieldBridge[]) relationData.getRightCMRField().getTableKeyFields();
128       for(int i = 0; i < rightFields.length; ++i)
129          index = rightFields[i].setPrimaryKeyParameters(ps, index, rightId);
130    }
131 }
132
Popular Tags