v1.2.10
This commit is contained in:
@@ -0,0 +1,210 @@
|
||||
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 SaxSportContactsData
|
||||
{
|
||||
//-------------------------------------------------------------------
|
||||
// Definitions.
|
||||
private static readonly String ElementName_Contacts = "Contacts";
|
||||
private static readonly String ElementName_Contact = "Contact";
|
||||
private static readonly String AttributeName_Type = "Type";
|
||||
private static readonly String ElementName_Type = "Type";
|
||||
private static readonly String ElementName_UserAccountId = "UserAccountId";
|
||||
private static readonly String ElementName_Name = "Name";
|
||||
private static readonly String ElementName_Phone = "Phone";
|
||||
private static readonly String ElementName_Email = "Email";
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Data members.
|
||||
private class ContactData
|
||||
{
|
||||
public Sax.SportContactType ContactType { get; set; }
|
||||
public SysId UserAccountId { get; set; }
|
||||
public String Name { get; set; }
|
||||
public String Phone { get; set; }
|
||||
public String Email { get; set; }
|
||||
}
|
||||
private Dictionary<Sax.SportContactType, ContactData> contacts { get; } = new Dictionary<Sax.SportContactType, ContactData>();
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Construction - from serialised data.
|
||||
public SaxSportContactsData(String contactsData = "")
|
||||
{
|
||||
FromXml(contactsData);
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Properties.
|
||||
public List<Sax.SportContactType> ContactTypes { get { return contacts.Keys.ToList(); } }
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Utilities.
|
||||
|
||||
/// <summary>Get the contact for the specified type.</summary>
|
||||
private ContactData GetContactData(Sax.SportContactType contactType)
|
||||
{
|
||||
if ((int)contactType < 1) return null;
|
||||
ContactData contact = contacts.GetValueOrDefault(contactType, null);
|
||||
if (contact == null)
|
||||
contacts.Add(contactType, contact = new ContactData() { ContactType = contactType });
|
||||
return contact;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Contacts.
|
||||
//-------------------------------------------------------------------
|
||||
/// <summary>Extract the name associated with the specified type.</summary>
|
||||
public String GetContactName(Sax.SportContactType contactType)
|
||||
{
|
||||
return contacts.GetValueOrDefault(contactType, new ContactData()).Name;
|
||||
}
|
||||
|
||||
/// <summary>Extract the phone associated with the specified type.</summary>
|
||||
public String GetContactPhone(Sax.SportContactType contactType)
|
||||
{
|
||||
return contacts.GetValueOrDefault(contactType, new ContactData()).Phone;
|
||||
}
|
||||
|
||||
/// <summary>Extract the email associated with the specified type.</summary>
|
||||
public String GetContactEmail(Sax.SportContactType contactType)
|
||||
{
|
||||
return contacts.GetValueOrDefault(contactType, new ContactData()).Email;
|
||||
}
|
||||
|
||||
/// <summary>Set the name associated with the specified contact.</summary>
|
||||
public bool SetContactName(Sax.SportContactType contactType, String name)
|
||||
{
|
||||
if ((int)contactType < 1 || name.IsEmpty()) return false;
|
||||
ContactData contact = GetContactData(contactType);
|
||||
if (contact == null) return false;
|
||||
contact.Name = name;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Set the phone associated with the specified contact.</summary>
|
||||
public bool SetContactPhone(Sax.SportContactType contactType, String phone)
|
||||
{
|
||||
if ((int)contactType < 1 || phone.IsEmpty()) return false;
|
||||
ContactData contact = GetContactData(contactType);
|
||||
if (contact == null) return false;
|
||||
contact.Phone = phone;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>Set the email associated with the specified contact.</summary>
|
||||
public bool SetContactEmail(Sax.SportContactType contactType, String email)
|
||||
{
|
||||
if ((int)contactType < 1 || email.IsEmpty()) return false;
|
||||
ContactData contact = GetContactData(contactType);
|
||||
if (contact == null) return false;
|
||||
contact.Email = email;
|
||||
return true;
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
/// <summary>Clear the value associated with the specified method.</summary>
|
||||
public bool ClearContact(Sax.SportContactType contactType)
|
||||
{
|
||||
return contacts.Remove(contactType);
|
||||
}
|
||||
|
||||
/// <summary>Clear all contact methods.</summary>
|
||||
public void ClearContacts()
|
||||
{
|
||||
contacts.Clear();
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
// Serialisation.
|
||||
//-------------------------------------------------------------------
|
||||
/// <summary>Serialise as XML.</summary>
|
||||
public String ToXml()
|
||||
{
|
||||
String fn = typeof(SaxContactData).FullName + ".ToXml";
|
||||
try
|
||||
{
|
||||
XDocument doc = new XDocument();
|
||||
XElement contactsElement = doc.AppendElement(ElementName_Contacts);
|
||||
foreach (ContactData contactData in contacts.Values)
|
||||
{
|
||||
if ((int)contactData.ContactType < 1 || (!SysId.IsValid(contactData.UserAccountId) && contactData.Name.IsEmpty())) continue;
|
||||
XElement contactElement = contactsElement.AppendElement(ElementName_Contact);
|
||||
contactElement.SetAttribute(AttributeName_Type, ((int)contactData.ContactType).ToString());
|
||||
contactElement.AppendElement(ElementName_Type, contactData.ContactType.ToString());
|
||||
if (!StringUtil.IsBlank(contactData.Name))
|
||||
contactElement.AppendElement(ElementName_Name, contactData.Name);
|
||||
if (!StringUtil.IsBlank(contactData.Phone))
|
||||
contactElement.AppendElement(ElementName_Phone, contactData.Phone);
|
||||
if (!StringUtil.IsBlank(contactData.Email))
|
||||
contactElement.AppendElement(ElementName_Email, contactData.Email);
|
||||
}
|
||||
return doc.GetXml();
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
SaLogger.Warn(fn + " " + ExceptionUtil.GetLogMessage(exc));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
/// <summary>Deserialise from XML data.</summary>
|
||||
private void FromXml(String contactsData)
|
||||
{
|
||||
String fn = typeof(SaxContactData).FullName + ".FromXml";
|
||||
contacts.Clear();
|
||||
if (StringUtil.IsEmpty(contactsData)) return;
|
||||
|
||||
try
|
||||
{
|
||||
// Load XML data.
|
||||
XDocument doc = XDocument.Parse(contactsData);
|
||||
XElement rootElement = doc.Root;
|
||||
foreach (XElement rootChildElement in rootElement.GetChildElements())
|
||||
{
|
||||
String rootChildElementName = rootChildElement.GetLocalName();
|
||||
if (rootChildElementName.EqualsCI(ElementName_Contact))
|
||||
{
|
||||
// Contact.
|
||||
String contactTypeValue = rootChildElement.GetChildText(ElementName_Type);
|
||||
Sax.SportContactType contactType = EnumUtil.ParseEnum(contactTypeValue, Sax.SportContactType.Unknown);
|
||||
if (contactType != Sax.SportContactType.Unknown)
|
||||
{
|
||||
ContactData contactData = new ContactData()
|
||||
{
|
||||
ContactType = contactType,
|
||||
UserAccountId = rootChildElement.GetChildText(ElementName_UserAccountId).ToSysId(),
|
||||
Name = rootChildElement.GetChildText(ElementName_Name),
|
||||
Phone = rootChildElement.GetChildText(ElementName_Phone),
|
||||
Email = rootChildElement.GetChildText(ElementName_Email),
|
||||
};
|
||||
contacts.Add(contactType, contactData);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Unrecognised contact type: " + contactType);
|
||||
}
|
||||
else SaLogger.Warn(fn + " Unexpected 'Root' child element: " + rootChildElementName);
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
SaLogger.Warn(fn + " " + ExceptionUtil.GetLogMessage(exc));
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------------------------
|
||||
}
|
||||
//=======================================================================
|
||||
}
|
||||
Reference in New Issue
Block a user