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

MappingReferenceKeyElementImpl

public class MappingReferenceKeyElementImpl extends MappingMemberElementImpl implements com.sun.jdo.api.persistence.model.mapping.MappingReferenceKeyElement
author
Mark Munro
author
Rochelle Raccah
version
%I%

Fields Summary
private ArrayList
_referencingKey
private com.sun.jdo.api.persistence.model.mapping.MappingTableElement
_table
Constructors Summary
public MappingReferenceKeyElementImpl()
Create new MappingReferenceKeyElementImpl with no corresponding name. This constructor should only be used for cloning and archiving.

		this((String)null);
	
public MappingReferenceKeyElementImpl(String name)
Creates new MappingReferenceKeyElementImpl with the corresponding name

param
name the name of the element

		super(name, null);
	
public MappingReferenceKeyElementImpl(com.sun.jdo.api.persistence.model.mapping.MappingTableElement table)
Creates new MappingReferenceKeyElementImpl with a corresponding mapping table.

param
table mapping table element to be used with this key.

		super(table.getName(), table.getDeclaringClass());
		setTableInternal(table);
	
Methods Summary
public voidaddColumnPair(ColumnPairElement pair)
Add a new column pair to the holder.

param
pair the pair to add
throws
ModelException if impossible

		addColumnPairs(new ColumnPairElement[]{pair});
	
public voidaddColumnPairs(ColumnPairElement[] pairs)
Add some new column pairs to the holder.

param
pairs the column pairs to add
throws
ModelException if impossible

		MappingTableElementImpl table = (MappingTableElementImpl)getTable();
		int i, count = ((pairs != null) ? pairs.length : 0);

		for (i = 0; i < count; i++)
		{
			ColumnPairElement pair = (ColumnPairElement)pairs[i];

			if (pair != null)
			{
				// check if entire pair matches
				// OK only add it if it has not been added before.
				if (getIndexOfColumnPair(NameUtil.getRelativeMemberName(
					pair.getName().getFullName())) == -1)
				{
					table.addKeyColumnInternal(pair.getReferencedColumn());
					addKeyColumn(pair.getLocalColumn());
				}
				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
			}
		}
	
private voidaddKeyColumn(ColumnElement column)
Adds a column to the list of key columns in this referencing key. This method is only called privately from addColumnPairs and assumes that the column is not null.

param
column column element to be added
exception
ModelException if impossible

		ArrayList referencingKey = getReferencingKey();
		String columnName = NameUtil.getRelativeMemberName(
			column.getName().getFullName());

		try
		{
			fireVetoableChange(PROP_KEY_COLUMNS, null, null);
			referencingKey.add(columnName);
			firePropertyChange(PROP_KEY_COLUMNS, null, null);
		}
		catch (PropertyVetoException e)
		{
			throw new ModelVetoException(e);
		}
	
public ColumnPairElementgetColumnPair(DBIdentifier name)
Find a column pair by name.

param
name the name of the column pair for which to look
return
the column pair or null if not found

		ColumnPairElement[] myPairs = getColumnPairs();
		int count = ((myPairs != null) ? myPairs.length : 0);
		String databaseRoot = getDeclaringClass().getDatabaseRoot();

		if (count > 0)
		{
			String absoluteTableName = NameUtil.getAbsoluteTableName(
				databaseRoot, getTable().getName());
			ColumnPairElement searchPair = (ColumnPairElement)
				TableElement.forName(absoluteTableName).getMember(name);
			int i;

			for (i = 0; i < count; i++)
			{
				if (myPairs[i].equals(searchPair))
					return searchPair;
			}
		}

		return null;
	
public java.util.ArrayListgetColumnPairNames()
Returns the list of relative column pair names in this referencing key.

return
the names of the column pairs in this referencing key

		ArrayList locals = getReferencingKey();
		ArrayList foreigns = getTable().getKey();
		int i, count = ((locals != null) ? locals.size() : 0);
		ArrayList pairs = new ArrayList();

		for (i = 0; i < count; i++)
			pairs.add(locals.get(i) + ";" + foreigns.get(i));	// NOI18N

		return pairs;
	
public ColumnPairElement[]getColumnPairs()
Get all column pairs in this holder.

return
the column pairs

		ArrayList pairNames = getColumnPairNames();
		TableElement table = getDeclaringTable();
		int i, count = ((pairNames != null) ? pairNames.size() : 0);
		ColumnPairElement[] pairs = new ColumnPairElement[count];
		String databaseRoot = getDeclaringClass().getDatabaseRoot();

		for (i = 0; i < count; i++)
		{
			String absoluteName = NameUtil.getAbsoluteMemberName(
				databaseRoot, (String)pairNames.get(i));

			pairs[i] = (ColumnPairElement)table.getMember(
				DBIdentifier.create(absoluteName));
		}

		return pairs;
	
public TableElementgetDeclaringTable()
Get the declaring table. This method is provided as part of the implementation of the ReferenceKey interface but should only be used when a ReferenceKey object is used or by the runtime.

return
the table that owns this reference key element, or null if the element is not attached to any table

		ArrayList locals = getReferencingKey();

		if ((locals != null) && (locals.size() > 0))
		{
			String absoluteName = NameUtil.getAbsoluteMemberName(
				getDeclaringClass().getDatabaseRoot(), 
				locals.get(0).toString());

			return TableElement.forName(NameUtil.getTableName(absoluteName));
		}

		return null;
	
private intgetIndexOfColumnPair(java.lang.String searchPairName)
Convenience method which takes a pair and returns its index.

param
searchPairName the relative name of the column pair for which to look
return
the index of the column pair or -1 if not found

		ArrayList myPairs = getColumnPairNames();
		int count = ((myPairs != null) ? myPairs.size() : 0);

		if (count > 0)
		{
			int i;

			for (i = 0; i < count; i++)
			{
				if (myPairs.get(i).equals(searchPairName))
					return i;
			}
		}

		return -1;
	
public java.lang.StringgetKeyName()
Get the name of this element.

return
the name

 return getName(); 
public ColumnElement[]getLocalColumns()
Get all local columns in this reference key. This method is provided as part of the implementation of the ReferenceKey interface but should only be used when a ReferenceKey object is used or by the runtime.

return
the columns

		ColumnPairElement[] columnPairs = getColumnPairs();
		int i, count = ((columnPairs != null) ? columnPairs.length : 0);
		ColumnElement[] columns = new ColumnElement[count];

		for (i = 0; i < count ; i++)
			columns[i] = columnPairs[i].getLocalColumn();

		return columns;
	
public ColumnElement[]getReferencedColumns()
Get all referenced columns in this reference key. This method is provided as part of the implementation of the ReferenceKey interface but should only be used when a ReferenceKey object is used or by the runtime.

return
the columns

		ColumnPairElement[] columnPairs = getColumnPairs();
		int i, count = ((columnPairs != null) ? columnPairs.length : 0);
		ColumnElement[] columns = new ColumnElement[count];

		for (i = 0; i < count ; i++)
			columns[i] = columnPairs[i].getReferencedColumn();

		return columns;
	
public TableElementgetReferencedTable()
Get the referenced table of the reference key. This method is provided as part of the implementation of the ReferenceKey interface but should only be used when a ReferenceKey object is used or by the runtime.

return
the referenced table

		ColumnPairElement[] columnPairs = getColumnPairs();

		if ((columnPairs != null) && (columnPairs.length > 0))
			return columnPairs[0].getReferencedColumn().getDeclaringTable();

		return null;
	
private java.util.ArrayListgetReferencingKey()
Returns the list of key column names in this referencing key. This method is private since API users should call the getColumnPairNames method.

return
the names of the columns in this referencing key

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

		return _referencingKey;
	
public com.sun.jdo.api.persistence.model.mapping.MappingTableElementgetTable()
Returns the mapping table element for this referencing key.

return
the meta data table for this referencing key

 return _table; 
public voidremoveColumnPair(java.lang.String pairName)
Remove a column pair from the holder. This method can be used to remove a pair by name when it cannot be resolved to an actual pair.

param
pairName the relative name of the column pair to remove
throws
ModelException if impossible

		ArrayList pairNames = new ArrayList(1);

		pairNames.add(pairName);
		removeColumnPairs(pairNames);
	
public voidremoveColumnPair(ColumnPairElement pair)
Remove a column pair from the holder.

param
pair the column pair to remove
throws
ModelException if impossible

		removeColumnPairs(new ColumnPairElement[]{pair});
	
public voidremoveColumnPairs(java.util.ArrayList pairNames)
Remove some column pairs from the holder. This method can be used to remove pairs by name when they cannot be resolved to actual pairs.

param
pairNames the relative names of the column pairs to remove
throws
ModelException if impossible

		ArrayList refKey = getReferencingKey();
		ArrayList key = getTable().getKey();
		int i, count = ((pairNames != null) ? pairNames.size() : 0);

		for (i = 0; i < count ; i++)
		{
			String pairName = (String)pairNames.get(i);
			int index = getIndexOfColumnPair(pairName);

			if (pairName != null)
			{
				try
				{
					Object remove1 = null, remove2 = null;

					fireVetoableChange(PROP_KEY_COLUMNS, null, null);

					remove1 = key.remove(index);
					remove2 = refKey.remove(index);

					if ((remove1 == null) || (remove2 == null))
					{
						// if only 1 failed, put the other one back
						if (remove1 != null)
							key.add(index, remove1);
						else if (remove2 != null)
							refKey.add(index, remove2);

						throw new ModelException(I18NHelper.getMessage(
							getMessages(), 
							"mapping.element.element_not_removed", 		// NOI18N
							pairName));
					}

					firePropertyChange(PROP_KEY_COLUMNS, null, null);
				}
				catch (PropertyVetoException e)
				{
					throw new ModelVetoException(e);
				}
			}
		}
	
public voidremoveColumnPairs(ColumnPairElement[] pairs)
Remove some column pairs from the holder.

param
pairs the column pairs to remove
throws
ModelException if impossible

		ArrayList pairNames = new ArrayList();
		int i, count = ((pairs != null) ? pairs.length : 0);

		for (i = 0; i < count ; i++)
		{
			ColumnPairElement pair = (ColumnPairElement)pairs[i];

			pairNames.add(NameUtil.getRelativeMemberName(
				pair.getName().getFullName()));
		}

		removeColumnPairs(pairNames);
	
public voidsetColumnPairs(ColumnPairElement[] pairs)
Set the column pairs for this holder. Previous column pairs are removed.

param
pairs the new column pairs
throws
ModelException if impossible

		removeColumnPairs(getColumnPairNames());	// remove the old ones
		addColumnPairs(pairs);						// add the new ones
	
public voidsetDeclaringTable(TableElement tableElement)
Set the mapping table for this referencing key to the mapping table based on the name of the supplied table. This method is provided as part of the implementation of the ReferenceKey interface but should only be used when a ReferenceKey object is used or by the runtime.

param
table table element to be used with this key.

		throw new UnsupportedOperationException();
	
public voidsetKeyName(java.lang.String name)
Set the name of this element.

param
name the name
throws
ModelException if impossible

		setName(name.toString());
	
public voidsetReferencingKey(java.util.ArrayList referencingKey)
Set the list of of key column names in this referencing key. This method should only be used internally and for cloning and archiving.

param
referencingKey the list of names of the columns in this referencing key

		_referencingKey = referencingKey;
	
public voidsetTable(com.sun.jdo.api.persistence.model.mapping.MappingTableElement table)
Set the mapping table for this referencing key to the supplied table.

param
table mapping table element to be used with this key.
exception
ModelException if impossible

		MappingTableElement old = getTable();

		try
		{
			fireVetoableChange(PROP_TABLE, old, table);
			setTableInternal(table);
			firePropertyChange(PROP_TABLE, old, table);
		}
		catch (PropertyVetoException e)
		{
			throw new ModelVetoException(e);
		}
	
private voidsetTableInternal(com.sun.jdo.api.persistence.model.mapping.MappingTableElement table)
Set the mapping table for this referencing key to the supplied table without firing any property change events.

param
table mapping table element to be used with this key.
exception
ModelException if impossible

		if (table == null)
		{
			throw new ModelException(I18NHelper.getMessage(getMessages(), 
				"mapping.element.null_argument"));					// NOI18N
		}

		_table = table;

		if (null == getDeclaringClass())
			_declaringClass = table.getDeclaringClass();

		if (null == getName())
			_name = table.getName();
	
protected voidstripSchemaName()
Boston to Pilsen conversion. This method converts the name of this MappingReferenceKeyElement and the absolute column names stored in _referencingKey to relative names.

		// handle _name (use getRelativeTableName since the name is derived 
		// from the name of the participating table)
		_name = NameUtil.getRelativeTableName(_name);

		// handle _referencingKey
		if (_referencingKey != 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 = _referencingKey.listIterator(); 

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