I have two models. One with data common to all application sessions and one with data that should be used for connecting to a database determined by data in the common model. This is the same as the setup in EOEditingContext docs "Getting Data from Multiple Sources". The difference is that one of the models is a per-session connection model and the other is (or at least can be) consistently connected to a single database.
The problem that I am running into is in changing the connection of the session specific model and running into adaptorChannel problems. I feel like I'm facing the wrong direction in looking for an answer because the usual answer to my questions about webobjects is that someone has figured this out before and it is much more straight forward than I can see at the moment.
Should I be looking to remove the EOCooperatingObjectStore related to the dynamic model, create a new one, add it to the EOObjectStoreCoordinator for the session editing context? Does anybody have any advice or examples or docs that might help with this?
public void setDatabaseConnection(String dbname) {
// Get the model and adaptor
EOModel model = EOModelGroup.defaultGroup().modelNamed(
Application.VAEMR_MODELNAME);
EODatabaseContext dc = EODatabaseContext
.registeredDatabaseContextForModel(model,
EOObjectStoreCoordinator.defaultCoordinator());
EOAdaptor adaptor = dc.adaptorContext().adaptor();
if (adaptor.hasOpenChannels()) {
log.debug("has adaptor channels");
NSArray<EOAdaptorContext> ctxs = adaptor.contexts();
log.debug(ctxs.count() + " contexts");
for (int i = 0; i < ctxs.count(); i++) {
EOAdaptorContext ctx = (EOAdaptorContext) ctxs.get(i);
NSArray chans = ctx.channels();
log.debug(chans.count() + " channels in this context");
for (int j = 0; j < chans.count(); j++) {
EOAdaptorChannel chan = (EOAdaptorChannel) chans.get(i);
chan.closeChannel();
}
}
}
NSDictionary<String,Object> prevConnDict = adaptor.connectionDictionary();
emrConnectionDict = new NSMutableDictionary<String, Object>(adaptor
.connectionDictionary());
Object[] args = { dbname };
emrConnectionDict.takeValueForKey(String.format(url, args), "URL");
try {
adaptor.setConnectionDictionary(emrConnectionDict);
adaptor.assertConnectionDictionaryIsValid();
} catch (Throwable exception) {
exception.printStackTrace();
log.error(exception.toString());
log.error("reverting to previous connectionk dictionary");
log.error(prevConnDict);
adaptor.setConnectionDictionary(prevConnDict);
}
EOModel common = EOModelGroup.defaultGroup().modelNamed(
Application.VACOMMON_MODELNAME);
EODatabaseContext dccommon = EODatabaseContext
.registeredDatabaseContextForModel(common, this
.defaultEditingContext());
EOAdaptor adaptorCommon = dccommon.adaptorContext().adaptor();
System.out.println("common connection");
System.out.println(adaptorCommon.connectionDictionary());
System.out.println("emr connection");
System.out.println(adaptor.connectionDictionary());
log.debug(emrConnectionDict);
}