1 23 24 29 30 package com.sun.jdo.api.persistence.model.mapping.impl; 31 32 import java.util.ArrayList ; 33 import java.util.ListIterator ; 34 import java.beans.PropertyVetoException ; 35 36 import org.netbeans.modules.dbschema.*; 37 import org.netbeans.modules.dbschema.util.NameUtil; 38 39 import com.sun.jdo.api.persistence.model.ModelException; 40 import com.sun.jdo.api.persistence.model.ModelVetoException; 41 import com.sun.jdo.api.persistence.model.mapping.MappingTableElement; 42 import com.sun.jdo.api.persistence.model.mapping.MappingReferenceKeyElement; 43 import com.sun.jdo.spi.persistence.utility.I18NHelper; 44 45 51 public class MappingReferenceKeyElementImpl extends MappingMemberElementImpl 52 implements MappingReferenceKeyElement 53 { 54 private ArrayList _referencingKey; private MappingTableElement _table; 56 57 60 public MappingReferenceKeyElementImpl () 61 { 62 this((String )null); 63 } 64 65 68 public MappingReferenceKeyElementImpl (String name) 69 { 70 super(name, null); 71 } 72 73 77 public MappingReferenceKeyElementImpl (MappingTableElement table) 78 throws ModelException 79 { 80 super(table.getName(), table.getDeclaringClass()); 81 setTableInternal(table); 82 } 83 84 87 public String getKeyName () { return getName(); } 88 89 93 public void setKeyName (String name) throws ModelException 94 { 95 setName(name.toString()); 96 } 97 98 100 103 public MappingTableElement getTable () { return _table; } 104 105 109 public void setTable (MappingTableElement table) throws ModelException 110 { 111 MappingTableElement old = getTable(); 112 113 try 114 { 115 fireVetoableChange(PROP_TABLE, old, table); 116 setTableInternal(table); 117 firePropertyChange(PROP_TABLE, old, table); 118 } 119 catch (PropertyVetoException e) 120 { 121 throw new ModelVetoException(e); 122 } 123 } 124 125 130 private void setTableInternal (MappingTableElement table) 131 throws ModelException 132 { 133 if (table == null) 134 { 135 throw new ModelException(I18NHelper.getMessage(getMessages(), 136 "mapping.element.null_argument")); } 138 139 _table = table; 140 141 if (null == getDeclaringClass()) 142 _declaringClass = table.getDeclaringClass(); 143 144 if (null == getName()) 145 _name = table.getName(); 146 } 147 148 154 public TableElement getDeclaringTable () 155 { 156 ArrayList locals = getReferencingKey(); 157 158 if ((locals != null) && (locals.size() > 0)) 159 { 160 String absoluteName = NameUtil.getAbsoluteMemberName( 161 getDeclaringClass().getDatabaseRoot(), 162 locals.get(0).toString()); 163 164 return TableElement.forName(NameUtil.getTableName(absoluteName)); 165 } 166 167 return null; 168 } 169 170 176 public void setDeclaringTable (TableElement tableElement) 177 { 178 throw new UnsupportedOperationException (); 179 } 180 181 187 public TableElement getReferencedTable () 188 { 189 ColumnPairElement[] columnPairs = getColumnPairs(); 190 191 if ((columnPairs != null) && (columnPairs.length > 0)) 192 return columnPairs[0].getReferencedColumn().getDeclaringTable(); 193 194 return null; 195 } 196 197 199 204 private ArrayList getReferencingKey () 205 { 206 if (_referencingKey == null) 207 _referencingKey = new ArrayList (); 208 209 return _referencingKey; 210 } 211 212 215 public ArrayList getColumnPairNames () 216 { 217 ArrayList locals = getReferencingKey(); 218 ArrayList foreigns = getTable().getKey(); 219 int i, count = ((locals != null) ? locals.size() : 0); 220 ArrayList pairs = new ArrayList (); 221 222 for (i = 0; i < count; i++) 223 pairs.add(locals.get(i) + ";" + foreigns.get(i)); 225 return pairs; 226 } 227 228 233 private int getIndexOfColumnPair (String searchPairName) 234 { 235 ArrayList myPairs = getColumnPairNames(); 236 int count = ((myPairs != null) ? myPairs.size() : 0); 237 238 if (count > 0) 239 { 240 int i; 241 242 for (i = 0; i < count; i++) 243 { 244 if (myPairs.get(i).equals(searchPairName)) 245 return i; 246 } 247 } 248 249 return -1; 250 } 251 252 258 private void addKeyColumn (ColumnElement column) throws ModelException 259 { 260 ArrayList referencingKey = getReferencingKey(); 261 String columnName = NameUtil.getRelativeMemberName( 262 column.getName().getFullName()); 263 264 try 265 { 266 fireVetoableChange(PROP_KEY_COLUMNS, null, null); 267 referencingKey.add(columnName); 268 firePropertyChange(PROP_KEY_COLUMNS, null, null); 269 } 270 catch (PropertyVetoException e) 271 { 272 throw new ModelVetoException(e); 273 } 274 } 275 276 282 public ColumnElement[] getLocalColumns () 283 { 284 ColumnPairElement[] columnPairs = getColumnPairs(); 285 int i, count = ((columnPairs != null) ? columnPairs.length : 0); 286 ColumnElement[] columns = new ColumnElement[count]; 287 288 for (i = 0; i < count ; i++) 289 columns[i] = columnPairs[i].getLocalColumn(); 290 291 return columns; 292 } 293 294 300 public ColumnElement[] getReferencedColumns () 301 { 302 ColumnPairElement[] columnPairs = getColumnPairs(); 303 int i, count = ((columnPairs != null) ? columnPairs.length : 0); 304 ColumnElement[] columns = new ColumnElement[count]; 305 306 for (i = 0; i < count ; i++) 307 columns[i] = columnPairs[i].getReferencedColumn(); 308 309 return columns; 310 } 311 312 317 public void removeColumnPair (String pairName) throws ModelException 318 { 319 ArrayList pairNames = new ArrayList (1); 320 321 pairNames.add(pairName); 322 removeColumnPairs(pairNames); 323 } 324 325 330 public void removeColumnPairs (ArrayList pairNames) throws ModelException 331 { 332 ArrayList refKey = getReferencingKey(); 333 ArrayList key = getTable().getKey(); 334 int i, count = ((pairNames != null) ? pairNames.size() : 0); 335 336 for (i = 0; i < count ; i++) 337 { 338 String pairName = (String )pairNames.get(i); 339 int index = getIndexOfColumnPair(pairName); 340 341 if (pairName != null) 342 { 343 try 344 { 345 Object remove1 = null, remove2 = null; 346 347 fireVetoableChange(PROP_KEY_COLUMNS, null, null); 348 349 remove1 = key.remove(index); 350 remove2 = refKey.remove(index); 351 352 if ((remove1 == null) || (remove2 == null)) 353 { 354 if (remove1 != null) 356 key.add(index, remove1); 357 else if (remove2 != null) 358 refKey.add(index, remove2); 359 360 throw new ModelException(I18NHelper.getMessage( 361 getMessages(), 362 "mapping.element.element_not_removed", pairName)); 364 } 365 366 firePropertyChange(PROP_KEY_COLUMNS, null, null); 367 } 368 catch (PropertyVetoException e) 369 { 370 throw new ModelVetoException(e); 371 } 372 } 373 } 374 } 375 376 378 382 public void addColumnPair (ColumnPairElement pair) throws ModelException 383 { 384 addColumnPairs(new ColumnPairElement[]{pair}); 385 } 386 387 391 public void addColumnPairs (ColumnPairElement[] pairs) throws ModelException 392 { 393 MappingTableElementImpl table = (MappingTableElementImpl)getTable(); 394 int i, count = ((pairs != null) ? pairs.length : 0); 395 396 for (i = 0; i < count; i++) 397 { 398 ColumnPairElement pair = (ColumnPairElement)pairs[i]; 399 400 if (pair != null) 401 { 402 if (getIndexOfColumnPair(NameUtil.getRelativeMemberName( 405 pair.getName().getFullName())) == -1) 406 { 407 table.addKeyColumnInternal(pair.getReferencedColumn()); 408 addKeyColumn(pair.getLocalColumn()); 409 } 410 else 411 { 412 } 414 } 415 else 416 { 417 throw new ModelException(I18NHelper.getMessage(getMessages(), 418 "mapping.element.null_argument")); } 420 } 421 } 422 423 427 public void removeColumnPair (ColumnPairElement pair) throws ModelException 428 { 429 removeColumnPairs(new ColumnPairElement[]{pair}); 430 } 431 432 436 public void removeColumnPairs (ColumnPairElement[] pairs) 437 throws ModelException 438 { 439 ArrayList pairNames = new ArrayList (); 440 int i, count = ((pairs != null) ? pairs.length : 0); 441 442 for (i = 0; i < count ; i++) 443 { 444 ColumnPairElement pair = (ColumnPairElement)pairs[i]; 445 446 pairNames.add(NameUtil.getRelativeMemberName( 447 pair.getName().getFullName())); 448 } 449 450 removeColumnPairs(pairNames); 451 } 452 453 458 public void setColumnPairs (ColumnPairElement[] pairs) throws ModelException 459 { 460 removeColumnPairs(getColumnPairNames()); addColumnPairs(pairs); } 463 464 467 public ColumnPairElement[] getColumnPairs () 468 { 469 ArrayList pairNames = getColumnPairNames(); 470 TableElement table = getDeclaringTable(); 471 int i, count = ((pairNames != null) ? pairNames.size() : 0); 472 ColumnPairElement[] pairs = new ColumnPairElement[count]; 473 String databaseRoot = getDeclaringClass().getDatabaseRoot(); 474 475 for (i = 0; i < count; i++) 476 { 477 String absoluteName = NameUtil.getAbsoluteMemberName( 478 databaseRoot, (String )pairNames.get(i)); 479 480 pairs[i] = (ColumnPairElement)table.getMember( 481 DBIdentifier.create(absoluteName)); 482 } 483 484 return pairs; 485 } 486 487 491 public ColumnPairElement getColumnPair (DBIdentifier name) 492 { 493 ColumnPairElement[] myPairs = getColumnPairs(); 494 int count = ((myPairs != null) ? myPairs.length : 0); 495 String databaseRoot = getDeclaringClass().getDatabaseRoot(); 496 497 if (count > 0) 498 { 499 String absoluteTableName = NameUtil.getAbsoluteTableName( 500 databaseRoot, getTable().getName()); 501 ColumnPairElement searchPair = (ColumnPairElement) 502 TableElement.forName(absoluteTableName).getMember(name); 503 int i; 504 505 for (i = 0; i < count; i++) 506 { 507 if (myPairs[i].equals(searchPair)) 508 return searchPair; 509 } 510 } 511 512 return null; 513 } 514 515 517 522 public void setReferencingKey (ArrayList referencingKey) 523 { 524 _referencingKey = referencingKey; 525 } 526 527 529 533 protected void stripSchemaName () 534 { 535 _name = NameUtil.getRelativeTableName(_name); 538 539 if (_referencingKey != null) 541 { 542 ListIterator i = _referencingKey.listIterator(); 546 547 while (i.hasNext()) 548 i.set(NameUtil.getRelativeMemberName((String )i.next())); 549 } 550 } 551 } 552 | Popular Tags |