1 package org.apache.torque.engine.database.model; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.Hashtable ; 21 import java.util.List ; 22 import org.xml.sax.Attributes ; 23 24 31 public class ForeignKey 32 { 33 private String foreignTableName; 34 private String name; 35 private String onUpdate; 36 private String onDelete; 37 private Table parentTable; 38 private List localColumns = new ArrayList (3); 39 private List foreignColumns = new ArrayList (3); 40 41 private static final String NONE = "NONE"; 43 private static final String SETNULL = "SETNULL"; 44 45 50 public void loadFromXML(Attributes attrib) 51 { 52 foreignTableName = attrib.getValue("foreignTable"); 53 name = attrib.getValue("name"); 54 onUpdate = attrib.getValue("onUpdate"); 55 onDelete = attrib.getValue("onDelete"); 56 onUpdate = normalizeFKey(onUpdate); 57 onDelete = normalizeFKey(onDelete); 58 } 59 60 66 private String normalizeFKey(String attrib) 67 { 68 if (attrib == null) 69 { 70 attrib = NONE; 71 } 72 73 attrib = attrib.toUpperCase(); 74 if (attrib.equals(SETNULL)) 75 { 76 attrib = "SET NULL"; 77 } 78 return attrib; 79 } 80 81 86 public boolean hasOnUpdate() 87 { 88 return !onUpdate.equals(NONE); 89 } 90 91 96 public boolean hasOnDelete() 97 { 98 return !onDelete.equals(NONE); 99 } 100 101 106 public String getOnUpdate() 107 { 108 return onUpdate; 109 } 110 111 116 public String getOnDelete() 117 { 118 return onDelete; 119 } 120 121 126 public void setOnDelete(String value) 127 { 128 onDelete = normalizeFKey(value); 129 } 130 131 136 public void setOnUpdate(String value) 137 { 138 onUpdate = normalizeFKey(value); 139 } 140 141 146 public String getName() 147 { 148 return name; 149 } 150 151 156 public void setName(String name) 157 { 158 this.name = name; 159 } 160 161 166 public String getForeignTableName() 167 { 168 return foreignTableName; 169 } 170 171 176 public void setForeignTableName(String tableName) 177 { 178 foreignTableName = tableName; 179 } 180 181 186 public void setTable(Table parent) 187 { 188 parentTable = parent; 189 } 190 191 196 public Table getTable() 197 { 198 return parentTable; 199 } 200 201 206 public String getTableName() 207 { 208 return parentTable.getName(); 209 } 210 211 216 public void addReference(Attributes attrib) 217 { 218 addReference(attrib.getValue("local"), attrib.getValue("foreign")); 219 } 220 221 227 public void addReference(String local, String foreign) 228 { 229 localColumns.add(local); 230 foreignColumns.add(foreign); 231 } 232 233 238 public String getLocalColumnNames() 239 { 240 return Column.makeList(getLocalColumns()); 241 } 242 243 248 public String getForeignColumnNames() 249 { 250 return Column.makeList(getForeignColumns()); 251 } 252 253 258 public List getLocalColumns() 259 { 260 return localColumns; 261 } 262 263 269 public Hashtable getLocalForeignMapping() 270 { 271 Hashtable h = new Hashtable (); 272 273 for (int i = 0; i < localColumns.size(); i++) 274 { 275 h.put(localColumns.get(i), foreignColumns.get(i)); 276 } 277 278 return h; 279 } 280 281 286 public List getForeignColumns() 287 { 288 return foreignColumns; 289 } 290 291 297 public Hashtable getForeignLocalMapping() 298 { 299 Hashtable h = new Hashtable (); 300 301 for (int i = 0; i < localColumns.size(); i++) 302 { 303 h.put(foreignColumns.get(i), localColumns.get(i)); 304 } 305 306 return h; 307 } 308 309 314 public String toString() 315 { 316 StringBuffer result = new StringBuffer (); 317 result.append(" <foreign-key foreignTable=\"") 318 .append(getForeignTableName()) 319 .append("\" name=\"") 320 .append(getName()) 321 .append("\">\n"); 322 323 for (int i = 0; i < localColumns.size(); i++) 324 { 325 result.append(" <reference local=\"") 326 .append(localColumns.get(i)) 327 .append("\" foreign=\"") 328 .append(foreignColumns.get(i)) 329 .append("\"/>\n"); 330 } 331 result.append(" </foreign-key>\n"); 332 return result.toString(); 333 } 334 } 335 | Popular Tags |