[tomboy] Use nullables to represent missing JSON fields in NoteInfo and UserInfo, and migrate conversion meth



commit 4f69abffd3233eab79e7d3b9a50a9ab9b4e466d9
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Sun May 17 07:39:18 2009 -0700

    Use nullables to represent missing JSON fields in NoteInfo and UserInfo, and migrate conversion methods to new NoteConvert class.
---
 Tomboy/Addins/WebSyncService/Api/NoteInfo.cs  |   92 +++++++++++++----------
 Tomboy/Addins/WebSyncService/Api/UserInfo.cs  |    2 +-
 Tomboy/Addins/WebSyncService/Makefile.am      |    1 +
 Tomboy/Addins/WebSyncService/NoteConvert.cs   |   99 +++++++++++++++++++++++++
 Tomboy/Addins/WebSyncService/WebSyncServer.cs |   65 +---------------
 5 files changed, 158 insertions(+), 101 deletions(-)

diff --git a/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs b/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs
index 2641a11..a634a6e 100644
--- a/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs
+++ b/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs
@@ -97,6 +97,10 @@ namespace Tomboy.WebSync.Api
 		{
 			Hyena.Json.JsonObject noteUpdateObj =
 				new Hyena.Json.JsonObject ();
+
+			if (string.IsNullOrEmpty (Guid))
+				throw new InvalidOperationException ("Cannot create a valid JSON representation without a Guid");
+			
 			noteUpdateObj [GuidElementName] = Guid;
 			
 			if (!string.IsNullOrEmpty (Command)) {
@@ -104,25 +108,35 @@ namespace Tomboy.WebSync.Api
 				return noteUpdateObj;
 			}
 
-			noteUpdateObj [TitleElementName] = Title;
-			noteUpdateObj [NoteContentElementName] = NoteContent;
-			noteUpdateObj [NoteContentVersionElementName] = NoteContentVersion;
-			
-			noteUpdateObj [LastChangeDateElementName] =
-				LastChangeDate.ToString (NoteArchiver.DATE_TIME_FORMAT);
-			noteUpdateObj [LastMetadataChangeDateElementName] =
-				LastMetadataChangeDate.ToString (NoteArchiver.DATE_TIME_FORMAT);
-			noteUpdateObj [CreateDateElementName] =
-				CreateDate.ToString (NoteArchiver.DATE_TIME_FORMAT);
-			
-			noteUpdateObj [LastSyncRevisionElementName] = LastSyncRevision;
-			noteUpdateObj [OpenOnStartupElementName] = OpenOnStartup;
-
-			Hyena.Json.JsonArray tagArray =
-				new Hyena.Json.JsonArray ();
-			foreach (string tag in Tags)
-				tagArray.Add (tag);
-			noteUpdateObj [TagsElementName] = tagArray;
+			if (Title != null)
+				noteUpdateObj [TitleElementName] = Title;
+			if (NoteContent != null)
+				noteUpdateObj [NoteContentElementName] = NoteContent;
+			if (NoteContentVersion.HasValue)
+				noteUpdateObj [NoteContentVersionElementName] = NoteContentVersion;
+
+			if (LastChangeDate.HasValue)
+				noteUpdateObj [LastChangeDateElementName] =
+					LastChangeDate.Value.ToString (NoteArchiver.DATE_TIME_FORMAT);
+			if (LastMetadataChangeDate.HasValue)
+				noteUpdateObj [LastMetadataChangeDateElementName] =
+					LastMetadataChangeDate.Value.ToString (NoteArchiver.DATE_TIME_FORMAT);
+			if (CreateDate.HasValue)
+				noteUpdateObj [CreateDateElementName] =
+					CreateDate.Value.ToString (NoteArchiver.DATE_TIME_FORMAT);
+
+			if (LastSyncRevision.HasValue)
+				noteUpdateObj [LastSyncRevisionElementName] = LastSyncRevision;
+			if (OpenOnStartup.HasValue)
+				noteUpdateObj [OpenOnStartupElementName] = OpenOnStartup;
+
+			if (Tags != null) {
+				Hyena.Json.JsonArray tagArray =
+					new Hyena.Json.JsonArray ();
+				foreach (string tag in Tags)
+					tagArray.Add (tag);
+				noteUpdateObj [TagsElementName] = tagArray;
+			}
 
 			return noteUpdateObj;
 		}
@@ -139,17 +153,17 @@ namespace Tomboy.WebSync.Api
 		
 		public string NoteContent { get; set; }
 
-		public double NoteContentVersion { get; set; }
+		public double? NoteContentVersion { get; set; }
 		
-		public DateTime LastChangeDate { get; set; }
+		public DateTime? LastChangeDate { get; set; }
 		
-		public DateTime LastMetadataChangeDate { get; set; }
+		public DateTime? LastMetadataChangeDate { get; set; }
 		
-		public DateTime CreateDate { get; set; }
+		public DateTime? CreateDate { get; set; }
 
-		public int LastSyncRevision { get; set; }
+		public int? LastSyncRevision { get; set; }
 		
-		public bool OpenOnStartup { get; set; }
+		public bool? OpenOnStartup { get; set; }
 		
 		public List<string> Tags { get; set; }
 
@@ -157,20 +171,20 @@ namespace Tomboy.WebSync.Api
 
 		#endregion
 
-		#region Public Constants
-
-		public const string GuidElementName = "guid";
-		public const string ResourceReferenceElementName = "ref";
-		public const string TitleElementName = "title";
-		public const string NoteContentElementName = "note-content";
-		public const string NoteContentVersionElementName = "note-content-version";
-		public const string LastChangeDateElementName = "last-change-date";
-		public const string LastMetadataChangeDateElementName = "last-metadata-change-date";
-		public const string CreateDateElementName = "create-date";
-		public const string LastSyncRevisionElementName = "last-sync-revision";
-		public const string OpenOnStartupElementName = "open-on-startup";
-		public const string TagsElementName = "tags";
-		public const string CommandElementName = "command";
+		#region Private Constants
+
+		private const string GuidElementName = "guid";
+		private const string ResourceReferenceElementName = "ref";
+		private const string TitleElementName = "title";
+		private const string NoteContentElementName = "note-content";
+		private const string NoteContentVersionElementName = "note-content-version";
+		private const string LastChangeDateElementName = "last-change-date";
+		private const string LastMetadataChangeDateElementName = "last-metadata-change-date";
+		private const string CreateDateElementName = "create-date";
+		private const string LastSyncRevisionElementName = "last-sync-revision";
+		private const string OpenOnStartupElementName = "open-on-startup";
+		private const string TagsElementName = "tags";
+		private const string CommandElementName = "command";
 
 		#endregion
 	}
diff --git a/Tomboy/Addins/WebSyncService/Api/UserInfo.cs b/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
index 3ae5aac..219a90a 100644
--- a/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
+++ b/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
@@ -79,7 +79,7 @@ namespace Tomboy.WebSync.Api
 
 		public string LastName { get; private set; }
 
-		public int LatestSyncRevision { get; private set; }
+		public int? LatestSyncRevision { get; private set; }
 
 		public ResourceReference Notes { get; private set; }
 
diff --git a/Tomboy/Addins/WebSyncService/Makefile.am b/Tomboy/Addins/WebSyncService/Makefile.am
index 0357fd6..203204d 100644
--- a/Tomboy/Addins/WebSyncService/Makefile.am
+++ b/Tomboy/Addins/WebSyncService/Makefile.am
@@ -23,6 +23,7 @@ ASSEMBLIES = \
 
 TARGET = WebSyncServiceAddin.dll
 CSFILES = \
+	$(srcdir)/NoteConvert.cs       	\
 	$(srcdir)/WebSyncServer.cs          \
 	$(srcdir)/WebSyncServiceAddin.cs	\
 	$(srcdir)/Api/NoteInfo.cs       	\
diff --git a/Tomboy/Addins/WebSyncService/NoteConvert.cs b/Tomboy/Addins/WebSyncService/NoteConvert.cs
new file mode 100644
index 0000000..e0486fa
--- /dev/null
+++ b/Tomboy/Addins/WebSyncService/NoteConvert.cs
@@ -0,0 +1,99 @@
+// Permission is hereby granted, free of charge, to any person obtaining 
+// a copy of this software and associated documentation files (the 
+// "Software"), to deal in the Software without restriction, including 
+// without limitation the rights to use, copy, modify, merge, publish, 
+// distribute, sublicense, and/or sell copies of the Software, and to 
+// permit persons to whom the Software is furnished to do so, subject to 
+// the following conditions: 
+//  
+// The above copyright notice and this permission notice shall be 
+// included in all copies or substantial portions of the Software. 
+//  
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
+// 
+// Copyright (c) 2008 Novell, Inc. (http://www.novell.com) 
+// 
+// Authors: 
+//      Sandy Armstrong <sanfordarmstrong gmail com>
+// 
+
+using System;
+using System.Collections.Generic;
+using System.Text.RegularExpressions;
+
+using Tomboy.Sync;
+using Tomboy.WebSync.Api;
+
+namespace Tomboy.WebSync
+{
+	public static class NoteConvert
+	{
+		public static NoteInfo ToNoteInfo (Note note)
+		{
+			NoteInfo noteInfo = new NoteInfo ();
+			
+			noteInfo.Guid = note.Id;
+			noteInfo.Title = note.Title;
+			noteInfo.OpenOnStartup = note.IsOpenOnStartup;
+			noteInfo.CreateDate = note.CreateDate;
+			noteInfo.LastChangeDate = note.ChangeDate;
+			noteInfo.LastMetadataChangeDate = note.MetadataChangeDate;
+
+			noteInfo.Tags = new List<string> ();
+			foreach (Tag tag in note.Tags)
+				noteInfo.Tags.Add (tag.Name);
+
+			const string noteContentRegex =
+				@"\A<note-content(\s+version=""(?<contentVersion>[^""]+)"")?>(?<innerContent>.*)</note-content>\Z";
+			Match m = Regex.Match (note.XmlContent, noteContentRegex);
+			Group versionGroup = m.Groups ["contentVersion"];
+			Group contentGroup = m.Groups ["innerContent"];
+
+			double contentVersion;
+			if (versionGroup.Success &&
+			    double.TryParse (versionGroup.Value, out contentVersion)) {
+				noteInfo.NoteContentVersion = contentVersion;
+			} else
+				noteInfo.NoteContentVersion = 0.1;
+
+			if (contentGroup.Success)
+				noteInfo.NoteContent = contentGroup.Value;
+			else
+				noteInfo.NoteContent = string.Empty;
+
+			return noteInfo;
+		}
+
+		public static NoteData ToNoteData (NoteInfo noteInfo)
+		{
+			NoteData noteData = new NoteData (noteInfo.Guid);
+			noteData.Title = noteInfo.Title;
+			noteData.Text =
+				"<note-content version=\"" + noteInfo.NoteContentVersion.ToString () + "\">" +
+				noteInfo.NoteContent + "</note-content>";
+			noteData.ChangeDate = noteInfo.LastChangeDate.Value;
+			noteData.MetadataChangeDate = noteInfo.LastMetadataChangeDate.Value;
+			noteData.CreateDate = noteInfo.CreateDate.Value;
+			noteData.IsOpenOnStartup = noteInfo.OpenOnStartup.Value;
+
+			foreach (string tagName in noteInfo.Tags) {
+				Tag tag = TagManager.GetOrCreateTag (tagName);
+				noteData.Tags [tag.NormalizedName] = tag;
+			}
+
+			return noteData;
+		}
+
+		public static string ToNoteXml (NoteInfo noteInfo)
+		{
+			NoteData noteData = ToNoteData (noteInfo);
+			return NoteArchiver.WriteString (noteData);
+		}
+	}
+}
diff --git a/Tomboy/Addins/WebSyncService/WebSyncServer.cs b/Tomboy/Addins/WebSyncService/WebSyncServer.cs
index 77492c4..adc5192 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncServer.cs
+++ b/Tomboy/Addins/WebSyncService/WebSyncServer.cs
@@ -97,11 +97,11 @@ namespace Tomboy.WebSync
 			Dictionary<string, NoteUpdate> updates =
 				new Dictionary<string, NoteUpdate> ();
 			foreach (NoteInfo noteInfo in user.GetNotes (true, revision)) {
-				string noteXml = CreateNoteXml (noteInfo);
+				string noteXml = NoteConvert.ToNoteXml (noteInfo);
 				NoteUpdate update = new NoteUpdate (noteXml,
 				                                    noteInfo.Title,
 				                                    noteInfo.Guid,
-				                                    noteInfo.LastSyncRevision);
+				                                    noteInfo.LastSyncRevision.Value);
 				updates.Add (noteInfo.Guid, update);
 			}
 			return updates;
@@ -116,14 +116,14 @@ namespace Tomboy.WebSync
 		public int LatestRevision {
 			get {
 				RefreshUser ();	// TODO: Test that latest sync rev hasn't changed
-				return user.LatestSyncRevision;
+				return user.LatestSyncRevision.Value;
 			}
 		}
 		
 		public void UploadNotes (IList<Note> notes)
 		{
 			foreach (Note note in notes) {
-				pendingCommits.Add (CreateNoteInfo (note));
+				pendingCommits.Add (NoteConvert.ToNoteInfo (note));
 			}
 		}
 		
@@ -136,63 +136,6 @@ namespace Tomboy.WebSync
 			user = UserInfo.GetUser (serverUrl + "/api/1.0/" + userName);
 		}
 
-		private NoteInfo CreateNoteInfo (Note note)
-		{
-			NoteInfo noteInfo = new NoteInfo ();
-			
-			noteInfo.Guid = note.Id;
-			noteInfo.Title = note.Title;
-			noteInfo.OpenOnStartup = note.IsOpenOnStartup;
-			noteInfo.CreateDate = note.CreateDate;
-			noteInfo.LastChangeDate = note.ChangeDate;
-			noteInfo.LastMetadataChangeDate = note.MetadataChangeDate;
-
-			noteInfo.Tags = new List<string> ();
-			foreach (Tag tag in note.Tags)
-				noteInfo.Tags.Add (tag.Name);
-
-			// TODO: content
-			const string noteContentRegex =
-				@"\A<note-content(\s+version=""(?<contentVersion>[^""]+)"")?>(?<innerContent>.*)</note-content>\Z";
-			Match m = Regex.Match (note.XmlContent, noteContentRegex);
-			Group versionGroup = m.Groups ["contentVersion"];
-			Group contentGroup = m.Groups ["innerContent"];
-
-			double contentVersion;
-			if (versionGroup.Success &&
-			    double.TryParse (versionGroup.Value, out contentVersion)) {
-				noteInfo.NoteContentVersion = contentVersion;
-			} else
-				noteInfo.NoteContentVersion = 0.1;
-
-			if (contentGroup.Success)
-				noteInfo.NoteContent = contentGroup.Value;
-			else
-				noteInfo.NoteContent = string.Empty;
-
-			return noteInfo;
-		}
-
-		private string CreateNoteXml (NoteInfo noteInfo)
-		{
-			NoteData noteData = new NoteData (noteInfo.Guid);
-			noteData.Title = noteInfo.Title;
-			noteData.Text =
-				"<note-content version=\"" + noteInfo.NoteContentVersion.ToString () + "\">" +
-				noteInfo.NoteContent + "</note-content>";
-			noteData.ChangeDate = noteInfo.LastChangeDate;
-			noteData.MetadataChangeDate = noteInfo.LastMetadataChangeDate;
-			noteData.CreateDate = noteInfo.CreateDate;
-			noteData.IsOpenOnStartup = noteInfo.OpenOnStartup;
-
-			foreach (string tagName in noteInfo.Tags) {
-				Tag tag = TagManager.GetOrCreateTag (tagName);
-				noteData.Tags [tag.NormalizedName] = tag;
-			}
-			
-			return NoteArchiver.WriteString (noteData);
-		}
-
 		#endregion
 	}
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]