1 21 package oracle.toplink.essentials.tools.schemaframework; 23 24 import java.io.*; 25 import java.util.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.internal.sessions.AbstractSession; 28 29 34 public class ForeignKeyConstraint implements Serializable { 35 protected String name; 36 protected Vector sourceFields; 37 protected Vector targetFields; 38 protected String targetTable; 39 protected boolean shouldCascadeOnDelete; 40 41 public ForeignKeyConstraint() { 42 this.name = ""; 43 this.sourceFields = new Vector(); 44 this.targetFields = new Vector(); 45 this.targetTable = ""; 46 this.shouldCascadeOnDelete = false; 47 } 48 49 public ForeignKeyConstraint(String name, String sourceField, String targetField, String targetTable) { 50 this(); 51 this.name = name; 52 sourceFields.addElement(sourceField); 53 targetFields.addElement(targetField); 54 this.targetTable = targetTable; 55 } 56 57 public void addSourceField(String sourceField) { 58 getSourceFields().addElement(sourceField); 59 } 60 61 public void addTargetField(String targetField) { 62 getTargetFields().addElement(targetField); 63 } 64 65 69 public void appendDBString(Writer writer, AbstractSession session) { 70 try { 71 writer.write("FOREIGN KEY ("); 72 for (Enumeration sourceEnum = getSourceFields().elements(); 73 sourceEnum.hasMoreElements();) { 74 writer.write((String )sourceEnum.nextElement()); 75 if (sourceEnum.hasMoreElements()) { 76 writer.write(", "); 77 } 78 } 79 writer.write(") REFERENCES "); 80 writer.write(getTargetTable()); 81 writer.write(" ("); 82 for (Enumeration targetEnum = getTargetFields().elements(); 83 targetEnum.hasMoreElements();) { 84 writer.write((String )targetEnum.nextElement()); 85 if (targetEnum.hasMoreElements()) { 86 writer.write(", "); 87 } 88 } 89 writer.write(")"); 90 if (shouldCascadeOnDelete()) { 91 writer.write(" ON DELETE CASCADE"); 92 } 93 } catch (IOException ioException) { 94 throw ValidationException.fileError(ioException); 95 } 96 } 97 98 103 public void cascadeOnDelete() { 104 setShouldCascadeOnDelete(true); 105 } 106 107 111 public void dontCascadeOnDelete() { 112 setShouldCascadeOnDelete(false); 113 } 114 115 public String getName() { 116 return name; 117 } 118 119 public Vector getSourceFields() { 120 return sourceFields; 121 } 122 123 public Vector getTargetFields() { 124 return targetFields; 125 } 126 127 public String getTargetTable() { 128 return targetTable; 129 } 130 131 public void setName(String name) { 132 this.name = name; 133 } 134 135 140 public void setShouldCascadeOnDelete(boolean shouldCascadeOnDelete) { 141 this.shouldCascadeOnDelete = shouldCascadeOnDelete; 142 } 143 144 public void setSourceFields(Vector sourceFields) { 145 this.sourceFields = sourceFields; 146 } 147 148 public void setTargetFields(Vector targetFields) { 149 this.targetFields = targetFields; 150 } 151 152 public void setTargetTable(String targetTable) { 153 this.targetTable = targetTable; 154 } 155 156 public boolean shouldCascadeOnDelete() { 157 return shouldCascadeOnDelete; 158 } 159 } 160 | Popular Tags |