@Override
public Object buildObject(ObjectBuildingQuery query, AbstractRecord databaseRow, JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
XMLRecord row = (XMLRecord) databaseRow;
row.setSession(query.getSession());
XMLUnmarshaller unmarshaller = row.getUnmarshaller();
Object parent = row.getOwningObject();
if (!(isXmlDescriptor() || getDescriptor().isDescriptorTypeAggregate())) {
return super.buildObject(query, databaseRow, joinManager);
}
query.getSession().startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
ClassDescriptor concreteDescriptor = getDescriptor();
Object domainObject = null;
// only need to check in the root case since the nested case is handled
// in the mapping
if (concreteDescriptor.hasInheritance() && (parent == null)) {
// look for an xsi:type attribute in the xml document
InheritancePolicy inheritancePolicy = concreteDescriptor.getInheritancePolicy();
Class classValue = inheritancePolicy.classFromRow(databaseRow, query.getSession());
if ((classValue == null) && isXmlDescriptor()) {
// no xsi:type attribute - look for type indicator on the
// default root element
QName leafElementType = ((XMLDescriptor) concreteDescriptor).getDefaultRootElementType();
// if we have a user-set type, try to get the class from the
// inheritance policy
if (leafElementType != null) {
XPathQName xpathQName = new XPathQName(leafElementType, row.isNamespaceAware());
Object indicator = inheritancePolicy.getClassIndicatorMapping().get(xpathQName);
if (indicator != null) {
classValue = (Class)indicator;
}
}
}
// if we found the class, use it - otherwise, use the descriptor
// class, if non-abstract
if (classValue != null) {
concreteDescriptor = query.getSession().getDescriptor(classValue);
if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) {
concreteDescriptor = getDescriptor();
}
if (concreteDescriptor == null) {
throw QueryException.noDescriptorForClassFromInheritancePolicy(query, classValue);
}
} else {
// make sure the class is non-abstract
if (Modifier.isAbstract(concreteDescriptor.getJavaClass().getModifiers())) {
// throw an exception
throw DescriptorException.missingClassIndicatorField(databaseRow, inheritancePolicy.getDescriptor());
}
}
}
domainObject = concreteDescriptor.getObjectBuilder().buildNewInstance();
row.setCurrentObject(domainObject);
if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
unmarshaller.getUnmarshalListener().beforeUnmarshal(domainObject, parent);
}
concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, null, databaseRow, query, joinManager, false, query.getSession());
if (isXmlDescriptor() && ((XMLDescriptor) concreteDescriptor).getPrimaryKeyFieldNames().size() > 0) {
Object pk = extractPrimaryKeyFromRow(databaseRow, query.getSession());
if ((pk != null) && (((CacheId) pk).getPrimaryKey().length > 0)) {
DOMRecord domRecord = (DOMRecord) databaseRow;
domRecord.getReferenceResolver().putValue(concreteDescriptor.getJavaClass(), pk, domainObject);
}
}
DocumentPreservationPolicy docPresPolicy = ((DOMRecord) row).getDocPresPolicy();
if (docPresPolicy != null) {
//EIS XML Cases won't have a doc pres policy set
((DOMRecord) row).getDocPresPolicy().addObjectToCache(domainObject, ((DOMRecord)row).getDOM());
}
query.getSession().endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
unmarshaller.getUnmarshalListener().afterUnmarshal(domainObject, parent);
}
return domainObject;
}