package org.apache.ojb.broker.accesslayer;
/* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.OJBRuntimeException;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.core.PersistenceBrokerImpl;
import org.apache.ojb.broker.core.proxy.CollectionProxyDefaultImpl;
import org.apache.ojb.broker.core.proxy.ProxyHelper;
import org.apache.ojb.broker.metadata.ClassDescriptor;
import org.apache.ojb.broker.metadata.CollectionDescriptor;
import org.apache.ojb.broker.metadata.FieldHelper;
import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;
import org.apache.ojb.broker.util.BrokerHelper;
import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
/**
* Relationship Prefetcher for Collections.
*
* @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
* @version $Id: CollectionPrefetcher.java,v 1.29 2004/06/11 18:49:09 brj Exp $
*/
public class CollectionPrefetcher extends RelationshipPrefetcherImpl
{
/**
* Constructor for CollectionPrefetcher.
*
* @param aBroker
* @param anOrd
*/
public CollectionPrefetcher(PersistenceBrokerImpl aBroker, ObjectReferenceDescriptor anOrd)
{
super(aBroker, anOrd);
}
/**
* Build the multiple queries for one relationship because of limitation of IN(...)
*
* @param owners Collection containing all objects of the ONE side
*/
protected Query[] buildPrefetchQueries(Collection owners, Collection children)
{
ClassDescriptor cld = getOwnerClassDescriptor();
Class topLevelClass = getBroker().getTopLevelClass(cld.getClassOfObject());
BrokerHelper helper = getBroker().serviceBrokerHelper();
Collection queries = new ArrayList(owners.size());
Collection idsSubset = new HashSet(owners.size());
Object[] fkValues;
Object owner;
Identity id;
Iterator iter = owners.iterator();
while (iter.hasNext())
{
owner = iter.next();
fkValues = helper.extractValueArray(helper.getKeyValues(cld, owner));
id = new Identity(null, topLevelClass, fkValues);
idsSubset.add(id);
if (idsSubset.size() == pkLimit)
{
queries.add(buildPrefetchQuery(idsSubset));
idsSubset.clear();
}
}
if (idsSubset.size() > 0)
{
queries.add(buildPrefetchQuery(idsSubset));
}
return (Query[]) queries.toArray(new Query[queries.size()]);
}
/**
* Build the query to perform a batched read get orderBy settings from CollectionDescriptor
*
* @param ids Collection containing all identities of objects of the ONE side
*/
protected Query buildPrefetchQuery(Collection ids)
{
CollectionDescriptor cds = getCollectionDescriptor();
QueryByCriteria query = buildPrefetchQuery(ids, cds.getForeignKeyFieldDescriptors(getItemClassDescriptor()));
// check if collection must be ordered
if (!cds.getOrderBy().isEmpty())
{
Iterator iter = cds.getOrderBy().iterator();
while (iter.hasNext())
{
query.addOrderBy((FieldHelper) iter.next());
}
}
return query;
}
/**
* 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());
// initialize the owner list map
for (Iterator it = owners.iterator(); it.hasNext();)
{
Object owner = it.next();
ownerIdsToLists.put(new Identity(owner, pb), 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 = new Identity(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 = new Identity(owner, pb);
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(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);
}
}
}
/**
* Create a Collection of class collectionClass
* if collectionClass is null return a RemovalAwareCollection
* @param collectionClass
* @return
*/
protected ManageableCollection createCollection(Class collectionClass)
{
ManageableCollection col;
if (collectionClass == null)
{
col = new RemovalAwareCollection();
}
else
{
try
{
col = (ManageableCollection) collectionClass.newInstance();
}
catch (Exception e)
{
throw new OJBRuntimeException("Can't create new Collection for owner", e);
}
}
return col;
}
protected CollectionDescriptor getCollectionDescriptor()
{
return (CollectionDescriptor) getObjectReferenceDescriptor();
}
}