1 package org.apache.ojb.tools.mapping.reversedb; 2 3 17 18 19 24 public class DBFKRelation implements MetadataNodeInterface, javax.swing.tree.TreeNode , org.apache.ojb.tools.mapping.reversedb.gui.PropertySheetModel 25 { 26 DBTable pkTable; 27 DBTable fkTable; 28 29 private boolean bAutoRetrieve = true; 30 private boolean bAutoUpdate = false; 31 private boolean bAutoDelete = false; 32 33 private String strFieldName = null; 34 35 39 private String strFieldType = null; 40 41 private boolean enabled = true; 42 43 boolean pkTableIsParent = true; 44 java.util.ArrayList alColumnPairs = new java.util.ArrayList (); 45 46 public DBFKRelation (DBTable pPkTable, DBTable pFkTable, boolean ppkTableIsParent) 47 { 48 pkTable = pPkTable; 49 fkTable = pFkTable; 50 pkTableIsParent = ppkTableIsParent; 51 if (pkTableIsParent) 52 { 53 this.strFieldName = "coll" + fkTable.getClassName (); 54 this.strFieldType = "java.util.Vector"; 55 } 56 else 57 { 58 this.strFieldName = "a" + pkTable.getClassName (); 59 } 60 } 61 62 public boolean isEnabled() 63 { 64 return this.enabled; 65 } 66 67 public void setEnabled(boolean b) 68 { 69 this.enabled = b; 70 } 71 72 public String getFieldName() 73 { 74 return this.strFieldName; 75 } 76 77 public void setFieldName(String s) 78 { 79 strFieldName = s; 80 } 81 82 public String getFieldType() 83 { 84 if (this.isPkTableParent ()) return this.strFieldType; 85 else return this.pkTable.getFQClassName(); 86 } 87 88 public void setFieldType(String s) 89 { 90 if (!this.isPkTableParent()) throw new java.lang.UnsupportedOperationException ("Cannot set Field type on this type of relation"); 91 strFieldType = s; 92 } 93 94 public boolean isPkTableParent() 95 { 96 return this.pkTableIsParent; 97 } 98 99 public void setAutoRetrieve(boolean b) 100 { 101 this.bAutoRetrieve = b; 102 } 103 104 public boolean getAutoRetrieve() 105 { 106 return this.bAutoRetrieve; 107 } 108 109 public void setAutoUpdate(boolean b) 110 { 111 this.bAutoUpdate = b; 112 } 113 114 public boolean getAutoUpdate() 115 { 116 return this.bAutoUpdate; 117 } 118 119 public void setAutoDelete(boolean b) 120 { 121 this.bAutoDelete = b; 122 } 123 124 public boolean getAutoDelete() 125 { 126 return this.bAutoDelete; 127 } 128 129 public DBTable getFKTable() 130 { 131 return fkTable; 132 } 133 134 public DBTable getPKTable() 135 { 136 return pkTable; 137 } 138 139 public boolean isTreeEnabled() 140 { 141 if (this.pkTableIsParent) return this.pkTable.isTreeEnabled() && this.isEnabled(); 142 else return this.fkTable.isTreeEnabled() && this.isEnabled(); 143 } 144 145 146 public void addColumnPair(DBColumn pPkColumn, DBColumn pFkColumn) 147 { 148 alColumnPairs.add(new Object []{pPkColumn, pFkColumn}); 149 } 150 151 public java.util.Iterator getColumnPairIterator() 152 { 153 return alColumnPairs.iterator(); 154 } 155 156 public java.util.Enumeration children () 157 { 158 if (pkTableIsParent) return this.fkTable.children(); 159 else return this.pkTable.children(); 160 } 161 162 public boolean getAllowsChildren () 163 { 164 if (pkTableIsParent) return this.fkTable.getAllowsChildren(); 165 else return this.pkTable.getAllowsChildren(); 166 } 167 168 public javax.swing.tree.TreeNode getChildAt (int param) 169 { 170 if (pkTableIsParent) return this.fkTable.getChildAt(param); 171 else return this.pkTable.getChildAt(param); 172 } 173 174 public int getChildCount () 175 { 176 if (pkTableIsParent) return this.fkTable.getChildCount(); 177 else return this.pkTable.getChildCount(); 178 } 179 180 public int getIndex (javax.swing.tree.TreeNode treeNode) 181 { 182 if (pkTableIsParent) return this.fkTable.getIndex(treeNode); 183 else return this.pkTable.getIndex(treeNode); 184 } 185 186 public javax.swing.tree.TreeNode getParent () 187 { 188 if (pkTableIsParent) return this.pkTable; 189 else return this.fkTable; 190 } 191 192 public boolean isLeaf () 193 { 194 if (pkTableIsParent) return this.fkTable.isLeaf(); 195 else return this.pkTable.isLeaf(); 196 } 197 198 public String toString() 199 { 200 if (pkTableIsParent) return this.fkTable.toString() + " (Collection)"; 201 else return this.pkTable.toString() + " (Reference)"; 202 } 203 204 205 public Class getPropertySheetClass () 206 { 207 return org.apache.ojb.tools.mapping.reversedb.gui.DBFKRelationPropertySheet.class; 208 } 209 210 private void writeXMLReference(java.io.PrintWriter pw) 211 { 212 pw.println(" <reference-descriptor"); 213 pw.println(" name=\"" + this.getFieldName()+ "\"" ); 214 pw.println(" class-ref=\"" + this.getPKTable().getFQClassName() + "\"" ); 215 pw.println(" auto-retrieve=\"" + this.getAutoRetrieve()+ "\"" ); 216 pw.println(" auto-update=\"" + this.getAutoUpdate() + "\""); 217 pw.println(" auto-delete=\"" + this.getAutoDelete() + "\">"); 218 219 pw.print(" <foreignkey field-ref=\""); 220 java.util.Iterator it = this.getColumnPairIterator(); 221 if (it.hasNext()) pw.print(((DBColumn)((Object [])it.next())[1]).getJavaFieldName()) ; 224 pw.println("\" />"); 225 226 pw.println(" </reference-descriptor>"); 227 } 228 229 private void writeXMLCollection(java.io.PrintWriter pw) 230 { 231 pw.println(" <collection-descriptor"); 232 pw.println(" name=\"" + this.getFieldName() + "\""); 233 pw.println(" element-class-ref=\"" + this.getFKTable().getFQClassName() + "\"" ); 234 pw.println(" auto-retrieve=\"" + this.getAutoRetrieve() + "\"" ); 235 pw.println(" auto-update=\"" + this.getAutoUpdate() + "\"" ); 236 pw.println(" auto-delete=\"" + this.getAutoDelete() + "\">"); 237 pw.print(" <inverse-foreignkey field-ref=\""); 238 java.util.Iterator it = this.getColumnPairIterator(); 239 while (it.hasNext()) pw.print(((DBColumn)((Object [])it.next())[1]).getJavaFieldName() + " "); 240 pw.println("\" />"); 241 pw.println(" </collection-descriptor>"); 242 } 243 244 public String getXML() 245 { 246 java.io.StringWriter sw = new java.io.StringWriter (); 247 writeXML(new java.io.PrintWriter (sw)); 248 return sw.getBuffer().toString(); 249 } 250 251 public void writeXML(java.io.PrintWriter pw) 252 { 253 if (this.isPkTableParent()) 254 { 255 writeXMLCollection(pw); 256 } 257 else 258 { 259 writeXMLReference(pw); 260 } 261 } 262 263 264 public void generateJava (java.io.File aFile, String strHeader, String strFooter) throws java.io.IOException , java.io.FileNotFoundException 265 { 266 throw new UnsupportedOperationException ("Generate Java on DBFKReference is not allowed"); 267 } 268 269 public void setPackage (String packageName) 270 { 271 throw new UnsupportedOperationException ("Set Package on DBFKReference is not allowed"); 272 } 273 274 public String getJavaFieldDefinition() 275 { 276 if (this.isTreeEnabled() 279 && this.getFKTable().isTreeEnabled() 280 && this.getPKTable().isTreeEnabled()) 281 { 282 return " private " + this.getFieldType() + " " + this.getFieldName() + ";"; 283 } 284 else return ""; 285 } 286 287 public String getJavaGetterSetterDefinition() 288 { 289 if (this.isTreeEnabled() 290 && this.getFKTable().isTreeEnabled() 291 && this.getPKTable().isTreeEnabled()) 292 { 293 String strReturn = ""; 294 strReturn = " public " + this.getFieldType() + " get" 295 + this.getFieldName().substring(0,1).toUpperCase() 296 + this.getFieldName().substring(1) + "()" 297 + System.getProperty("line.separator"); 298 299 strReturn += " {" + System.getProperty("line.separator"); 300 strReturn += " return this." + this.getFieldName() + ";" + System.getProperty("line.separator"); 301 strReturn += " }" + System.getProperty("line.separator"); 302 strReturn += " public void set" 303 + this.getFieldName().substring(0,1).toUpperCase() 304 + this.getFieldName().substring(1) + "(" + this.getFieldType() + " param)" 305 + System.getProperty("line.separator"); 306 strReturn += " {" + System.getProperty("line.separator"); 307 strReturn += " this." + this.getFieldName() + " = param;" + System.getProperty("line.separator"); 308 strReturn += " }" + System.getProperty("line.separator"); 309 return strReturn; 310 } 311 else return ""; 312 } 313 314 315 } 316 317 384 | Popular Tags |