private string GetCpvValuePropertyName()
 {
     if (string.IsNullOrEmpty(_cpvValuePropertyName))
     {
         _cpvValuePropertyName = CpvHelper.GetPropertyName(typeof(T), CustomParamValue.CPVValuePropertyName);
     }
     return(_cpvValuePropertyName);
 }
 protected override void SourceObjectPropertyChanged(object sender, PropertyChangedEventArgs e)
 {
     base.SourceObjectPropertyChanged(sender, e);
     if (Source != null && e.PropertyName == GetCpvValuePropertyName())
     {
         if (string.IsNullOrEmpty(Source.CPVValue) && Source.Cp != null &&
             !string.IsNullOrEmpty(Source.Cp.CustomParamDefault))
         {
             Source.CPVValue = Source.Cp.CustomParamDefault;
             return;
         }
         Source.FormattedValue = CpvHelper.GetFormattedValue(Source);
     }
     OnNeedRefresh();
 }
        private CustomParamValue CreateItem(CustomParam cp, decimal?parentCpvId)
        {
            var cpv = (CustomParamValue)Activator.CreateInstance(typeof(T));

            cpv.CPVID              = --_cpvid;
            cpv.CustomParamCode    = cp.GetKey <string>();
            cpv.CPV2Entity         = CpEntity;
            cpv.CPVKey             = CpKey;
            cpv.CPVValue           = cp.CustomParamDefault;
            cpv.VCustomParamParent = cp.CustomParamParent;
            cpv.Cp                = cp;
            cpv.FormattedValue    = CpvHelper.GetFormattedValue(cpv);
            cpv.CPVParent         = parentCpvId;
            cpv.VCustomParamCount = cp.CustomParamCount;
            cpv.VCustomParamDesc  = cp.CustomParamDesc;
            return(cpv);
        }
        protected override ObservableCollection <DataField> GetFields(SettingDisplay displaySetting)
        {
            var       result        = base.GetFields(displaySetting);
            var       field         = result.SingleOrDefault(p => p.FieldName == GetCpvValuePropertyName());
            var       countField    = result.FirstOrDefault(p => p.FieldName == CpvHelper.GetPropertyName(typeof(T), CustomParamValue.VCUSTOMPARAMCOUNTPropertyName));
            var       descField     = result.FirstOrDefault(p => p.FieldName == CpvHelper.GetPropertyName(typeof(T), CustomParamValue.VCUSTOMPARAMDESCPropertyName));
            DataField mustSetField  = null;
            DataField mustHaveField = null;

            result.Clear();

            if (field == null)
            {
                throw new DeveloperException("Can't find item with FieldName '{0}' in Fields.", GetCpvValuePropertyName());
            }

            field.IsChangeLookupCode = true;
            field.IsEnabled          = false;

            if (Source != null && Source.Cp != null)
            {
                field.LookupCode    = Source.Cp.ObjectlookupCode_R;
                field.DisplayFormat = Source.Cp.CustomParamFormat;
                var isnotlookup = string.IsNullOrEmpty(field.LookupCode);

                field.FieldType = CpvHelper.GetValueType(Source);
                field.IsEnabled = !(Source.Cp.CustomparamInputdisable || Source.Cp.IsReadOnly || field.FieldType == null);

                //Пост-обработка
                if (field.FieldType != null)
                {
                    var type = field.FieldType.GetNonNullableType();
                    if (type == typeof(DateTime))
                    {
                        field.Set(ValueDataFieldConstants.BindingIValueConverter, new StringToDateTimeConverter());
                        field.Set(ValueDataFieldConstants.Parameter, CpvHelper.GetDateTimeFormat());
                    }
                    else if (type == typeof(bool))
                    {
                        field.Set(ValueDataFieldConstants.BindingIValueConverter, new StringToDbBool());
                    }
                    else if (type.IsPrimitive || type == typeof(decimal))
                    {
                        field.Set(ValueDataFieldConstants.BindingIValueConverter, new StringToNumericConverter());
                        field.Set(ValueDataFieldConstants.Parameter, field.FieldType);
                        if (isnotlookup)
                        {
                            field.Set(ValueDataFieldConstants.UseSpinEdit, true);
                        }
                    }

                    //if (isnotlookup && string.IsNullOrEmpty(field.DisplayFormat) &&
                    //    ((type == typeof(float) || type == typeof(double) || type == typeof(decimal))))
                    //    field.DisplayFormat = "N4";
                }

                mustSetField = new DataField()
                {
                    Name        = CustomParamValue.IsMustSetPropertyName,
                    FieldName   = CustomParamValue.IsMustSetPropertyName,
                    SourceName  = CustomParamValue.IsMustSetPropertyName,
                    IsEnabled   = false,
                    Visible     = true,
                    Caption     = MustSetCaption,
                    Description = MustSetDescription,
                    FieldType   = typeof(bool)
                };

                mustHaveField = new DataField()
                {
                    Name        = CustomParamValue.IsMustHavePropertyName,
                    FieldName   = CustomParamValue.IsMustHavePropertyName,
                    SourceName  = CustomParamValue.IsMustHavePropertyName,
                    IsEnabled   = false,
                    Visible     = true,
                    Caption     = MustHaveCaption,
                    Description = MustHaveDescription,
                    FieldType   = typeof(bool)
                };
            }
            result.Add(field);
            if (countField != null)
            {
                countField.IsEnabled = false;
                result.Add(countField);
            }
            if (descField != null)
            {
                descField.IsEnabled = false;
                result.Add(descField);
            }
            if (mustSetField != null)
            {
                result.Add(mustSetField);
            }
            if (mustHaveField != null)
            {
                result.Add(mustHaveField);
            }
            return(result);
        }
Exemple #5
0
        private void OnDelete()
        {
            if (!ConnectionManager.Instance.AllowRequest())
            {
                return;
            }

            if (!CanDelete())
            {
                return;
            }

            try
            {
                if (GetViewService().ShowDialog(StringResources.Confirmation
                                                , StringResources.ConfirmationDeleteObject
                                                , MessageBoxButton.YesNo
                                                , MessageBoxImage.Question
                                                , MessageBoxResult.Yes) != MessageBoxResult.Yes)
                {
                    return;
                }

                var selectedItem = (CustomParamValue)CurrentItem;
                var deletes      = CpvHelper.GetChildsCpvByParentCpv <CustomParamValue>(Source.Cast <CustomParamValue>(), selectedItem, true);

                if (ShouldUpdateSeparately)
                {
                    var uowFactory = IoC.Instance.Resolve <IUnitOfWorkFactory>();
                    using (var uow = uowFactory.Create(false))
                    {
                        try
                        {
                            uow.BeginChanges();
                            var mng = GetManager(_itemType);
                            mng.SetUnitOfWork(uow);

                            foreach (var p in deletes)
                            {
                                mng.Delete(p);
                            }
                            mng.Delete(CurrentItem);

                            uow.CommitChanges();
                        }
                        catch
                        {
                            uow.RollbackChanges();
                            throw;
                        }
                    }

                    if (ParentViewModel != null)
                    {
                        ParentViewModel.RefreshData();
                    }
                }
                else
                {
                    foreach (var p in deletes)
                    {
                        Source.Remove(p);
                    }
                    Source.Remove(CurrentItem);
                }
            }
            catch (Exception ex)
            {
                throw new OperationException(ExceptionResources.ItemCantDelete, ex);
            }
        }
        private bool OnSaveInternal(bool canSave)
        {
            if (!canSave)
            {
                return(true);
            }

            if (!ConnectionManager.Instance.AllowRequest())
            {
                return(false);
            }

            try
            {
                _isUpdating = true;
                WaitStart();

                var changes = GetChanges(Source, true);

                var relatedChanges = CollectRelatedParams(Source, changes);

                if (!Validation(relatedChanges))
                {
                    return(false);
                }

                var selectedItem = SelectedItems[0];

                if (ShouldUpdateSeparately)
                {
                    var uowFactory = IoC.Instance.Resolve <IUnitOfWorkFactory>();
                    using (var uow = uowFactory.Create(false))
                    {
                        try
                        {
                            uow.BeginChanges();
                            var mng = GetManager();
                            mng.SetUnitOfWork(uow);

                            var sources = Source.OrderByDescending(cpv => cpv.CPVParent, new SpecialComparer());
                            foreach (var cpv in sources)
                            {
                                var key = cpv.GetKey <decimal>();
                                if (key < 0)
                                {
                                    var item = cpv;

                                    if (!string.IsNullOrEmpty(cpv.CPVValue) || //Если были проставлены значения по умолчанию - сохраняем
                                        cpv.Cp.CUSTOMPARAMSAVEMODE || HasChanges(GetChanges(CpvHelper.GetChildsCpvByParentCpv <T>(sources, cpv, false), true)))
                                    {
                                        mng.Insert(ref item);

                                        item.Cp             = cpv.Cp;
                                        item.FormattedValue = CpvHelper.GetFormattedValue(item);
                                        SourceUpdate(item, cpv);
                                        var childs = sources.Where(i => i.CPVParent == key);
                                        foreach (var child in childs)
                                        {
                                            child.CPVParent = item.CPVID;
                                        }
                                    }
                                    else
                                    {
                                        cpv.AcceptChanges();
                                    }
                                }
                                else
                                {
                                    //Удаляем параметры, у которых не установлен CUSTOMPARAMSAVEMODE и CPVValue is null и нет деток
                                    if (string.IsNullOrEmpty(cpv.CPVValue) && !cpv.Cp.CUSTOMPARAMSAVEMODE &&
                                        CpvHelper.GetChildsCpvByParentCpv <T>(sources, cpv, false).Length == 0)
                                    {
                                        mng.Delete(cpv);
                                        cpv.AcceptChanges();
                                    }
                                    else if (cpv.IsDirty)
                                    {
                                        mng.Update(cpv);
                                    }
                                }
                            }

                            uow.CommitChanges();
                        }
                        catch
                        {
                            uow.RollbackChanges();
                            throw;
                        }
                    }
                }

                _hasAddCpv = false;
                if (selectedItem != null)
                {
                    SelectedItem = selectedItem;
                }
                OnSourceUpdated();
            }
            catch (Exception ex)
            {
                if (!ExceptionHandler(ex, ExceptionResources.ItemCantSave))
                {
                    throw;
                }
                return(false);
            }
            finally
            {
                _isUpdating = false;
                WaitStop();
            }

            return(true);
        }
        private List <T> GetEditModelSource(out double lastQueryExecutionTime)
        {
            lastQueryExecutionTime = 0;
            if (_cps != null)
            {
                _cps.Clear();
            }
            _cps = null;

            if (string.IsNullOrEmpty(CpEntity))
            {
                throw new DeveloperException("Property CpEntity is undefined.");
            }
            if (string.IsNullOrEmpty(CpKey))
            {
                throw new DeveloperException("Property CpKey is undefined.");
            }

            //Получаем список cpv
            var mto       = IoC.Instance.Resolve <IManagerForObject>();
            var modeltype = typeof(T);
            var mgrType   = mto.GetManagerByTypeName(modeltype.Name);

            if (mgrType == null)
            {
                throw new DeveloperException(string.Format("Unknown source type '{0}'.", modeltype.Name));
            }

            List <CustomParamValue> items;

            using (var mgr = IoC.Instance.Resolve(mgrType, null) as IBaseManager)
            {
                if (mgr == null)
                {
                    throw new DeveloperException(string.Format("Can't resolve IBaseManager by '{0}'.", mgrType.Name));
                }
                var cpvtype = typeof(CustomParamValue);
                items = mgr.GetFiltered(string.Format("{0} = '{1}' AND {2} = '{3}'",
                                                      SourceNameHelper.Instance.GetPropertySourceName(cpvtype, CustomParamValue.CPV2EntityPropertyName),
                                                      CpEntity,
                                                      SourceNameHelper.Instance.GetPropertySourceName(cpvtype, CustomParamValue.CPVKeyPropertyName),
                                                      CpKey), GetModeEnum.Partial)
                        .OfType <CustomParamValue>().ToList();
                lastQueryExecutionTime += mgr.LastQueryExecutionTime;
            }

            using (var mgr = IoC.Instance.Resolve <IBaseManager <CustomParam> >())
            {
                //Получаем список параметров для данного item.CustomParamCode с учетом манданта
                _cps = ((ICustomParamManager)mgr).GetCPByInstance(CpEntity, CpKey, null, CpSource, CpTarget).ToList();
                lastQueryExecutionTime += mgr.LastQueryExecutionTime;
            }

            //проверяем наличие родительских параметров в CP
            var badcps = ValidateParentCustomParam(_cps);

            if (badcps.Length > 0)
            {
                CustomParam[] cpall;
                using (var mgr = IoC.Instance.Resolve <IBaseManager <CustomParam> >())
                {
                    //Получаем список параметров для данного item.CustomParamCode без учета манданта
                    cpall =
                        mgr.GetFiltered(string.Format("{0} = '{1}'",
                                                      SourceNameHelper.Instance.GetPropertySourceName(typeof(CustomParam), CustomParam.CustomParam2EntityPropertyName), CpEntity), GetModeEnum.Partial)
                        .ToArray();
                    lastQueryExecutionTime += mgr.LastQueryExecutionTime;
                }
                foreach (var bcp in badcps)
                {
                    var parent = GetParentCustomParam(cpall, bcp);
                    if (parent == null)
                    {
                        throw new DeveloperException("Can't find parent for '{0}'.", bcp);
                    }

                    if (_cps.Any(p => p.GetKey <string>() == parent.GetKey <string>()))
                    {
                        continue;
                    }

                    parent.IsReadOnly = true;
                    _cps.Add(parent);
                }
            }

            if (_cps == null || !_cps.Any())
            {
                _errorMessage = string.IsNullOrEmpty(MandantCode)
                    ? "Проверьте привязку параметров к манданту."
                    : string.Format("Проверьте привязку параметров к манданту '{0}'.", MandantCode);
            }

            foreach (var item in items)
            {
                item.Cp             = _cps.FirstOrDefault(i => i.GetKey <string>() == item.CustomParamCode);
                item.FormattedValue = CpvHelper.GetFormattedValue(item);
            }

            // получим корневые элементы cp - они обязаны быть
            var rootElements = _cps.Where(i => string.IsNullOrEmpty(i.CustomParamParent)).ToArray();

            if (rootElements.Length == 0 && string.IsNullOrEmpty(_errorMessage))
            {
                throw new DeveloperException("Отсутствуют корневые элементы параметров!");
            }

            // добавим корневые cpv, которые отсутствуют
            foreach (var root in rootElements)
            {
                var exist = items.FirstOrDefault(i => i.CustomParamCode.EqIgnoreCase(root.GetKey <string>()));
                if (exist == null)
                {
                    items.Add(CreateItem(root, null));
                }
            }

            var rootCpvs = items.Where(i => i.CPVParent == null).ToArray();

            // проходим по всем корневым cpv и добираем отсутствущие элементы
            foreach (var cpv in rootCpvs)
            {
                CollectItems(cpv, _cps.ToArray(), items);
            }

            //cpv, которые не должны были быть показаны, но показываем
            var badcpvs = items.Where(cpv => cpv.Cp == null).ToArray();

            if (badcpvs.Any())
            {
                using (var mgr = IoC.Instance.Resolve <IBaseManager <CustomParam> >())
                {
                    foreach (var cpv in badcpvs)
                    {
                        var cp = mgr.Get(cpv.CustomParamCode);
                        lastQueryExecutionTime += mgr.LastQueryExecutionTime;
                        if (cp == null)
                        {
                            items.Remove(cpv);
                        }
                        else
                        {
                            cp.IsReadOnly = true;
                            cpv.Cp        = cp;
                            if (string.IsNullOrEmpty(cpv.CPVValue))
                            {
                                cpv.CPVValue = cp.CustomParamDefault;
                            }
                            cpv.FormattedValue = CpvHelper.GetFormattedValue(cpv);
                        }
                    }
                }
            }

            var result = new List <T>();

            items.ForEach(p =>
            {
                p.AcceptChanges();
                result.Add((T)p);
            });
            return(result);
        }
        protected override void Delete()
        {
            if (!CanDelete())
            {
                return;
            }

            try
            {
                WaitStart();

                if (!ConnectionManager.Instance.AllowRequest())
                {
                    return;
                }

                var selectedItem = SelectedItems[0];
                var deletes      = CpvHelper.GetChildsCpvByParentCpv <T>(Source, selectedItem, true);

                var message       = StringResources.ConfirmationDeleteCpv;
                var messageFormat = StringResources.CpvChanges;
                if (deletes.Length > 1)
                {
                    deletes       = deletes.OrderByDescending(p => p.CPVParent).ToArray();
                    message       = StringResources.ConfirmationDeleteCpvs;
                    messageFormat = StringResources.CpvsChange;
                }
                if (HasChanges(GetChanges(deletes)))
                {
                    message = string.Format(messageFormat, message);
                }
                if (!DeleteConfirmation(message))
                {
                    return;
                }

                if (ShouldUpdateSeparately)
                {
                    var dbdeletes = deletes.Where(p => p.CPVID >= 0).ToArray();
                    if (dbdeletes.Any())
                    {
                        using (var mng = GetManager())
                        {
                            mng.Delete(dbdeletes);
                        }
                    }
                }

                foreach (var d in deletes)
                {
                    Source.Remove(d);
                }

                OnSourceUpdated();
                if (HasSelectedItems())
                {
                    SelectedItem = SelectedItems[0];
                }
            }
            catch (Exception ex)
            {
                if (!ExceptionHandler(ex, ExceptionResources.ItemsCantDelete))
                {
                    throw;
                }
            }
            finally
            {
                WaitStop();
                OnNeedChangeFocusRow();
            }
        }