/// <summary> /// Read markers for a single animal record from the database. /// </summary> /// <param name="Data"> /// The animal attributes object that will contain the marker data /// </param> protected override void ReadMarkers(cAnimalAttributes Data) { if (mvarConnection == null) { ThrowConnectionException(); } if (Data == null) { ThrowAnimalAttributesException(); } // return false if we have reached the end of the record set if (ReadPosition < mvarDataSet.Tables["Animals"].Rows.Count) { // copy the data to the Attributes object DataTable dt = mvarDataSet.Tables["Animals"]; // get the datarow at the current read position DataRow dr = dt.Rows[ReadPosition]; // get the basic animal data Data.AutoMarker = Convert.ToString(dr["AutoMarker"]); Data.Marker = Convert.ToString(dr["Marker"]); try { Data.PartnerMarker = Convert.ToString(dr["PartnerMarker"]); } catch { Data.PartnerMarker = ""; } } }
/// <summary> /// Create a new specific animal type /// </summary> /// <param name="Animal">The animal attributes used to define the animal.</param> /// <param name="BG">The background in which the animal will live.</param> /// <returns>The new animal as a cAnimal type.</returns> protected override cAnimal GetNewAnimal(cAnimalAttributes NewAnimal, cBackground BG) { // make sure parameters are not null if (NewAnimal == null) { ThrowAnimalsException(); } if (BG == null) { ThrowBackgroundException(); } // return a new raccoon return(new cRaccoon(NewAnimal, BG)); }
/// <summary> /// Write markers for a single animal record into the database. /// </summary> /// <param name="Data"> /// The animal attributes object containing the data to be written. /// </param> /// <param name="DeleteFirst"> /// A flag indicating whether or not an existing record with the same ID should /// be deleted before the current data is written. /// </param> protected override void WriteMarkers(cAnimalAttributes Data, bool DeleteFirst) { if (mvarConnection == null) { ThrowConnectionException(); } if (this.ReadOnly) { ThrowReadOnlyException(); } if (Data == null) { ThrowAnimalAttributesException(); } // create the command object whch will be used to run the required queries OleDbCommand cmd = new OleDbCommand(); cmd.Connection = mvarConnection; cmd.CommandType = CommandType.StoredProcedure; if (DeleteFirst) { // run a DELETE query to remove current version of this record RunDeleteQuery("DeleteMarkers", Data.ID, cmd); } // write the data // run the stored procedure to insert the main record cmd.CommandText = "InsertMarkers"; cmd.Parameters.Clear(); // set parameters cmd.Parameters.AddWithValue("New_AnimalID", Data.ID); cmd.Parameters.AddWithValue("New_AutoMarker", Data.AutoMarker); cmd.Parameters.AddWithValue("New_Marker", Data.Marker); cmd.Parameters.AddWithValue("New_PartnerMarker", (Data.PartnerMarker == null ? "" : Data.PartnerMarker)); // execute the query cmd.ExecuteNonQuery(); }
/// <summary> /// Get the next record in the fox data source. An InvalidOperationException /// exception is raised if the datasource has not been connected. /// </summary> /// <param name="Attributes"> /// The attributes to add or update. An ArgumentNullException exception is raised /// if Attributes is null. /// </param> /// <param name="Background"> /// The background object in which this animal will live. An ArgumentNullException /// exception is raised if Background is null. /// </param> /// <returns>False if the last record has already been read.</returns> protected override bool GetNextAnimalRecord(cAnimalAttributes Attributes, cBackground Background) { if (mvarConnection == null) { ThrowConnectionException(); } if (Attributes == null) { ThrowAnimalAttributesException(); } if (Background == null) { ThrowBackgroundException(); } // return false if we have reached the end of the record set if (ReadPosition == mvarDataSet.Tables["Animals"].Rows.Count) { return(false); } // copy the data to the Attributes object DataTable dt = mvarDataSet.Tables["Animals"]; // get the datarow at the current read position DataRow dr = dt.Rows[ReadPosition]; // get the basic animal data Attributes.ID = Convert.ToString(dr["ID"]); Attributes.ParentID = Convert.ToString(dr["Parent_ID"]); Attributes.Gender = (enumGender)Convert.ToInt32(dr["Gender"]); Attributes.IsAlive = Convert.ToBoolean(dr["IsAlive"]); Attributes.IsIndependent = Convert.ToBoolean(dr["IsIndependent"]); Attributes.YearDied = Convert.ToInt32(dr["YearDied"]); Attributes.WeekDied = Convert.ToInt32(dr["WeekDied"]); Attributes.Age = Convert.ToInt32(dr["Age"]); if (dt.Columns.Contains("ListIndex")) { Attributes.ListIndex = Convert.ToInt32(dr["ListIndex"]); } try { Attributes.CannotGiveBirth = Convert.ToInt32(dr["CannotGiveBirth"]); } catch { Attributes.CannotGiveBirth = 0; } // now get cells, disease, offspring and vaccine info DataRow[] drChildren; // get the cells info drChildren = dr.GetChildRows(mvarDataSet.Relations["CellsRelation"]); foreach (DataRow drChild in drChildren) { cCellTime CT = new cCellTime(); CT.CellID = Convert.ToString(drChild["Cell_ID"]); CT.Year = Convert.ToInt32(drChild["Year"]); CT.Week = Convert.ToInt32(drChild["Week"]); Attributes.Cells.Add(CT); } // get the offspring info drChildren = dr.GetChildRows(mvarDataSet.Relations["OffspringRelation"]); foreach (DataRow drChild in drChildren) { Attributes.Offspring.Add(Convert.ToString(drChild["Offspring_ID"])); } // get the disease info drChildren = dr.GetChildRows(mvarDataSet.Relations["DiseaseRelation"]); foreach (DataRow drChild in drChildren) { string DiseaseName = Convert.ToString(drChild["Disease_Name"]); int Year = Convert.ToInt32(drChild["Year"]); int Week = Convert.ToInt32(drChild["Week"]); int IncubationPeriod = Convert.ToInt32(drChild["IncubationPeriod"]); int InfectiousPeriod = Convert.ToInt32(drChild["InfectiousPeriod"]); string InfectingAnimalID = Convert.ToString(drChild["InfectingAnimalID"]); string NaturalImmunity = Convert.ToString(drChild["Natural_Immunity"]); Attributes.Infections.Add(new cInfection(Background, DiseaseName, Year, Week, IncubationPeriod, InfectiousPeriod, InfectingAnimalID)); } // get vaccine info drChildren = dr.GetChildRows(mvarDataSet.Relations["VaccinesRelation"]); foreach (DataRow drChild in drChildren) { string DiseaseName = Convert.ToString(drChild["Disease_Name"]); int Year = Convert.ToInt32(drChild["Year"]); int Week = Convert.ToInt32(drChild["Week"]); int EffectivePeriod = Convert.ToInt32(drChild["Effective_Period"]); Attributes.Vaccines.Add(new cVaccine(DiseaseName, Year, Week, EffectivePeriod)); } // return true, indicating that a value was read return(true); }
/// <summary> /// Write a single animal attributes record to the database. The record is /// either added to the datasource or updated in the datasource if it already /// exists. An InvalidOperationException exception is raised if the datasource /// is open for reading only or if the datasource has not been connected. /// </summary> /// <param name="Attributes"> /// The attributes to add or update. An ArgumentNullException exception is raised /// if Attributes is null. /// </param> /// <param name="DeleteFirst"> /// A flag indicating whether or not an existing record with the same ID should /// be deleted before the current data is written. /// </param> protected override void WriteAnimalRecord(cAnimalAttributes Attributes, bool DeleteFirst) { if (mvarConnection == null) { ThrowConnectionException(); } if (this.ReadOnly) { ThrowReadOnlyException(); } if (Attributes == null) { ThrowAnimalAttributesException(); } // create the command object whch will be used to run the required queries OleDbCommand cmd = new OleDbCommand(); cmd.Connection = mvarConnection; cmd.CommandType = CommandType.StoredProcedure; if (DeleteFirst) { // run DELETE queries to remove current version of this record RunDeleteQuery("DeleteAnimals", Attributes.ID, cmd); RunDeleteQuery("DeleteCells", Attributes.ID, cmd); RunDeleteQuery("DeleteDisease", Attributes.ID, cmd); RunDeleteQuery("DeleteVaccines", Attributes.ID, cmd); RunDeleteQuery("DeleteOffspring", Attributes.ID, cmd); } // write the data // run the stored procedure to insert the main record cmd.CommandText = "InsertAnimal"; cmd.Parameters.Clear(); // set parameters cmd.Parameters.AddWithValue("New_ID", Attributes.ID); cmd.Parameters.AddWithValue("New_Age", Attributes.Age); if (Attributes.ParentID.Length > 0) { cmd.Parameters.AddWithValue("New_ParentID", Attributes.ParentID); } else { cmd.Parameters.AddWithValue("New_ParentID", -1); } cmd.Parameters.AddWithValue("New_Gender", Convert.ToInt32(Attributes.Gender)); cmd.Parameters.AddWithValue("New_IsAlive", Attributes.IsAlive); cmd.Parameters.AddWithValue("New_YearBorn", Attributes.Cells[0].Year); cmd.Parameters.AddWithValue("New_WeekBorn", Attributes.Cells[0].Week); cmd.Parameters.AddWithValue("New_YearDied", Attributes.YearDied); cmd.Parameters.AddWithValue("New_WeekDied", Attributes.WeekDied); cmd.Parameters.AddWithValue("New_IsIndependent", Attributes.IsIndependent); cmd.Parameters.AddWithValue("New_CannotGiveBirth", Attributes.CannotGiveBirth); cmd.Parameters.AddWithValue("New_ListIndex", Attributes.ListIndex); cmd.ExecuteNonQuery(); // update the cells table cmd.CommandText = "InsertCell"; foreach (cCellTime CT in Attributes.Cells) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("New_Animal_ID", Attributes.ID); cmd.Parameters.AddWithValue("New_Cell_ID", CT.CellID); cmd.Parameters.AddWithValue("New_Year", CT.Year); cmd.Parameters.AddWithValue("New_Week", CT.Week); cmd.ExecuteNonQuery(); } // update the offspring table cmd.CommandText = "InsertOffspring"; foreach (string OffspringID in Attributes.Offspring) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("New_Animal_ID", Attributes.ID); cmd.Parameters.AddWithValue("New_Offspring_ID", OffspringID); cmd.ExecuteNonQuery(); } // update the disease table cmd.CommandText = "InsertDisease"; foreach (cInfection Inf in Attributes.Infections) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("New_Animal_ID", Attributes.ID); cmd.Parameters.AddWithValue("New_Disease_Name", Inf.Disease.Name); cmd.Parameters.AddWithValue("New_Year", Inf.Year); cmd.Parameters.AddWithValue("New_Week", Inf.Week); cmd.Parameters.AddWithValue("New_IncubationPeriod", Inf.IncubationPeriod); cmd.Parameters.AddWithValue("New_InfectiousPeriod", Inf.InfectiousPeriod); cmd.Parameters.AddWithValue("New_InfectingAnimalID", Inf.InfectingAnimalID); cmd.ExecuteNonQuery(); } // update the vaccines table cmd.CommandText = "InsertVaccine"; foreach (cVaccine Vaccine in Attributes.Vaccines) { cmd.Parameters.Clear(); cmd.Parameters.AddWithValue("New_Animal_ID", Attributes.ID); cmd.Parameters.AddWithValue("New_Disease_Name", Vaccine.DiseaseName); cmd.Parameters.AddWithValue("New_Year", Vaccine.Year); cmd.Parameters.AddWithValue("New_Week", Vaccine.Week); cmd.Parameters.AddWithValue("New_EffectivePeriod", Vaccine.EffectivePeriod); cmd.ExecuteNonQuery(); } }
/// <summary> /// Construct a fox from a cAnimalAttributes object. This constructor is /// used when loading fox data from a datasource. /// </summary> /// <param name="Attributes"> /// The cAnimalAttributes object containing the attributes for the fox. An /// ArgumentNullException exception is raised if Attributes is null. /// </param> /// <param name="Background"> /// The background object in which the fox lives. An ArgumentNullException /// exception is raised if Background is null. /// </param> public cFox(cAnimalAttributes Attributes, cBackground Background) : base(Attributes, Background) { }
/// <summary> /// Construct a raccoon from a cAnimalAttributes object. This constructor is /// used when loading raccoon data from a datasource. /// </summary> /// <param name="Attributes"> /// The cAnimalAttributes object containing the attributes for the raccoon. An /// ArgumentNullException exception is raised if Attributes is null. /// </param> /// <param name="Background"> /// The background object in which the raccoon lives. An ArgumentNullException /// exception is raised if Background is null. /// </param> public cRaccoon(cAnimalAttributes Attributes, cBackground Background) : base(Attributes, Background) { }