1 23 24 29 30 package com.sun.jdo.api.persistence.model.mapping.impl; 31 32 import java.util.*; 33 import java.beans.PropertyVetoException ; 34 35 import org.netbeans.modules.dbschema.TableElement; 36 import org.netbeans.modules.dbschema.ColumnElement; 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.*; 42 import com.sun.jdo.spi.persistence.utility.I18NHelper; 43 44 50 public class MappingTableElementImpl extends MappingMemberElementImpl 51 implements MappingTableElement 52 { 53 private ArrayList _key; private transient ArrayList _keyObjects; private ArrayList _referencingKeys; private String _table; 58 private transient TableElement _tableObject; 61 65 public MappingTableElementImpl () 66 { 67 this((String )null, null); 68 } 69 70 75 public MappingTableElementImpl (String name, 76 MappingClassElement declaringClass) 77 { 78 super(name, declaringClass); 79 } 80 81 86 public MappingTableElementImpl (TableElement table, 87 MappingClassElement declaringClass) throws ModelException 88 { 89 this(table.toString(), declaringClass); 90 91 _table = getName(); 93 } 94 95 97 100 public String getTable () { return _table; } 101 102 106 public void setTable (TableElement table) throws ModelException 107 { 108 String old = getTable(); 109 String newName = table.toString(); 110 111 try 112 { 113 fireVetoableChange(PROP_TABLE, old, newName); 114 _table = newName; 115 firePropertyChange(PROP_TABLE, old, newName); 116 setName(_table); 117 118 _tableObject = null; 121 } 122 catch (PropertyVetoException e) 123 { 124 throw new ModelVetoException(e); 125 } 126 } 127 128 133 public void setName (String name) throws ModelException 134 { 135 super.setName(name); 136 137 if (getTable() == null) 138 _table = name; 139 } 140 141 146 public boolean isEqual (TableElement table) 147 { 148 return ((table != null) ? getTable().equals(table.toString()) : false); 149 } 150 151 153 158 public ArrayList getKey () 159 { 160 if (_key == null) 161 _key = new ArrayList(); 162 163 return _key; 164 } 165 166 173 public void addKeyColumn (ColumnElement column) throws ModelException 174 { 175 if (column != null) 176 { 177 String columnName = NameUtil.getRelativeMemberName( 178 column.getName().getFullName()); 179 180 if (!getKey().contains(columnName)) 181 addKeyColumnInternal(column); 182 else 183 { 184 } 186 } 187 else 188 { 189 throw new ModelException(I18NHelper.getMessage(getMessages(), 190 "mapping.element.null_argument")); } 192 } 193 194 202 protected void addKeyColumnInternal (ColumnElement column) throws ModelException 203 { 204 ArrayList key = getKey(); 205 String columnName = NameUtil.getRelativeMemberName( 206 column.getName().getFullName()); 207 208 try 209 { 210 fireVetoableChange(PROP_KEY_COLUMNS, null, null); 211 key.add(columnName); 212 firePropertyChange(PROP_KEY_COLUMNS, null, null); 213 214 _keyObjects = null; 218 } 219 catch (PropertyVetoException e) 220 { 221 throw new ModelVetoException(e); 222 } 223 } 224 225 232 public void removeKeyColumn (String columnName) throws ModelException 233 { 234 if (columnName != null) 235 { 236 try 237 { 238 fireVetoableChange(PROP_KEY_COLUMNS, null, null); 239 240 if (!getKey().remove(columnName)) 241 { 242 throw new ModelException( 243 I18NHelper.getMessage(getMessages(), 244 "mapping.element.element_not_removed", columnName)); 246 } 247 248 firePropertyChange(PROP_KEY_COLUMNS, null, null); 249 250 _keyObjects = null; 254 } 255 catch (PropertyVetoException e) 256 { 257 throw new ModelVetoException(e); 258 } 259 } 260 } 261 262 264 269 public ArrayList getReferencingKeys () 270 { 271 if (_referencingKeys == null) 272 _referencingKeys = new ArrayList(); 273 274 return _referencingKeys; 275 } 276 277 281 public void addReferencingKey (MappingReferenceKeyElement referencingKey) 282 throws ModelException 283 { 284 try 285 { 286 fireVetoableChange(PROP_REFERENCING_KEYS, null, null); 287 getReferencingKeys().add(referencingKey); 288 firePropertyChange(PROP_REFERENCING_KEYS, null, null); 289 } 290 catch (PropertyVetoException e) 291 { 292 throw new ModelVetoException(e); 293 } 294 } 295 296 301 public void removeReference (MappingTableElement table) 302 throws ModelException 303 { 304 if (table != null) 305 { 306 Iterator keyIterator = getReferencingKeys().iterator(); 307 308 while (keyIterator.hasNext()) 309 { 310 MappingReferenceKeyElement nextKey = 311 (MappingReferenceKeyElement)keyIterator.next(); 312 313 if (nextKey.getTable().equals(table)) 314 { 315 try 316 { 317 fireVetoableChange(PROP_REFERENCING_KEYS, null, null); 318 keyIterator.remove(); 319 firePropertyChange(PROP_REFERENCING_KEYS, null, null); 320 } 321 catch (PropertyVetoException e) 322 { 323 throw new ModelVetoException(e); 324 } 325 } 326 } 327 } 328 else 329 { 330 throw new ModelException(I18NHelper.getMessage(getMessages(), 331 "mapping.element.null_argument")); } 333 } 334 335 337 341 public TableElement getTableObject () 342 { 343 if (_tableObject == null) 344 { 345 String absoluteTableName = NameUtil.getAbsoluteTableName( 346 getDeclaringClass().getDatabaseRoot(), _table); 347 348 _tableObject = TableElement.forName(absoluteTableName); 349 } 350 351 return _tableObject; 352 } 353 354 358 public ArrayList getKeyObjects () 359 { 360 if (_keyObjects == null) 361 { 362 _keyObjects = MappingClassElementImpl.toColumnObjects( 366 getDeclaringClass().getDatabaseRoot(), getKey()); 367 } 368 369 return _keyObjects; 370 } 371 372 374 378 public void setTable (String table) 379 { 380 _table = table; 381 } 382 383 389 public void setKey (ArrayList key) { _key = key; } 390 391 397 public void setReferencingKeys (ArrayList referencingKeys) 398 { 399 _referencingKeys = referencingKeys; 400 } 401 402 404 410 protected void stripSchemaName () 411 { 412 _name = NameUtil.getRelativeTableName(_name); 414 415 _table = NameUtil.getRelativeTableName(_table); 417 418 if (_referencingKeys != null) 422 { 423 Iterator i = _referencingKeys.iterator(); 424 425 while (i.hasNext()) 426 { 427 MappingReferenceKeyElementImpl refKey = 428 (MappingReferenceKeyElementImpl)i.next(); 429 430 refKey.stripSchemaName(); 431 } 432 } 433 434 if (_key != null) 436 { 437 ListIterator i = _key.listIterator(); 441 442 while (i.hasNext()) 443 i.set(NameUtil.getRelativeMemberName((String )i.next())); 444 } 445 } 446 } 447 | Popular Tags |