1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import java.util.*; 25 import oracle.toplink.essentials.descriptors.InheritancePolicy; 26 import oracle.toplink.essentials.mappings.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.internal.localization.*; 29 import oracle.toplink.essentials.descriptors.ClassDescriptor; 30 31 35 public class CommitOrderDependencyNode { 36 protected CommitOrderCalculator owner; 37 protected ClassDescriptor descriptor; 38 protected AbstractSession session; 39 40 protected Vector relatedNodes; 42 protected CommitOrderDependencyNode predecessor; 43 44 protected int traversalState; 46 static public int NotVisited = 1; 47 static public int InProgress = 2; 48 static public int Visited = 3; 49 50 protected int discoveryTime; 52 53 protected int finishingTime; 55 56 public CommitOrderDependencyNode(CommitOrderCalculator calculator, ClassDescriptor descriptor, AbstractSession session) { 57 this.owner = calculator; 58 this.descriptor = descriptor; 59 this.relatedNodes = new Vector(); 60 this.session = session; 61 } 62 63 public ClassDescriptor getDescriptor() { 64 return descriptor; 65 } 66 67 public int getFinishingTime() { 68 return finishingTime; 69 } 70 71 public CommitOrderCalculator getOwner() { 72 return owner; 73 } 74 75 public CommitOrderDependencyNode getPredecessor() { 76 return predecessor; 77 } 78 79 public Vector getRelatedNodes() { 80 return relatedNodes; 81 } 82 83 public boolean hasBeenVisited() { 84 return (traversalState == Visited); 85 } 86 87 public boolean hasNotBeenVisited() { 88 return (traversalState == NotVisited); 89 } 90 91 public void markInProgress() { 92 traversalState = InProgress; 93 } 94 95 public void markNotVisited() { 96 traversalState = NotVisited; 97 } 98 99 public void markVisited() { 100 traversalState = Visited; 101 } 102 103 109 public void recordMappingDependencies() { 110 for (Enumeration mappings = getDescriptor().getMappings().elements(); 111 mappings.hasMoreElements();) { 112 DatabaseMapping mapping = (DatabaseMapping)mappings.nextElement(); 113 if (mapping.isForeignReferenceMapping()) { 114 if (((ForeignReferenceMapping)mapping).hasConstraintDependency()) { 115 Class ownedClass; 116 ClassDescriptor refDescriptor = ((ForeignReferenceMapping)mapping).getReferenceDescriptor(); 117 if (refDescriptor == null) { 118 refDescriptor = session.getDescriptor(((ForeignReferenceMapping)mapping).getReferenceClass()); 119 } 120 ownedClass = refDescriptor.getJavaClass(); 121 122 if (ownedClass == null) { 123 throw oracle.toplink.essentials.exceptions.DescriptorException.referenceClassNotSpecified(mapping); 124 } 125 CommitOrderDependencyNode node = getOwner().nodeFor(ownedClass); 126 Vector ownedNodes = withAllSubclasses(node); 127 128 Helper.addAllToVector(relatedNodes, ownedNodes); 130 } else if (((ForeignReferenceMapping)mapping).hasInverseConstraintDependency()) { 131 Class ownerClass; 132 ClassDescriptor refDescriptor = ((ForeignReferenceMapping)mapping).getReferenceDescriptor(); 133 if (refDescriptor == null) { 134 refDescriptor = session.getDescriptor(((ForeignReferenceMapping)mapping).getReferenceClass()); 135 } 136 ownerClass = refDescriptor.getJavaClass(); 137 if (ownerClass == null) { 138 throw oracle.toplink.essentials.exceptions.DescriptorException.referenceClassNotSpecified(mapping); 139 } 140 CommitOrderDependencyNode ownerNode = getOwner().nodeFor(ownerClass); 141 Vector ownedNodes = withAllSubclasses(this); 142 143 Helper.addAllToVector(ownerNode.getRelatedNodes(), ownedNodes); 145 } 146 } 147 } 148 } 149 150 156 public void recordSpecifiedDependencies() { 157 for (Enumeration constraintsEnum = getDescriptor().getConstraintDependencies().elements(); 158 constraintsEnum.hasMoreElements();) { 159 Class ownedClass = (Class )constraintsEnum.nextElement(); 160 CommitOrderDependencyNode node = getOwner().nodeFor(ownedClass); 161 Vector ownedNodes = withAllSubclasses(node); 162 163 Helper.addAllToVector(relatedNodes, ownedNodes); 165 } 166 } 167 168 public void setDiscoveryTime(int time) { 169 discoveryTime = time; 170 } 171 172 public void setFinishingTime(int time) { 173 finishingTime = time; 174 } 175 176 public void setPredecessor(CommitOrderDependencyNode n) { 177 predecessor = n; 178 } 179 180 public String toString() { 181 if (descriptor == null) { 182 return ToStringLocalization.buildMessage("empty_commit_order_dependency_node", (Object [])null); 183 } else { 184 Object [] args = { descriptor }; 185 return ToStringLocalization.buildMessage("node", args); 186 } 187 } 188 189 public void visit() { 190 int startTime; 192 markInProgress(); 193 startTime = getOwner().getNextTime(); 194 setDiscoveryTime(startTime); 195 196 for (Enumeration e = getRelatedNodes().elements(); e.hasMoreElements();) { 197 CommitOrderDependencyNode node = (CommitOrderDependencyNode)e.nextElement(); 198 if (node.hasNotBeenVisited()) { 199 node.setPredecessor(this); 200 node.visit(); 201 } 202 if (node.getPredecessor() == null) { 203 node.setPredecessor(this); 204 } 205 } 206 207 markVisited(); 208 setFinishingTime(getOwner().getNextTime()); 209 } 210 211 public Vector withAllSubclasses(CommitOrderDependencyNode node) { 213 Vector results = new Vector(); 214 results.addElement(node); 215 216 if (node.getDescriptor().hasInheritance()) { 217 InheritancePolicy p = node.getDescriptor().getInheritancePolicy(); 218 219 for (Enumeration e = p.getAllChildDescriptors().elements(); e.hasMoreElements();) { 221 results.addElement(getOwner().nodeFor((ClassDescriptor)e.nextElement())); 222 } 223 } 224 return results; 225 } 226 } 227 | Popular Tags |