Methods Summary |
---|
public void | addField(MappingFieldElement field)Adds a field to the list of fields in this mapping class.
ArrayList fields = getFields();
if (!fields.contains(field))
{
try
{
fireVetoableChange(PROP_FIELDS, null, null);
fields.add(field);
firePropertyChange(PROP_FIELDS, null, null);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
}
|
public MappingReferenceKeyElement | addSecondaryTable(MappingTableElement parentTable, TableElement table)Adds a reference to the supplied table as a secondary table for this
mapping class. It creates a MappingReferenceKeyElement for the supplied
primary/secondary table pair.
ArrayList tables = getTables();
if ((parentTable == null) || (table == null))
{
throw new ModelException(I18NHelper.getMessage(getMessages(),
"mapping.element.null_argument")); // NOI18N
}
else if (!tables.contains(parentTable))
{
throw new ModelException(I18NHelper.getMessage(getMessages(),
"mapping.table.parent_table_not_found", // NOI18N
parentTable.getTable()));
}
else
{
// Check the parent table's reference keys to make sure that this
// secondary table has not already been added to this parent table.
// If it has, throw an exception
Iterator iterator = parentTable.getReferencingKeys().iterator();
MappingTableElement mappingTable =
new MappingTableElementImpl(table, this);
MappingReferenceKeyElement key =
new MappingReferenceKeyElementImpl(mappingTable);
while (iterator.hasNext())
{
MappingTableElement compareTable =
((MappingReferenceKeyElement)iterator.next()).getTable();
if (compareTable.isEqual(table))
{
throw new ModelException(I18NHelper.getMessage(
getMessages(),
"mapping.table.secondary_table_defined", // NOI18N
new Object[]{table, parentTable.getTable()}));
}
}
try
{
fireVetoableChange(PROP_TABLES, null, null);
parentTable.addReferencingKey(key);
tables.add(mappingTable);
firePropertyChange(PROP_TABLES, null, null);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
return key;
}
|
public void | addTable(TableElement table)Convenience method which accepts a table element and attempts to add
it as either a primary or secondary table depending on the existing list
of tables and the foreign keys for the table.
if (table != null)
{
ArrayList tables = getTables();
// If the table list is empty, this should be the primary table
if (tables.isEmpty())
setPrimaryTable(table);
else
{
HashMap newSecondaryTables = new HashMap();
Iterator iterator = tables.iterator();
boolean found = false;
// If this table has already been added just skip it and return
while (iterator.hasNext())
if (((MappingTableElement)iterator.next()).isEqual(table))
return;
// Add the table as a secondary table as long as there are
// relevant fks setup. Otherwise, throw an exception
iterator = tables.iterator();
while (iterator.hasNext())
{
MappingTableElement mappingTable =
(MappingTableElement)iterator.next();
String absoluteTableName = NameUtil.getAbsoluteTableName(
_databaseRoot, mappingTable.getTable());
ForeignKeyElement[] foreignKeys = TableElement.forName(
absoluteTableName).getForeignKeys();
int i, count =
((foreignKeys != null) ? foreignKeys.length : 0);
for (i = 0; i < count; i++)
{
ForeignKeyElement fk = foreignKeys[i];
if (table == fk.getReferencedTable())
{
// store it so it can be added after we finish
// iterating the array (can't now because of
// concurrent modification restrictions)
newSecondaryTables.put(mappingTable, fk);
found = true;
}
}
}
if (found) // add the secondary tables now
{
iterator = newSecondaryTables.keySet().iterator();
while (iterator.hasNext())
{
MappingTableElement mappingTable =
(MappingTableElement)iterator.next();
MappingReferenceKeyElement refKey =
addSecondaryTable(mappingTable, table);
refKey.addColumnPairs(((ForeignKeyElement)
newSecondaryTables.get(mappingTable)).
getColumnPairs());
}
}
else
{
throw new ModelException(I18NHelper.getMessage(
getMessages(),
"mapping.table.foreign_key_not_found", table)); // NOI18N
}
}
}
else
{
throw new ModelException(I18NHelper.getMessage(getMessages(),
"mapping.table.null_argument")); // NOI18N
}
|
protected final void | firePropertyChange(java.lang.String name, java.lang.Object o, java.lang.Object n)Fires property change event. This method overrides that of
MappingElementImpl to update the mapping class element's modified
status.
// even though o == null and n == null will signify a change, that
// is consistent with PropertyChangeSupport's behavior and is
// necessary for this to work
boolean noChange = ((o != null) && (n != null) && o.equals(n));
super.firePropertyChange(name, o, n);
if (!(PROP_MODIFIED.equals(name)) && !noChange)
setModified(true);
|
protected final void | fireVetoableChange(java.lang.String name, java.lang.Object o, java.lang.Object n)Fires vetoable change event. This method overrides that of
MappingElementImpl to give listeners a chance to block
changes on the mapping class element modified status.
// even though o == null and n == null will signify a change, that
// is consistent with PropertyChangeSupport's behavior and is
// necessary for this to work
boolean noChange = ((o != null) && (n != null) && o.equals(n));
super.fireVetoableChange(name, o, n);
if (!(PROP_MODIFIED.equals(name)) && !noChange)
fireVetoableChange(PROP_MODIFIED, Boolean.FALSE, Boolean.TRUE);
|
public static MappingClassElement | forName(java.lang.String name, Model model)Returns the mapping class element associated with the class with the
given string name, using the given model object to look it up.
return model.getMappingClass(name);
|
public int | getConsistencyLevel()Gets the consistency level of this mapping class. return _consistencyLevel;
|
public java.lang.String | getDatabaseRoot()Returns the name of the SchemaElement which represents the
database used by the tables mapped to this mapping class. return _databaseRoot;
|
public MappingFieldElement | getField(java.lang.String name)Scans through this mapping class looking for a field whose
name matches the name passed in.
Iterator fieldIterator = getFields().iterator();
while (fieldIterator.hasNext())
{
MappingFieldElement field =
(MappingFieldElement)fieldIterator.next();
if (name.equals(field.getName()))
return field;
}
return null;
|
public java.util.ArrayList | getFields()Returns the list of fields (MappingFieldElements) in this mapping
class. This list includes both local and relationship fields.
if (_fields == null)
_fields = new ArrayList();
return _fields;
|
public java.lang.String | getKeyClass()Get the fully qualified name of the primary key class for this class
element. This value is only used if getObjectIdentityType
returns APPLICATION_IDENTITY
return getPersistenceElement().getKeyClass();
|
public final com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement | getPersistenceElement()
return _persistenceElement;
|
public int | getProperties() return _properties;
|
public MappingTableElement | getTable(java.lang.String name)Scans through this mapping class looking for a table whose
name matches the name passed in.
Iterator tableIterator = getTables().iterator();
while (tableIterator.hasNext())
{
MappingTableElement table =
(MappingTableElement)tableIterator.next();
if (table.getName().equals(name))
return table;
}
return null;
|
public java.util.ArrayList | getTables()Returns the list of tables (MappingTableElements) used by this mapping
class.
if (_tables == null)
_tables = new ArrayList();
return _tables;
|
public java.util.List | getVersionFields()Returns the list of version fields (MappingFieldElements) in this
mapping class. This list only includes fields if the consistency
level is {@link #VERSION_CONSISTENCY}.
List versionFields = new ArrayList();
if (VERSION_CONSISTENCY == getConsistencyLevel())
{
Iterator iterator = getFields().iterator();
while (iterator.hasNext())
{
MappingFieldElement fieldCandidate =
(MappingFieldElement)iterator.next();
if (fieldCandidate.isVersion())
versionFields.add(fieldCandidate);
}
}
return versionFields;
|
public int | getVersionNumber()Returns the version number of this MappingClassElement object.
Please note, the returned version number reflects the version number at
the last save, NOT the version number of the memory representation. return versionNo;
|
public boolean | hasOldVersionNumber()Returns true if the version number of this MappingClassElement object
is older than the current version number of the archiving scheme.
return (getVersionNumber() < CURRENT_VERSION_NO);
|
public boolean | isModified()Gets the modified flag for this mapping class. return _isModified;
|
public boolean | isNavigable()Gets the navigable flag for this mapping class. return ((_properties & NAVIGABLE) > 0);
|
public void | postUnarchive()This method is called after a MappingClassElement is unarchived
from a .mapping file. This method provides a hook to do any checking
(version number checking) and conversion after unarchiving.
// check version number
switch (versionNo)
{
case 0: // outdated version number
case 1: // outdated version number
throw new ModelException (I18NHelper.getMessage(getMessages(),
"file.incompatible_version", getName())); //NOI18N
case 2:
// Boston format => convert to Pilsen format
stripSchemaName();
break;
case 3: // same as 4 except package names are different
case 4: // same as 5 except version field not a choice for MFE
case MappingClassElementImpl.CURRENT_VERSION_NO:
// OK
break;
default: // version number is unknown
throw new ModelException (I18NHelper.getMessage(getMessages(),
"file.incompatible_version", getName())); //NOI18N
}
|
public void | preArchive()This method is called prior to storing a MappingClassElement in a
.mapping file. This method provides a hook to do any conversion
before archiving.
Note, the signature of preArchive in the interface MappingClassElement
includes a throws clause (ModelException), but the actual implementation
does not throw an exception.
// update version number
setVersionNumber(CURRENT_VERSION_NO);
|
public void | removeField(MappingFieldElement field)Removes a field from the list of fields in this mapping class.
try
{
fireVetoableChange(PROP_FIELDS, null, null);
if (!getFields().remove(field))
{
throw new ModelException(I18NHelper.getMessage(getMessages(),
"mapping.element.element_not_removed", field)); // NOI18N
}
firePropertyChange(PROP_FIELDS, null, null);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
|
public void | removeTable(MappingTableElement table)Removes the reference to the supplied table as a mapped table for this
mapping class. This works whether the table is the primary table or a
secondary table.
if (table != null)
{
Collection tables = getTables();
Iterator iterator = null;
boolean found = false;
try
{
fireVetoableChange(PROP_TABLES, null, null);
found = tables.remove(table);
firePropertyChange(PROP_TABLES, null, null);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
// remove all references to this table
iterator = tables.iterator();
while (iterator.hasNext())
{
MappingTableElement nextTable =
(MappingTableElement)iterator.next();
nextTable.removeReference(table);
}
if (found) // remove any fields mapped to that table
{
ArrayList fieldsToRemove = new ArrayList();
iterator = getFields().iterator();
while (iterator.hasNext())
{
MappingFieldElementImpl mappingField =
(MappingFieldElementImpl)iterator.next();
if (mappingField.isMappedToTable(table))
fieldsToRemove.add(mappingField);
}
iterator = fieldsToRemove.iterator();
while (iterator.hasNext())
{
MappingFieldElement mappingField =
(MappingFieldElement)iterator.next();
boolean versionField = mappingField.isVersion();
removeField(mappingField);
// if it is a version field, add back an unmapped
// field which retains the version flag setting
if (versionField)
{
mappingField = new MappingFieldElementImpl(
mappingField.getName(), this);
mappingField.setVersion(true);
addField(mappingField);
}
}
}
else
{
throw new ModelException(I18NHelper.getMessage(getMessages(),
"mapping.element.element_not_removed", table)); // NOI18N
}
}
else
{
throw new ModelException(I18NHelper.getMessage(getMessages(),
"mapping.element.null_argument")); // NOI18N
}
|
public void | setConsistencyLevel(int level)Set the consistency level of this mapping class.
Integer old = new Integer(getConsistencyLevel());
Integer newLevel = new Integer(level);
try
{
fireVetoableChange(PROP_CONSISTENCY, old, newLevel);
_consistencyLevel = level;
firePropertyChange(PROP_CONSISTENCY, old, newLevel);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
|
public void | setDatabaseRoot(SchemaElement root)Set the database root for this MappingClassElement.
The root represents the database used by the tables mapped to
this mapping class.
String old = getDatabaseRoot();
String newRoot = ((root != null) ? root.getName().getFullName() : null);
try
{
fireVetoableChange(PROP_DATABASE_ROOT, old, newRoot);
_databaseRoot = newRoot;
firePropertyChange(PROP_DATABASE_ROOT, old, newRoot);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
|
public void | setFields(java.util.ArrayList fields)Set the list of fields (MappingFieldElements) in this mapping
class. This method should only be used internally and for cloning
and archiving. _fields = fields;
|
public void | setModified(boolean flag)Set the modified flag for this mapping class to flag. This is usually
set to true by property changes and false
after a save.
boolean oldFlag = isModified();
if (flag != oldFlag)
{
_isModified = flag;
firePropertyChange(PROP_MODIFIED, JavaTypeHelper.valueOf(oldFlag),
JavaTypeHelper.valueOf(flag));
}
|
public void | setNavigable(boolean flag)Set the navigable flag for this mapping class to flag.
Boolean old = JavaTypeHelper.valueOf(isNavigable());
Boolean newFlag = JavaTypeHelper.valueOf(flag);
try
{
fireVetoableChange(PROP_NAVIGABLE, old, newFlag);
_properties = (flag) ?
(_properties | NAVIGABLE) : (_properties & ~NAVIGABLE);
firePropertyChange(PROP_NAVIGABLE, old, newFlag);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
|
public void | setPersistenceElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement element)Set the persistence class element for this mapping class element.
_persistenceElement = element;
|
public void | setPrimaryTable(TableElement table)Set the primary table for this mapping class to the supplied table.
ArrayList tables = getTables();
if (!tables.isEmpty())
{
throw new ModelException(I18NHelper.getMessage(getMessages(),
"mapping.table.primary_table_defined", table)); // NOI18N
}
else
{
UniqueKeyElement key = table.getPrimaryKey();
MappingTableElement mappingTable =
new MappingTableElementImpl(table, this);
SchemaElement schema = table.getDeclaringSchema();
String currentRoot = getDatabaseRoot();
if (currentRoot == null) // set database root
setDatabaseRoot(schema);
else if (!currentRoot.equals(schema.getName().getFullName()))
{
// if database root was set before, it must match
throw new ModelException(I18NHelper.getMessage(
getMessages(), "mapping.table.schema_mismatch", // NOI18N
table.toString(), currentRoot));
}
try
{
fireVetoableChange(PROP_TABLES, null, null);
tables.add(mappingTable);
firePropertyChange(PROP_TABLES, null, null);
}
catch (PropertyVetoException e)
{
throw new ModelVetoException(e);
}
// If can't find a primary key, settle for first unique key.
if (key == null)
{
UniqueKeyElement[] uniqueKeys = table.getUniqueKeys();
if ((uniqueKeys != null) && (uniqueKeys.length > 0))
key = uniqueKeys[0];
}
if (key == null)
{
// This is a warning -- we can still use the table but we
// cannot perform update operations on it. Also the user
// may define the key later.
}
else
{
ColumnElement[] columns = key.getColumns();
int i, count = ((columns != null) ? columns.length : 0);
for (i = 0; i < count; i++)
mappingTable.addKeyColumn(columns[i]);
}
}
|
private void | setVersionNumber(int version)Set the version number of this MappingClassElement. versionNo = version;
|
protected void | stripSchemaName()Boston to Pilsen conversion.
This method converts the absolute db element names to relative names and
stores the database root (meaning the schema name) in the
MappingClassElement. The method is recursively called for all
MappingTableElements and MappingFieldElements attached to this
MappingClassElement.
String schemaName = null;
// calculate schemaName from first MappingTableElement
if (_tables != null && !_tables.isEmpty())
{
schemaName = NameUtil.getSchemaName(
((MappingTableElement)_tables.get(0)).getTable());
}
// set the schemaName as database root
_databaseRoot = schemaName;
// do not change the _isModified flag
// handle _tables
if (_tables != null)
{
Iterator i = _tables.iterator();
while (i.hasNext())
((MappingTableElementImpl)i.next()).stripSchemaName();
}
// handle _fields
if (_fields != null)
{
Iterator i = _fields.iterator();
while (i.hasNext())
((MappingFieldElementImpl)i.next()).stripSchemaName();
}
|
protected static java.util.ArrayList | toColumnObjects(java.lang.String schemaName, java.util.ArrayList columnNames)Accept an arraylist of column names and return an array list containing
the corresponding column or column pair objects.
Iterator iterator = columnNames.iterator();
ArrayList objects = new ArrayList();
while (iterator.hasNext())
{
String columnName = (String)iterator.next();
String absoluteColumnName =
NameUtil.getAbsoluteMemberName(schemaName, columnName);
final TableElement table =
TableElement.forName(NameUtil.getTableName(absoluteColumnName));
objects.add(table.getMember(
DBIdentifier.create(absoluteColumnName)));
}
return objects;
|