コード例 #1
0
        private void dgvPartyFusions_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_cellChanged)
            {
                string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;
                _logger.OpenSection(location);

                _logger.Info("Called with row index " + e.RowIndex + ", column index = " + e.ColumnIndex);
                var currentRow = this.dgvPartyFusions.Rows[e.RowIndex];

                Domain.Demon rowDemon         = null;
                Domain.Demon databaseDemon    = null;
                bool         insertedNewDemon = false;

                // THIS CODE IS COPIED FROM THE DEMONS LIST FORM
                // FIXME: REFACTOR / DO SOMETHING BETTER
                using (var transaction = _dbSession.BeginTransaction())
                {
                    rowDemon = GetDemonFromDataGridViewRow(currentRow, true);
                    if (rowDemon != null)
                    {
                        databaseDemon = rowDemon;
                        bool insertDemon  = true;
                        bool insertFusion = true;
                        if (rowDemon.Id == null)
                        {
                            // ID is null but maybe the demon is already in the DB. Look in the database
                            _logger.Info("Looking in database if this demon already exists...");
                            databaseDemon = _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                                            SelectMany(x => x.Demons).Where(y => y.Name == rowDemon.Name).FirstOrDefault();
                            if (databaseDemon != null)
                            {
                                _logger.Info("Found demon ID " + databaseDemon.Id + ".");
                                if (databaseDemon.Equals(rowDemon))
                                {
                                    _logger.Info("Demons are exactly the same; no need to update DB.");
                                }
                            }
                            else
                            {
                                insertDemon   = true;
                                databaseDemon = rowDemon;
                            }
                        }


                        if (insertDemon)
                        {
                            _logger.Info("Inserting new demon...");

                            // TODO: UPDATING
                            _logger.Info("Row demon is " + rowDemon.ToString() + ". " +
                                         (databaseDemon.Id == null ? "Inserting..." : "Updating..."));

                            if (databaseDemon.Id == null)
                            {
                                _dbSession.Save(databaseDemon); // insert
                                insertedNewDemon = true;
                            }

                            _logger.Info("Demon saved (for now; need to commit).");
                        }

                        if (insertFusion)
                        {
                            _logger.Info("Inserting new fusion...");
                            Domain.Fusion f = new Domain.Fusion(
                                (Domain.Race)currentRow.Cells[(int)MyDataGridColumns.colRaceObject1].Value,
                                (Domain.Race)currentRow.Cells[(int)MyDataGridColumns.colRaceObject2].Value,
                                databaseDemon.Race);
                            _dbSession.Save(f);

                            _logger.Info("Fusion saved (for now; need to commit).");
                        }

                        if (insertDemon || insertFusion)
                        {
                            try
                            {
                                _logger.Info("Committing...");
                                transaction.Commit();
                                _logger.Info("Transaction complete.");
                                LoadData();
                            }
                            catch (Exception ex)
                            {
                                _logger.Error(ex);
                                MessageBox.Show("ERROR: " + ex.Message +
                                                ex.InnerException == null ? "" : "\r\n" + ex.InnerException.Message);
                            }
                        }
                    }
                } // using (var transaction = _dbSession.BeginTransaction())

                var numberOfDemonsForFusions =
                    _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                    SelectMany(x => x.Demons).Where(y => y.UseInFusionCalculatorBoolean).ToList();
                if (numberOfDemonsForFusions.Count == 2)
                {
                    GlobalObjects.MainForm.ForceUpdateFusions();
                }

                if (insertedNewDemon)
                {
                    RemoveHandlers();
                    currentRow.Cells[(int)MyDataGridColumns.colId3].Value = databaseDemon.Id;
                    AddHandlers();
                }
                // END OF COPIED CODE
                // FIXME: REFACTOR / DO SOMETHING BETTER

                _logger.CloseSection(location);
            }
        }
コード例 #2
0
        private void dgvDemons_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_cellChanged)
            {
                string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;
                _logger.OpenSection(location);

                _logger.Info("Called with row index " + e.RowIndex + ", column index = " + e.ColumnIndex);
                var currentRow = this.dgvDemons.Rows[e.RowIndex];

                Domain.Demon rowDemon         = null;
                Domain.Demon databaseDemon    = null;
                bool         insertedNewDemon = false;

                using (var transaction = _dbSession.BeginTransaction())
                {
                    rowDemon = GetDemonFromDataGridViewRowDemonColumn(currentRow);
                    this.RemoveHandlers();
                    if (e.ColumnIndex == COLUMN_NAME)
                    {
                        this.dgvDemons.Rows.Remove(currentRow);
                    }
                    if (rowDemon != null)
                    {
                        if (e.ColumnIndex == COLUMN_IN_PARTY)
                        {
                            rowDemon.InParty = Math.Abs(1 - rowDemon.InParty);
                            //_dbSession.SaveOrUpdate(rowDemon);
                        }
                        else if (e.ColumnIndex == COLUMN_NAME)
                        {
                            if (rowDemon.UseInFusionCalculatorBoolean)
                            {
                                MessageBox.Show("Cannot have more than one " + rowDemon.Name + ".");
                            }
                            else
                            {
                                _logger.Info("Adding the following demon to fusion calculator: '" + rowDemon.ToString() + "'");
                                rowDemon.UseInFusionCalculator = 1;
                                _dbSession.SaveOrUpdate(rowDemon);
                                _logger.Info("Added to fusion calculator.");

                                _logger.Info("Updating interface...");
                                this.dgvDemons.Rows.Add(CreateRow(rowDemon));
                                _logger.Info("Updated.");
                            }
                        }
                        SetDataGridViewReadOnlyPropertyAndColors();
                    } // if (rowDemon != null)

                    this.AddHandlers();
                    transaction.Commit();
                } // using (var transaction = _dbSession.BeginTransaction())

                if (_addingRow)
                {
                    _addingRow = false;
                    GlobalObjects.MainForm.UpdateFusions();
                }
                _logger.CloseSection(location);
            }
        }
コード例 #3
0
        private Domain.Demon GetDemonFromDataGridViewRowDemonColumn(DataGridViewRow dgvr)
        {
            string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;

            _logger.OpenSection(location);

            Domain.Demon returnDemon = null;

            object nameValue = dgvr.Cells[(int)MyDataGridColumns.colName3].Value;

            _logger.Info("Cell contains name '" + nameValue + "'");

            if (nameValue != null)
            {
                string nameString       = nameValue.ToString();
                var    currentGameRaces = _dbSession.Get <Domain.Game>(GlobalObjects.CurrentGame.Id).Races;
                returnDemon = currentGameRaces.SelectMany(x => x.Demons).Where(y => y.Name == nameString).FirstOrDefault();
                _logger.Info(returnDemon == null ? "Could not find a demon." : "Found demon '" + returnDemon.ToString() + "'");
            }
            return(returnDemon);
        }
コード例 #4
0
        private void dgvDemons_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (_cellChanged)
            {
                string location = this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name;
                _logger.OpenSection(location);

                _logger.Info("Called with row index " + e.RowIndex + ", column index = " + e.ColumnIndex);
                var currentRow = this.dgvDemons.Rows[e.RowIndex];

                Domain.Demon rowDemon         = null;
                Domain.Demon databaseDemon    = null;
                bool         insertedNewDemon = false;

                using (var transaction = _dbSession.BeginTransaction())
                {
                    rowDemon = GetDemonFromDataGridViewRow(currentRow, true);
                    if (rowDemon != null)
                    {
                        databaseDemon = rowDemon;
                        bool proceedWithDatabaseOperation = true;
                        if (rowDemon.Id == null)
                        {
                            // ID is null but maybe the demon is already in the DB. Look in the database
                            _logger.Info("Looking in database if this demon already exists...");
                            databaseDemon = _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                                            SelectMany(x => x.Demons).Where(y => y.Name == rowDemon.Name).FirstOrDefault();
                            if (databaseDemon != null)
                            {
                                _logger.Info("Found demon ID " + databaseDemon.Id + ".");
                                if (databaseDemon.Equals(rowDemon))
                                {
                                    _logger.Info("Demons are exactly the same; no need to update DB.");
                                    proceedWithDatabaseOperation = false;
                                }
                            }
                            else
                            {
                                // if null just revert to the rowDemon again, we'll add that.
                                databaseDemon = rowDemon;
                            }
                        }

                        if (proceedWithDatabaseOperation)
                        {
                            _logger.Info("Row demon is " + rowDemon.ToString() + ". " +
                                         (databaseDemon.Id == null ? "Inserting..." : "Updating..."));

                            if (databaseDemon.Id == null)
                            {
                                _dbSession.Save(databaseDemon); // insert
                                insertedNewDemon = true;
                            }
                            else
                            {
                                if (e.ColumnIndex == (int)MyDataGridColumns.columnInParty)
                                {
                                    databaseDemon.InParty =
                                        (bool)this.dgvDemons.Rows[e.RowIndex].Cells[(int)MyDataGridColumns.columnInParty].Value ? 1 : 0;
                                }
                                else if (e.ColumnIndex == (int)MyDataGridColumns.columnUseInFusions)
                                {
                                    databaseDemon.UseInFusionCalculator =
                                        (bool)this.dgvDemons.Rows[e.RowIndex].Cells[(int)MyDataGridColumns.columnUseInFusions].Value ? 1 : 0;
                                }
                                _dbSession.SaveOrUpdate(databaseDemon); // update
                            }
                            transaction.Commit();
                            _logger.Info("Demon saved.");
                        }
                    }
                } // using (var transaction = _dbSession.BeginTransaction())

                var numberOfDemonsForFusions =
                    _dbSession.CreateCriteria <Domain.Race>().List <Domain.Race>().
                    SelectMany(x => x.Demons).Where(y => y.UseInFusionCalculatorBoolean).ToList();
                if (numberOfDemonsForFusions.Count == 2)
                {
                    GlobalObjects.MainForm.ForceUpdateFusions();
                }

                if (insertedNewDemon)
                {
                    RemoveHandlers();
                    currentRow.Cells[(int)MyDataGridColumns.columnObject].Value = databaseDemon;
                    currentRow.Cells[(int)MyDataGridColumns.columnId].Value     = databaseDemon.Id;
                    AddHandlers();
                }

                _logger.CloseSection(location);
            }
        }