using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Helpers; using Sarand.Net.Standard.Base.Utils; using Saxonite.Base.App; using Saxonite.Net.Core.Web.Base.Controllers; using Saxonite.Net.Standard.Base.App; using Saxonite.Net.Standard.Base.Bus; using Saxonite.Net.Standard.Base.Helpers; using System; namespace Saxonite.Net.Core.Web.Online.Models { //======================================================================= /// /// Organisation model data properties. /// public class ModelData_Organisation : ModelData_Online { //------------------------------------------------------------------- // Construction. public ModelData_Organisation(SaxModel_Online model, String controlNameSuffix = "Organisation") : base(model) { this.ControlNameSuffix = StringUtil.EmptyOnNull(controlNameSuffix); Address = new ModelData_Address(model, ControlNameSuffix); } //------------------------------------------------------------------- // Properties. private String ControlNameSuffix { get; } // Organisation properties. public String Id { get; set; } public String Name { get; set; } public String Type { get; set; } public Sax.OrganisationType GetOrganisationType() { return (Sax.OrganisationType)Model.OrganisationTypeCodes.GetTypeForCode(Type); } public String Category { get; set; } public Sax.OrganisationCategory GetOrganisationCategory() { return (Sax.OrganisationCategory)Model.OrganisationCategoryCodes.GetTypeForCode(Category); } public String SportId { get; set; } public String RegionIdPrimary { get; set; } public String About { get; set; } public String Facilities { get; set; } // Contact Methods. /// Placeholder field value to indicate the number of Contact Methods in form (even if none are populated). public String ContactMethods { get; set; } public String[] ContactMethod { get; set; } public String[] ContactValue { get; set; } /// Contact methods to be populated during validation. public SaxContactData ContactData { get; set; } // Address. public ModelData_Address Address { get; } //------------------------------------------------------------------- // Validation. //------------------------------------------------------------------- // Organisation. public bool ValidateOrganisationData(SaxController_Base.AjaxResponse_Data responseData) { String fn = this.GetType().Name + ".ValidateOrganisationData"; // Name - mandatory. responseData.SetControlValue(ControlNameSuffix + nameof(Name), Name); if (StringUtil.IsEmpty(Name)) responseData.SetControlError(ControlNameSuffix + nameof(Name), Model.Localiser["msg.validation.mandatory.value"]); else if (StringUtil.GetLength(Name) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Name)) responseData.SetControlError(ControlNameSuffix + nameof(Name), Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Name)]); else if (SysId.IsValid(Model.ExtractSysId(RegionIdPrimary))) { // Ensure that the organisation name is unique within the region. SaxOrganisation.Params prmOrganisation = new SaxOrganisation.Params(); if (SysId.IsValid(Model.ExtractSysId(Id))) prmOrganisation.IdsExcluded.Add(Model.ExtractSysId(Id)); prmOrganisation.NameFull = Name; prmOrganisation.RegionIdsIncluded.Add(Model.ExtractSysId(RegionIdPrimary)); prmOrganisation.StatusCat = SaxOrganisation.StatusCat.Any; if (Model.AccountMgr.GetOrganisationCount(prmOrganisation) > 0) responseData.SetControlError(ControlNameSuffix + nameof(Name), Model.Localiser["msg.validation.duplicate.value"]); } // Organisation Category - mandatory. if (StringUtil.IsEmpty(Category)) responseData.SetControlError(ControlNameSuffix + nameof(Category), Model.Localiser["msg.validation.mandatory.value"]); // Organisation Type - mandatory. if (StringUtil.IsEmpty(Type)) responseData.SetControlError(ControlNameSuffix + nameof(Type), Model.Localiser["msg.validation.mandatory.value"]); // Sport - mandatory for certain organisation categories. if (StringUtil.IsEmpty(SportId) && SaxOrganisation.IsSportMandatory(GetOrganisationCategory())) responseData.SetControlError(ControlNameSuffix + nameof(SportId), Model.Localiser["msg.validation.mandatory.value"]); else if (!StringUtil.IsEmpty(SportId) && !SysId.IsValid(Model.ExtractSysId(SportId))) responseData.SetControlError(ControlNameSuffix + nameof(SportId), Model.Localiser["msg.validation.invalid.value"]); // Primary Region - mandatory. if (StringUtil.IsEmpty(RegionIdPrimary)) responseData.SetControlError(ControlNameSuffix + nameof(RegionIdPrimary), Model.Localiser["msg.validation.mandatory.value"]); else if (!SysId.IsValid(Model.ExtractSysId(RegionIdPrimary))) responseData.SetControlError(ControlNameSuffix + nameof(RegionIdPrimary), Model.Localiser["msg.validation.invalid.value"]); // About - optional. responseData.SetControlValue(ControlNameSuffix + nameof(About), About); if (StringUtil.GetLength(About) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_About)) responseData.SetControlError(ControlNameSuffix + nameof(About), Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_About)]); // Facilities - optional. responseData.SetControlValue(ControlNameSuffix + nameof(Facilities), Facilities); if (StringUtil.GetLength(Facilities) > Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Facilities)) responseData.SetControlError(ControlNameSuffix + nameof(Facilities), Model.Localiser["msg.validation.maximum.length", Model.GetControlMaxLength(SaxDbDfn_Core.ColumnIdx.Organisation_Facilities)]); // Contact Methods. if (ContactMethods != null) { ContactData = new SaxContactData(""); ContactMethod = ContactMethod ?? new String[0]; ContactValue = ContactValue ?? new String[0]; for (int idx = 0; idx < ContactMethod.Length && idx < ContactValue.Length; idx++) { String methodValue = ContactMethod[idx]; String value = ContactValue[idx]; if (methodValue == null && value == null) continue; Sax.ContactMethod method = EnumUtil.ParseEnum(methodValue, Sax.ContactMethod.Unknown); if (method > 0 && !value.IsEmpty() && SaxContactMethodHelper.IsValidContactMethodValue(method, value)) ContactData.SetContactMethodValue(method, value); else SaLogger.Warn(fn + " Invalid Contact Method data. Method: " + methodValue + ". Value: " + value); } if (ContactData.Methods.Count != ContactMethods.ToInt()) { SaLogger.Warn(fn + " Inconsistent Contact Methods received. Expected: " + ContactMethods + ". Received: " + ContactData.Methods.Count); responseData.SetMessage("caption.contact.method", "System error"); } } // Address. Address.ValidateAddressData(responseData); return !responseData.HasControlErrors() && responseData.Message == null; } //------------------------------------------------------------------- } //======================================================================= }