private void ensureReferencedFKs ( ModelDef modelDef , CollectionDescriptorDef collDef ) throws ConstraintException { String elementClassName = collDef . getProperty ( PropertyHelper . OJB_PROPERTY_ELEMENT_CLASS_REF ) ; ClassDescriptorDef elementClassDef = modelDef . getClass ( elementClassName ) ; String fkFieldNames = collDef . getProperty ( PropertyHelper . OJB_PROPERTY_FOREIGNKEY ) ; ArrayList missingFields = new ArrayList ( ) ; SequencedHashMap fkFields = new SequencedHashMap ( ) ; // first we gather all field names for ( CommaListIterator it = new CommaListIterator ( fkFieldNames ) ; it . hasNext ( ) ; ) { String fieldName = ( String ) it . next ( ) ; FieldDescriptorDef fieldDef = elementClassDef . getField ( fieldName ) ; if ( fieldDef == null ) { missingFields . add ( fieldName ) ; } fkFields . put ( fieldName , fieldDef ) ; } // next we traverse all sub types and gather fields as we go for ( Iterator it = elementClassDef . getAllExtentClasses ( ) ; it . hasNext ( ) && ! missingFields . isEmpty ( ) ; ) { ClassDescriptorDef subTypeDef = ( ClassDescriptorDef ) it . next ( ) ; for ( int idx = 0 ; idx < missingFields . size ( ) ; ) { FieldDescriptorDef fieldDef = subTypeDef . getField ( ( String ) missingFields . get ( idx ) ) ; if ( fieldDef != null ) { fkFields . put ( fieldDef . getName ( ) , fieldDef ) ; missingFields . remove ( idx ) ; } else { idx ++ ; } } } if ( ! missingFields . isEmpty ( ) ) { throw new ConstraintException ( "Cannot find field " + missingFields . get ( 0 ) . toString ( ) + " in the hierarchy with root type " + elementClassDef . getName ( ) + " which is used as foreignkey in collection " + collDef . getName ( ) + " in " + collDef . getOwner ( ) . getName ( ) ) ; } // copy the found fields into the element class ensureFields ( elementClassDef , fkFields . values ( ) ) ; }
Ensures that the foreign keys required by the given collection are present in the element class .