Click here to Skip to main content
15,880,608 members
Articles / Web Development / ASP.NET

OneNote on iPhone and Palm Pré using Windows Azure

Rate me:
Please Sign up or sign in to vote.
4.96/5 (39 votes)
25 Dec 2009CPOL12 min read 80.3K   322   53  
Learn how to synchronize your OneNote notebooks on Windows Azure and access it from your iPhone or your Palm Pré.
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using Microsoft.Samples.ServiceHosting.StorageClient;


namespace Rino
{
    /// <summary>
    /// Class to handle Windows Azure storage.
    /// </summary>
    public class AzureStorage
    {
        /// <summary>
        ///  Constant for location
        /// </summary>
        public const string UserContainerKey = "UserContainerName";
        public const string UserContainerName = "usercontainer";
        public const string NotebookContainerKey = "NotebookContainerName";
        public const string NotebookContainerName = "notebookcontainer";
        public const string PageContainerKey = "PageContainerName";
        public const string PageContainerName = "pagecontainer";

        /// <summary>
        /// Connect to blob.
        /// </summary>
        /// <returns>blob storage object</returns>
        private static BlobStorage ConnectToBlob()
        {
            StorageAccountInfo account = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration();
            BlobStorage blobStorage = BlobStorage.Create(account);
            blobStorage.RetryPolicy = RetryPolicies.RetryN(2, TimeSpan.FromMilliseconds(100));

            return blobStorage;
        }

        /// <summary>
        ///  Create containers into storage.
        /// </summary>
        /// <returns>true if </returns>
        public static bool Initialize()
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();
            BlobContainer container;

            try
            {
                // Create user container
                string userContainer = StorageAccountInfo.GetConfigurationSetting(UserContainerKey, UserContainerName, true);
                container = blobStorage.GetBlobContainer(userContainer);
                container.CreateContainer(null, ContainerAccessControl.Private);

                // Create notebook container
                string notebookContainer = StorageAccountInfo.GetConfigurationSetting(NotebookContainerKey, NotebookContainerName, true);
                container = blobStorage.GetBlobContainer(notebookContainer);
                container.CreateContainer(null, ContainerAccessControl.Private);

                // Create page container
                string pageContainer = StorageAccountInfo.GetConfigurationSetting(PageContainerKey, PageContainerName, true);
                container = blobStorage.GetBlobContainer(pageContainer);
                container.CreateContainer(null, ContainerAccessControl.Private); 
                
                return true;
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }

        /// <summary>
        /// Delete containers into storage.
        /// </summary>
        /// <returns></returns>
        public static bool Terminate()
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();
            BlobContainer container;

            try
            {
                // Delete user container
                string userContainer = StorageAccountInfo.GetConfigurationSetting(UserContainerKey, UserContainerName, true);
                container = blobStorage.GetBlobContainer(userContainer);
                container.DeleteContainer();

                // Delete notebook container
                string notebookContainer = StorageAccountInfo.GetConfigurationSetting(NotebookContainerKey, NotebookContainerName, true);
                container = blobStorage.GetBlobContainer(notebookContainer);
                container.DeleteContainer();

                // Delete page container
                string pageContainer = StorageAccountInfo.GetConfigurationSetting(PageContainerKey, PageContainerName, true);
                container = blobStorage.GetBlobContainer(pageContainer);
                container.DeleteContainer();

                return true;
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }

        /// <summary>
        /// Serialize and save an object into the storage.
        /// </summary>
        /// <param name="container">container to store</param>
        /// <param name="name">blob name</param>
        /// <param name="o">object to store</param>
        /// <param name="t">type of object</param>
        /// <returns>true if OK</returns>
        private static bool SaveObject(BlobContainer container, string name, object o, Type t)
        {
            try
            {
                // Serialize object as an XML stream
                MemoryStream stream = new MemoryStream();
                DataContractSerializer serializer = new DataContractSerializer(t);
                serializer.WriteObject(stream, o);
                stream.Position = 0;

                // Create object
                container.CreateBlob(new BlobProperties(name), new BlobContents(stream), true);

                // Close stream
                stream.Close();

                return true;
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }

        /// <summary>
        /// Load and deserialize an object from the storage.
        /// </summary>
        /// <param name="container">container where object is located</param>
        /// <param name="name">object name</param>
        /// <param name="t">object type</param>
        /// <returns>object reference</returns>
        private static object LoadObject(BlobContainer container, string name, Type t)
        {
            try
            {
                // Load object
                BlobContents contents = new BlobContents(new MemoryStream());
                BlobProperties properties = container.GetBlob(name, contents, false);
                if (properties.Name != name)
                    return null;

                // Convert to specified type
                DataContractSerializer serializer = new DataContractSerializer(t);
                Stream stream = contents.AsStream;
                stream.Position = 0;
                object o = serializer.ReadObject(stream);

                // Close stream
                stream.Close();

                return o;
            }
            catch (StorageException se)
            {
                // Catch only Not found object
                if (se.StatusCode == HttpStatusCode.NotFound)
                    return null;
                else
                    throw se;
            }
        }

        #region User operations
        /// <summary>
        /// Save user object into the storage. 
        /// </summary>
        /// <param name="user">user to save</param>
        /// <returns>true if okay</returns>
        public static bool SaveUser(User user)
        {
            // Update user last modified time
            user.LastModifiedTime = DateTime.Now;

            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get user container
                string userContainer = StorageAccountInfo.GetConfigurationSetting(UserContainerKey, UserContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(userContainer);

                // Save object
                SaveObject(container, user.ID, user, typeof(User));

                return true;
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }

        /// <summary>
        /// Load an user from the storage.
        /// </summary>
        /// <param name="id">user id</param>
        /// <returns>object read or null if don't exist</returns>
        public static User LoadUser(string id)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get user container
                string userContainer = StorageAccountInfo.GetConfigurationSetting(UserContainerKey, UserContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(userContainer);

                // Load object
                object o = LoadObject(container, id, typeof(User));
                if (o == null)
                    return null;

                // Convert to user
                return o as User;
            }
            catch (StorageException se)
            {
                // Catch only Not found object
                if (se.StatusCode == HttpStatusCode.NotFound)
                    return null;
                else
                    throw se;
            }
        }

        /// <summary>
        /// Delete an user from the storage.
        /// </summary>
        /// <param name="id">user id</param>
        /// <returns>true if deleted</returns>
        public static bool DeleteUser(string id)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get user container
                string userContainer = StorageAccountInfo.GetConfigurationSetting(UserContainerKey, UserContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(userContainer);

                // Delete object
                return container.DeleteBlob(id);
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }
        #endregion

        #region Notebook operations
        /// <summary>
        /// Save notebook object into the storage. 
        /// </summary>
        /// <param name="user">notebook to save</param>
        /// <returns>true if okay</returns>
        public static bool SaveNotebook(Notebook notebook)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get user container
                string notebookContainer = StorageAccountInfo.GetConfigurationSetting(NotebookContainerKey, NotebookContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(notebookContainer);

                // Save object
                SaveObject(container, notebook.ID, notebook, typeof(Notebook));

                return true;
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }

        /// <summary>
        /// Load a notebook from the storage.
        /// </summary>
        /// <param name="id">notebook id</param>
        /// <returns>object read or null if don't exist</returns>
        public static Notebook LoadNotebook(string id)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get notebook container
                string notebookContainer = StorageAccountInfo.GetConfigurationSetting(NotebookContainerKey, NotebookContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(notebookContainer);

                // Load object
                object o = LoadObject(container, id, typeof(Notebook));
                if (o == null)
                    return null;

                // Convert to user
                return o as Notebook;
            }
            catch (StorageException se)
            {
                // Catch only Not found object
                if (se.StatusCode == HttpStatusCode.NotFound)
                    return null;
                else
                    throw se;
            }
        }

        /// <summary>
        /// Delete a notebook from the storage.
        /// </summary>
        /// <param name="id">notebook id</param>
        /// <returns>true if deleted</returns>
        public static bool DeleteNotebook(string id)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get user container
                string notebookContainer = StorageAccountInfo.GetConfigurationSetting(NotebookContainerKey, NotebookContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(notebookContainer);

                // Delete object
                return container.DeleteBlob(id);
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }
        #endregion

        #region Page operations
        /// <summary>
        /// Save user object into the storage. 
        /// </summary>
        /// <param name="user">user to save</param>
        /// <returns>true if okay</returns>
        public static bool SavePage(Page page)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get page container
                string pageContainer = StorageAccountInfo.GetConfigurationSetting(PageContainerKey, PageContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(pageContainer);

                // Save object
                SaveObject(container, page.ID, page, typeof(Page));

                return true;
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }

        /// <summary>
        /// Load a page from the storage.
        /// </summary>
        /// <param name="id">page id</param>
        /// <returns>object read or null if don't exist</returns>
        public static Page LoadPage(string id)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get page container
                string pageContainer = StorageAccountInfo.GetConfigurationSetting(PageContainerKey, PageContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(pageContainer);

                // Load object
                object o = LoadObject(container, id, typeof(Page));
                if (o == null)
                    return null;

                // Convert to user
                return o as Page;
            }
            catch (StorageException se)
            {
                // Catch only Not found object
                if (se.StatusCode == HttpStatusCode.NotFound)
                    return null;
                else
                    throw se;
            }
        }

        /// <summary>
        /// Delete a page from the storage.
        /// </summary>
        /// <param name="id">page id</param>
        /// <returns>true if deleted</returns>
        public static bool DeletePage(string id)
        {
            // Connect to blob
            BlobStorage blobStorage = ConnectToBlob();

            try
            {
                // Get user container
                string pageContainer = StorageAccountInfo.GetConfigurationSetting(PageContainerKey, PageContainerName, true);
                BlobContainer container = blobStorage.GetBlobContainer(pageContainer);

                // Delete object
                return container.DeleteBlob(id);
            }
            catch (StorageException)
            {
                return false;
            }
            catch (WebException)
            {
                return false;
            }
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect C2S
France France
Lionel is a software architect at C2S, a software company based in France and subsidiary of the Bouygues group.
Lionel is also the author of Liogo, an open-source Logo compiler for .NET.
Lionel is a contributor of DotNetGuru and Dr.Dobb's Journal.
Lionel is President and co-founder of OLPC France.

Comments and Discussions