/// <summary>
        /// Instantiate the context instance, if it doesn't yet exist
        /// </summary>
        /// <returns>OECContext</returns>
        public static OECContext Context()
        {
            // Support multithreaded applications through 'double-checked locking':
            // - first program asking for the context locks everyone else out, then instantiates it
            // - when the lock is released, locked-out programs skip instantiation
            if (_context == null)         // if instance already exists, skip to end
            {
                lock (syncLock)           // first one here locks everyone else out until the instance is created
                {
                    if (_context == null) // people who were locked out now see instance & skip to end
                    {
                        var optionsBuilder = new DbContextOptionsBuilder <OECContext>();
                        optionsBuilder.UseSqlServer(@"Server =.\sqlexpress;Database=OEC;Trusted_Connection=True");

                        _context = new OECContext(optionsBuilder.Options);
                    }
                }
            }
            return(_context);
        }
Ejemplo n.º 2
0
        //1dii creates a validate method
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            OECContext _context = OEC_Singleton.Context();

            //trimm all strings of leading and trailing spaces
            if (Name != null)
            {
                Name = Name.ToLower().Trim();
                Name = ADValidation.ADCapitalize(Name);
            }
            if (Address != null)
            {
                Address = Address.ToLower().Trim();
                Address = ADValidation.ADCapitalize(Address);
            }
            if (Town != null)
            {
                Town = Town.ToLower().Trim();
                Town = ADValidation.ADCapitalize(Town);
            }
            if (County != null)
            {
                County = County.ToLower().Trim();
                County = ADValidation.ADCapitalize(County);
            }
            if (ProvinceCode != null)
            {
                ProvinceCode = ProvinceCode.Trim();
                ProvinceCode = ProvinceCode.ToUpper();
            }
            if (PostalCode != null)
            {
                PostalCode = PostalCode.Trim();
            }
            if (Email != null)
            {
                Email = Email.Trim();
            }
            if (Directions != null)
            {
                Directions = Directions.Trim();
            }

            //either the town or country must be provided
            if (String.IsNullOrWhiteSpace(Town) && String.IsNullOrWhiteSpace(County))
            {
                yield return(new ValidationResult("At least one town or country must be provided."));
            }

            if (String.IsNullOrWhiteSpace(Email))
            {
                if (String.IsNullOrWhiteSpace(Address) || String.IsNullOrWhiteSpace(PostalCode))
                {
                    yield return(new ValidationResult("If no email, you must provide an address and postal code."));
                }
            }

            //if postal code provided, validate and format it using postalcodevalidation or zipcode validation, depending which country
            if (!String.IsNullOrWhiteSpace(PostalCode))
            {
                if (!String.IsNullOrWhiteSpace(ProvinceCode))
                {
                    var countryCode = "";

                    if (ProvinceCode.Length == 2)
                    {
                        countryCode = _context.Province.SingleOrDefault(p => p.ProvinceCode == ProvinceCode).CountryCode;
                    }
                    else
                    {
                        countryCode = _context.Province.SingleOrDefault(p => p.Name == ProvinceCode).CountryCode;
                    }

                    string postalCode = PostalCode;
                    if (countryCode == "CA")
                    {
                        if (!ADValidation.ADPostalCodeValidation(ref postalCode))
                        {
                            yield return(new ValidationResult("Postal Code is not a valid pattern (N5G 6Z6)", new string[] { nameof(PostalCode) }));
                        }
                        else
                        {
                            PostalCode = postalCode;
                        }
                    }
                    if (countryCode == "US")
                    {
                        if (!ADValidation.ADZipCodeValidation(ref postalCode))
                        {
                            yield return(new ValidationResult("Zip Code is not a valid pattern (12345 or 12345-1234)", new string[] { nameof(PostalCode) }));
                        }
                        PostalCode = postalCode;
                    }
                }
            }

            //either home phone or cellphone must be provided
            if (String.IsNullOrWhiteSpace(HomePhone) && String.IsNullOrWhiteSpace(CellPhone))
            {
                yield return(new ValidationResult("You must provide either your cell or home phone number."));
            }
            else
            {
                Regex pattern = new Regex(@"\d{10}", RegexOptions.IgnoreCase);

                if (!String.IsNullOrWhiteSpace(HomePhone))
                {
                    //get rid of all punctuation and trim and space
                    HomePhone = Regex.Replace(HomePhone, @"[^\w\s]", "").Trim();
                    HomePhone = Regex.Replace(HomePhone, "[^0-9]", "");

                    if (!pattern.IsMatch(HomePhone))
                    {
                        yield return(new ValidationResult("Home phone is incorrect pattern: ", new string[] { nameof(HomePhone) }));
                    }
                    else
                    {
                        //insert dashes into phone number
                        HomePhone = Regex.Replace(HomePhone, @"^(...)(...)(....)$", "$1-$2-$3");
                    }
                }

                if (!String.IsNullOrWhiteSpace(CellPhone))
                {
                    //get rid of all punctuation and trim and space
                    CellPhone = Regex.Replace(CellPhone, @"[^\w\s]", "").Trim();
                    CellPhone = Regex.Replace(CellPhone, "[^0-9]", "");

                    if (!pattern.IsMatch(CellPhone))
                    {
                        yield return(new ValidationResult("Cell phone is incorrect pattern: ", new string[] { nameof(CellPhone) }));
                    }
                    else
                    {
                        //insert dashes into phone number
                        CellPhone = Regex.Replace(CellPhone, @"^(...)(...)(....)$", "$1-$2-$3");
                    }
                }
            }

            //lastcontact date cantbe provided unless datejoined
            if (LastContactDate != null && DateJoined == null)
            {
                yield return(new ValidationResult("You must also provide Date Joined.", new string[] { nameof(DateJoined) }));
            }
            //last contact date cant be before date joined
            if (DateJoined > LastContactDate)
            {
                yield return(new ValidationResult("Last contact date can not be prior to date joined."));
            }

            //1diii replace the throw statement
            yield return(ValidationResult.Success);
            //throw new NotImplementedException();
        }
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            OECContext _context = OEC_Singleton.Context();

            FarmId = Convert.ToInt32(FarmId.ToString().Trim());
            if (Name != null)
            {
                Name = Name.Trim();
                Name = HKValidations.HKCapitalize(Name);
            }
            if (Address != null)
            {
                Address = Address.Trim();
                Address = HKValidations.HKCapitalize(Address);
            }
            if (Town != null)
            {
                Town = Town.Trim();
                Town = HKValidations.HKCapitalize(Town);
            }
            if (County != null)
            {
                County = County.Trim();
                County = HKValidations.HKCapitalize(County);
            }
            if (ProvinceCode != null)
            {
                ProvinceCode = ProvinceCode.Trim();
                ProvinceCode = ProvinceCode.ToUpper();
            }
            if (PostalCode != null)
            {
                PostalCode = PostalCode.Trim();
            }
            if (HomePhone != null)
            {
                HomePhone = HomePhone.Trim();
            }
            if (CellPhone != null)
            {
                CellPhone = CellPhone.Trim();
            }
            if (Email != null)
            {
                Email = Email.Trim();
            }
            if (Directions != null)
            {
                Directions = Directions.Trim();
            }

            if (String.IsNullOrWhiteSpace(Name) || String.IsNullOrWhiteSpace(ProvinceCode))
            {
                if (String.IsNullOrWhiteSpace(Name))
                {
                    yield return(new ValidationResult("Name is required", new[] { "Name" }));
                }
                if (String.IsNullOrWhiteSpace(ProvinceCode))
                {
                    yield return(new ValidationResult("Province Code is required", new[] { "ProvinceCode" }));
                }
            }

            if (String.IsNullOrWhiteSpace(Town) && String.IsNullOrWhiteSpace(County))
            {
                yield return(new ValidationResult("Either the town or county must be provided.", new[] { "Town", "County" }));
            }

            if (String.IsNullOrWhiteSpace(Email))
            {
                if (string.IsNullOrWhiteSpace(Address))
                {
                    yield return(new ValidationResult("Address must be provided.", new[] { "Address" }));
                }
                if (string.IsNullOrWhiteSpace(PostalCode))
                {
                    yield return(new ValidationResult("Postal Code must be provided.", new[] { "PostalCode" }));
                }
            }
            if (!String.IsNullOrEmpty(PostalCode) && !String.IsNullOrWhiteSpace(ProvinceCode))
            {
                string countryCode = "";
                if (ProvinceCode.Length == 2)
                {
                    countryCode = _context.Province.SingleOrDefault(p => p.ProvinceCode == ProvinceCode).CountryCode;
                }
                else
                {
                    countryCode = _context.Province.SingleOrDefault(p => p.Name == ProvinceCode).CountryCode;
                }

                if (countryCode == "CA")
                {
                    string pCode = PostalCode;

                    if (!HKValidations.HKPostalCodeValidation(ref pCode))
                    {
                        yield return(new ValidationResult("Please enter valid postal code.", new[] { "PostalCode" }));
                    }
                    PostalCode = pCode;
                }
                else if (countryCode == "US")
                {
                    string zCode = PostalCode;
                    if (!HKValidations.HKZipCodeValidation(ref zCode))
                    {
                        yield return(new ValidationResult("Please enter valid zip code.", new[] { "PostalCode" }));
                    }
                    PostalCode = zCode;
                }
            }

            if (String.IsNullOrWhiteSpace(HomePhone) && String.IsNullOrWhiteSpace(CellPhone))
            {
                yield return(new ValidationResult("Either the home phone number or cell phone number must be provided.", new[] { "HomePhone", "CellPhone" }));
            }
            else
            {
                if (!String.IsNullOrWhiteSpace(HomePhone))
                {
                    string hPhone = HomePhone;
                    if (!HKValidations.HKPhoneNumberValidation(ref hPhone))
                    {
                        yield return(new ValidationResult("Home phone number must be 10 digits only.", new[] { "HomePhone" }));
                    }
                    HomePhone = hPhone;
                }
                if (!String.IsNullOrWhiteSpace(CellPhone))
                {
                    string cPhone = CellPhone;
                    if (!HKValidations.HKPhoneNumberValidation(ref cPhone))
                    {
                        yield return(new ValidationResult("Cell phone number must be 10 digits only.", new[] { "CellPhone" }));
                    }
                    HomePhone = cPhone;
                }
            }

            yield return(ValidationResult.Success);
        }