if ( !pkeyStuffInitialized )
initPkeyInfo();
int cmpFieldCount = cmpFields.size();
if (cmpFieldCount==0) {
throw new DeploymentException("No cmp field defined for CMP EJB " + parentDesc.getName());
}
int fkeyCount = 0;
if ( fkeyFields != null )
fkeyCount = fkeyFields.length;
persFieldInfo = new PersistentFieldInfo[cmpFieldCount + fkeyCount];
// Add CMP fields
int fcount = 0;
Iterator itr = cmpFields.iterator();
while ( itr.hasNext() ) {
persFieldInfo[fcount] = new PersistentFieldInfo();
persFieldInfo[fcount].name =((FieldDescriptor)itr.next()).getName();
fcount++;
}
// Add foreign key fields
if ( fkeyFields != null ) {
for ( int i=0; i<fkeyFields.length; i++ ) {
persFieldInfo[fcount] = fkeyFields[i];
fcount++;
}
}
// sort persFieldInfo in alphabetical order
for ( int i=persFieldInfo.length-1; i>0; i-- ) {
for ( int j=0; j<i; j++ ) {
if ( persFieldInfo[j].name.compareTo(persFieldInfo[j+1].name)
> 0 ) {
PersistentFieldInfo tmp = persFieldInfo[j];
persFieldInfo[j] = persFieldInfo[j+1];
persFieldInfo[j+1] = tmp;
}
}
}
// Initialize pkeyFieldInfo[] and persNoPkeyFieldInfo[]
// They contain the same PersistentFieldInfo objects as persFieldInfo.
pkeyFieldInfo = new PersistentFieldInfo[pkeyFields.size()];
if ( pkeyFieldSpecified ) {
// check if PK class has public non-persistent fields
StringBuffer nonPersFieldsInPK = new StringBuffer();
for ( Iterator it=pkeyFields.iterator(); it.hasNext(); ) {
FieldDescriptor fd = (FieldDescriptor)it.next();
boolean isPersistent = false;
for ( int i=0; i<persFieldInfo.length; i++ ) {
if ( fd.getName().equals(persFieldInfo[i].name) ) {
isPersistent = true;
break;
}
}
if ( !isPersistent ) {
// if not the first non-persistent field
if ( nonPersFieldsInPK.length() != 0 ) {
nonPersFieldsInPK.append(", ");
}
nonPersFieldsInPK.append(fd.getName());
}
}
if ( nonPersFieldsInPK.length() != 0 ) {
throw new DeploymentException(localStrings.getLocalString(
"enterprise.deployment.pkhasnopersistentfields",
"CMP bean [{0}], primary key class [{1}] has " +
"public non-persistent field(s) [{2}].",
new Object[] {getParentDescriptor().getName(),
primaryKeyClassName,
nonPersFieldsInPK.toString()}));
}
persNoPkeyFieldInfo = new PersistentFieldInfo[persFieldInfo.length -
pkeyFieldInfo.length];
int pkeyCount = 0;
int noPkeyCount = 0;
for ( int i=0; i<persFieldInfo.length; i++ ) {
boolean isPkey = false;
for ( Iterator it=pkeyFields.iterator(); it.hasNext(); ) {
FieldDescriptor fd = (FieldDescriptor)it.next();
if ( fd.getName().equals(persFieldInfo[i].name) ) {
isPkey = true;
break;
}
}
if ( isPkey )
pkeyFieldInfo[pkeyCount++] = persFieldInfo[i];
else
persNoPkeyFieldInfo[noPkeyCount++] = persFieldInfo[i];
}
}
if ( pkeyIsOneField && pkeyFieldSpecified) {
// Initialize pkey field type. This is needed for
// beans with no pkey field (i.e. PM-generated pkey)
// because they wont have get/set for the pkey in the bean class
// so the getCMPFieldType() below will bomb if type is not set.
// Note: pkeyFieldInfo and persFieldInfo arrays share the same
// PersistentFieldInfo objects.
pkeyFieldInfo[0].type = getPrimaryKeyClass();
}
// Initialize Java types in persFieldInfo
for ( int i=0; i<persFieldInfo.length; i++ ) {
// fill type for CMP fields if not there
// (fkey types will already be filled)
if ( persFieldInfo[i].type == null )
persFieldInfo[i].type = getCMPFieldType(persFieldInfo[i].name);
}
// When called from Deploytool, the bean class is abstract,
// and doesnt have the CMP fields: they will be in the generated
// code after deployment. So all the java.lang.reflect.Field
// values in PersistentFieldInfo can only be initialized at runtime.
try {
if ( persistentClass != null
&& !Modifier.isAbstract(persistentClass.getModifiers()) ) {
for ( int i=0; i<persFieldInfo.length; i++ ) {
persFieldInfo[i].field = getField(getStateClass(),
persFieldInfo[i].name);
}
}
// Initialize pkeyClassPkeyFields
if ( !pkeyIsOneField && primaryKeyClass != null
&& !Modifier.isAbstract(primaryKeyClass.getModifiers()) ) {
pkeyClassPkeyFields = new Field[pkeyFieldInfo.length];
for ( int i=0; i<pkeyFieldInfo.length; i++ ) {
pkeyClassPkeyFields[i] = primaryKeyClass.getField(
pkeyFieldInfo[i].name);
}
}
} catch ( NoSuchFieldException ex ) {
if(DOLUtils.getDefaultLogger().isLoggable(Level.FINE)) {
DOLUtils.getDefaultLogger().log(Level.FINE, ex.toString(), ex);
}
throw new DeploymentException(ex);
}
fieldInfoInitialized = true;
}