*/
public ApplicationConnection
connectApplication(final Long accountId, final String apiKey) throws Exception{
final Application application = getApplicationByKey(apiKey);
final UserAccount account = getAccountDaoImp().getUserAccountById(accountId);
final ApplicationConnection app = searchConnectionByAppIdAndUserId(account, application);
ApplicationConnection applicationConnection = null;
if (app != null) {
log.debug("Removing application connection id "+app.getConnectionId());
getHibernateTemplate().delete(app);
}
//create new application connection.
applicationConnection = new ApplicationConnection();
final String accessToken = keyGenerator.generateKey();
final String secret = keyGenerator.generateKey();
applicationConnection.setApiKey(apiKey);
applicationConnection.setApplication(application);
applicationConnection.setAccessToken(accessToken);
applicationConnection.setSecret(secret);
applicationConnection.setAccount(account);
getHibernateTemplate().saveOrUpdate(applicationConnection);
log.debug("Created New Application Connection "+applicationConnection.getConnectionId());
return applicationConnection;
}