예제 #1
0
        /// <summary>Validations done to a country before it is saved.</summary>
        /// <param name="validateEntity">The country to validate.</param>
        /// <exception cref="GTSport_DT.Countries.CountryDescriptionNotSetException">
        /// If the description is not filled.
        /// </exception>
        /// <exception cref="GTSport_DT.Countries.CountryRegionKeyNotSetException">
        /// If the region key is not filled.
        /// </exception>
        /// <exception cref="GTSport_DT.Countries.CountryDescriptionAlreadyExistsException">
        /// If the description already exists in another country record.
        /// </exception>
        /// <exception cref="RegionNotFoundException">
        /// If the region key is not found in the region table.
        /// </exception>
        public override void ValidateSave(Country validateEntity)
        {
            if (String.IsNullOrWhiteSpace(validateEntity.Description))
            {
                throw new CountryDescriptionNotSetException(CountryDescriptionNotSetException.CountryDescriptionNotSetMsg);
            }

            if (String.IsNullOrWhiteSpace(validateEntity.RegionKey))
            {
                throw new CountryRegionKeyNotSetException(CountryRegionKeyNotSetException.CountryRegionKeyNotSetMsg);
            }

            Country country = countriesRespository.GetByDescription(validateEntity.Description);

            if (country != null)
            {
                if (String.IsNullOrWhiteSpace(validateEntity.PrimaryKey) || validateEntity.PrimaryKey != country.PrimaryKey)
                {
                    throw new CountryDescriptionAlreadyExistsException(CountryDescriptionAlreadyExistsException.CountryDescriptionAlreadyExistsMsg, validateEntity.Description);
                }
            }

            Region region = regionsRepository.GetById(validateEntity.RegionKey);

            if (region == null)
            {
                throw new RegionNotFoundException(RegionNotFoundException.RegionKeyNotFoundMsg, validateEntity.RegionKey);
            }
        }