KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27
28 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge;
29
30 /**
31  * This class holds data about one relationship. It maintains a lists of
32  * which relations have been added and removed. When the transaction is
33  * committed these list are retrieved and used to update the relation table.
34  *
35  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
36  * @version $Revision: 37459 $
37  */

38 public final class RelationData
39 {
40    private final JDBCCMRFieldBridge leftCMRField;
41    private final JDBCCMRFieldBridge rightCMRField;
42
43    public final Set JavaDoc addedRelations = new HashSet JavaDoc();
44    public final Set JavaDoc removedRelations = new HashSet JavaDoc();
45    public final Set JavaDoc notRelatedPairs = new HashSet JavaDoc();
46
47    public RelationData(JDBCCMRFieldBridge leftCMRField, JDBCCMRFieldBridge rightCMRField)
48    {
49
50       this.leftCMRField = leftCMRField;
51       this.rightCMRField = rightCMRField;
52    }
53
54    public JDBCCMRFieldBridge getLeftCMRField()
55    {
56       return leftCMRField;
57    }
58
59    public JDBCCMRFieldBridge getRightCMRField()
60    {
61       return rightCMRField;
62    }
63
64    public void addRelation(JDBCCMRFieldBridge leftCMRField,
65                            Object JavaDoc leftId,
66                            JDBCCMRFieldBridge rightCMRField,
67                            Object JavaDoc rightId)
68    {
69       // only need to bother if neither side has a foreign key
70
if(!leftCMRField.hasForeignKey() && !rightCMRField.hasForeignKey())
71       {
72          RelationPair pair = createRelationPair(leftCMRField, leftId, rightCMRField, rightId);
73          if(removedRelations.contains(pair))
74          {
75             // we were going to remove this relation
76
// and now we are adding it. Just
77
// remove it from the remove set and we are ok.
78
removedRelations.remove(pair);
79          }
80          else
81          {
82             addedRelations.add(pair);
83
84             // if pair was specifically marked as
85
// not related, remove it to the not
86
// related set. See below.
87
if(notRelatedPairs.contains(pair))
88             {
89                notRelatedPairs.remove(pair);
90             }
91          }
92       }
93    }
94
95    public void removeRelation(JDBCCMRFieldBridge leftCMRField,
96                               Object JavaDoc leftId,
97                               JDBCCMRFieldBridge rightCMRField,
98                               Object JavaDoc rightId)
99    {
100       // only need to bother if neither side has a foreign key
101
if(!leftCMRField.hasForeignKey() && !rightCMRField.hasForeignKey())
102       {
103          RelationPair pair = createRelationPair(leftCMRField, leftId, rightCMRField, rightId);
104          if(addedRelations.contains(pair))
105          {
106             // we were going to add this relation
107
// and now we are removing it. Just
108
// remove it from the add set and we are ok.
109
addedRelations.remove(pair);
110
111             // add it to the set of not related pairs
112
// so if remove is called again it is not
113
// added to the remove list. This avoids
114
// an extra 'DELETE FROM...' query.
115
// This happend when a object is moved from
116
// one relation to another. See
117
// JDBCCMRFieldBridge.createRelationLinks
118
notRelatedPairs.add(pair);
119          }
120          else
121          {
122             // if pair is related (not not related)
123
// add it to the remove set. See above.
124
if(!notRelatedPairs.contains(pair))
125             {
126                removedRelations.add(pair);
127             }
128          }
129       }
130    }
131
132    public boolean isDirty()
133    {
134       return addedRelations.size() > 0 || removedRelations.size() > 0;
135    }
136
137    private RelationPair createRelationPair(JDBCCMRFieldBridge leftCMRField,
138                                            Object JavaDoc leftId,
139                                            JDBCCMRFieldBridge rightCMRField,
140                                            Object JavaDoc rightId)
141    {
142       if(this.leftCMRField == leftCMRField && this.rightCMRField == rightCMRField)
143       {
144          return new RelationPair(leftCMRField, leftId, rightCMRField, rightId);
145       }
146
147       if(this.leftCMRField == rightCMRField && this.rightCMRField == leftCMRField)
148       {
149          return new RelationPair(rightCMRField, rightId, leftCMRField, leftId);
150       }
151       throw new EJBException JavaDoc("Error: cmrFields are of wrong type");
152    }
153 }
154
155
156
Popular Tags