context: /*****************************************************************************
  *
  * Copyright (c) [2009] Novell, Inc.
  * All Rights Reserved.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License as
  * published by the Free Software Foundation.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, contact Novell, Inc.
  *
  * To contact Novell about this file by physical or electronic mail,
  * you may find current contact information at www.novell.com
  *
  *-----------------------------------------------------------------------------
  *
  *                 $Author: Bruce Getter <bgetter@novell.com>
  *                 $Modified by: <Modifier>
  *                 $Mod Date: <Date Modified>
  *                 $Revision: 0.1
  *-----------------------------------------------------------------------------
  * This module is used to:
  *        <Description of the functionality of the file >
  *
  *
  *******************************************************************************/
  
  using System;
  using System.Xml;
  
  using Simias.Client;
  using Simias.Storage;
  using Simias.Sync;
  
  namespace Simias.POBox
  {
  	/// <summary>
  	/// A POBox object is a specialized collection used to hold messages.
  	/// </summary>
  	public class POBox : Collection
  	{
  		#region Class Members
  		private static readonly ISimiasLog log = SimiasLogManager.GetLogger(typeof(POBox));
  
  		/// <summary>
  		/// The name of the property storing the DirNode name.
  		/// </summary>
  		public const string POServiceUrlProperty = "POServiceUrl";
  
  		#endregion
  
  		#region Properties
  		
  		/// <summary>
  		/// Gets/sets the post-office service url.
  		/// </summary>
  		public string POServiceUrl
  		{
  			get
  			{
  				string result = null;
  
  				Property p = Properties.GetSingleProperty(POServiceUrlProperty);
  
  				if (p != null)
  				{
  					result = p.ToString();
  				}
  				else
  				{
  					result = this.MasterUrl.ToString() + "/POService.asmx";
  				}
  
  				return result;
  			}
  			set
  			{
  				Property p = new Property(POServiceUrlProperty, value);
  				p.LocalProperty = true;
  
  				Properties.ModifyProperty(p);
  			}
  		}
  
  		#endregion
  
  		#region Constructors
  		/// <summary>
  		/// Constructor to create a POBox object from a Node object.
  		/// </summary>
  		/// <param name="storeObject">Store object that this POBox belongs to.</param>
  		/// <param name="node">Node object to construct POBox object from.</param>
  		internal POBox(Store storeObject, Node node) :
  			base (storeObject, node)
  		{
  		}
  
  		/// <summary>
  		/// Constructor to create a POBox object from a ShallowNode object.
  		/// </summary>
  		/// <param name="storeObject">The Store object that this POBox belongs to.</param>
  		/// <param name="shallowNode">The ShallowNode object to contruct the POBox object from.</param>
  		internal POBox(Store storeObject, ShallowNode shallowNode) :
  			base (storeObject, shallowNode)
  		{
  		}
  
  		/// <summary>
  		/// Constructor to create an existing POBox object from an Xml document object.
  		/// </summary>
  		/// <param name="storeObject">Store object that this collection belongs to.</param>
  		/// <param name="document">Xml document object to construct this object from.</param>
  		internal POBox( Store storeObject, XmlDocument document ) :
  			base( storeObject, document )
  		{
  		}
  
  		/// <summary>
  		/// Constructor to create a POBox object.
  		/// </summary>
  		/// <param name="storeObject">The Store object that the POBox will belong to.</param>
  		/// <param name="collectionName">The name of the POBox.</param>
  		/// <param name="domainName">The name of the domain that the POBox belongs to.</param>
  		public POBox(Store storeObject, string collectionName, string domainName) :
  			this (storeObject, collectionName, Guid.NewGuid().ToString(), domainName)
  		{
  		}
  
  		/// <summary>
  		/// Constructor to create a POBox object.
  		/// </summary>
  		/// <param name="storeObject">The Store object that the POBox will belong to.</param>
  		/// <param name="collectionName">The name of the POBox.</param>
  		/// <param name="collectionID">The identifier of the POBox.</param>
  		/// <param name="domainName">The name of the domain that the POBox belongs to.</param>
  		internal POBox(Store storeObject, string collectionName, string collectionID, string domainName) :
  			base( storeObject, collectionName, collectionID, NodeTypes.POBoxType, domainName )
  		{
  		}
  
  		#endregion
  
  		#region Private Methods
  
  		/// <summary>
  		/// Deserialize the serialized Subscription Node.
  		/// </summary>
  		/// <param name="subscription">The XML node.</param>
  		/// <returns>The Subscription</returns>
  		static private Subscription DeserializeSubscription(string subscription)
  		{
  			// Get the subscription Node.
  			XmlDocument xNode = new XmlDocument();
  			xNode.LoadXml(subscription);
  			return (Subscription)Node.NodeFactory(Store.GetStore(), xNode);
  		}
  		
  		#endregion
  
  		#region Internal Methods
  		/// <summary>
  		/// POBox factory method that constructs a POBox object for the specified user in the specified domain.
  		/// </summary>
  		/// <param name="storeObject">The Store object that the POBox belongs to.</param>
  		/// <param name="domainId">The ID of the domain that the POBox belongs to.</param>
  		/// <param name="userId">The ID of the user that the POBox belongs to.</param>
  		/// <returns></returns>
  		public static POBox FindPOBox(Store storeObject, string domainId, string userId)
  		{
  			POBox poBox = null;
  
  			// Build the name of the POBox.
  			string name = "POBox:" + domainId + ":" + userId;
  
  			// Search for the POBox.
  			ICSEnumerator listEnum = storeObject.GetCollectionsByName(name).GetEnumerator() as ICSEnumerator;
  
  			// There should only be one value returned...
  			if (listEnum.MoveNext())
  			{
  				ShallowNode shallowNode = (ShallowNode)listEnum.Current;
  
  				if (listEnum.MoveNext())
  				{
  					// TODO: multiple values were returned ... throw an exception.
  				}
  
  				poBox = new POBox(storeObject, shallowNode);
  			}
  
  			listEnum.Dispose();
  			return poBox;
  		}
  		#endregion
  
  		#region Public Methods
  		
  		/// <summary>
  		/// POBox factory method that constructs a POBox object for the specified domain ID.
  		/// </summary>
  		/// <param name="storeObject">The Store object that the POBox belongs to.</param>
  		/// <param name="domainId">The ID of the domain that the POBox belongs to.</param>
  		/// <returns></returns>
  		public static POBox GetPOBox(Store storeObject, string domainId)
  		{
  			return GetPOBox(storeObject, domainId, storeObject.GetUserIDFromDomainID(domainId));
  		}
  
  		/// <summary>
  		/// POBox factory method that constructs a POBox object for the specified user in the specified domain.
  		/// </summary>
  		/// <param name="storeObject">The Store object that the POBox belongs to.</param>
  		/// <param name="domainId">The ID of the domain that the POBox belongs to.</param>
  		/// <param name="userId">The ID of the user that the POBox belongs to.</param>
  		/// <returns></returns>
  		public static POBox GetPOBox(Store storeObject, string domainId, string userId)
  		{
  			POBox poBox = FindPOBox( storeObject, domainId, userId );
  
gt: 			if (poBox == null)