}
if (bRead)
{
Query query = Query.createRead(m_metaclass, attributes,
new Pair(Symbol.AT).eq(m_oid), null, -1, 0, false,
Query.SEC_NONE, m_context);
for (int i = 0, nCount = m_metaclass.getInstanceAttributeCount(); i < nCount; ++i)
{
Attribute attribute = m_metaclass.getInstanceAttribute(i);
if ((isLazy() || getValueDirect(attribute.getOrdinal()) == Undefined.VALUE) &&
!attribute.isLazy())
{
query.addAttribute(Query.ASSOC_QUERY, attribute, null, false, Query.OUTPUT_EAGER);
}
}
if (query.getFirstOutputField() != null &&
(query.getFirstOutputField().getNext() != null ||
query.getFirstOutputField().getAttribute() !=
mapping.getLockingAttribute()) ||
query.getAssocCount(Query.ASSOC_QUERY) > 0)
{
if (s_logger.isDebugEnabled())
{
String sMsg = "Loading attributes " + attributes + " of " + this;
if (s_logger.isDumpEnabled())
{
StackTrace t = new StackTrace();
m_context.getMachine().updateStackTrace(t);
s_logger.dump(sMsg, t);
}
else
{
s_logger.debug(sMsg);
}
}
if (query.read().isEmpty())
{
if (bSafe)
{
return attributes;
}
// The instance could not be found
if (isLazy())
{
// Just the OID was available - check the referrer
for (Iterator itr = m_context.getInstanceRefIterator(); itr.hasNext();)
{
InstanceRef ref = (InstanceRef)itr.next();
if (ref != null)
{
Instance instance = ref.getInstance();
if (instance != null && !instance.isLazy() &&
instance.getState() != NEW && instance.getOID() != null)
{
Metaclass metaclass = instance.getMetaclass();
if (metaclass.getPersistenceMapping() != null)
{
for (int i = 0, n = metaclass.getInstanceAttributeCount(); i < n; ++i)
{
Attribute attribute = metaclass.getInstanceAttribute(i);
if (!attribute.isCollection() && attribute.isPersistent() &&
instance.getOldValueDirect(i) == this)
{
if (!Query.createRead(metaclass, new Pair(attribute.getSymbol()),
new Pair(Symbol.AT).eq(instance), null, -1, 0, false, Query.SEC_NONE,
m_context).read().isEmpty() &&
instance.getOldValueDirect(i) == this)
{
throw new AssociationIntegrityException(instance, attribute.getName());
}
throw new OptimisticLockException(this);
}
}
}
}
}
}
}
else if (!Query.createRead(getLazyMetaclass(), null, new Pair(Symbol.AT).eq(m_oid),
null, -1, 0, false, Query.SEC_NONE, m_context).read().isEmpty())
{
throw new AssociationIntegrityException(this);
}
throw new OptimisticLockException(this);
}
}
if (bLoad)
{
attributes = preload(attributes, null, false);
}
}
else
{
bRead = true;
}
}
else
{
attributes = preload(attributes, null, false);
}
for (; attributes != null; attributes = attributes.getNext())
{
Object head = attributes.getHead();
Pair assoc = (head instanceof Pair) ? (Pair)head : null;
Attribute attribute = m_metaclass.getAttribute(
(Symbol)((assoc != null) ? assoc.getHead() : head));
if (!attribute.isStatic())
{
Object value = getValueDirect(attribute.getOrdinal());
if (value instanceof Undefined)
{
switch (m_nState)
{
case INIT:
case NEW:
if (attribute.getInitializerFunction() != null)
{
setCalcValue(attribute,
m_context.getMachine().invoke(attribute.getInitializerFunction(), this, (Object[])null));
continue;
}
break;
case CLEAN:
case DIRTY:
if (attribute.getValueFunction() != null)
{
if (attribute.isCached())
{
setCalcValue(attribute,
m_context.getMachine().invoke(attribute.getValueFunction(), this, (Object[])null));
}
continue;
}
break;
}
if (attribute.isCollection())
{
InstanceList list = new InstanceArrayList(0);
setValueDirect(attribute.getOrdinal(), list);
list.setAssociation(this, attribute, true);
list.setLazy(false);
}
else
{
setValueDirect(attribute.getOrdinal(), null);
}
if (s_logger.isDebugEnabled())
{
s_logger.debug("Uninitialized " + attribute + ", set to " + getValueDirect(attribute.getOrdinal()));
}
}
else if (value != null && !bRead)
{
if (assoc != null && assoc.getTail() != null)
{
if (value instanceof InstanceList)
{
InstanceList list = (InstanceList)value;
for (int i = 0, n = list.getCount(); i < n; ++i)
{
Instance instance = list.getInstance(i);
if (instance != null && !instance.isLoading())
{
instance.invoke("load", assoc.getNext());
}
}
}
else if (value instanceof Instance)
{
Instance instance = (Instance)value;
if (!instance.isLoading())
{
instance.invoke("load", assoc.getNext());
}
}
}
}
}