/// <summary> /// Function to Save a ConnectionPointEntity in the database. /// </summary> /// <param name="connectionPoint">ConnectionPointEntity to save</param> /// <param name="scope">Interna structure to avoid circular reference locks. Provide an instance when calling from other data access object.</param> /// <exception cref="ArgumentNullException"> /// If <paramref name="connectionPoint"/> is not a <c>ConnectionPointEntity</c>. /// </exception> /// <exception cref="UtnEmallDataAccessException"> /// If an DbException occurs in the try block while accessing the database. /// </exception> public void Save(ConnectionPointEntity connectionPoint, Dictionary <string, IEntity> scope) { if (connectionPoint == null) { throw new ArgumentException("The argument can't be null"); } // Create a unique key to identify the object in the internal scope string scopeKey = connectionPoint.Id.ToString(NumberFormatInfo.InvariantInfo) + "ConnectionPoint"; if (scope != null) { // If it's on the scope return it, don't save again if (scope.ContainsKey(scopeKey)) { return; } } else { // Create a new scope if it's not provided scope = new Dictionary <string, IEntity>(); } try { // Open a DbConnection and a new transaction if it isn't on a higher level one if (!isGlobalTransaction) { dbConnection = dataAccess.GetNewConnection(); dbConnection.Open(); dbTransaction = dbConnection.BeginTransaction(); } string commandName = ""; bool isUpdate = false; // Check if it is an insert or update command if (connectionPoint.IsNew || !DataAccessConnection.ExistsEntity(connectionPoint.Id, "ConnectionPoint", "idConnectionPoint", dbConnection, dbTransaction)) { commandName = "SaveConnectionPoint"; } else { isUpdate = true; commandName = "UpdateConnectionPoint"; ConnectionPointEntity connectionPointTemp0 = new ConnectionPointEntity(); connectionPointTemp0.Id = connectionPoint.Id; LoadRelationParentComponent(connectionPointTemp0, scope); if (connectionPointTemp0.ParentComponent != null && connectionPointTemp0.IdParentComponent != connectionPoint.IdParentComponent) { ComponentDataAccess componentDataAccess = new ComponentDataAccess(); componentDataAccess.SetConnectionObjects(dbConnection, dbTransaction); componentDataAccess.Delete(connectionPointTemp0.ParentComponent, scope); } } // Create a db command IDbCommand sqlCommand = dataAccess.GetNewCommand(commandName, dbConnection, dbTransaction); sqlCommand.CommandType = CommandType.StoredProcedure; // Add parameters values to current command IDbDataParameter parameter; if (isUpdate) { parameter = dataAccess.GetNewDataParameter("@idConnectionPoint", DbType.Int32); parameter.Value = connectionPoint.Id; sqlCommand.Parameters.Add(parameter); } FillSaveParameters(connectionPoint, sqlCommand); // Execute the command if (isUpdate) { sqlCommand.ExecuteNonQuery(); } else { IDbDataParameter parameterIdOutput = dataAccess.GetNewDataParameter("@idConnectionPoint", DbType.Int32); parameterIdOutput.Direction = ParameterDirection.ReturnValue; sqlCommand.Parameters.Add(parameterIdOutput); sqlCommand.ExecuteNonQuery(); connectionPoint.Id = Convert.ToInt32(parameterIdOutput.Value, NumberFormatInfo.InvariantInfo); } scopeKey = connectionPoint.Id.ToString(NumberFormatInfo.InvariantInfo) + "ConnectionPoint"; // Add entity to current internal scope scope.Add(scopeKey, connectionPoint); // Save collections of related objects to current entity // Save objects related to current entity if (connectionPoint.ParentComponent != null) { ComponentDataAccess componentDataAccess = new ComponentDataAccess(); componentDataAccess.SetConnectionObjects(dbConnection, dbTransaction); componentDataAccess.Save(connectionPoint.ParentComponent, scope); } if (connectionPoint.Component != null) { ComponentDataAccess componentDataAccess = new ComponentDataAccess(); componentDataAccess.SetConnectionObjects(dbConnection, dbTransaction); componentDataAccess.Save(connectionPoint.Component, scope); } // Update Update(connectionPoint); // Close transaction if initiated by me if (!isGlobalTransaction) { dbTransaction.Commit(); } // Update new and changed flags connectionPoint.IsNew = false; connectionPoint.Changed = false; } catch (DbException dbException) { // Rollback transaction if (!isGlobalTransaction) { dbTransaction.Rollback(); } // Rethrow as custom exception throw new UtnEmallDataAccessException(dbException.Message, dbException); } finally { // Close connection if initiated by me if (!isGlobalTransaction) { dbConnection.Close(); dbConnection = null; dbTransaction = null; } } }
/// <summary> /// Function to Delete a TableEntity from database. /// </summary> /// <param name="table">TableEntity to delete</param> /// <param name="scope">Internal structure to avoid circular reference locks. Must provide an instance while calling from other data access object.</param> /// <exception cref="ArgumentNullException"> /// If <paramref name="table"/> is not a <c>TableEntity</c>. /// </exception> /// <exception cref="UtnEmallDataAccessException"> /// If an DbException occurs in the try block while accessing the database. /// </exception> public void Delete(TableEntity table, Dictionary <string, IEntity> scope) { if (table == null) { throw new ArgumentException("The argument can't be null"); } try { // Open connection and initialize a transaction if needed if (!isGlobalTransaction) { dbConnection = dataAccess.GetNewConnection(); dbConnection.Open(); dbTransaction = dbConnection.BeginTransaction(); } // Reload the entity to ensure deletion of older data table = this.Load(table.Id, true); if (table == null) { throw new UtnEmallDataAccessException("Error retrieving data while trying to delete."); } // Create a command for delete string cmdText = "DeleteTable"; IDbCommand sqlCommand = dataAccess.GetNewCommand(cmdText, dbConnection, dbTransaction); sqlCommand.CommandType = CommandType.StoredProcedure; // Add values to parameters IDbDataParameter parameterID = dataAccess.GetNewDataParameter("@idTable", DbType.Int32); parameterID.Value = table.Id; sqlCommand.Parameters.Add(parameterID); // Execute the command sqlCommand.ExecuteNonQuery(); // Delete related objects if (table.Fields != null) { this.DeleteFieldCollection(new FieldDataAccess(), table.Fields, scope); } if (table.Component != null) { ComponentDataAccess componentDataAccess = new ComponentDataAccess(); componentDataAccess.SetConnectionObjects(dbConnection, dbTransaction); componentDataAccess.Delete(table.Component, scope); } // Commit transaction if is mine if (!isGlobalTransaction) { dbTransaction.Commit(); } // Remove entity from loaded objects inMemoryEntities.Remove(table.Id); // Remove entity from current internal scope if (scope != null) { string scopeKey = table.Id.ToString(NumberFormatInfo.InvariantInfo) + "Table"; scope.Remove(scopeKey); } } catch (DbException dbException) { // Rollback transaction if (!isGlobalTransaction) { dbTransaction.Rollback(); } // Rethrow as custom exception throw new UtnEmallDataAccessException(dbException.Message, dbException); } finally { // Close connection if it was initiated by this instance if (!isGlobalTransaction) { dbConnection.Close(); dbConnection = null; dbTransaction = null; } } }