📄 compoundprimarykeycolumnspec.java
字号:
/*
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.mozilla.org/MPL
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*
* The Original Code is jRelationalFramework.
*
* The Initial Developer of the Original Code is is.com.
* Portions created by is.com are Copyright (C) 2000 is.com.
* All Rights Reserved.
*
* Contributor(s): Jonathan Carlson (joncrlsn@users.sf.net)
* Contributor(s): ____________________________________
*
* Alternatively, the contents of this file may be used under the terms of
* the GNU General Public License (the "GPL") or the GNU Lesser General
* Public license (the "LGPL"), in which case the provisions of the GPL or
* LGPL are applicable instead of those above. If you wish to allow use of
* your version of this file only under the terms of either the GPL or LGPL
* and not to allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and replace them
* with the notice and other provisions required by either the GPL or LGPL
* License. If you do not delete the provisions above, a recipient may use
* your version of this file under either the MPL or GPL or LGPL License.
*
*/
package com.is.jrf;
import com.is.util.sql.JDBCHelper;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
/**
* This implementer of ColumnSpec holds multiple AbstractColumnSpec subclass
* instances that make up the primary key for a table.
*/
public class CompoundPrimaryKeyColumnSpec
implements ColumnSpec
{
/*
* =============== Static Variables ===============
*/
protected static Class s_class = null;
private List i_columnSpecs = new ArrayList();
/*
* =============== Constructors ===============
*/
/**
* Constructor for the CompoundPrimaryKeyColumnSpec object
* use the method addColumnSpec(...) in conjunction with this.
*/
public CompoundPrimaryKeyColumnSpec()
{
}
/**
* Constructor for the CompoundPrimaryKeyColumnSpec object
*
* @param columnOne a ColumnSpec
* @param columnTwo a ColumnSpec
*/
public CompoundPrimaryKeyColumnSpec(
ColumnSpec columnOne,
ColumnSpec columnTwo)
{
this.addColumnSpec(columnOne);
this.addColumnSpec(columnTwo);
}
/**
* Constructor for the CompoundPrimaryKeyColumnSpec object
*
* @param columnOne a ColumnSpec
* @param columnTwo a ColumnSpec
* @param columnThree a ColumnSpec
*/
public CompoundPrimaryKeyColumnSpec(
ColumnSpec columnOne,
ColumnSpec columnTwo,
ColumnSpec columnThree)
{
this(columnOne, columnTwo);
this.addColumnSpec(columnThree);
}
/**
* Constructor for the CompoundPrimaryKeyColumnSpec object
*
* @param columnOne a ColumnSpec
* @param columnTwo a ColumnSpec
* @param columnThree a ColumnSpec
* @param columnFour a ColumnSpec
*/
public CompoundPrimaryKeyColumnSpec(ColumnSpec columnOne, ColumnSpec columnTwo, ColumnSpec columnThree, ColumnSpec columnFour)
{
this(columnOne, columnTwo, columnThree);
this.addColumnSpec(columnFour);
}
/**
* Constructor for the CompoundPrimaryKeyColumnSpec object
*
* @param columnOne a ColumnSpec
* @param columnTwo a ColumnSpec
* @param columnThree a ColumnSpec
* @param columnFour a ColumnSpec
* @param columnFive a ColumnSpec
*/
public CompoundPrimaryKeyColumnSpec(ColumnSpec columnOne, ColumnSpec columnTwo, ColumnSpec columnThree, ColumnSpec columnFour, ColumnSpec columnFive)
{
this(columnOne, columnTwo, columnThree, columnFour);
this.addColumnSpec(columnFive);
}
/**
* Constructor for the CompoundPrimaryKeyColumnSpec object
*
* @param columnOne a ColumnSpec
* @param columnTwo a ColumnSpec
* @param columnThree a ColumnSpec
* @param columnFour a ColumnSpec
* @param columnFive a ColumnSpec
* @param columnSix a ColumnSpec
*/
public CompoundPrimaryKeyColumnSpec(ColumnSpec columnOne, ColumnSpec columnTwo, ColumnSpec columnThree, ColumnSpec columnFour, ColumnSpec columnFive, ColumnSpec columnSix)
{
this(columnOne, columnTwo, columnThree, columnFour, columnFive);
this.addColumnSpec(columnSix);
}
/**
* Adds a feature to the ColumnSpec attribute of the
* CompoundPrimaryKeyColumnSpec object
*
* @param aColumnSpec The feature to be added to the ColumnSpec attribute
*/
public void addColumnSpec(ColumnSpec aColumnSpec)
{
aColumnSpec.setRequired(true);
i_columnSpecs.add(aColumnSpec);
}
/**
* Ask each of my ColumnSpec instances to get the appropriate column from
* the helper and stick it into the PersistentObject.
*
* @param helper a value of type 'JDBCHelper'
* @param aPO a value of type 'PersistentObject'
* @exception SQLException if an error occurs
*/
public void copyColumnValueToPersistentObject(JDBCHelper helper,
PersistentObject aPO)
throws SQLException
{
ColumnSpec spec = null;
Iterator iterator = i_columnSpecs.iterator();
while (iterator.hasNext())
{
spec = (ColumnSpec) iterator.next();
spec.copyColumnValueToPersistentObject(helper, aPO);
}
}
/**
* Convert the attribute for this object to a String that can be unconverted
* later by the decodeToPersistentObject(...) method. The result of this
* method is not for use in SQL.
*
* @param aPO a value of type 'PersistentObject'
* @return Description of the Returned Value
*/
public String encodeFromPersistentObject(PersistentObject aPO)
{
StringBuffer buffer = new StringBuffer();
ColumnSpec spec = null;
Iterator iterator = i_columnSpecs.iterator();
while (iterator.hasNext())
{
spec = (ColumnSpec) iterator.next();
buffer.append(spec.encodeFromPersistentObject(aPO));
if (iterator.hasNext())
{
buffer.append("|");
}
}// while
return buffer.toString();
}// encodeFromPersistentObject(aPO)
/**
* The String parameter must have been encoded with the
* encodeFromPersistentObject(...) method. This is used by
* AbstractDomain.decodePrimaryKey(aString). Parse out the String into
* tokens. Ask each columnspec to convert the string to the appropriate
* object, then for each of my column specs, convert the given String into a
* PersistentObject.
*
* @param aString a value of type 'String'
* @param aPO a value of type 'PersistentObject'
*/
public void decodeToPersistentObject(String aString,
PersistentObject aPO)
{
StringTokenizer tokenizer = new StringTokenizer(aString, "|");
Iterator iterator = i_columnSpecs.iterator();
while (tokenizer.hasMoreTokens())
{
String token = tokenizer.nextToken();
AbstractColumnSpec spec = (AbstractColumnSpec) iterator.next();
spec.decodeToPersistentObject(token, aPO);
}
}// decodeToPersistentObject(aString, aPO)
/**
* This is an override of the method in ColumnSpec.
*
* @param aPO1 a value of type 'PersistentObject'
* @param aPO2 a value of type 'PersistentObject'
*/
public void copyAttribute(PersistentObject aPO1,
PersistentObject aPO2)
{
ColumnSpec spec = null;
Iterator iterator = i_columnSpecs.iterator();
while (iterator.hasNext())
{
spec = (ColumnSpec) iterator.next();
spec.copyAttribute(aPO1, aPO2);
}
}// copyAttribute(aPO, aPO)
/**
* Build a compound where clause (without the WHERE).
*
* @param aPO a value of type 'PersistentObject'
* @param separator a value of type 'String'
* @param tableName a value of type 'String'
* @param dbPolicy a value of type 'DatabasePolicy'
* @return a value of type 'String'
*/
public String buildWhereClause(Object aPO,
String separator,
String tableName,
DatabasePolicy dbPolicy)
{
if (!(aPO instanceof PersistentObject))
{
throw new IllegalArgumentException(
"This primary key is compound. Must pass in a PersistentObject "
+ "with it's primary keys populated.");
}
ColumnSpec spec = null;
Iterator iterator = i_columnSpecs.iterator();
StringBuffer buffer = new StringBuffer();
while (iterator.hasNext())
{
spec = (ColumnSpec) iterator.next();
buffer.append(spec.buildNameValuePair(aPO,
separator,
tableName,
dbPolicy));
if (iterator.hasNext())
{
buffer.append(" AND ");
}
}// for
return buffer.toString();
}// buildWhereClause(...)
/**
* Make sure the PersistentObject object has these attributes. All compound
* primary key columns are required by definition.
*
* @param aPO a value of type 'PersistentObject'
* @return Description of the Returned Value
* @exception MissingAttributeException if an error occurs
*/
public Object validateRequired(PersistentObject aPO)
throws MissingAttributeException
{
ColumnSpec spec = null;
Iterator iterator = i_columnSpecs.iterator();
while (iterator.hasNext())
{
spec = (ColumnSpec) iterator.next();
spec.validateRequired(aPO);
}
return null;
}// validateRequired(aPO)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -