Exemple #1
0
        public ErrorWrapper[] PartnerLoad(PartnerWrapper item)
        {
            Contract.Requires(item != null);

            var retMessage = new List <ErrorWrapper>();

            Log.InfoFormat("Start of PartnerLoad");
            Log.Debug(item.DumpToXML());
            var startAllTime = DateTime.Now;

            try
            {
                IUnitOfWork uow = null;
                try
                {
                    uow = UnitOfWorkHelper.GetUnit();

                    item.MANDANTID = CheckMandant(item.MANDANTID, item.MandantCode, uow);
                    if (item.MANDANTID == null)
                    {
                        throw new NullReferenceException("MandantCode");
                    }
                    Log.DebugFormat("Мандант = {0}", item.MandantCode);

                    var isPermit = item.PARTNERLOCKED;
                    if (isPermit)
                    {
                        item.PARTNERLOCKED = false;
                    }

                    var lockobj = LockPartnerLoadPerMandant.GetOrAdd(item.MANDANTID.Value, new object());

                    lock (lockobj)
                    {
                        Log.InfoFormat("Process PartnerLoad locked = {0:hh:mm:ss.fff}", DateTime.Now);

                        uow.BeginChanges();

                        //var partner = new Partner();
                        var partnerType   = typeof(Partner);
                        var partnername   = FilterHelper.ValidateFilterString(item.PARTNERNAME);
                        var partnerFilter = string.Format("{0} = {1} AND (upper({2}) = '{3}' or upper({4}) = '{5}')",
                                                          SourceNameHelper.Instance.GetPropertySourceName(partnerType, Partner.MANDANTIDPropertyName),
                                                          item.MANDANTID,
                                                          SourceNameHelper.Instance.GetPropertySourceName(partnerType, Partner.PARTNERNAMEPropertyName),
                                                          string.IsNullOrEmpty(partnername) ? null : partnername.ToUpper(),
                                                          SourceNameHelper.Instance.GetPropertySourceName(partnerType, Partner.PARTNERHOSTREFPropertyName),
                                                          string.IsNullOrEmpty(item.PARTNERHOSTREF) ? null : item.PARTNERHOSTREF.ToUpper());

                        //Получим не пустые адреса, исключая ADR_CLIENTRECIPIENT
                        var addresstypes = AddressHelper.GetAddressTypes(AddressBookType.ADR_CLIENTRECIPIENT);
                        var wrapperaddrs = AddressHelper.GetNotEmptyAddressBookByTypes(item.ADDRESS, addresstypes);

                        //Преобразуем в wms-адреса
                        var wmsaddresscol = new List <AddressBook>();
                        if (wrapperaddrs != null && wrapperaddrs.Length > 0)
                        {
                            foreach (var ad in wrapperaddrs)
                            {
                                var wmsadr = new AddressBook();
                                MapTo(ad, wmsadr);
                                if (!AddressHelper.IsAddressEmpty(wmsadr))
                                {
                                    wmsaddresscol.Add(wmsadr);
                                }
                            }
                        }

                        using (var mgr = IoC.Instance.Resolve <IBaseManager <Partner> >())
                        {
                            mgr.SetUnitOfWork(uow);

                            var     partners = mgr.GetFiltered(partnerFilter).ToArray();
                            decimal?partnerId;
                            if (partners.Length == 0)
                            {
                                partnerId = PartnerLoadInternal(item, wmsaddresscol, retMessage, mgr, uow);
                            }
                            else
                            {
                                partnerId = PartnerUpdateInternal(item, partners, wmsaddresscol, isPermit, retMessage, mgr, uow);
                            }

                            if (partnerId.HasValue)
                            {
                                if (!ProcessPartnerGroup(item, partnerId.Value, uow))
                                {
                                    Log.DebugFormat("Для партнера «{0}» не требуется привязка к группе",
                                                    item.PARTNERNAME);
                                }
                            }
                        }
                        uow.CommitChanges();
                        Log.InfoFormat("Process PartnerLoad unlocked = {0:hh:mm:ss.fff}", DateTime.Now);
                    }
                }
                catch (IntegrationLogicalException iex)
                {
                    if (uow != null)
                    {
                        uow.RollbackChanges();
                    }

                    var message = ExceptionHelper.ExceptionToString(iex);
                    Log.Error(message, iex);

                    var ew = new ErrorWrapper {
                        ERRORCODE = MessageHelper.ErrorCode.ToString(), ERRORMESSAGE = message
                    };
                    retMessage.Add(ew);
                    throw;
                }
                catch (Exception ex)
                {
                    if (uow != null)
                    {
                        uow.RollbackChanges();
                    }

                    var message = ExceptionHelper.ExceptionToString(ex);
                    Log.Error(message, ex);

                    var ew = new ErrorWrapper {
                        ERRORCODE = MessageHelper.ErrorCode.ToString(), ERRORMESSAGE = "Системная ошибка: " + message
                    };
                    retMessage.Add(ew);
                    throw;
                }
                finally
                {
                    if (uow != null)
                    {
                        uow.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                var message = ExceptionHelper.ExceptionToString(ex);
                Log.Error(message, ex);
                throw new FaultException <string>(message, new FaultReason(message));
            }
            finally
            {
                Log.DebugFormat("PartnerLoad - общее время загрузки {0}", (DateTime.Now - startAllTime));
                Log.InfoFormat("End of PartnerLoad");
            }
            return(retMessage.ToArray());
        }