Esempio n. 1
0
        /// <summary>
        /// 批量保存本地事件
        /// </summary>
        /// <param name="ontology">本体</param>
        /// <param name="commands"></param>
        public ProcessResult SaveCommands(OntologyDescriptor ontology, MessageEntity[] commands)
        {
            if (ontology == null)
            {
                return new ProcessResult(new ArgumentNullException("ontology", @"ontology参数为null"));
            }
            if (commands == null)
            {
                return new ProcessResult(new ArgumentNullException("commands"));
            }
            foreach (var command in commands)
            {
                if (command == null)
                {
                    return new ProcessResult(new ArgumentNullException("command", "命令数组中有null"));
                }
                else if (!ontology.Ontology.Code.Equals(command.Ontology, StringComparison.OrdinalIgnoreCase))
                {
                    return new ProcessResult(new ArgumentNullException("command.Ontology", "同一命令数组中的命令必须在相同本体下"));
                }
            }
            ProcessResult r = ProcessResult.Ok;
            // 存放异常信息
            StringBuilder sb = new StringBuilder();
            int l = sb.Length;
            // 按照命令类型分组,不同类型的命令可能存储在不同的数据库表
            var gs = commands.GroupBy(c => c.CommandType);
            foreach (var g in gs)
            {
                // 本组命令类型所对应的数据库表
                string tableId = string.Format("[{0}][{1}]", ontology.Ontology.MessageSchemaName, GetTableName(g.Key));
                DbTable dbTable;
                if (!this.GetCommandDb(ontology).TryGetDbTable(tableId, out dbTable))
                {
                    r = new ProcessResult(new AnycmdException("意外的数据库表标识" + tableId));
                }
                // 当前命令表模式克隆得到的新表
                var dt = this.GetCommandDb(ontology).NewTable(dbTable);
                foreach (var command in g)
                {
                    if (command.CommandType == MessageTypeKind.Invalid || command.CommandType == MessageTypeKind.AnyCommand)
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "Invalid和AnyCommand类型命令不能持久化");
                    }
                    else if (string.IsNullOrEmpty(command.Verb.Code))
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "Verb为空或null不能持久化");
                    }
                    else if (string.IsNullOrEmpty(command.MessageId)
                        || command.MessageId.Length > 50)
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "MessageID为空或null或者长度大于50字符不能持久化");
                    }
                    else if (string.IsNullOrEmpty(command.LocalEntityId))
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "LocalEntityID为空或null不能持久化");
                    }
                    else if (command.DataTuple.IdItems.IsEmpty)
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "当前命令的信息标识为空则不能持久化");
                    }
                    else if (!command.TimeStamp.IsValid())
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "TimeStamp非法则不能持久化");
                    }
                    else if (string.IsNullOrEmpty(command.Ontology))
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "Ontology为空或null不能持久化");
                    }
                    else if (string.IsNullOrEmpty(command.ClientId))
                    {
                        r = new ProcessResult(false, Status.InvalidArgument, "ClientID为空或null不能持久化");
                    }
                    if (r.IsSuccess)
                    {
                        // 将当前命令转化DataRow,一个命令对应一行
                        var row = command.ToDataRow(dt);
                        int ll = sb.Length;
                        // 检测每一列对应的值是否超出了数据库定义的长度
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            var dbCol = dt.Columns[i];
                            if (dbCol.MaxLength != -1)
                            {
                                if (row[i].ToString().Length > dbCol.MaxLength)
                                {
                                    if (sb.Length != ll)
                                    {
                                        sb.Append(";");
                                    }
                                    sb.Append(tableId).Append("表:");
                                    sb.Append(dbCol.ColumnName + "超过最大长度,最长" + dbCol.MaxLength.ToString());
                                }
                            }
                        }
                        if (sb.Length == ll)
                        {
                            // 如果不是哑命令。哑命令是不能爆炸的。
                            if (!command.IsDumb)
                            {
                                dt.Rows.Add(row);
                            }
                        }
                    }
                    else
                    {
                        sb.Append(r.Description);
                    }
                }

                this.GetCommandDb(ontology).WriteToServer(dt);
            }

            if (sb.Length != l)
            {
                r = new ProcessResult(new AnycmdException(sb.ToString()));
            }
            else
            {
                r = ProcessResult.Ok;
            }

            if (r.Exception != null)
            {
                ontology.Host.LoggingService.Error(r.Exception);
            }

            return r;
        }