/// <summary>
        /// 单选模式下的写值操作
        /// </summary>
        /// <param name="curEntity">正在编辑的实体。</param>
        /// <param name="svm">选择视图模型</param>
        /// <param name="selectedEntities">当前被选择的实体</param>
        private SetSelectionResult SyncSelectionToValue_Single(Entity curEntity, SelectionViewMeta svm, IList<Entity> selectedEntities)
        {
            bool success = false;

            //引用属性,应该先尝试设置实体属性,再设置 Id 属性。
            var rp = this.Meta.PropertyMeta.ManagedProperty as IRefProperty;
            if (selectedEntities.Count > 0)
            {
                var selectedEntity = selectedEntities[0] as Entity;
                if (rp != null)
                {
                    //如果 SelectedValuePath 是一个引用属性,或者直接就是一个实体属性,
                    //则应该获取相应的实体的值。
                    var valuePath = svm.SelectedValuePath;
                    if (valuePath != null)
                    {
                        if (valuePath is IRefProperty)
                        {
                            selectedEntity = selectedEntity.GetRefEntity((valuePath as IRefProperty).RefEntityProperty);
                        }
                        else if (rp.RefEntityType.IsAssignableFrom(valuePath.PropertyType))
                        {
                            selectedEntity = this.GetSelectedValue(selectedEntity) as Entity;
                        }
                    }

                    //设置实体到本引用属性上。
                    this.OnReferenceEntityChanging();
                    curEntity.SetRefEntity(rp.RefEntityProperty, selectedEntity, ManagedPropertyChangedSource.FromUIOperating);
                    success = curEntity.GetRefEntity(rp.RefEntityProperty) == selectedEntity;
                    if (success) { this.OnReferenceEntityChanged(); }
                }
                else
                {
                    var value = this.GetSelectedValue(selectedEntity);
                    this.PropertyValue = value;
                    success = this.PropertyValue == value;
                    if (success && svm.RefIdHost != null)
                    {
                        curEntity.SetProperty(svm.RefIdHost, selectedEntity.Id, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
            }
            else
            {
                if (rp != null)
                {
                    this.OnReferenceEntityChanging();
                    curEntity.SetRefEntity(rp.RefEntityProperty, null, ManagedPropertyChangedSource.FromUIOperating);
                    success = curEntity.GetRefEntity(rp.RefEntityProperty) == null;
                    if (success) { this.OnReferenceEntityChanged(); }
                }
                else
                {
                    this.PropertyValue = null;
                    success = this.PropertyValue == null;
                    if (success && svm.RefIdHost != null)
                    {
                        curEntity.SetProperty(svm.RefIdHost, null, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
            }

            return success ? SetSelectionResult.Success : SetSelectionResult.Cancel;
        }
        /// <summary>
        /// 多选模式下的写值操作
        /// </summary>
        /// <param name="curEntity">正在编辑的实体。</param>
        /// <param name="svm">选择视图模型</param>
        /// <param name="selectedEntities">当前被选择的实体</param>
        private SetSelectionResult SyncSelectionToValue_Multiple(Entity curEntity, SelectionViewMeta rvm, IList<Entity> selectedEntities)
        {
            var result = string.Join(rvm.SplitterIfMulti, selectedEntities.Select(i => this.GetSelectedValue(i)));

            //赋值给this.PropertyValue
            this.PropertyValue = result;

            SetSelectionResult res = (this.PropertyValue as string) == result ? SetSelectionResult.Success : SetSelectionResult.Cancel;
            if (res == SetSelectionResult.Success)
            {
                //此时这个属性应该是一个 int 数组类型
                if (rvm.RefIdHost != null)
                {
                    var idArray = selectedEntities.Select(i => i.Id).ToArray();
                    curEntity.SetProperty(rvm.RefIdHost, idArray, ManagedPropertyChangedSource.FromUIOperating);
                }
            }

            return res;
        }
Example #3
0
        private void TrySetProperty(Entity e, string pName, JToken value)
        {
            //有些小写的客户端数据被传输到了服务端,需要被过滤掉。
            if (char.IsLower(pName[0])) { return; }

            var pm = _entityMeta.Property(pName) as PropertyMeta ??
                _entityMeta.ChildrenProperty(pName);
            if (pm != null)
            {
                var mp = pm.ManagedProperty;
                if (mp is IListProperty)
                {
                    //todo: 此处的性能可能需要优化,聚合保存子列表时,重复的查询 Repository
                    var entityType = EntityMatrix.FindByList(pm.PropertyType).EntityType;
                    var repo = RF.Find(entityType);

                    //列表属性的设置不能使用 SetProperty,否则,list.Parent 将会无值。
                    //但是也不能直接使用 LoadProperty,否则会导致调用 list.MarkOld,从而不会保存这个列表。
                    //所以只能先装载一个空列表,然后再把 json 中的数据转换为实体加入到这个列表中。
                    var list = repo.NewList();
                    e.LoadProperty(mp, list);

                    ListReader.JsonToEntity(value as JObject, repo, list);
                }
                else
                {
                    var rawValue = (value as JValue).Value;

                    rawValue = EntityJsonConverter.ToServerValue(pm.PropertyType, rawValue);

                    e.SetProperty(mp, rawValue, ManagedPropertyChangedSource.FromUIOperating);
                }
            }
            else
            {
                var rawValue = (value as JValue).Value;

                //如果没有找到一般的属性,则尝试查找外键属性
                for (int i = 0, c = _refIdProperties.Count; i < c; i++)
                {
                    var rip = _refIdProperties[i];
                    if (rip.Name == pName)
                    {
                        e.SetRefId(rip, rawValue);
                        break;
                    }
                }
                //只读属性。
                //if(notFound)
                //{
                //    throw new InvalidOperationException("没有在实体中找到这个属性:" + pName);
                //}
            }
        }
Example #4
0
 private static void CalculateCollectValue(Entity entity, IManagedProperty property, ManagedPropertyChangedEventArgs args)
 {
     var distance = Convert.ToDouble(args.NewValue) - Convert.ToDouble(args.OldValue);
     var oldValue = Convert.ToDouble(entity.GetProperty(property));
     entity.SetProperty(property, oldValue + distance);
 }
Example #5
0
        /// <summary>
        /// 遍历 JSON 对象的属性,并使用托管属性 API 来设置一般属性的值。
        /// </summary>
        /// <param name="jObject"></param>
        /// <param name="entity"></param>
        private void DeserializeProperties(JObject jObject, Entity entity)
        {
            var properties = entity.PropertiesContainer.GetAvailableProperties();
            foreach (var propertyValue in jObject)
            {
                var propertyName = propertyValue.Key;
                var jValue = propertyValue.Value;
                var mp = properties.Find(propertyName, true);
                if (mp != null)
                {
                    //id 属性需要提前处理,而不需要在这里直接反序列化。
                    if (mp == Entity.IdProperty) continue;

                    if (mp is IListProperty)
                    {
                        DeserializeList(entity, mp as IListProperty, jValue as JArray);
                    }
                    else if (mp is IRefEntityProperty)
                    {
                        //一般引用属性不支持反序列化。
                    }
                    //一般属性。
                    else
                    {
                        object value = null;

                        //对于数组的泛型列表类型,需要进行特殊的处理。
                        if (jValue is JArray)
                        {
                            var jArray = jValue as JArray;
                            var propertyType = mp.PropertyType;
                            if (propertyType.IsArray)
                            {
                                var elementType = propertyType.GetElementType();
                                var array = Array.CreateInstance(elementType, jArray.Count);
                                for (int i = 0, c = jArray.Count; i < c; i++)
                                {
                                    var itemTyped = TypeHelper.CoerceValue(elementType, jArray[i]);
                                    array.SetValue(itemTyped, i);
                                }
                                value = array;
                            }
                            else if (TypeHelper.IsGenericType(propertyType, typeof(List<>)))//List<string>
                            {
                                var elementType = propertyType.GetGenericArguments()[0];
                                var list = Activator.CreateInstance(propertyType) as IList;
                                for (int i = 0, c = jArray.Count; i < c; i++)
                                {
                                    var itemTyped = TypeHelper.CoerceValue(elementType, jArray[i]);
                                    list.Add(itemTyped);
                                }
                                value = list;
                            }
                            else
                            {
                                //如果不是数组类型或者泛型列表类型的属性,则不支持反序列化。
                                //do nothing;
                            }
                        }
                        else
                        {
                            value = (jValue as JValue).Value;

                            if (value is string && mp.PropertyType == typeof(byte[]))
                            {
                                value = Convert.FromBase64String(value as string);
                            }
                        }

                        entity.SetProperty(mp, value, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
                else
                {
                    //如果指定了状态,则主动设置该实体的状态。
                    if (propertyName.EqualsIgnoreCase(PersistenceStatusProperty))
                    {
                        var value = (jValue as JValue).Value;
                        var status = (PersistenceStatus)Enum.Parse(typeof(PersistenceStatus), value.ToString(), true);
                        entity.PersistenceStatus = status;
                    }
                }
            }

            //可以使用 Json.NET 来遍历给实体属性赋值。
            //using (var jsonTextReader = new StringReader(strContent))
            //{
            //    var jsonSerializer = JsonSerializer.Create(this.SerializerSettings);
            //    jsonSerializer.Populate(jsonTextReader, entity);
            //}
        }
Example #6
0
 /// <summary>
 /// 有必要的话,就修改Name
 /// </summary>
 /// <param name="newObject"></param>
 private void TryModifyName(Entity newObject, bool modifyName)
 {
     if (modifyName)
     {
         if (newObject is IHasHame)
         {
             var no = newObject as IHasHame;
             no.Name += "-新增";
         }
         else
         {
             var mp = newObject.PropertiesContainer.GetAvailableProperties().Find("Name");
             if (mp != null && !mp.IsReadOnly)
             {
                 string name = newObject.GetProperty(mp).ToString();
                 newObject.SetProperty(mp, name + "-新增");
             }
         }
     }
 }