Beispiel #1
0
        public PropertyValueBaseViewModel(IViewModelsFactory <IPickAssetViewModel> vmFactory, PropertyAndPropertyValueBase item, string locale)
        {
            InnerItem  = item;
            _vmFactory = vmFactory;
            _locale    = locale;

            if (InnerItem.IsEnum)
            {
                if (InnerItem.IsMultiValue)
                {
                    foreach (var value in InnerItem.Values)
                    {
                        var propertyValue = InnerItem.Property.PropertyValues.FirstOrDefault(x => x.PropertyValueId == value.KeyValue);
                        if (propertyValue != null)
                        {
                            value.BooleanValue   = propertyValue.BooleanValue;
                            value.DateTimeValue  = propertyValue.DateTimeValue;
                            value.DecimalValue   = propertyValue.DecimalValue;
                            value.IntegerValue   = propertyValue.IntegerValue;
                            value.LongTextValue  = propertyValue.LongTextValue;
                            value.ShortTextValue = propertyValue.ShortTextValue;
                            value.KeyValue       = propertyValue.PropertyValueId;
                        }
                    }
                }

                var defaultView = CollectionViewSource.GetDefaultView(InnerItem.Property.PropertyValues);
                defaultView.Filter = FilterPropertyValues;
            }

            SetVisibility();
            AssetPickCommand     = new DelegateCommand(RaiseAssetPickInteractionRequest);
            AssetRemoveCommand   = new DelegateCommand(RaiseAssetRemoveInteractionRequest);
            CommonConfirmRequest = new InteractionRequest <Confirmation>();
        }
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var    parameterString = parameter as string;
            object retVal          = null;

            if (parameterString == "PropertyValueType")
            {
                retVal = ((PropertyValueType)value).ToString();
            }
            else if (parameterString == "Int2PropertyValueType")
            {
                retVal = (PropertyValueType)value;
            }
            else
            {
                PropertyAndPropertyValueBase input = null;
                var item = value as PropertyValueBase;
                if (item == null)
                {
                    input = value as PropertyAndPropertyValueBase;
                    if (input != null)
                    {
                        item = input.Value;
                    }
                }

                // decoding dictionary value
                if (input != null && input.Property != null)
                {
                    var parameterProperty = input.Property;
                    if (!parameterProperty.IsEnum)
                    {
                        item = input.Value;
                    }
                    else if (parameterProperty.IsMultiValue && input.Values != null && input.Values.Any())
                    {
                        retVal = string.Join(", ", input.Values.ToList().Select(val => parameterProperty.PropertyValues.FirstOrDefault(x => x.PropertyValueId == val.KeyValue).ToString()));
                    }
                    else if (input.Value != null)
                    {
                        item = parameterProperty.PropertyValues.FirstOrDefault(x => x.PropertyValueId == input.Value.KeyValue);
                    }
                }

                if (item != null &&
                    (input == null || input.Property == null || !input.Property.IsMultiValue))
                {
                    retVal = item.ToString();
                }

                if (retVal == null)
                {
                    retVal = "N/A".Localize(null, LocalizationScope.DefaultCategory);
                }
            }

            return(retVal);
        }
        // function almost duplicated in ItemViewModel
        private void OnItemValueConfirmed(PropertyAndPropertyValueBase originalItem, PropertyAndPropertyValueBase item)
        {
            if (!item.IsMultiValue)
            {
                if (originalItem.Property != null)
                {
                    item.Value.Name   = originalItem.Property.Name;
                    item.Value.Locale = originalItem.Locale;
                }

                if (originalItem.Value == null)
                {
                    ((CategoryPropertyValue)item.Value).CategoryId = InnerItem.CategoryId;
                    InnerItem.CategoryPropertyValues.Add((CategoryPropertyValue)item.Value);
                }
                else
                {
                    UpdatePropertyValue(item.Value);
                }
            }
            else
            {
                if (originalItem.Values == null)
                {
                    originalItem.Values = new ObservableCollection <PropertyValueBase>();
                }

                var listToRemove = originalItem.Values.Where(val => item.Values.All(y => y.PropertyValueId != val.PropertyValueId)).ToList();
                listToRemove.ForEach(x =>
                {
                    var itemToRemove = InnerItem.CategoryPropertyValues.First(y => y.KeyValue == x.KeyValue);
                    InnerItem.CategoryPropertyValues.Remove(itemToRemove);
                    originalItem.Values.Remove(x);
                });

                foreach (var value in item.Values)
                {
                    if (originalItem.Values.All(y => y.PropertyValueId != value.PropertyValueId))
                    {
                        InnerItem.CategoryPropertyValues.Add(new CategoryPropertyValue
                        {
                            CategoryId = InnerItem.CategoryId,
                            Name       = item.PropertyName,
                            Locale     = value.Locale,
                            KeyValue   = value.PropertyValueId,
                            ValueType  = item.PropertyValueType
                        });
                        originalItem.Values.Add(new PropertyValue
                        {
                            Name      = item.PropertyName,
                            Locale    = value.Locale,
                            KeyValue  = value.PropertyValueId,
                            ValueType = item.PropertyValueType
                        });
                    }
                }
            }

            // for GUI update
            originalItem.Value = originalItem.Value ?? item.Value;
            OnViewModelPropertyChangedUI(null, null);
        }
        internal static void SetupPropertiesAndValues <T>(PropertySet PropertySet, ObservableCollection <T> PropertyValues, List <string> locales, ObservableCollection <PropertyAndPropertyValueBase> PropertiesAndValues, bool isWizardMode) where T : PropertyValueBase
        {
            PropertiesAndValues.Clear();
            if (PropertySet != null)
            {
                // generate value objects from PropertySet.PropertySetProperties and fill current values
                PropertySet.PropertySetProperties.Select(x => x.Property).ToList().ForEach(x =>
                {
                    if (x.IsLocaleDependant)
                    {
                        locales.ForEach(y =>
                        {
                            var item = new PropertyAndPropertyValueBase {
                                Property = x, Locale = y
                            };
                            if (x.IsMultiValue)
                            {
                                var values  = PropertyValues.Where(z => z.Name == x.Name && z.Locale == y);
                                item.Values = new ObservableCollection <PropertyValueBase>(values);
                            }
                            else
                            {
                                item.Value = PropertyValues.FirstOrDefault(z => z.Name == x.Name && z.Locale == y);
                            }

                            PropertiesAndValues.Add(item);
                        });
                    }
                    else
                    {
                        var item = new PropertyAndPropertyValueBase {
                            Property = x
                        };
                        if (x.IsMultiValue)
                        {
                            var values  = PropertyValues.Where(z => z.Name == x.Name);
                            item.Values = new ObservableCollection <PropertyValueBase>(values);
                        }
                        else
                        {
                            item.Value = PropertyValues.FirstOrDefault(z => z.Name == x.Name);
                        }

                        PropertiesAndValues.Add(item);
                    }
                });
            }

            // fill values that misses properties. Only in edit dialog
            if (!isWizardMode)
            {
                PropertyValues.ToList().ForEach(x =>
                {
                    var placeholderForValue = PropertiesAndValues.FirstOrDefault(y => y.PropertyName == x.Name);
                    if (placeholderForValue == null)
                    {
                        PropertiesAndValues.Add(new PropertyAndPropertyValueBase {
                            Value = x
                        });
                    }
                    //else
                    //{
                    //    if (placeholderForValue.IsMultiValue)
                    //    {
                    //        if (placeholderForValue.Values == null)
                    //            placeholderForValue.Values = new ObservableCollection<PropertyValueBase>();
                    //        placeholderForValue.Values.Add(placeholderForValue.Property.PropertyValues.First(y => y.PropertyValueId == x.KeyValue));
                    //    }
                    //    else
                    //        placeholderForValue.Value = x;
                    //}
                });
            }
        }
        internal static void RaisePropertyValueEditInteractionRequest <T>(IViewModelsFactory <IPropertyValueBaseViewModel> _vmFactory, InteractionRequest <Confirmation> confirmRequest, ICatalogEntityFactory entityFactory, Action <PropertyAndPropertyValueBase, PropertyAndPropertyValueBase> finalAction, PropertyAndPropertyValueBase originalItem, string locale) where T : PropertyValueBase
        {
            var item = originalItem.DeepClone(entityFactory as CatalogEntityFactory);

            T itemValue;

            if (!originalItem.IsMultiValue)
            {
                item.Values = new ObservableCollection <PropertyValueBase>();
                if (originalItem.Value == null)
                {
                    itemValue = (T)entityFactory.CreateEntityForType(typeof(T));
                    if (originalItem.Property != null)
                    {
                        itemValue.ValueType = originalItem.Property.PropertyValueType;
                    }
                    item.Value = itemValue;
                }
            }
            else if (originalItem.Values == null)
            {
                //itemValue = (T)entityFactory.CreateEntityForType(typeof(T));
                // item.CategoryId = InnerItem.CategoryId;
                //if (originalItem.Property != null)
                //	itemValue.ValueType = originalItem.Property.PropertyValueType;
                item.Values = new ObservableCollection <PropertyValueBase>();
            }
            //else
            //{
            //	itemValue = (T)originalItem.Value.DeepClone(entityFactory as CatalogEntityFactory);
            //}


            var itemVM = _vmFactory.GetViewModelInstance(
                new KeyValuePair <string, object>("item", item),
                new KeyValuePair <string, object>("locale", locale));

            var confirmation = new ConditionalConfirmation(itemVM.Validate)
            {
                Title = "Edit property value".Localize(), Content = itemVM
            };

            confirmRequest.Raise(confirmation, (x) =>
            {
                if (x.Confirmed)
                {
                    finalAction(originalItem, item);
                }
            });
        }