public void RecordWatsonResults(ActionToAnalyze actionToAnalyze)
        {
            // insert ActionSentiment
            ActionSentiment sentiment = InsertActionSentiment(_db, actionToAnalyze);

            _db.SubmitChanges();    // get the DB generated ID
            int actionSentimentID = sentiment.ActionSentimentID;

            // insert child records - ActionSentimentScore(s)
            List <Tones> tones = actionToAnalyze.GetTones();
            List <ActionSentimentScore> scores = InsertSentimentScores(tones, _db, actionSentimentID);

            // update the corresponding ticket sentiment
            if (!actionToAnalyze.IsAgent)
            {
                ActionSentimentScore maxScore = scores.Where(s => s.SentimentScore == scores.Max(a => a.SentimentScore)).First();
                TicketSentiment.TicketSentimentStrategy(_db, actionToAnalyze, maxScore);
            }

            // Delete ActionToAnalyze
            actionToAnalyze.DeleteOnSubmit(_db);
            _db.SubmitChanges();
        }
示例#2
0
        /// <summary>
        /// Constructor to wrap the "using" of SqlConnection, SqlTransaction, and DataContext
        /// </summary>
        /// <param name="tones"></param>
        /// <param name="actionToAnalyze"></param>
        public WatsonResultsTransaction(Utterance utterance, ActionToAnalyze actionToAnalyze, Action <WatsonTransactionCallback> callback)
        {
            List <Tones> tones = utterance.tones;

            if (tones == null)
            {
                return; // ?
            }
            // open the connection
            try
            {
                _singleThreadedTransactions.WaitOne();
                string connectionString = ConfigurationManager.AppSettings.Get("ConnectionString");
                _connection = new SqlConnection(connectionString); // using
                _connection.Open();                                // connection must be open to begin transaction

                // start the transaction
                _transaction = _connection.BeginTransaction();  // using

                // create a data context
                _db             = new DataContext(_connection); // using
                _db.Transaction = _transaction;

                // insert ActionSentiment
                ActionSentiment sentiment = InsertActionSentiment(_db, actionToAnalyze);
                _db.SubmitChanges();    // get the DB generated ID
                int actionSentimentID = sentiment.ActionSentimentID;

                // insert child records - ActionSentimentScore(s)
                List <ActionSentimentScore> scores = InsertSentimentScores(tones, _db, actionSentimentID);

                // Delete ActionToAnalyze
                actionToAnalyze.DeleteOnSubmit(_db);
                _db.SubmitChanges();

                if (callback != null)
                {
                    WatsonTransactionCallback transaction = new WatsonTransactionCallback()
                    {
                        _db        = _db,
                        _sentiment = sentiment,
                        _scores    = scores
                    };
                    callback(transaction);
                }

                // Success!
                _transaction.Commit();
            }
            catch (Exception e)
            {
                if (_transaction != null)
                {
                    _transaction.Rollback();
                }

                EventLog.WriteEntry(EVENT_SOURCE, "********************PublishToTable SubmitTransaction: Exception " + e.Message + " ### " + e.Source + " ### " + e.StackTrace.ToString());
                Console.WriteLine("Exception at insert into Action Sentiment:" + e.Message + "###" + e.Source + " ----- STACK: " + e.StackTrace.ToString());
            }
            finally
            {
                _singleThreadedTransactions.ReleaseMutex();
            }
        }