/// <summary> /// Updates the return visit. /// </summary> /// <param name="newRv">The new rv.</param> public static bool UpdateReturnVisit(ref ReturnVisitData newRv) { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { try { int itemId = newRv.ItemId; ReturnVisitDataItem rv = db.ReturnVisitItems.Single(s => s.ItemId == itemId); rv.AddressOne = newRv.AddressOne; rv.AddressTwo = newRv.AddressTwo; rv.Age = newRv.Age; rv.City = newRv.City; rv.Country = newRv.Country; rv.FullName = newRv.FullName; rv.ImageSrc = newRv.ImageSrc; rv.OtherNotes = newRv.OtherNotes; rv.PhoneNumber = newRv.PhoneNumber; rv.PhysicalDescription = newRv.PhysicalDescription; rv.PostalCode = newRv.PostalCode; rv.StateProvince = newRv.StateProvince; rv.Gender = newRv.Gender; rv.Latitude = newRv.Latitude; rv.Longitude = newRv.Longitude; rv.LastVisitDate = newRv.LastVisitDate; db.SubmitChanges(); return(true); } catch (InvalidOperationException) { return(AddNewReturnVisit(ref newRv)); //rv not found, lets create it. } } }
/// <summary> /// Gets the return visit. /// </summary> /// <param name="itemID">The item ID.</param> /// <returns>ReturnVisitData.</returns> public static ReturnVisitData GetReturnVisit(int itemID) { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { try { ReturnVisitDataItem r = db.ReturnVisitItems.Single(s => s.ItemId == itemID); var rr = new ReturnVisitData { ItemId = r.ItemId, DateCreated = r.DateCreated, AddressOne = r.AddressOne, AddressTwo = r.AddressTwo, Age = r.Age, City = r.City, Country = r.Country, FullName = r.FullName, Gender = r.Gender, OtherNotes = r.OtherNotes, PhysicalDescription = r.PhysicalDescription, PostalCode = r.PostalCode, StateProvince = r.StateProvince, ImageSrc = r.ImageSrc, PhoneNumber = r.PhoneNumber, Latitude = r.Latitude ?? 0, Longitude = r.Longitude ?? 0, LastVisitDate = r.LastVisitDate ?? GetLastVisitDate(r) }; return(rr); } catch { return(new ReturnVisitData()); } } }
public ReturnVisitViewModel(bool loadPreviousVisits) { _returnVisitData = new ReturnVisitData { DateCreated = DateTime.Now }; PreviousVisits = new ObservableCollection<PreviousVisitViewModel>(); _loadPreviousVisits = loadPreviousVisits; }
/// <summary> /// Gets the return visits of a given quantity and sorted. /// </summary> /// <param name="so">The sort order.</param> /// <param name="maxReturnCount">The max return count. -1 for all.</param> /// <returns><c>ReturnVisitData</c> Array</returns> public static ReturnVisitData[] GetReturnVisits(SortOrder so, int maxReturnCount = 25) { var rvs = new List <ReturnVisitData>(); using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { if (maxReturnCount == -1) { maxReturnCount = db.ReturnVisitItems.Count(); } IQueryable <ReturnVisitDataItem> q; IEnumerable <ReturnVisitDataItem> demRVs = null; if (so == SortOrder.DateNewestToOldest || so == SortOrder.DateOldestToNewest) { q = from x in db.ReturnVisitItems orderby x.DateCreated select x; q = q.Take(maxReturnCount); demRVs = so == SortOrder.DateNewestToOldest ? q.ToArray().Reverse() : q.ToArray(); } else if (so == SortOrder.CityAToZ || so == SortOrder.CityZToA) { q = from x in db.ReturnVisitItems orderby x.City select x; q = q.Take(maxReturnCount); demRVs = so == SortOrder.CityZToA ? q.ToArray().Reverse() : q.ToArray(); } if (demRVs != null) { foreach (ReturnVisitDataItem r in demRVs) { var rr = new ReturnVisitData { ItemId = r.ItemId, DateCreated = r.DateCreated, AddressOne = r.AddressOne, AddressTwo = r.AddressTwo, Age = r.Age, City = r.City, Country = r.Country, FullName = r.FullName, Gender = r.Gender, OtherNotes = r.OtherNotes, PhysicalDescription = r.PhysicalDescription, PostalCode = r.PostalCode, StateProvince = r.StateProvince, ImageSrc = r.ImageSrc, PhoneNumber = r.PhoneNumber, Latitude = r.Latitude ?? 0, Longitude = r.Longitude ?? 0, LastVisitDate = r.LastVisitDate ?? GetLastVisitDate(r) }; rvs.Add(rr); } } } return(rvs.ToArray()); }
public static bool AddOrUpdateRV(ref ReturnVisitData rv) { try { if (rv.ItemId < 0) { return(AddNewReturnVisit(ref rv)); } return(UpdateReturnVisit(ref rv)); } catch (ReturnVisitAlreadyExistsException) { return(UpdateReturnVisit(ref rv)); } }
internal static ReturnVisitDataItem Copy(ReturnVisitData newRv) { return(new ReturnVisitDataItem { AddressOne = newRv.AddressOne, AddressTwo = newRv.AddressTwo, Age = newRv.Age, City = newRv.City, Country = newRv.Country, DateCreated = newRv.DateCreated, FullName = newRv.FullName, Gender = newRv.Gender, OtherNotes = newRv.OtherNotes, PhysicalDescription = newRv.PhysicalDescription, PostalCode = newRv.PostalCode, StateProvince = newRv.StateProvince, ImageSrc = newRv.ImageSrc, PhoneNumber = newRv.PhoneNumber, Latitude = newRv.Latitude, Longitude = newRv.Longitude, LastVisitDate = newRv.LastVisitDate }); }
/// <summary> /// Adds the new return visit. /// </summary> /// <param name="newRv">The new rv.</param> /// <returns><c>True</c> if successful and <c>False</c> if unsuccessful.</returns> /// <exception cref="MyTimeDatabaseLib.ReturnVisitAlreadyExistsException">The Return Visit already exists.</exception> public static bool AddNewReturnVisit(ref ReturnVisitData newRv) { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { var r = ReturnVisitData.Copy(newRv); IQueryable <ReturnVisitDataItem> qry = from x in db.ReturnVisitItems where x.AddressOne.Equals(r.AddressOne) && x.AddressTwo.Equals(r.AddressTwo) && x.City.Equals(r.City) && x.Country.Equals(r.Country) && x.StateProvince.Equals(r.StateProvince) && x.PostalCode.Equals(r.PostalCode) && x.FullName.Equals(r.FullName) select x; if (qry.Any()) { throw new ReturnVisitAlreadyExistsException("The Return Visit already exists.", qry.First().ItemId); } db.ReturnVisitItems.InsertOnSubmit(r); db.SubmitChanges(); newRv.ItemId = r.ItemId; return(newRv.ItemId >= 0); } }
/// <summary> /// Checks the database. /// </summary> public static void CheckDatabase() { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { if (db.DatabaseExists() == false) { db.CreateDatabase(); DatabaseSchemaUpdater dbUpdater = db.CreateDatabaseSchemaUpdater(); dbUpdater.DatabaseSchemaVersion = APP_VERSION; dbUpdater.Execute(); } else { var dbUpdater = db.CreateDatabaseSchemaUpdater(); if (dbUpdater.DatabaseSchemaVersion < 2) { //update from 1.0 to 2.0 db version dbUpdater.AddColumn <ReturnVisitDataItem>("Longitude"); dbUpdater.AddColumn <ReturnVisitDataItem>("Latitude"); dbUpdater.AddColumn <ReturnVisitDataItem>("LastVisitDate"); dbUpdater.DatabaseSchemaVersion = APP_VERSION; dbUpdater.Execute(); var rvList = GetReturnVisits(SortOrder.DateNewestToOldest, -1); foreach (var r in rvList) { var x = r; if (r.LastVisitDate > SqlCeConstants.DateTimeMinValue) { r.LastVisitDate = GetLastVisitDate(ReturnVisitData.Copy(x)); } UpdateReturnVisit(ref x); } } } } }
private Address GetGeocodeAddress(ReturnVisitData r) { switch (GetCountryCode(r.Country)) { case "gb": return new Address { AddressLine = r.AddressOne, Locality = r.City, CountryRegion = string.IsNullOrEmpty(r.Country) ? "England" : r.Country }; case "th": return new Address { AddressLine = string.Format("{0}{1}", r.AddressOne, string.IsNullOrEmpty(r.AddressTwo) ? string.Empty : string.Format(" {0}", r.AddressTwo)), Locality = r.City, AdminDistrict = r.StateProvince, CountryRegion = string.IsNullOrEmpty(r.Country) ? "Thailand" : r.Country }; case "it": case "us": default: return new Address { AddressLine = r.AddressOne, AdminDistrict = r.StateProvince, Locality = r.City, PostalCode = r.PostalCode, CountryRegion = string.IsNullOrEmpty(r.Country) ? "United States" : r.Country }; } }
public static List <ReturnVisitData> GetReturnVisitsByLastVisitDate(int maxReturnCount = 8) { try { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { var qry = from x in db.ReturnVisitItems orderby x.LastVisitDate //where x.LastVisitDate != SqlCeConstants.DateTimeMinValue select x; if (!qry.Any()) { return(null); } bool save = false; foreach (var r in qry) { if (r.LastVisitDate == null) { r.LastVisitDate = GetLastVisitDate(r); save = true; } } if (save) { db.SubmitChanges(); } return(qry.Take(maxReturnCount == -1 ? qry.Count() : maxReturnCount).Select(rv => ReturnVisitData.Copy(rv)).ToList()); } } catch { return(null); } }
/// <summary> /// Updates the return visit. /// </summary> /// <param name="newRv">The new rv.</param> public static bool UpdateReturnVisit(ref ReturnVisitData newRv) { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { try { int itemId = newRv.ItemId; ReturnVisitDataItem rv = db.ReturnVisitItems.Single(s => s.ItemId == itemId); rv.AddressOne = newRv.AddressOne; rv.AddressTwo = newRv.AddressTwo; rv.Age = newRv.Age; rv.City = newRv.City; rv.Country = newRv.Country; rv.FullName = newRv.FullName; rv.ImageSrc = newRv.ImageSrc; rv.OtherNotes = newRv.OtherNotes; rv.PhoneNumber = newRv.PhoneNumber; rv.PhysicalDescription = newRv.PhysicalDescription; rv.PostalCode = newRv.PostalCode; rv.StateProvince = newRv.StateProvince; rv.Gender = newRv.Gender; rv.Latitude = newRv.Latitude; rv.Longitude = newRv.Longitude; rv.LastVisitDate = newRv.LastVisitDate; db.SubmitChanges(); return true; } catch (InvalidOperationException) { return AddNewReturnVisit(ref newRv); //rv not found, lets create it. } } }
/// <summary> /// Gets the return visits of a given quantity and sorted. /// </summary> /// <param name="so">The sort order.</param> /// <param name="maxReturnCount">The max return count. -1 for all.</param> /// <returns><c>ReturnVisitData</c> Array</returns> public static ReturnVisitData[] GetReturnVisits(SortOrder so, int maxReturnCount = 25) { var rvs = new List<ReturnVisitData>(); using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { if (maxReturnCount == -1) maxReturnCount = db.ReturnVisitItems.Count(); IQueryable<ReturnVisitDataItem> q; IEnumerable<ReturnVisitDataItem> demRVs = null; if (so == SortOrder.DateNewestToOldest || so == SortOrder.DateOldestToNewest) { q = from x in db.ReturnVisitItems orderby x.DateCreated select x; q = q.Take(maxReturnCount); demRVs = so == SortOrder.DateNewestToOldest ? q.ToArray().Reverse() : q.ToArray(); } else if (so == SortOrder.CityAToZ || so == SortOrder.CityZToA) { q = from x in db.ReturnVisitItems orderby x.City select x; q = q.Take(maxReturnCount); demRVs = so == SortOrder.CityZToA ? q.ToArray().Reverse() : q.ToArray(); } if (demRVs != null) foreach (ReturnVisitDataItem r in demRVs) { var rr = new ReturnVisitData { ItemId = r.ItemId, DateCreated = r.DateCreated, AddressOne = r.AddressOne, AddressTwo = r.AddressTwo, Age = r.Age, City = r.City, Country = r.Country, FullName = r.FullName, Gender = r.Gender, OtherNotes = r.OtherNotes, PhysicalDescription = r.PhysicalDescription, PostalCode = r.PostalCode, StateProvince = r.StateProvince, ImageSrc = r.ImageSrc, PhoneNumber = r.PhoneNumber, Latitude = r.Latitude ?? 0, Longitude = r.Longitude ?? 0, LastVisitDate = r.LastVisitDate ?? GetLastVisitDate(r) }; rvs.Add(rr); } } return rvs.ToArray(); }
/// <summary> /// Gets the return visit. /// </summary> /// <param name="itemID">The item ID.</param> /// <returns>ReturnVisitData.</returns> public static ReturnVisitData GetReturnVisit(int itemID) { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { try { ReturnVisitDataItem r = db.ReturnVisitItems.Single(s => s.ItemId == itemID); var rr = new ReturnVisitData { ItemId = r.ItemId, DateCreated = r.DateCreated, AddressOne = r.AddressOne, AddressTwo = r.AddressTwo, Age = r.Age, City = r.City, Country = r.Country, FullName = r.FullName, Gender = r.Gender, OtherNotes = r.OtherNotes, PhysicalDescription = r.PhysicalDescription, PostalCode = r.PostalCode, StateProvince = r.StateProvince, ImageSrc = r.ImageSrc, PhoneNumber = r.PhoneNumber, Latitude = r.Latitude ?? 0, Longitude = r.Longitude ?? 0, LastVisitDate = r.LastVisitDate ?? GetLastVisitDate(r) }; return rr; } catch { return new ReturnVisitData(); } } }
public static bool AddOrUpdateRV(ref ReturnVisitData rv) { try { if (rv.ItemId < 0) return AddNewReturnVisit(ref rv); return UpdateReturnVisit(ref rv); } catch (ReturnVisitAlreadyExistsException) { return UpdateReturnVisit(ref rv); } }
/// <summary> /// Adds the new return visit. /// </summary> /// <param name="newRv">The new rv.</param> /// <returns><c>True</c> if successful and <c>False</c> if unsuccessful.</returns> /// <exception cref="MyTimeDatabaseLib.ReturnVisitAlreadyExistsException">The Return Visit already exists.</exception> public static bool AddNewReturnVisit(ref ReturnVisitData newRv) { using (var db = new ReturnVisitDataContext(ReturnVisitDataContext.DBConnectionString)) { var r = ReturnVisitData.Copy(newRv); IQueryable<ReturnVisitDataItem> qry = from x in db.ReturnVisitItems where x.AddressOne.Equals(r.AddressOne) && x.AddressTwo.Equals(r.AddressTwo) && x.City.Equals(r.City) && x.Country.Equals(r.Country) && x.StateProvince.Equals(r.StateProvince) && x.PostalCode.Equals(r.PostalCode) && x.FullName.Equals(r.FullName) select x; if (qry.Any()) throw new ReturnVisitAlreadyExistsException("The Return Visit already exists.", qry.First().ItemId); db.ReturnVisitItems.InsertOnSubmit(r); db.SubmitChanges(); newRv.ItemId = r.ItemId; return newRv.ItemId >= 0; } }
internal static ReturnVisitDataItem Copy(ReturnVisitData newRv) { return new ReturnVisitDataItem { AddressOne = newRv.AddressOne, AddressTwo = newRv.AddressTwo, Age = newRv.Age, City = newRv.City, Country = newRv.Country, DateCreated = newRv.DateCreated, FullName = newRv.FullName, Gender = newRv.Gender, OtherNotes = newRv.OtherNotes, PhysicalDescription = newRv.PhysicalDescription, PostalCode = newRv.PostalCode, StateProvince = newRv.StateProvince, ImageSrc = newRv.ImageSrc, PhoneNumber = newRv.PhoneNumber, Latitude = newRv.Latitude, Longitude = newRv.Longitude, LastVisitDate = newRv.LastVisitDate }; }