private void DoSync() { if (!FishSQL.OpenConnection()) { return; } try { StartDBTransaction(Resources.DBSyncMessageStart); FishDB.MarkAllFishNew(); FishDB.GetUpdates(); FishSQL.BackgroundUpload(); FishSQL.DoDownloadFish(); } catch (Exception e) { Error(e.ToString()); } EndDBTransaction(Resources.DBSyncMessageDone); }
/// <summary> /// Download any new fish and renames. /// </summary> public static void DoDownloadFish() { Dictionary <string, DateTime> updateTimes = new Dictionary <string, DateTime>(); if (null != StatusDisplay) { StatusDisplay.SetDownloadRodNumber(Dictionaries.rodDictionary.Count); } foreach (string rod in Dictionaries.rodDictionary.Keys) { if (null != StatusDisplay) { StatusDisplay.SetDownloadRod(rod); } DateTime newest = NewestDBModificationTime(rod); if (FishDB.UpdatesByRod[rod].dbDate < newest) { try { List <SQLFishie> newFishies = DownloadNewFish(rod, FishDB.UpdatesByRod[rod].dbDate); if (null != StatusDisplay) { StatusDisplay.SetDownloadRodFish(newFishies.Count); } foreach (SQLFishie fish in newFishies) { string name = fish.name; if (null != StatusDisplay) { StatusDisplay.SetDownloadFish(name); if (null == fish.zone) { StatusDisplay.SetFishBaitOrZone(name, fish.bait); } else if (null == fish.bait) { StatusDisplay.SetFishBaitOrZone(name, fish.zone); } else { StatusDisplay.SetFishBaitOrZone(name, string.Format("{0} using {1}", fish.zone, fish.bait)); } } FishDB.AddNewFish(ref name, fish.zone, fish.bait, fish.rod, fish.ID1, fish.ID2, fish.ID3, false, true); } } catch (Exception e) { if (StatusDisplay != null) { StatusDisplay.Error(string.Format(Resources.SQLMessageFormatErrorDownloadingFishRod, rod)); StatusDisplay.Info(e.ToString()); } } try { Dictionary <SQLFishie, string> renamedFish = DownloadRenamedFish(rod, FishDB.UpdatesByRod[rod].dbDate); if (null != StatusDisplay) { StatusDisplay.SetDownloadRenameRodFish(renamedFish.Count); } foreach (SQLFishie fish in renamedFish.Keys) { if (null != StatusDisplay) { StatusDisplay.SetDownloadRenameFish(renamedFish[fish], fish.name); } try { FishDB.ChangeName(fish.ToFishDBFishie(), renamedFish[fish], true); } catch { } } } catch (Exception e) { if (StatusDisplay != null) { StatusDisplay.Error(string.Format(Resources.SQLMessageFormatErrorDownloadingRenameRod, rod)); StatusDisplay.Info(e.ToString()); } } updateTimes[rod] = newest; } } CloseConnection(); foreach (string rod in updateTimes.Keys) { FishDB.DBUpdated(rod, updateTimes[rod]); } if (updateTimes.Count > 0) { FishDB.UpdatesDBChanged(); } }
/// <summary> /// Upload any fish marked as new or renamed. /// </summary> /// <note>This is highly coupled with FishDB, perhaps it can be done better.</note> public static void DoUploadFish() { if (null != StatusDisplay) { StatusDisplay.SetUploadFishNumber(FishDB.DBNewFish.Count + FishDB.DBRenamedFish.Count); } if (FishDB.DBNewFish.Count > 0 || FishDB.DBRenamedFish.Count > 0) { Dictionary <string, DateTime> updateTimes = new Dictionary <string, DateTime>(); HashSet <string> updatedRods = new HashSet <string>(); List <XmlNode> uploadFish = new List <XmlNode>(FishDB.DBNewFish); FishDB.DBNewFish.Clear(); Dictionary <XmlNode, XmlNode> updatedNodes = new Dictionary <XmlNode, XmlNode>(); Dictionary <XmlNode, XmlNode> updatedBait = new Dictionary <XmlNode, XmlNode>(); Dictionary <XmlNode, XmlNode> updatedZones = new Dictionary <XmlNode, XmlNode>(); foreach (XmlNode fishNode in uploadFish) { List <XmlNode> baits = new List <XmlNode>(); List <XmlNode> zones = new List <XmlNode>(); string rod = fishNode.OwnerDocument.SelectSingleNode(FishDB.XPathRodNode).Attributes[FishDB.XMLAttrName].Value; string fish = fishNode.Attributes[MySQLParamFishName].Value; if (null != StatusDisplay) { StatusDisplay.SetUploadRodAndFish(rod, fish); } if (null != fishNode.Attributes[FishDB.XMLAttrNew]) { baits.AddRange(fishNode[FishDB.XMLNodeBaits].ChildNodes.Cast <XmlNode>()); zones.AddRange(fishNode[FishDB.XMLNodeZones].ChildNodes.Cast <XmlNode>()); try { if (UploadFish(fish, rod, fishNode.Attributes[FishDB.XMLAttrID1].Value, fishNode.Attributes[FishDB.XMLAttrID2].Value, fishNode.Attributes[FishDB.XMLAttrID3].Value)) { try { updatedNodes.Add(fishNode, fishNode); } catch (ArgumentException) { // In case it's been added already somehow } } } catch (Exception e) { if (StatusDisplay != null) { StatusDisplay.Error(string.Format(Resources.SQLMessageFormatErrorUploadingFishRod, fish, rod)); StatusDisplay.Info(e.ToString()); } } } else { baits.AddRange(fishNode.SelectNodes(FishDB.XPathNewBait).Cast <XmlNode>()); zones.AddRange(fishNode.SelectNodes(FishDB.XPathNewZones).Cast <XmlNode>()); } if (baits.Count > 0 || zones.Count > 0) { try { UploadBaitAndZone(fish, rod, fishNode.Attributes[FishDB.XMLAttrID1].Value, fishNode.Attributes[FishDB.XMLAttrID2].Value, fishNode.Attributes[FishDB.XMLAttrID3].Value, baits, zones, fishNode, ref updatedBait, ref updatedZones); } catch (Exception e) { if (StatusDisplay != null) { StatusDisplay.Error(string.Format(Resources.SQLMessageFormatErrorUploadingBaitZones, fish, rod)); StatusDisplay.Info(e.ToString()); } } } updatedRods.Add(rod); } List <XmlNode> renameFish = new List <XmlNode>(FishDB.DBRenamedFish); FishDB.DBRenamedFish.Clear(); List <XmlNode> renamedNodes = new List <XmlNode>(); foreach (XmlNode fishNode in renameFish) { string rod = fishNode.OwnerDocument.SelectSingleNode(FishDB.XPathRodNode).Attributes[MySQLParamFishName].Value; string fish = fishNode.Attributes[MySQLParamFishName].Value; if (null != StatusDisplay) { StatusDisplay.SetUploadRenameRodAndFish(rod, fish, fishNode.Attributes[FishDB.XMLAttrRename].Value); } try { if (RenameFish(fish, fishNode.Attributes[FishDB.XMLAttrRename].Value, rod, fishNode.Attributes[FishDB.XMLAttrID1].Value, fishNode.Attributes[FishDB.XMLAttrID2].Value, fishNode.Attributes[FishDB.XMLAttrID3].Value)) { renamedNodes.Add(fishNode); } } catch (Exception e) { if (StatusDisplay != null) { StatusDisplay.Error(string.Format(Resources.SQLMessageFormatErrorUploadingRename, fish, fishNode.Attributes[FishDB.XMLAttrRename].Value, rod)); StatusDisplay.Info(e.ToString()); } } updatedRods.Add(rod); } foreach (string rod in updatedRods) { updateTimes[rod] = NewestDBModificationTime(rod); } CloseConnection(); foreach (XmlNode node in updatedNodes.Keys) { FishDB.UnsetNew(node, updatedNodes[node]); } foreach (XmlNode node in updatedBait.Keys) { FishDB.UnsetNew(node, updatedBait[node]); } foreach (XmlNode node in updatedZones.Keys) { FishDB.UnsetNew(node, updatedZones[node]); } foreach (XmlNode node in renamedNodes) { FishDB.UnsetRename(node); } foreach (string rod in updateTimes.Keys) { FishDB.DBUpdated(rod, updateTimes[rod]); } if (updateTimes.Count > 0) { FishDB.UpdatesDBChanged(); } } }