示例#1
0
        public static bool IsValidateRequired <T>(PXGraph aGraph, T aAddress)
            where T : IAddressBase
        {
            if (aAddress?.CountryID != null)
            {
                CS.Country country = CS.Country.PK.Find(aGraph, aAddress.CountryID);
                return(country.AddressValidatorPluginID != null);
            }

            return(false);
        }
        public static bool Validate <T>(PXGraph aGraph, T aAddress, bool aSynchronous, bool updateToValidAddress)
            where T : IAddressBase, IValidatedAddress
        {
            CS.Country country = PXSelect <CS.Country, Where <CS.Country.countryID, Equal <Required <CS.Country.countryID> > > > .Select(aGraph, aAddress.CountryID);

            if (string.IsNullOrEmpty(country.AddressVerificationTypeName))
            {
                if (aSynchronous)
                {
                    PXCache cache            = aGraph.Caches[typeof(T)];
                    string  countryFieldName = GetFieldName(typeof(T), Fields.CountryID);
                    cache.RaiseExceptionHandling(countryFieldName, aAddress, aAddress.CountryID,
                                                 new PXSetPropertyException(Messages.AddressVerificationServiceIsNotSetup,
                                                                            PXErrorLevel.Warning, aAddress.CountryID));
                    return(false);
                }
                else
                {
                    throw new PXException(Messages.AddressVerificationServiceIsNotSetup, aAddress.CountryID);
                }
            }

            IAddressValidationService service = Create(country);

            if (service != null)
            {
                PXCache cache = aGraph.Caches[typeof(T)];
                if (!service.IsServiceActive(aGraph))
                {
                    if (aSynchronous)
                    {
                        string countryFieldName = GetFieldName(typeof(T), Fields.CountryID);
                        cache.RaiseExceptionHandling(countryFieldName, aAddress, aAddress.CountryID,
                                                     new PXSetPropertyException(Messages.AddressVerificationServiceIsNotActive,
                                                                                PXErrorLevel.Error, aAddress.CountryID));
                        return(false);
                    }
                    else
                    {
                        throw new PXException(Messages.AddressVerificationServiceIsNotActive, aAddress.CountryID);
                    }
                }
                service.SetupService(aGraph);
                bool isValid;
                Dictionary <Fields, string> messages = new Dictionary <Fields, string>();

                T copy = (T)cache.CreateCopy(aAddress);
                if (!service.ValidateAddress(copy, out isValid, messages))
                {
                    throw new PXException(Messages.UnknownErrorOnAddressValidation);
                }
                if (isValid)
                {
                    T copyToUpdate = copy;
                    if (!updateToValidAddress)
                    {
                        copyToUpdate = (T)cache.CreateCopy(aAddress);                        //Clear changes made by ValidateAddress
                    }
                    copyToUpdate.IsValidated = true;
                    aAddress = (T)cache.Update(copyToUpdate);
                    if (!updateToValidAddress && aSynchronous)
                    {
                        string[] fields = { GetFieldName(typeof(T), Fields.AddressLine1),
                                            GetFieldName(typeof(T), Fields.AddressLine2),
                                            GetFieldName(typeof(T), Fields.City),
                                            GetFieldName(typeof(T), Fields.State),
                                            GetFieldName(typeof(T), Fields.PostalCode) };
                        foreach (string iFld in fields)
                        {
                            string ourValue = ((string)cache.GetValue(aAddress, iFld)) ?? String.Empty;
                            string extValue = ((string)cache.GetValue(copy, iFld)) ?? String.Empty;

                            if (String.Compare(ourValue.Trim(), extValue.Trim(), true) != 0)
                            {
                                cache.RaiseExceptionHandling(iFld, aAddress, ourValue,
                                                             new PXSetPropertyException(Messages.AddressVerificationServiceReturnsField, PXErrorLevel.Warning, extValue));
                            }
                        }
                    }
                }
                else
                {
                    string        message        = string.Empty;
                    StringBuilder messageBuilder = new StringBuilder();
                    int           count          = 0;
                    foreach (KeyValuePair <Fields, string> iMsg in messages)
                    {
                        string fieldName = GetFieldName(typeof(T), iMsg.Key);
                        if (!aSynchronous)
                        {
                            if (count > 0)
                            {
                                messageBuilder.Append(",");
                            }
                            messageBuilder.AppendFormat("{0}:{1}", fieldName, iMsg.Value);
                            count++;
                        }
                        else
                        {
                            object value = cache.GetValue(aAddress, fieldName);
                            cache.RaiseExceptionHandling(fieldName, aAddress, value, new PXSetPropertyException(iMsg.Value));
                        }
                    }
                    if (!aSynchronous)
                    {
                        throw new PXException(messageBuilder.ToString());
                    }
                    return(false);
                }
            }
            return(true);
        }
示例#3
0
        public static bool Validate <T>(PXGraph aGraph, T aAddress, bool aSynchronous, bool updateToValidAddress)
            where T : IAddressBase, IValidatedAddress
        {
            if (!AddressValidatorPluginMaint.IsActive(aGraph, aAddress.CountryID))
            {
                if (aSynchronous)
                {
                    PXCache cache            = aGraph.Caches[typeof(T)];
                    string  countryFieldName = nameof(IAddressBase.CountryID);
                    cache.RaiseExceptionHandling(countryFieldName, aAddress, aAddress.CountryID,
                                                 new PXSetPropertyException(Messages.AddressVerificationServiceIsNotSetup,
                                                                            PXErrorLevel.Warning, aAddress.CountryID));
                    return(false);
                }
                else
                {
                    throw new PXException(Messages.AddressVerificationServiceIsNotSetup, aAddress.CountryID);
                }
            }

            CS.Country country = PXSelect <CS.Country, Where <CS.Country.countryID, Equal <Required <CS.Country.countryID> > > > .Select(aGraph, aAddress.CountryID);

            IAddressValidator service = AddressValidatorPluginMaint.CreateAddressValidator(aGraph, country.AddressValidatorPluginID);

            if (service != null)
            {
                PXCache cache = aGraph.Caches[typeof(T)];
                bool    isValid;
                Dictionary <string, string> messages = new Dictionary <string, string>();

                T copy = (T)cache.CreateCopy(aAddress);

                try
                {
                    isValid = service.ValidateAddress(copy, messages);
                }
                catch
                {
                    throw new PXException(Messages.UnknownErrorOnAddressValidation);
                }

                if (isValid)
                {
                    T copyToUpdate = copy;
                    if (!updateToValidAddress)
                    {
                        copyToUpdate = (T)cache.CreateCopy(aAddress);                        //Clear changes made by ValidateAddress
                    }
                    copyToUpdate.IsValidated = true;
                    aAddress = (T)cache.Update(copyToUpdate);
                    if (!updateToValidAddress && aSynchronous)
                    {
                        string[] fields =
                        {
                            nameof(IAddressBase.AddressLine1),
                            nameof(IAddressBase.AddressLine2),
                            //nameof(IAddressBase.AddressLine3),
                            nameof(IAddressBase.City),
                            nameof(IAddressBase.State),
                            nameof(IAddressBase.PostalCode)
                        };

                        foreach (string iFld in fields)
                        {
                            string ourValue = ((string)cache.GetValue(aAddress, iFld)) ?? String.Empty;
                            string extValue = ((string)cache.GetValue(copy, iFld)) ?? String.Empty;

                            if (String.Compare(ourValue.Trim(), extValue.Trim(), StringComparison.OrdinalIgnoreCase) != 0)
                            {
                                cache.RaiseExceptionHandling(iFld, aAddress, ourValue,
                                                             new PXSetPropertyException(Messages.AddressVerificationServiceReturnsField, PXErrorLevel.Warning, extValue));
                            }
                        }
                    }
                }
                else
                {
                    string        message        = string.Empty;
                    StringBuilder messageBuilder = new StringBuilder();
                    int           count          = 0;
                    foreach (var iMsg in messages)
                    {
                        if (!aSynchronous)
                        {
                            if (count > 0)
                            {
                                messageBuilder.Append(",");
                            }
                            messageBuilder.AppendFormat("{0}:{1}", iMsg.Key, iMsg.Value);
                            count++;
                        }
                        else
                        {
                            object value = cache.GetValue(aAddress, iMsg.Key);
                            cache.RaiseExceptionHandling(iMsg.Key, aAddress, value, new PXSetPropertyException(iMsg.Value));
                        }
                    }
                    if (!aSynchronous)
                    {
                        throw new PXException(messageBuilder.ToString());
                    }
                    return(false);
                }
            }
            return(true);
        }