* associate the batched Children with their owner object loop over children
*/
protected void associateBatched(Collection owners, Collection children)
{
CollectionDescriptor cds = getCollectionDescriptor();
PersistentField field = cds.getPersistentField();
PersistenceBroker pb = getBroker();
Class ownerTopLevelClass = pb.getTopLevelClass(getOwnerClassDescriptor().getClassOfObject());
Class collectionClass = cds.getCollectionClass(); // this collection type will be used:
HashMap ownerIdsToLists = new HashMap(owners.size());
IdentityFactory identityFactory = pb.serviceIdentity();
// initialize the owner list map
for (Iterator it = owners.iterator(); it.hasNext();)
{
Object owner = it.next();
ownerIdsToLists.put(identityFactory.buildIdentity(getOwnerClassDescriptor(), owner), new ArrayList());
}
// build the children lists for the owners
for (Iterator it = children.iterator(); it.hasNext();)
{
Object child = it.next();
// BRJ: use cld for real class, relatedObject could be Proxy
ClassDescriptor cld = getDescriptorRepository().getDescriptorFor(ProxyHelper.getRealClass(child));
Object[] fkValues = cds.getForeignKeyValues(child, cld);
Identity ownerId = identityFactory.buildIdentity(null, ownerTopLevelClass, fkValues);
List list = (List) ownerIdsToLists.get(ownerId);
if (list != null)
{
list.add(child);
}
}
// connect children list to owners
for (Iterator it = owners.iterator(); it.hasNext();)
{
Object result;
Object owner = it.next();
Identity ownerId = identityFactory.buildIdentity(owner);
List list = (List) ownerIdsToLists.get(ownerId);
if ((collectionClass == null) && field.getType().isArray())
{
int length = list.size();
Class itemtype = field.getType().getComponentType();
result = Array.newInstance(itemtype, length);
for (int j = 0; j < length; j++)
{
Array.set(result, j, list.get(j));
}
}
else
{
ManageableCollection col = createCollection(cds, collectionClass);
for (Iterator it2 = list.iterator(); it2.hasNext();)
{
col.ojbAdd(it2.next());
}
result = col;
}
Object value = field.get(owner);
if ((value instanceof CollectionProxyDefaultImpl) && (result instanceof Collection))
{
((CollectionProxyDefaultImpl) value).setData((Collection) result);
}
else
{
field.set(owner, result);
}
}
}