Esempio n. 1
0
        /// <summary>
        /// SysStore及SqlStore通用
        /// </summary>
        private static async Task RemoveIndex(DesignHub hub, string appName, EntityModel entityModel, byte indexId)
        {
            if (entityModel.StoreOptions == null)
            {
                throw new InvalidOperationException("Can't remove index for DTO");
            }
            if (!entityModel.StoreOptions.HasIndexes)
            {
                throw new Exception($"EntityModel[{entityModel.Name}] not has any indexes.");
            }

            var index = entityModel.StoreOptions.Indexes.SingleOrDefault(t => t.IndexId == indexId);

            if (index == null)
            {
                throw new Exception($"EntityModel[{entityModel.Name}] has no index: {indexId}");
            }

            if (entityModel.SysStoreOptions != null)
            {
                //查询服务代码引用项
                var refs = await RefactoringService.FindUsagesAsync(hub,
                                                                    ModelReferenceType.EntityIndexName, appName, entityModel.Name, index.Name);

                if (refs != null && refs.Count > 0)
                {
                    throw new Exception($"EntityIndex[{entityModel.Name}.{index.Name}] has references.");
                }
            }

            index.MarkDeleted();
        }
Esempio n. 2
0
        public async Task <object> Handle(DesignHub hub, InvokeArgs args)
        {
            var refType = (ModelReferenceType)args.GetInt32();
            var modelID = args.GetString();
            var oldName = args.GetString();
            var newName = args.GetString();

            return(await RefactoringService.RenameAsync(hub, refType,
                                                        ulong.Parse(modelID), oldName, newName));
        }
Esempio n. 3
0
        public async Task <object> Handle(DesignHub hub, InvokeArgs args)
        {
            var refType       = (ModelReferenceType)args.GetInt32();
            var modelIDString = args.GetString();
            var memberName    = args.GetString();
            var modelId       = ulong.Parse(modelIDString);

            ModelType modelType = IdUtil.GetModelTypeFromModelId(modelId);
            var       modelNode = hub.DesignTree.FindModelNode(modelType, modelId);

            if (modelNode == null)
            {
                throw new Exception("Can't find model");
            }

            return(await RefactoringService.FindUsagesAsync(hub, refType,
                                                            modelNode.AppNode.Model.Name, modelNode.Model.Name, memberName));
        }
Esempio n. 4
0
        public async Task <object> Handle(DesignHub hub, InvokeArgs args)
        {
            // 获取接收到的参数
            string modelId  = args.GetString();
            string itemName = args.GetString();

            var modelNode = hub.DesignTree.FindModelNode(ModelType.Enum, ulong.Parse(modelId));

            if (modelNode == null)
            {
                throw new Exception("Can't find Enum node");
            }
            var model = (EnumModel)modelNode.Model;

            //查找成员引用
            var refs = await RefactoringService.FindUsagesAsync(hub,
                                                                ModelReferenceType.EnumModelItemName, modelNode.AppNode.Model.Name, model.Name, itemName);

            if (refs != null && refs.Count > 0) //有引用项不做删除操作
            {
                return(refs);                   //TODO:直接报错,不返回引用项
            }
            var item = model.Items.FirstOrDefault(t => t.Name == itemName);

            if (item != null)
            {
                model.Items.Remove(item);
            }
            // 保存到本地
            await modelNode.SaveAsync(null);

            // 更新RoslynDocument
            await hub.TypeSystem.UpdateModelDocumentAsync(modelNode);

            return(null);
        }
Esempio n. 5
0
        public async Task <object> Handle(DesignHub hub, InvokeArgs args)
        {
            // 获取接收到的参数
            string modelId    = args.GetString();
            string memberName = args.GetString();

            var node = hub.DesignTree.FindNode(DesignNodeType.EntityModelNode, modelId);

            if (node == null)
            {
                throw new Exception("Can't find entity model node");
            }
            var modelNode = node as ModelNode;
            var model     = modelNode.Model as EntityModel;

            if (!modelNode.IsCheckoutByMe)
            {
                throw new Exception("Node has not checkout");
            }
            var mm = model.GetMember(memberName, true);

            //判断是否外键及被索引使用,仅DataField
            if (mm.Type == EntityMemberType.DataField)
            {
                var dfm = (DataFieldModel)mm;
                if (dfm.IsForeignKey)
                {
                    throw new Exception("Can't delete a foregn key member");
                }

                if (model.StoreOptions != null && model.StoreOptions.HasIndexes)
                {
                    foreach (var index in model.StoreOptions.Indexes)
                    {
                        //排除已标为删除的
                        if (index.PersistentState != PersistentState.Deleted)
                        {
                            if (index.Fields.Any(t => t.MemberId == mm.MemberId))
                            {
                                throw new Exception($"Member are used in Index[{index.Name}]");
                            }
                            if (index.HasStoringFields)
                            {
                                if (index.StoringFields.Any(t => t == mm.MemberId))
                                {
                                    throw new Exception($"Member are used in Index[{index.Name}]");
                                }
                            }
                        }
                    }
                }
            }

            //查找成员引用
            var refs = await RefactoringService.FindUsagesAsync(hub,
                                                                ModelReferenceType.EntityMemberName, modelNode.AppNode.Model.Name, model.Name, memberName);

            if (refs != null && refs.Count > 0) //有引用项不做删除操作
            {
                return(refs);                   //TODO:直接报错,不返回引用项
            }
            model.RemoveMember(memberName);

            // 保存到本地
            await modelNode.SaveAsync(null);

            // 更新RoslynDocument
            await hub.TypeSystem.UpdateModelDocumentAsync(modelNode);

            return(null);
        }
Esempio n. 6
0
        private static async Task <DesignNode> DeleteModelNode(DesignHub hub, ModelNode node)
        {
            // 查找ModelRootNode
            var  rootNode            = hub.DesignTree.FindModelRootNode(node.Model.AppId, node.Model.ModelType);
            bool rootNodeHasCheckout = rootNode.IsCheckoutByMe;
            // 尝试签出模型节点及根节点
            bool nodeCheckout = await node.Checkout();

            bool rootCheckout = await rootNode.Checkout();

            if (!nodeCheckout || !rootCheckout)
            {
                throw new Exception("Can't checkout nodes.");
            }
            // 注意:如果自动签出了模型根节点,当前选择的节点需要重新指向,因为Node.Checkout()时已重新加载
            if (!rootNodeHasCheckout)
            {
                node = rootNode.FindModelNode(node.Model.Id);
            }
            if (node == null) //可能已不存在
            {
                throw new Exception("Delete target not exists, please refresh.");
            }
            // 判断当前节点所属层是否是系统层
            if (node.Model.ModleLayer == ModelLayer.SYS)
            {
                throw new Exception("Can't delete system model.");
            }
            var model = node.Model;
            // 查找引用项
            var usages = await RefactoringService.FindModelReferencesAsync(hub, model.ModelType,
                                                                           node.AppNode.Model.Name, model.Name);

            if (usages != null && usages.Count > 0)
            {
                //注意排除自身引用
                usages = usages.Where(u => !(u.ModelNode.Model.Id == model.Id)).ToArray();
                if (usages.Count > 0)
                {
#if DEBUG
                    foreach (var item in usages)
                    {
                        Log.Warn(item.ToString());
                    }
#endif
                    throw new Exception("Has usages, Can't delete it.");
                }
            }

            // 判断当前模型是否已持久化到数据库中
            if (model.PersistentState == PersistentState.Detached)
            {
                await StagedService.DeleteModelAsync(model.Id);
            }
            else
            {
                model.MarkDeleted();
                await node.SaveAsync(null);
            }
            // 移除对应节点
            rootNode.RemoveModel(node);
            // 删除Roslyn相关
            RemoveRoslynFromModelNode(hub, node);

            return(rootNodeHasCheckout ? null : rootNode);
        }