Esempio n. 1
0
        public static bool UpdateQueueSingleFlat(int flatId)
        {
            var option = new TransactionOptions();
            option.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
            option.Timeout = TimeSpan.FromSeconds(30);
            using (var scope = new TransactionScope(TransactionScopeOption.Required, option))
            {
                try
                {
                    DbApplications dbApplicationsObj = new DbApplications();
                    DataSet ds = dbApplicationsObj.GetApplicationsByFlat(flatId);
                    if (ds.Tables[0].Rows.Count == 0)
                        return false;

                    DataTable dt = ds.Tables[0];
                    dt.DefaultView.Sort = "Score Desc";
                    dt = dt.DefaultView.ToTable();
                    int queue = 1;

                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["QueueNumber"] = queue;
                        Console.WriteLine("FID: " + dr["FlatId"].ToString().Trim() + " Score: " + dr["Score"] + " QN: " + dr["QueueNumber"].ToString().Trim());
                        queue++;
                    }

                    ds.Tables.Remove("Applications");
                    ds.Tables.Add(dt);

                    foreach(DataRow row in ds.Tables[0].Rows)
                    {
                        Console.WriteLine("Iterating sorted DataSet...");
                        Console.WriteLine("Flat ID: " + Convert.ToInt32(row["FlatId"]));
                        Console.WriteLine("Score: " + Convert.ToInt32(row["Score"]));
                        Console.WriteLine("Queue: " + Convert.ToInt32(row["QueueNumber"]));

                        if (!dbApplicationsObj.UpdateApplicationsByFlat(row))
                        {
                            Console.WriteLine("ROLLBACK");
                            Transaction.Current.Rollback();
                        }
                    }

                    Console.WriteLine("Flat updated!");
                    scope.Complete();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unable to update flat - ERROR!");
                    Console.WriteLine(e);
                    Transaction.Current.Rollback();
                    return false;
                }
                finally
                {
                    if (scope != null)
                        ((IDisposable)scope).Dispose();
                }
            }
        }