public void AddRecordToRepo(Sampling s) { EditedEntity = null; if (s == null) { throw new ArgumentNullException("Error: The argument is Null"); } SamplingCollection.Add(s); }
private List <Sampling> getSamplings() { if (BSCEntities.ProjectSettingViewModel == null) { BSCEntities.ProjectSettingViewModel = new ProjectSettingViewModel(); } List <Sampling> listSamplings = new List <Sampling>(); var dt = new DataTable(); using (var conection = new OleDbConnection(Global.ConnectionString)) { try { conection.Open(); string query = $@"SELECT * from Sampling"; var adapter = new OleDbDataAdapter(query, conection); adapter.Fill(dt); if (dt.Rows.Count > 0) { listSamplings.Clear(); foreach (DataRow dr in dt.Rows) { Sampling s = new Sampling { FisherGPS = BSCEntities.FisherGPSViewModel.GetFisherGPS(dr["FisherGPSID"].ToString()), RowID = dr["RowID"].ToString(), DateTimeDeparted = (DateTime)dr["DateTimeDeparted"], DateTimeArrived = (DateTime)dr["DateTimeArrived"], DateTimeSampled = (DateTime)dr["DateTimeSampled"], NSAPSamplingID = dr["NSAPSamplingID"].ToString(), Gear = BSCEntities.GearViewModel.GetGear(dr["GearID"].ToString()), ProjectSetting = BSCEntities.ProjectSettingViewModel.GetProjectSetting(dr["Project"].ToString()), LandingSite = BSCEntities.LandingSiteViewModel.GetLandingSite(dr["LandingSite"].ToString()), DateAdded = (DateTime)dr["DateAdded"] }; listSamplings.Add(s); } } } catch (Exception ex) { Logger.Log(ex); } } return(listSamplings); }
public void UpdateRecordInRepo(Sampling s) { EditedEntity = null; if (s.RowID == null) { throw new Exception("Error: ID cannot be null"); } int index = 0; while (index < SamplingCollection.Count) { if (SamplingCollection[index].RowID == s.RowID) { SamplingCollection[index] = s; break; } index++; } }
public bool Add(Sampling s) { bool success = false; s.DateAdded = DateTime.Now; using (OleDbConnection conn = new OleDbConnection(Global.ConnectionString)) { conn.Open(); var sql = $@"Insert into Sampling (FisherGPSID, RowID, DateTimeDeparted, DateTimeArrived, NSAPSamplingID, GearID, Project, DateTimeSampled, LandingSite, DateAdded) Values ({{{s.FisherGPS.RowID}}},{{{s.RowID}}},'{s.DateTimeDeparted}', '{s.DateTimeArrived}', '{s.NSAPSamplingID}',{{{s.Gear.GearID}}}, {{{s.ProjectSetting.ProjectID}}}, '{s.DateTimeSampled}', {{{s.LandingSite.LandingSiteID}}},'{s.DateAdded}')"; using (OleDbCommand update = new OleDbCommand(sql, conn)) { success = update.ExecuteNonQuery() > 0; } } return(success); }
private void SamplingCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: { int newIndex = e.NewStartingIndex; Sampling editedEntity = SamplingCollection[newIndex]; if (Samplings.Add(editedEntity)) { EditedEntity = new EditedEntity(EditAction.Add, editedEntity); } } break; case NotifyCollectionChangedAction.Remove: { List <Sampling> tempListOfRemovedItems = e.OldItems.OfType <Sampling>().ToList(); Sampling editedEntity = tempListOfRemovedItems[0]; if (Samplings.Delete(editedEntity.RowID)) { EditedEntity = new EditedEntity(EditAction.Delete, editedEntity); } } break; case NotifyCollectionChangedAction.Replace: { List <Sampling> tempListOfSamplings = e.NewItems.OfType <Sampling>().ToList(); Sampling editedEntity = tempListOfSamplings[0]; if (Samplings.Update(editedEntity)) // As the IDs are unique, only one row will be effected hence first index only { EditedEntity = new EditedEntity(EditAction.Update, editedEntity); } } break; } }
public bool Update(Sampling s) { bool success = false; using (OleDbConnection conn = new OleDbConnection(Global.ConnectionString)) { conn.Open(); var sql = $@"Update Sampling set FisherGPSID = {{{s.FisherGPS.RowID}}}, DateTimeDeparted = '{s.DateTimeDeparted}', DateTimeArrived='{s.DateTimeArrived}', NSAPSamplingID = '{s.NSAPSamplingID}', Project = {{{s.ProjectSetting.ProjectID}}}, GearID = {{{s.Gear.GearID}}}, LandingSite = {{{s.LandingSite.LandingSiteID}}}, DateTimeSampled = '{s.DateTimeSampled}' WHERE RowID={{{s.RowID}}}"; using (OleDbCommand update = new OleDbCommand(sql, conn)) { success = update.ExecuteNonQuery() > 0; } } return(success); }
public bool EntityValidated(Sampling sampling, out List <EntityValidationMessage> messages)//, bool isNew = false) { DateTime?gpsAssignedDate = null; DateTime?gpsReturnedDate = null; if (sampling.FisherGPS != null) { gpsAssignedDate = (DateTime)sampling.FisherGPS.DateAssigned; gpsReturnedDate = sampling.FisherGPS.DateReturned == null ? DateTime.Now : (DateTime)sampling.FisherGPS.DateReturned; } messages = new List <EntityValidationMessage>(); if (sampling.ProjectSetting == null) { messages.Add(new EntityValidationMessage("Project cannot be empty")); } if (!(sampling.NSAPSamplingID.Length == 0 || sampling.NSAPSamplingID.Length > 3)) { messages.Add(new EntityValidationMessage("NSAP sampling ID should be more than 3 characters long")); } if (sampling.FisherGPS == null) { messages.Add(new EntityValidationMessage("Fisher assigned with GPS should be complete")); } if (sampling.LandingSite == null) { messages.Add(new EntityValidationMessage("Landing site cannot be empty")); } if (sampling.Gear == null) { messages.Add(new EntityValidationMessage("Gear used cannot be empty")); } if (sampling.FisherGPS != null) { if (sampling.DateTimeDeparted == null) { messages.Add(new EntityValidationMessage("Date and time of departure cannot be empty")); } else if (sampling.DateTimeDeparted > DateTime.Now) { messages.Add(new EntityValidationMessage("Date and time of departure cannot a future date")); } else if (!(sampling.DateTimeDeparted >= gpsAssignedDate && sampling.DateTimeDeparted <= ((DateTime)gpsReturnedDate).AddHours(24))) { messages.Add(new EntityValidationMessage("Date and time of departure must be within dates when GPS was assigned to fisher")); } if (sampling.DateTimeArrived == null) { messages.Add(new EntityValidationMessage("Date and time of arrival cannot be empty")); } else if (sampling.DateTimeArrived > DateTime.Now) { messages.Add(new EntityValidationMessage("Date and time of arrival cannot a future date")); } else if (!(sampling.DateTimeArrived >= gpsAssignedDate && sampling.DateTimeArrived <= ((DateTime)gpsReturnedDate).AddHours(24))) { messages.Add(new EntityValidationMessage("Date and time of arrival must be within dates when GPS was assigned to fisher")); } if (sampling.DateTimeSampled == null) { messages.Add(new EntityValidationMessage("Date and time of sampling cannot be empty")); } else if (sampling.DateTimeSampled > DateTime.Now) { messages.Add(new EntityValidationMessage("Sampling date and time cannot be a future date")); } else if (!(sampling.DateTimeSampled >= gpsAssignedDate && sampling.DateTimeSampled <= ((DateTime)gpsReturnedDate).AddHours(24))) { messages.Add(new EntityValidationMessage("Date and time of sampling must be within dates when GPS was assigned to fisher")); } } return(messages.Count == 0); }