@Override
public void saveSessionForContext(WOContext context) {
WOSession session = context.session();
EOEditingContext ec = ERXEC.newEditingContext();
ERSessionInfo info = ERSessionInfo.clazz.objectMatchingKeyAndValue(ec, ERSessionInfo.SESSION_ID_KEY, session.sessionID());
if(info == null) {
info = ERSessionInfo.clazz.createAndInsertObject(ec);
info.setSessionID(session.sessionID());
}
NSTimestamp expires = new NSTimestamp(System.currentTimeMillis() + session.timeOutMillis());
info.setExpirationDate(expires);
try {
/*
* An error here can later hang the instance when the session is restored.
* If the session fails to archive, delete it.
*/
info.archiveDataFromSession(session);
} catch (Exception e) {
log.error("Error archiving session! Deleting session.");
ERXApplication app = ERXApplication.erxApplication();
NSMutableDictionary extraInfo = app.extraInformationForExceptionInContext(e, context);
app.reportException(e, context, extraInfo);
/*
* If the session info is new, just don't save it.
* Otherwise, we need to delete the session.
*/
if(!info.isNewObject()) {
removeSessionWithID(session.sessionID());
}
return;
}
ec.saveChanges();