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); } }
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); } }