public void Update(Training training)
 {
     using (ISession session = NHibernateHelper.OpenSession())
     using (ITransaction transaction = session.BeginTransaction())
     {
         session.Update(training);
         transaction.Commit();
     }
 }
Ejemplo n.º 2
0
        public void Can_add_multiple_new_trainings()
        {
            ISession session = _sessionFactory.OpenSession();

            int countBefore = getTrainingTableSize(session);

            ITransaction tx = session.BeginTransaction();
            Training training1 = new Training();
            training1.Date = DateTime.Now;
            training1.ClassType = _classType;
            training1.ClassCost = _classCost;
            training1.Dog = _dog1;
            training1.User = _user1;
            session.Save(training1);

            Training training2 = new Training();
            training2.Date = DateTime.Now;
            training2.ClassType = _classType;
            training2.ClassCost = _classCost;
            training2.Dog = _dog2;
            training2.User = _user2;
            session.Save(training2);

            Training training3 = new Training();
            training3.Date = DateTime.Now;
            training3.ClassType = _classType;
            training3.ClassCost = _classCost;
            training3.Dog = _dog1;
            training3.User = _user1;
            session.Save(training3);

            tx.Commit();

            Assert.AreEqual(countBefore + 3, getTrainingTableSize(session));

            tx = session.BeginTransaction();

            session.Delete(training1);
            session.Delete(training2);
            session.Delete(training3);

            tx.Commit();

            session.Close();
        }
Ejemplo n.º 3
0
        public void Can_add_new_training()
        {
            DateTime date = DateTime.Now;
            // Set the Milliseconds to 0 since MySQL DATETIME does not support milliseconds.
            date = DateTime.ParseExact(date.ToString(), "M/d/yyyy h:mm:ss tt", null);

            var training = new Training(date, _classType, _classCost, _dog1, _user1);
            ITrainingRepository repository = new TrainingRepository();
            repository.Add(training);

            // use session to try to load the training
            using (ISession session = _sessionFactory.OpenSession())
            {
                var fromDb = session.Get<Training>(training.TrainingId);
                // Test that the training was successfully inserted
                Assert.IsNotNull(fromDb);
                Assert.AreNotSame(training, fromDb);
                Assert.AreEqual(training.Date, fromDb.Date);
                Assert.AreEqual(training.Dog, fromDb.Dog);
                Assert.AreEqual(training.User, fromDb.User);
                Assert.AreEqual(training.ClassType, fromDb.ClassType);
                Assert.AreEqual(training.ClassCost, fromDb.ClassCost);
                Assert.AreEqual(training.PreK9DaycareCost, fromDb.PreK9DaycareCost);
            }

            repository.Remove(training);
        }
Ejemplo n.º 4
0
        private bool IsInCollection(Training training, ICollection<Training> fromDb)
        {
            bool result = false;

            foreach (var item in fromDb)
            {
                if (training.TrainingId == item.TrainingId)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
Ejemplo n.º 5
0
        private void CreateInitialData()
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction transaction = session.BeginTransaction())
            {
                // Save the user to populate its UserId.
                session.Save(_user1);
                session.Save(_user2);

                session.Save(_dog1);
                session.Save(_dog2);

                session.Save(_classCost);
                session.Save(_preK9DaycareCost);
                session.Save(_newClassCost);

                session.Save(_classType);

                // Get rid of milliseconds to make comparisons work, since MySQL
                // DateTime does not support milliseconds.
                DateTime date = DateTime.ParseExact(DateTime.Now.ToString(), "M/d/yyyy h:mm:ss tt", null);

                _trainings = new Training [4];
                _trainings[0] = new Training(date, _classType, _classCost, _dog1, _user1);
                _trainings[1] = new Training(date, _classType, _classCost, _dog1, _user2);
                _trainings[2] = new Training(date, _classType, _classCost, _dog2, _user1);
                _trainings[3] = new Training(date, _classType, _classCost, _dog2, _user2);

                foreach (var training in _trainings)
                {
                    session.Save(training);
                }

                transaction.Commit();
            }
        }
        private bool dataGridView1_RowValidating(int index, Training training)
        {
            if (training.Date < payrollStartDate || training.Date > payrollEndDate)
            {
                dataGridView1.Rows[index].ErrorText = "Training Date must be for current payroll period (" + payrollStartDate + " - " + payrollEndDate + ").";
                return false;
            }

            if (training.Dog == null)
            {
                dataGridView1.Rows[index].ErrorText = "Dog's Name must not be empty.";
                return false;
            }
            else if (training.ClassType == null)
            {
                dataGridView1.Rows[index].ErrorText = "Class must not be empty.";
                return false;
            }
            else if (training.ClassCost == null)
            {
                dataGridView1.Rows[index].ErrorText = "Cost of Class must not be empty.";
                return false;
            }

            return true;
        }
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
            {
                if (dirtyObjectsMap.Keys.Contains(e.RowIndex))
                {
                    dirtyObjectsMap.Remove(e.RowIndex);
                }

                Training training = new Training();

                DateTime date = (DateTime)dataGridView1.Rows[e.RowIndex].Cells["DateColumn"].Value;
                training.Date = date;

                string dogId = (string)dataGridView1.Rows[e.RowIndex].Cells["DogNameColumn"].Value;
                DogRepository dogRepository = new DogRepository();
                if (dogId != null)
                {
                    Dog dog = dogRepository.GetById(dogId);
                    training.Dog = dog;
                }

                CostRepository costRepository = new CostRepository();
                CostTypeRepository costTypeRepository = new CostTypeRepository();
                string classTypeId = (string)dataGridView1.Rows[e.RowIndex].Cells["ClassColumn"].Value;
                if (classTypeId != null)
                {
                    CostType costType = costTypeRepository.GetById(classTypeId);
                    training.ClassType = costType;

                    // If Class column value is Pre-K9, then enable Pre-K9 Daycare
                    // Cost column.
                    if (TRAINING_CLASS_PRE_K9.Equals(costType.CostName))
                    {
                        DataGridViewComboBoxCell preK9DaycareComboBoxCell = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells["PreK9DaycareCostColumn"];
                        preK9DaycareComboBoxCell.ReadOnly = false;
                    }
                    else
                    {
                        DataGridViewComboBoxCell preK9DaycareComboBoxCell = (DataGridViewComboBoxCell)dataGridView1.Rows[e.RowIndex].Cells["PreK9DaycareCostColumn"];
                        preK9DaycareComboBoxCell.ReadOnly = true;
                    }

                    // Has Class column combobox value changed?
                    if (e.ColumnIndex == 3)
                    {
                        // Yes, Class column value has changed, so update
                        // Class Cost column combobox with appropriate
                        // values for new Class.

                        // Sort the costs.
                        IList<Cost> possibleCosts1 = costType.PossibleCosts;
                        ArrayList.Adapter((IList)possibleCosts1).Sort();

                        DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(dataGridView1.Rows[e.RowIndex].Cells["CostOfClassColumn"]);

                        // Now that a class type has been selected, we can populate
                        // the cost of class drop down box appropriately.
                        cell.Value = null;
                        cell.DataSource = null;
                        if (cell.Items != null)
                        {
                            cell.Items.Clear();
                        }
                        cell.DataSource = possibleCosts1;
                        cell.DisplayMember = "CostValue";
                        cell.ValueMember = "CostId";
                    }
                }

                string classCostId = (string)dataGridView1.Rows[e.RowIndex].Cells["CostOfClassColumn"].Value;
                if (classCostId != null)
                {
                    Cost cost = costRepository.GetById(classCostId);
                    training.ClassCost = cost;
                }

                string preK9DaycareCostId = (string)dataGridView1.Rows[e.RowIndex].Cells["PreK9DaycareCostColumn"].Value;
                if (preK9DaycareCostId != null)
                {
                    Cost cost = costRepository.GetById(preK9DaycareCostId);
                    training.PreK9DaycareCost = cost;
                }

                training.User = user;

                string trainingId = (string)dataGridView1.Rows[e.RowIndex].Cells["TrainingIdColumn"].Value;

                training.TrainingId = trainingId;

                // Add object to dirty objects map.
                if (!dirtyObjectsMap.Keys.Contains(e.RowIndex))
                {
                    dirtyObjectsMap.Add(e.RowIndex, training);
                }

                // Remove the entry from the delete map, if
                // an entry for the Daycare exists in the
                // delete map already.
                if (deleteObjectsMap.Keys.Contains(e.RowIndex))
                {
                    deleteObjectsMap.Remove(e.RowIndex);
                }

                var isSelected = dataGridView1.Rows[e.RowIndex].Cells["SelectColumn"].Value;

                if (isSelected != null && (bool)isSelected)
                {
                    deleteObjectsMap.Add(e.RowIndex, training);
                }
            }
        }