/// <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;
        }
Example #2
0
        private static object GetRootId(Entity entity)
        {
            var parentProeprtyMeta = entity.GetRepository().FindParentPropertyInfo(false);
            if (parentProeprtyMeta == null) { return entity.Id; }

            var refMP = parentProeprtyMeta.ManagedProperty.CastTo<IRefEntityProperty>();
            var parent = entity.GetRefEntity(refMP);

            return GetRootId(parent);
        }
Example #3
0
        internal static void EntityToJson(EntityViewMeta evm, Entity entity, EntityJson entityJson)
        {
            var isTree = evm.EntityMeta.IsTreeEntity;

            foreach (var propertyVM in evm.EntityProperties)
            {
                var property = propertyVM.PropertyMeta;
                var mp = property.ManagedProperty;
                if (mp != null)
                {
                    //如果非树型实体,则需要排除树型实体的两个属性。
                    if (!isTree && (mp == Entity.TreeIndexProperty || mp == Entity.TreePIdProperty)) { continue; }

                    //引用属性
                    if (propertyVM.IsReference)
                    {
                        var refMp = mp as IRefProperty;
                        object value = string.Empty;
                        var id = entity.GetRefNullableId(refMp.RefIdProperty);
                        if (id != null) { value = id; }

                        var idName = refMp.RefIdProperty.Name;
                        entityJson.SetProperty(idName, value);

                        //同时写入引用属性的视图属性,如 BookCategoryId_Label
                        if (id != null && propertyVM.CanShowIn(ShowInWhere.List))
                        {
                            var titleProperty = propertyVM.SelectionViewMeta.RefTypeDefaultView.TitleProperty;
                            if (titleProperty != null)
                            {
                                var lazyRefEntity = entity.GetRefEntity(refMp.RefEntityProperty);
                                var titleMp = titleProperty.PropertyMeta.ManagedProperty;
                                if (titleMp != null)
                                {
                                    value = lazyRefEntity.GetProperty(titleMp);
                                }
                                else
                                {
                                    value = ObjectHelper.GetPropertyValue(lazyRefEntity, titleProperty.Name);
                                }

                                var name = EntityModelGenerator.LabeledRefProperty(idName);
                                entityJson.SetProperty(name, value);
                            }
                        }
                    }
                    //一般托管属性
                    else
                    {
                        var pRuntimeType = property.PropertyType;
                        var serverType = ServerTypeHelper.GetServerType(pRuntimeType);
                        if (serverType.Name != SupportedServerType.Unknown)
                        {
                            var value = entity.GetProperty(mp);
                            value = ToClientValue(pRuntimeType, value);
                            entityJson.SetProperty(mp.Name, value);
                        }
                    }
                }
                //一般 CLR 属性
                else
                {
                    var pRuntimeType = property.PropertyType;
                    var serverType = ServerTypeHelper.GetServerType(pRuntimeType);
                    if (serverType.Name != SupportedServerType.Unknown)
                    {
                        var value = ObjectHelper.GetPropertyValue(entity, property.Name);
                        value = ToClientValue(pRuntimeType, value);
                        entityJson.SetProperty(property.Name, value);
                    }
                }
            }
        }