[tomboy] Flesh-out API classes and WebSyncServer.



commit ed3fc94f03bc9c669ae5db491121ce0bf2068398
Author: Sandy Armstrong <sanfordarmstrong gmail com>
Date:   Fri May 15 12:22:32 2009 -0700

    Flesh-out API classes and WebSyncServer.
---
 Tomboy/Addins/WebSyncService/Api/NoteInfo.cs  |   22 +++++---
 Tomboy/Addins/WebSyncService/Api/UserInfo.cs  |   44 ++++++++++++++-
 Tomboy/Addins/WebSyncService/WebSyncServer.cs |   73 +++++++++++++++++++++++--
 3 files changed, 121 insertions(+), 18 deletions(-)

diff --git a/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs b/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs
index 6ddf78a..ca87b0a 100644
--- a/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs
+++ b/Tomboy/Addins/WebSyncService/Api/NoteInfo.cs
@@ -30,22 +30,26 @@ namespace Tomboy.WebSync.Api
 {
 	public class NoteInfo
 	{
-		public string Guid { get; private set; }
+		public string Guid { get; set; }
 		
-		public string Uri { get; private set; }
+		public ResourceReference ResourceReference { get; set; }
 		
-		public string Title { get; private set; }
+		public string Title { get; set; }
 		
-		public string XmlContent { get; private set; }
+		public string NoteContent { get; set; }
 		
-		public DateTime LastChangeDate { get; private set; }
+		public DateTime LastChangeDate { get; set; }
 		
-		public DateTime LastMetadataChangeDate { get; private set; }
+		public DateTime LastMetadataChangeDate { get; set; }
 		
-		public DateTime CreateDate { get; private set; }
+		public DateTime CreateDate { get; set; }
+
+		public int LastSyncRevision { get; set; }
 		
-		public bool OpenOnStartup { get; private set; }
+		public bool OpenOnStartup { get; set; }
 		
-		public List<string> Tags { get; private set; }
+		public List<string> Tags { get; set; }
+
+		public string Command { get; set; }
 	}
 }
diff --git a/Tomboy/Addins/WebSyncService/Api/UserInfo.cs b/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
index ad23a91..1fe4861 100644
--- a/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
+++ b/Tomboy/Addins/WebSyncService/Api/UserInfo.cs
@@ -31,22 +31,60 @@ namespace Tomboy.WebSync.Api
 {
 	public class UserInfo
 	{
+		public static UserInfo GetUser (string uri)
+		{
+			// TODO: Error-handling in GET and Deserialize
+			WebHelper helper = new WebHelper ();
+			string jsonString = helper.Get (uri, null);
+
+			JavaScriptSerializer ser = new JavaScriptSerializer ();
+			return ser.Deserialize <UserInfo> (jsonString);
+		}
+		
 		public string FirstName { get; private set; }
 
 		public string LastName { get; private set; }
 
-		public string NotesUri { get; private set; }
+		public ResourceReference Notes { get; private set; }
 
-		public string FriendsUri { get; private set; }
+		public ResourceReference Friends { get; private set; }
+
+		public int LatestSyncRevision { get; private set; }
 
 		public IList<NoteInfo> GetNotes (bool includeContent)
 		{
+			return GetNotes (includeContent, -1);
+		}
+
+		public IList<NoteInfo> GetNotes (bool includeContent, int sinceRevision)
+		{
+			// TODO: Error-handling in GET and Deserialize
+			WebHelper helper = new WebHelper ();
 			string jsonString = string.Empty;
 
-			// TODO: Do request using NotesUri
+			Dictionary<string, string> parameters =
+				new Dictionary<string, string> ();
+			if (includeContent)
+				parameters ["include_notes"] = "true";
+			if (sinceRevision >= 0)
+				parameters ["since"] = sinceRevision.ToString ();
+			
+			jsonString = helper.Get (Notes.ApiRef, parameters);
 
 			JavaScriptSerializer ser = new JavaScriptSerializer ();
 			return ser.Deserialize <List<NoteInfo>> (jsonString);
 		}
+
+		public void UpdateNotes (IList<NoteInfo> noteUpdates)
+		{
+			// TODO: Error-handling in PUT, Serialize, and Deserialize
+			WebHelper helper = new WebHelper ();
+			JavaScriptSerializer ser = new JavaScriptSerializer ();
+
+			string jsonString =
+				helper.PutJson (Notes.ApiRef, null, ser.Serialize (noteUpdates));
+			
+			ser.Deserialize <List<NoteInfo>> (jsonString);
+		}
 	}
 }
diff --git a/Tomboy/Addins/WebSyncService/WebSyncServer.cs b/Tomboy/Addins/WebSyncService/WebSyncServer.cs
index 5a00e0a..e00c8c2 100644
--- a/Tomboy/Addins/WebSyncService/WebSyncServer.cs
+++ b/Tomboy/Addins/WebSyncService/WebSyncServer.cs
@@ -27,6 +27,7 @@ using System;
 using System.Collections.Generic;
 
 using Tomboy.Sync;
+using Tomboy.WebSync.Api;
 
 namespace Tomboy.WebSync
 {
@@ -34,11 +35,15 @@ namespace Tomboy.WebSync
 	{
 		private string serverUrl;
 		private string userName;
+		private UserInfo user;
+		private List<NoteInfo> pendingCommits;
 		
 		public WebSyncServer (string serverUrl, string userName)
 		{
 			this.serverUrl = serverUrl;
 			this.userName = userName;
+
+			pendingCommits = new List<NoteInfo> ();
 		}
 
 		#region SyncServer implementation
@@ -46,6 +51,7 @@ namespace Tomboy.WebSync
 		public bool BeginSyncTransaction ()
 		{
 			// TODO: Check connection and auth
+			RefreshUser ();
 			return true;
 		}
 		
@@ -57,8 +63,9 @@ namespace Tomboy.WebSync
 		
 		public bool CommitSyncTransaction ()
 		{
-			// TODO: PUT request from info cached during
-			//       UploadNotes and DeleteNotes
+			RefreshUser ();	// TODO: Test that latest sync rev hasn't changed
+			user.UpdateNotes (pendingCommits);
+			// TODO: Check for problems
 			return true;
 		}
 		
@@ -70,7 +77,12 @@ namespace Tomboy.WebSync
 		
 		public void DeleteNotes (IList<string> deletedNoteUUIDs)
 		{
-			// TODO: Build relevant piece of PUT request
+			foreach (string uuid in deletedNoteUUIDs) {
+				NoteInfo noteInfo = new NoteInfo ();
+				noteInfo.Command = "delete";
+				noteInfo.Guid = uuid;
+				pendingCommits.Add (noteInfo);
+			}
 		}
 		
 		public IList<string> GetAllNoteUUIDs ()
@@ -80,7 +92,18 @@ namespace Tomboy.WebSync
 		
 		public IDictionary<string, NoteUpdate> GetNoteUpdatesSince (int revision)
 		{
-			throw new System.NotImplementedException();
+			RefreshUser ();	// TODO: Test that latest sync rev hasn't changed
+			Dictionary<string, NoteUpdate> updates =
+				new Dictionary<string, NoteUpdate> ();
+			foreach (NoteInfo noteInfo in user.GetNotes (true, revision)) {
+				string noteXml = CreateNoteXml (noteInfo);
+				NoteUpdate update = new NoteUpdate (noteXml,
+				                                    noteInfo.Title,
+				                                    noteInfo.Guid,
+				                                    noteInfo.LastSyncRevision);
+				updates.Add (noteInfo.Guid, update);
+			}
+			return updates;
 		}
 		
 		public string Id {
@@ -91,15 +114,53 @@ namespace Tomboy.WebSync
 		
 		public int LatestRevision {
 			get {
-				throw new System.NotImplementedException();
+				RefreshUser ();	// TODO: Test that latest sync rev hasn't changed
+				return user.LatestSyncRevision;
 			}
 		}
 		
 		public void UploadNotes (IList<Note> notes)
 		{
-			// TODO: Build relevant piece of PUT request
+			foreach (Note note in notes) {
+				pendingCommits.Add (CreateNoteInfo (note));
+			}
 		}
 		
 		#endregion
+
+		#region Private Methods
+		
+		private void RefreshUser ()
+		{
+			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
+
+			return noteInfo;
+		}
+
+		private string CreateNoteXml (NoteInfo noteInfo)
+		{
+			// TODO: Everything
+			return string.Empty;
+		}
+
+		#endregion
 	}
 }



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