/// <summary> /// Modify the given DataSource in the database /// </summary> public virtual void Modify(Model.DataSource modifiedDataSource) { try { Trace.WriteVerbose("({0})", "Modify", CLASSNAME, modifiedDataSource.ToString()); var helper = Database.GetDbHelper(); int recordsAffected = helper.ExecuteSPNonQuery(_storedProcedure_Modify, helper.CreateInputParam("@Id", modifiedDataSource.Id), helper.CreateInputParam("@ListName", modifiedDataSource.ListName), helper.CreateInputParam("@Object_Id", modifiedDataSource.Object_Id), helper.CreateInputParam("@Key_Column_Id", modifiedDataSource.Key_Column_Id), helper.CreateInputParam("@Value_Column_Id", modifiedDataSource.Value_Column_Id)); if (recordsAffected == 0) { throw new DalNothingUpdatedException("No records were updated (Table: DataSources). DataSource=" + modifiedDataSource.ToString()); } } catch (Exception ex) { Trace.WriteError("({0})", "Modify", CLASSNAME, ex, modifiedDataSource.ToString()); throw DbHelper.TranslateException(ex); } }
/// <summary> /// Delete the given DataSource from the database /// </summary> public virtual void Delete(Model.DataSource delDataSource) { try { Trace.WriteInformation("({0})", "Delete", CLASSNAME, delDataSource); //Begin Checks if (!Exists(delDataSource)) { throw new BusinessException(string.Format("There is no DataSource with this id. ({0})", delDataSource)); } DataAccess.DataSources dataSources = new DataAccess.DataSources(); dataSources.Delete(delDataSource); } catch (DalForeignKeyException ex_fk) { Trace.WriteError("({0})", "Delete", CLASSNAME, ex_fk, delDataSource); throw new BusinessException(string.Format("The DataSource is still used by {0}", ex_fk.Table), ex_fk); } catch (Exception ex) { Trace.WriteError("({0})", "Delete", CLASSNAME, ex, delDataSource); throw; } }
/// <summary> /// Add a new DataSource to the database /// </summary> public virtual Int32 Add(Model.DataSource newDataSource) { try { Trace.WriteVerbose("({0})", "Add", CLASSNAME, newDataSource.ToString()); var helper = Database.GetDbHelper(); DbParameter IdParam = helper.CreateOutputParam("@Id", DbType.Int32); int recordsAffected = helper.ExecuteSPNonQuery(_storedProcedure_Add, IdParam, helper.CreateInputParam("@ListName", newDataSource.ListName), helper.CreateInputParam("@Object_Id", newDataSource.Object_Id), helper.CreateInputParam("@Key_Column_Id", newDataSource.Key_Column_Id), helper.CreateInputParam("@Value_Column_Id", newDataSource.Value_Column_Id)); if (recordsAffected == 0) { throw new DalNothingUpdatedException("Unable to add DataSource with Id={0}", newDataSource); } return((Int32)IdParam.Value); } catch (Exception ex) { Trace.WriteError("({0})", "Add", CLASSNAME, ex, newDataSource.ToString()); throw DbHelper.TranslateException(ex); } }
/// <summary> /// Get a DataSource by id from the database /// </summary> public virtual Model.DataSource GetById(Int32 id) { DbDataReader reader = null; try { var helper = Database.GetDbHelper(); reader = helper.ExecuteSPReader(_storedProcedure_GetById, helper.CreateInputParam("@Id", id)); Model.DataSource result = null; if (reader.Read()) { result = CreateDataSource(reader); } return(result); } catch (Exception ex) { Trace.WriteError("{0}", "GetById", CLASSNAME, ex, id); throw DbHelper.TranslateException(ex); } finally { if (reader != null) { reader.Close(); } } }
/// <summary> /// Equals function to compare class /// </summary> public virtual bool Exists(Model.DataSource dataSource) { try { return(this.GetById(dataSource.Id) != null); } catch (Exception ex) { Trace.WriteError("({0})", "Exists", CLASSNAME, ex, dataSource); throw; } }
/// <summary> /// Check Datafield constraints /// </summary> protected virtual void CheckConstraints(Model.DataSource dataSource) { //Range checks, etc checks go here if (dataSource.ListName == null) { throw new BusinessException(string.Format("ListName may not be NULL. ({0})", dataSource.ListName)); } if (dataSource.ListName.Length > 50) { throw new BusinessException(string.Format("ListName may not be longer than 50 characters. ({0})", dataSource.ListName)); } }
/// <summary> /// Get a DataSource by id from the database /// </summary> public virtual Model.DataSource GetById(Int32 id) { try { DataAccess.DataSources dataSources = new DataAccess.DataSources(); Model.DataSource result = dataSources.GetById(id); return(result); } catch (Exception ex) { Trace.WriteError("{0}", "GetById", CLASSNAME, ex, id); throw; } }
/// <summary> /// Delete the given DataSource from the database /// </summary> public virtual void Delete(Model.DataSource dataSource) { try { Trace.WriteVerbose("({0})", "Delete", CLASSNAME, dataSource.ToString()); var helper = Database.GetDbHelper(); helper.ExecuteSPNonQuery(_storedProcedure_Delete, helper.CreateInputParam("@Id", dataSource.Id)); } catch (Exception ex) { Trace.WriteError("({0})", "Delete", CLASSNAME, ex, dataSource.ToString()); throw DbHelper.TranslateException(ex); } }
/// <summary> /// Create a Model.DataSource /// </summary> protected virtual Model.DataSource CreateDataSource(DbDataReader reader) { try { Model.DataSource result = new Model.DataSource( (Int32)reader["Id"], (String)reader["ListName"], (Int32)reader["Object_Id"], (Int32)reader["Key_Column_Id"], (Int32)reader["Value_Column_Id"] ); return(result); } catch (Exception ex) { Trace.WriteError("", "CreateDataSource", CLASSNAME, ex); throw DbHelper.TranslateException(ex); } }
/// <summary> /// Add a new DataSource to the database /// </summary> public virtual Int32 Add(Model.DataSource newDataSource) { try { Trace.WriteInformation("({0})", Trace.GetMethodName(), CLASSNAME, newDataSource); CheckConstraints(newDataSource); DataAccess.DataSources dataSources = new DataAccess.DataSources(); return(dataSources.Add(newDataSource)); } catch (DalForeignKeyException ex_fk) { Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex_fk, newDataSource); throw new BusinessException(string.Format("No related object found in {0}", ex_fk.Table), ex_fk); } catch (Exception ex) { Trace.WriteError("({0})", Trace.GetMethodName(), CLASSNAME, ex, newDataSource); throw; } }
/// <summary> /// Modify the given DataSource in the database /// </summary> public virtual void Modify(Model.DataSource modifiedDataSource) { try { Trace.WriteInformation("({0})", "Modify", CLASSNAME, modifiedDataSource); //Begin Checks CheckConstraints(modifiedDataSource); if (!Exists(modifiedDataSource)) { throw new BusinessException(string.Format("There is no DataSource with this id. ({0})", modifiedDataSource)); } DataAccess.DataSources dataSources = new DataAccess.DataSources(); dataSources.Modify(modifiedDataSource); } catch (Exception ex) { Trace.WriteError("({0})", "Modify", CLASSNAME, ex, modifiedDataSource); throw; } }