FileDocCategorySizeDatePackage
MappingTableElementImpl.javaAPI DocGlassfish v2 API14450Fri May 04 22:34:44 BST 2007com.sun.jdo.api.persistence.model.mapping.impl

MappingTableElementImpl

public class MappingTableElementImpl extends MappingMemberElementImpl implements MappingTableElement
author
Mark Munro
author
Rochelle Raccah
version
%I%

Fields Summary
private ArrayList
_key
private transient ArrayList
_keyObjects
private ArrayList
_referencingKeys
private String
_table
private transient org.netbeans.modules.dbschema.TableElement
_tableObject
Constructors Summary
public MappingTableElementImpl()
Create new MappingTableElementImpl with no corresponding name or declaring class. This constructor should only be used for cloning and archiving.

		this((String)null, null);
	
public MappingTableElementImpl(String name, MappingClassElement declaringClass)
Create new MappingTableElementImpl with the corresponding name and declaring class.

param
name the name of the element
param
declaringClass the class to attach to

		super(name, declaringClass);
	
public MappingTableElementImpl(org.netbeans.modules.dbschema.TableElement table, MappingClassElement declaringClass)
Creates new MappingTableElementImpl with a corresponding table and declaring class.

param
table table element to be used by the mapping table.
param
declaringClass the class to attach to

		this(table.toString(), declaringClass);

		// don't use setTable so as not to fire property change events
		_table = getName();
	
Methods Summary
public voidaddKeyColumn(org.netbeans.modules.dbschema.ColumnElement column)
Adds a column to the primary key of columns in this mapping table. This method should only be used to manipulate the key columns of the primary table. The secondary table key columns should be manipulated using MappingReferenceKeyElement methods for pairs.

param
column column element to be added
exception
ModelException if impossible

		if (column != null)
		{
			String columnName = NameUtil.getRelativeMemberName(
				column.getName().getFullName());

			if (!getKey().contains(columnName))
				addKeyColumnInternal(column);
			else
			{
				// this part was blank -- do we want an error or skip here?
			}
		}
		else
		{
			throw new ModelException(I18NHelper.getMessage(getMessages(), 
				"mapping.element.null_argument"));				// NOI18N
		}
	
protected voidaddKeyColumnInternal(org.netbeans.modules.dbschema.ColumnElement column)
Adds a column to the primary key of columns in this mapping table. This method is used internally to manipulate primary key columns that have passed the null and duplicate tests in addKeyColumn and secondary table key columns when pairs are being set up and ignoring duplicates is done at the pair level.

param
column column element to be added
exception
ModelException if impossible

		ArrayList key = getKey();
		String columnName = NameUtil.getRelativeMemberName(
			column.getName().getFullName());

		try
		{
			fireVetoableChange(PROP_KEY_COLUMNS, null, null);
			key.add(columnName);
			firePropertyChange(PROP_KEY_COLUMNS, null, null);

			// sync up runtime's object list too
			//@olsen: rather clear objects instead of maintaining them
			//getKeyObjects().add(column);
			_keyObjects = null;
		}
		catch (PropertyVetoException e)
		{
			throw new ModelVetoException(e);
		}
	
public voidaddReferencingKey(MappingReferenceKeyElement referencingKey)
Adds a referencing key to the list of keys in this mapping table.

param
referencingKey referencing key element to be added
exception
ModelException if impossible

		try
		{
			fireVetoableChange(PROP_REFERENCING_KEYS, null, null);
			getReferencingKeys().add(referencingKey);
			firePropertyChange(PROP_REFERENCING_KEYS, null, null);
		}
		catch (PropertyVetoException e)
		{
			throw new ModelVetoException(e);
		}
	
public java.util.ArrayListgetKey()
Returns the list of column names in the primary key for this mapping table.

return
the names of the columns in the primary key for this mapping table

		if (_key == null)
			_key = new ArrayList();

		return _key;
	
public java.util.ArrayListgetKeyObjects()
Returns the list of columns (ColumnElements) in the primary key for this mapping table. This method should only be used by the runtime.

return
the column elements in the primary key for this mapping table

		if (_keyObjects == null)
		{
			//@olsen: calculate the key objects based on
			//        the key names as stored in _key
			//_keyObjects = new ArrayList();
			_keyObjects = MappingClassElementImpl.toColumnObjects(
				getDeclaringClass().getDatabaseRoot(), getKey());
		}

		return _keyObjects;
	
public java.util.ArrayListgetReferencingKeys()
Returns the list of keys (MappingReferenceKeyElements) for this mapping table. There will be keys for foreign keys and "fake" foreign keys.

return
the reference key elements for this mapping table

		if (_referencingKeys == null)
			_referencingKeys = new ArrayList();

		return _referencingKeys;
	
public java.lang.StringgetTable()
Returns the name of the table element used by this mapping table.

return
the table name for this mapping table

 return _table; 
public org.netbeans.modules.dbschema.TableElementgetTableObject()
Returns the table element (TableElement) used by this mapping table. This method should only be used by the runtime.

return
the table element for this mapping table

		if (_tableObject == null)
		{
			String absoluteTableName = NameUtil.getAbsoluteTableName(
				getDeclaringClass().getDatabaseRoot(), _table);

			_tableObject = TableElement.forName(absoluteTableName);
		}

		return _tableObject;
	
public booleanisEqual(org.netbeans.modules.dbschema.TableElement table)
Returns true if the table element used by this mapping table is equal to the supplied table.

return
true if table elements are equal, false otherwise.

		return ((table != null) ? getTable().equals(table.toString()) : false);
	
public voidremoveKeyColumn(java.lang.String columnName)
Removes a column from the primary key of columns in this mapping table. This method should only be used to manipulate the key columns of the primary table. The secondary table key columns should be manipulated using MappingReferenceKeyElement methods for pairs.

param
columnName the relative name of the column to be removed
exception
ModelException if impossible

		if (columnName != null)
		{
			try
			{
				fireVetoableChange(PROP_KEY_COLUMNS, null, null);

				if (!getKey().remove(columnName))
				{
					throw new ModelException(
						I18NHelper.getMessage(getMessages(), 
						"mapping.element.element_not_removed",		// NOI18N
						columnName));
				}

				firePropertyChange(PROP_KEY_COLUMNS, null, null);

				// sync up runtime's object list too
				//@olsen: rather clear objects instead of maintaining them
				//getKeyObjects().remove(column);
				_keyObjects = null;
			}
			catch (PropertyVetoException e)
			{
				throw new ModelVetoException(e);
			}
		}
	
public voidremoveReference(MappingTableElement table)
Removes the referencing key for the supplied table element from list of keys in this mapping table.

param
table mapping table element for which to remove referencing keys
exception
ModelException if impossible

		if (table != null)
		{
			Iterator keyIterator = getReferencingKeys().iterator();

			while (keyIterator.hasNext())
			{
				MappingReferenceKeyElement nextKey = 
					(MappingReferenceKeyElement)keyIterator.next();

				if (nextKey.getTable().equals(table))
				{
					try
					{
						fireVetoableChange(PROP_REFERENCING_KEYS, null, null);
						keyIterator.remove();
						firePropertyChange(PROP_REFERENCING_KEYS, null, null);
					}
					catch (PropertyVetoException e)
					{
						throw new ModelVetoException(e);
					}
				}
			}
		}
		else
		{
			throw new ModelException(I18NHelper.getMessage(getMessages(), 
				"mapping.element.null_argument"));					// NOI18N
		}
	
public voidsetKey(java.util.ArrayList key)
Set the list of column names in the primary key for this mapping table. This method should only be used internally and for cloning and archiving.

param
key the list of names of the columns in the primary key for this mapping table

 _key = key; 
public voidsetName(java.lang.String name)
Override method in MappingElementImpl to set the _table variable if necessary (used for unarchiving).

param
name the name
exception
ModelException if impossible

		super.setName(name);

		if (getTable() == null)
			_table = name;
	
public voidsetReferencingKeys(java.util.ArrayList referencingKeys)
Set the list of keys (MappingReferenceKeyElements) for this mapping table. This method should only be used internally and for cloning and archiving.

param
referencingKeys the list of reference key elements for this mapping table

		_referencingKeys = referencingKeys;
	
public voidsetTable(java.lang.String table)
Set the name of the table element used by this mapping table. This method should only be used internally and for cloning and archiving.

param
table the table name for this mapping table

		_table = table;
	
public voidsetTable(org.netbeans.modules.dbschema.TableElement table)
Set the table element for this mapping table to the supplied table.

param
table table element to be used by the mapping table.
exception
ModelException if impossible

		String old = getTable();
		String newName = table.toString();

		try
		{
			fireVetoableChange(PROP_TABLE, old, newName);
			_table = newName;
			firePropertyChange(PROP_TABLE, old, newName);
			setName(_table);

			// sync up runtime's object too: force next
			// access to getTableObject to recompute it
			_tableObject = null;
		}
		catch (PropertyVetoException e)
		{
			throw new ModelVetoException(e);
		}
	
protected voidstripSchemaName()
Boston to Pilsen conversion. This method converts the absolute db element names to relative names. This affects the name of the MappingTableElement itself and the column names stored in _keys. The method is recursively called for all MappingReferenceKeyElements attached to this MappingTableElement.

		// handle _name
		_name = NameUtil.getRelativeTableName(_name);

		// handle _table
		_table = NameUtil.getRelativeTableName(_table);

		// handle _referencingKeys
		// call method stripSchemaName on the MappingReferenceKeyElementImpl 
		// objects
		if (_referencingKeys != null)
		{
			Iterator i = _referencingKeys.iterator(); 

			while (i.hasNext())
			{
				MappingReferenceKeyElementImpl refKey = 
					(MappingReferenceKeyElementImpl)i.next();

				refKey.stripSchemaName();
			}
		}
		
		// handle _key
		if (_key != null)
		{
			// Use ListIterator here, because I want to replace the value 
			// stored in the ArrayList.  The ListIterator returned by 
			// ArrayList.listIterator() supports the set method.
			ListIterator i = _key.listIterator(); 

			while (i.hasNext())
				i.set(NameUtil.getRelativeMemberName((String)i.next()));
		}