152 lines
5.3 KiB
C#
152 lines
5.3 KiB
C#
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Saxonite.Net.Standard.Base.Bus
|
|
{
|
|
//=======================================================================
|
|
// Contact data - associated with Organisations, Users etc.
|
|
//=======================================================================
|
|
public class SaxContactData
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
private static readonly String ElementName_ContactMethods = "contactMethods";
|
|
private static readonly String ElementName_ContactMethod = "contactMethod";
|
|
private static readonly String AttributeName_Method = "method";
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private class ContactMethodData
|
|
{
|
|
public Sax.ContactMethod Method { get; set; }
|
|
public String Value { get; set; }
|
|
}
|
|
private Dictionary<Sax.ContactMethod, ContactMethodData> contactMethods { get; } = new Dictionary<Sax.ContactMethod, ContactMethodData>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction - from serialised data.
|
|
public SaxContactData(String contactData = "")
|
|
{
|
|
ExtractContactData(contactData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public List<Sax.ContactMethod> Methods { get { return contactMethods.Keys.ToList(); } }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Contact Methods.
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Extract the value associated with the specified method.</summary>
|
|
public String GetContactMethodValue(Sax.ContactMethod method)
|
|
{
|
|
return contactMethods.GetValueOrDefault(method, new ContactMethodData()).Value;
|
|
}
|
|
|
|
/// <summary>Set the value associated with the specified method.</summary>
|
|
public bool SetContactMethodValue(Sax.ContactMethod method, String value)
|
|
{
|
|
if ((int)method < 1 || value.IsEmpty()) return false;
|
|
ContactMethodData contactMethod = contactMethods.GetValueOrDefault(method, null);
|
|
if (contactMethod == null)
|
|
contactMethods.Add(method, contactMethod = new ContactMethodData() { Method = method });
|
|
contactMethod.Value = value;
|
|
return true;
|
|
}
|
|
|
|
/// <summary>Clear the value associated with the specified method.</summary>
|
|
public bool ClearContactMethod(Sax.ContactMethod method)
|
|
{
|
|
return contactMethods.Remove(method);
|
|
}
|
|
|
|
/// <summary>Clear all contact methods.</summary>
|
|
public void ClearContactMethods(Sax.ContactMethod method)
|
|
{
|
|
contactMethods.Clear();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Serialisation.
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Extract contact methods from serialised data.
|
|
/// </summary>
|
|
private void ExtractContactData(String contactData)
|
|
{
|
|
String fn = typeof(SaxContactData).FullName + ".ExtractContent";
|
|
contactMethods.Clear();
|
|
if (StringUtil.IsEmpty(contactData)) return;
|
|
|
|
try
|
|
{
|
|
// Load XML data.
|
|
XDocument doc = XDocument.Parse(contactData);
|
|
XElement rootElement = doc.Root;
|
|
foreach (XElement rootChildElement in rootElement.GetChildElements())
|
|
{
|
|
String rootChildElementName = rootChildElement.GetLocalName();
|
|
if (rootChildElementName.EqualsCI(ElementName_ContactMethod))
|
|
{
|
|
// Contact Method.
|
|
String methodValue = rootChildElement.GetAttributeValue(AttributeName_Method);
|
|
Sax.ContactMethod method = EnumUtil.ParseEnum(methodValue, Sax.ContactMethod.Unknown);
|
|
if (method != Sax.ContactMethod.Unknown)
|
|
{
|
|
ContactMethodData contactMethod = new ContactMethodData()
|
|
{
|
|
Method = method,
|
|
Value = rootChildElement.Value
|
|
};
|
|
contactMethods.Add(method, contactMethod);
|
|
}
|
|
else SaLogger.Warn(fn + " Unrecognised contact method: " + methodValue);
|
|
}
|
|
else SaLogger.Warn(fn + " Unexpected 'Root' child element: " + rootChildElementName);
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(fn + " " + ExceptionUtil.GetLogMessage(exc));
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>
|
|
/// Convert to serialised data.
|
|
/// </summary>
|
|
public String GetContactData()
|
|
{
|
|
String fn = typeof(SaxContactData).FullName + ".GetContactData";
|
|
try
|
|
{
|
|
XDocument doc = new XDocument();
|
|
XElement contactMethodsElement = doc.AppendElement(ElementName_ContactMethods);
|
|
foreach (ContactMethodData contactMethod in contactMethods.Values)
|
|
{
|
|
if ((int)contactMethod.Method < 1 || contactMethod.Value.IsEmpty()) continue;
|
|
XElement contactMethodElement = contactMethodsElement.AppendElement(ElementName_ContactMethod, contactMethod.Value);
|
|
contactMethodElement.SetAttribute(AttributeName_Method, contactMethod.Method.ToString());
|
|
}
|
|
return doc.GetXml();
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Warn(fn + " " + ExceptionUtil.GetLogMessage(exc));
|
|
}
|
|
return "";
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|