1 22 package org.jboss.ejb.plugins.cmp.jdbc; 23 24 import java.util.HashSet ; 25 import java.util.Set ; 26 import javax.ejb.EJBException ; 27 28 import org.jboss.ejb.plugins.cmp.jdbc.bridge.JDBCCMRFieldBridge; 29 30 38 public final class RelationData 39 { 40 private final JDBCCMRFieldBridge leftCMRField; 41 private final JDBCCMRFieldBridge rightCMRField; 42 43 public final Set addedRelations = new HashSet (); 44 public final Set removedRelations = new HashSet (); 45 public final Set notRelatedPairs = new HashSet (); 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 leftId, 66 JDBCCMRFieldBridge rightCMRField, 67 Object rightId) 68 { 69 if(!leftCMRField.hasForeignKey() && !rightCMRField.hasForeignKey()) 71 { 72 RelationPair pair = createRelationPair(leftCMRField, leftId, rightCMRField, rightId); 73 if(removedRelations.contains(pair)) 74 { 75 removedRelations.remove(pair); 79 } 80 else 81 { 82 addedRelations.add(pair); 83 84 if(notRelatedPairs.contains(pair)) 88 { 89 notRelatedPairs.remove(pair); 90 } 91 } 92 } 93 } 94 95 public void removeRelation(JDBCCMRFieldBridge leftCMRField, 96 Object leftId, 97 JDBCCMRFieldBridge rightCMRField, 98 Object rightId) 99 { 100 if(!leftCMRField.hasForeignKey() && !rightCMRField.hasForeignKey()) 102 { 103 RelationPair pair = createRelationPair(leftCMRField, leftId, rightCMRField, rightId); 104 if(addedRelations.contains(pair)) 105 { 106 addedRelations.remove(pair); 110 111 notRelatedPairs.add(pair); 119 } 120 else 121 { 122 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 leftId, 139 JDBCCMRFieldBridge rightCMRField, 140 Object 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 ("Error: cmrFields are of wrong type"); 152 } 153 } 154 155 156 | Popular Tags |