EOEditingContext ec = handler.editingContext();
EOEnterpriseObject source = ec.faultForGlobalID(handler.sourceGlobalID(), ec);
if (source instanceof AutoBatchFaultingEnterpriseObject) {
String key = handler.relationshipName();
EOEntityClassDescription cd = (EOEntityClassDescription) source.classDescription();
EORelationship relationship = cd.entity().relationshipNamed(key);
if (_handler.batchSizeForRelationship(ec, relationship) > 0) {
markStart("ToMany.Calculation", source, key);
NSArray<EOEnterpriseObject> candidates = null;
NSArray currentObjects = (NSArray) ERXThreadStorage.valueForKey(THREAD_KEY);
boolean fromThreadStorage = false;
if (currentObjects != null) {
NSMutableArray<EOEnterpriseObject> tmpList = new NSMutableArray<EOEnterpriseObject>();
for (Object tmpItem : currentObjects) {
if (tmpItem instanceof AutoBatchFaultingEnterpriseObject) {
tmpList.add((EOEnterpriseObject) tmpItem);
}
}
if (tmpList.count() > 0) {
candidates = tmpList;
fromThreadStorage = true;
}
}
if (candidates == null) {
candidates = ec.registeredObjects();
}
long timestamp = ((AutoBatchFaultingEnterpriseObject) source).batchFaultingTimeStamp();
NSMutableArray<EOEnterpriseObject> eos = new NSMutableArray<EOEnterpriseObject>();
NSMutableArray faults = new NSMutableArray();
for (EOEnterpriseObject current : candidates) {
if (current instanceof AutoBatchFaultingEnterpriseObject) {
AutoBatchFaultingEnterpriseObject currentEO = (AutoBatchFaultingEnterpriseObject) current;
if (currentEO.batchFaultingTimeStamp() == timestamp || fromThreadStorage) {
if (!EOFaultHandler.isFault(currentEO) && currentEO.classDescription() == source.classDescription()) {
Object fault = currentEO.storedValueForKey(key);
if (EOFaultHandler.isFault(fault)) {
faults.addObject(fault);
eos.addObject(currentEO);
if (eos.count() == autoBatchFetchSize()) {
break;
}
}
}
}
}
}
markEnd("ToMany.Calculation", source, key);
if (eos.count() > 1) {
markStart("ToMany.Fetching", source, key);
doFetch(dbc, ec, relationship, eos);
int cnt = 0;
for(Object fault: faults) {
if(!EOFaultHandler.isFault(fault)) {
NSArray array = (NSArray)fault;
freshenFetchTimestamps(array, timestamp);
cnt += array.count();
}
}
markEnd("ToMany.Fetching", source, key);
if(batchLog.isDebugEnabled()) {
batchLog.debug("Fetched " + cnt + " to-many " + relationship.destinationEntity().name() + " from " + eos.count() + " " + source.entityName() + " for " + key);
}
return EOFaultHandler.isFault(obj);
}
}
}