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 contacts { get; } = new Dictionary(); //------------------------------------------------------------------- // Construction - from serialised data. public SaxSportContactsData(String contactsData = "") { FromXml(contactsData); } //------------------------------------------------------------------- // Properties. public List ContactTypes { get { return contacts.Keys.ToList(); } } //------------------------------------------------------------------- // Utilities. /// Get the contact for the specified type. 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. //------------------------------------------------------------------- /// Extract the name associated with the specified type. public String GetContactName(Sax.SportContactType contactType) { return contacts.GetValueOrDefault(contactType, new ContactData()).Name; } /// Extract the phone associated with the specified type. public String GetContactPhone(Sax.SportContactType contactType) { return contacts.GetValueOrDefault(contactType, new ContactData()).Phone; } /// Extract the email associated with the specified type. public String GetContactEmail(Sax.SportContactType contactType) { return contacts.GetValueOrDefault(contactType, new ContactData()).Email; } /// Set the name associated with the specified contact. 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; } /// Set the phone associated with the specified contact. 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; } /// Set the email associated with the specified contact. 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; } //------------------------------------------------------------------- /// Clear the value associated with the specified method. public bool ClearContact(Sax.SportContactType contactType) { return contacts.Remove(contactType); } /// Clear all contact methods. public void ClearContacts() { contacts.Clear(); } //------------------------------------------------------------------- // Serialisation. //------------------------------------------------------------------- /// Serialise as XML. 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 ""; } //------------------------------------------------------------------- /// Deserialise from XML data. 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)); } } //------------------------------------------------------------------- } //======================================================================= }