예제 #1
0
        public static void RefreshReplies()
        {
            using (var dataBinding = new MessageDataAccessor())
            using (var service = new MyMobileAPIService.APISoapClient())
            {
                foreach (var account in dataBinding.GetAccounts())
                {
                    bool recordsProcessed;

                    do
                    {
                        recordsProcessed = false;

                        ReplyServiceCallEntity data = new ReplyServiceCallEntity()
                        {
                            Settings = dataBinding.GetReplySettings(account.AccountId)
                        };

                        DataSet dataSet = new DataSet("reply");
                        DataTable settingsTable = new DataTable("settings");
                        settingsTable.Columns.Add("id");
                        settingsTable.Columns.Add("max_recs");
                        settingsTable.Columns.Add("cols_returned");
                        settingsTable.Columns.Add("date_format");

                        DataRow mainRow = settingsTable.NewRow();
                        mainRow["id"] = data.Settings.LatestId;
                        mainRow["max_recs"] = data.Settings.RecordCount;
                        mainRow["cols_returned"] = data.Settings.ReturnColumns;
                        mainRow["date_format"] = data.Settings.DateFormat;
                        settingsTable.Rows.Add(mainRow);
                        dataSet.Tables.Add(settingsTable);

                        DataSet results = service.Reply_DS_DS(account.Username, account.Password, dataSet);

                        if (results != null && results.Tables.Contains("data"))
                        {
                            results.Tables["data"].AsEnumerable().ForAll(r =>
                            {
                                Guid tempGuid;
                                MessageEntity message;

                                if (Guid.TryParse(TypeSafety.GetValue<string>(r["sentcustomerid"]), out tempGuid))
                                {
                                    message = dataBinding.GetMessageByRowGuid(tempGuid);
                                    if (message != null)
                                    {
                                        message.Text = TypeSafety.GetValue<string>(r["sentdata"]);
                                        message.EventId = TypeSafety.GetValue<int>(r["eventid"]);
                                        message.Status = "REPLY";
                                    }
                                    else
                                    {
                                        message = new MessageEntity()
                                        {
                                            NumberTo = string.Empty,
                                            Sender = TypeSafety.GetValue<string>(r["numfrom"]),
                                            Text = TypeSafety.GetValue<string>(r["sentdata"]),
                                            DateCreated = TypeSafety.GetValue<DateTime>(r["sentdatetime"], DateTime.Now),
                                            OriginId = 5, // Legacy
                                            UniqueId = TypeSafety.GetValue<int>(r["eventid"]),
                                            EventId = TypeSafety.GetValue<int>(r["eventid"]),
                                            RowGuid = Guid.NewGuid(),
                                            Status = "UNKNOWN WITH REPLY"
                                        };

                                    }
                                    message = dataBinding.SetMessageForced(message);
                                }
                                else
                                {
                                    message = new MessageEntity()
                                    {
                                        NumberTo = string.Empty,
                                        Sender = TypeSafety.GetValue<string>(r["numfrom"]),
                                        Text = TypeSafety.GetValue<string>(r["sentdata"]),
                                        DateCreated = TypeSafety.GetValue<DateTime>(r["sentdatetime"], DateTime.Now),
                                        OriginId = 5, // Legacy
                                        UniqueId = TypeSafety.GetValue<int>(r["sentcustomerid"],0),
                                        EventId = TypeSafety.GetValue<int>(r["eventid"]),
                                        RowGuid = Guid.NewGuid(),
                                        Status = "UNKNOWN WITH REPLY"
                                    };

                                    message = dataBinding.SetMessageForced(message);
                                }

                                if (message != null)
                                {
                                    MessageReplyEntity reply = new MessageReplyEntity()
                                    {
                                        ReplyId = TypeSafety.GetValue<int>(r["replyid"]),
                                        MessageId = message.MessageId,
                                        Text = TypeSafety.GetValue<string>(r["receiveddata"]),
                                        DateReceived = TypeSafety.GetValue<DateTime>(r["received"], DateTime.Now)
                                    };

                                    dataBinding.InsertMessageReply(reply);           
                                }
                            });

                            recordsProcessed = results.Tables["data"].Rows.Count > 0;
                            
                            if (recordsProcessed)
                            {
                                int latestID = results.Tables["data"].AsEnumerable().Max(s => TypeSafety.GetValue<int>(s["replyid"]));

                                dataBinding.UpdateReplySettingsLatestID(latestID, account.AccountId);
                                dataBinding.AddStopReplies();
                            }
                        }

                    } while (recordsProcessed);
                }
            }
        }