* entity.
*/
@SuppressWarnings("unchecked")
public static NSDictionary<String, Object> newPrimaryKeyDictionaryForEntityNamed(EOEditingContext ec, String entityName) {
EOEntity entity = ERXEOAccessUtilities.entityNamed(ec, entityName);
EODatabaseContext dbContext = EODatabaseContext.registeredDatabaseContextForModel(entity.model(), ec);
NSDictionary<String, Object> primaryKey = null;
try {
dbContext.lock();
boolean willRetryAfterHandlingDroppedConnection = true;
while (willRetryAfterHandlingDroppedConnection) {
try {
EOAdaptorChannel adaptorChannel = dbContext.availableChannel().adaptorChannel();
if (!adaptorChannel.isOpen()) {
adaptorChannel.openChannel();
}
NSArray<NSDictionary<String, Object>> arr = adaptorChannel.primaryKeysForNewRowsWithEntity(1, entity);
if (arr != null) {
primaryKey = arr.lastObject();
} else {
log.warn("Could not get primary key array for entity: " + entityName);
}
willRetryAfterHandlingDroppedConnection = false;
}
catch (Exception localException) {
if (willRetryAfterHandlingDroppedConnection &&
dbContext._isDroppedConnectionException(localException)) {
try {
dbContext.database().handleDroppedConnection();
}
catch(Exception ex) {
throw NSForwardException._runtimeExceptionForThrowable(ex);
}
}
else {
throw NSForwardException._runtimeExceptionForThrowable(localException);
}
}
}
} catch (Exception e) {
log.error("Caught exception when generating primary key for entity: " + entityName, e);
throw new NSForwardException(e);
} finally {
dbContext.unlock();
}
return primaryKey;
}