Esempio n. 1
0
        public static void RefreshDeliveryReport()
        {
            using (var dataBinding = new MessageDataAccessor())
            using (var service = new MyMobileAPIService.APISoapClient())
            {
                DataSet dataSet = null;
                DataTable settingsTable = null;

                foreach (var account in dataBinding.GetAccounts())
                {
                    bool recordsProcessed;

                    do
                    {
                        recordsProcessed = false;

                        SentServiceCallEntity data = new SentServiceCallEntity()
                        {
                            Settings = dataBinding.GetSentSettings(account.AccountId)
                        };

                        dataSet = new DataSet("sent");
                        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.Sent_DS_DS(account.Username, account.Password, dataSet);

                        SaveXmlStringToFile(results.GetXml(),"DELIVERIES");

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

                                    if (Guid.TryParse(TypeSafety.GetValue<string>(r["customerid"]), out tempGuid))
                                    {
                                        message = dataBinding.GetMessageByRowGuid(tempGuid);
                                        // we only update the values of an existing message
                                        if (message != null)
                                        {
                                            message.NumberTo = TypeSafety.GetValue<string>(r["numto"]);
                                            message.NumberTo = (message.NumberTo.Length > 15) ? message.NumberTo.Substring(0, 15) : message.NumberTo;
                                            message.Text = TypeSafety.GetValue<string>(r["data"]);
                                            message.EventId = TypeSafety.GetValue<int>(r["eventid"]);
                                            message.Status = TypeSafety.GetValue<string>(r["status"]);
                                        }
                                        else
                                        {
                                            message = new MessageEntity()
                                            {
                                                // we create a new message as it does not exist
                                                NumberTo = TypeSafety.GetValue<string>(r["numto"]),
                                                Sender = string.Empty,
                                                Text = TypeSafety.GetValue<string>(r["data"]),
                                                DateCreated = DateTime.Now,
                                                OriginId = 5, // Legacy System
                                                UniqueId = TypeSafety.GetValue<int>(r["customerid"], 0),
                                                EventId = TypeSafety.GetValue<int>(r["eventid"]),
                                                RowGuid = Guid.NewGuid(),
                                                Status = "UNKOWN " + TypeSafety.GetValue<string>(r["status"])
                                            };
                                        }
                                        message = dataBinding.SetMessageForced(message);
                                    }
                                    else
                                    {
                                        message = new MessageEntity()
                                            {
                                                // we create a new message as it does not exist
                                                NumberTo = TypeSafety.GetValue<string>(r["numto"]),
                                                Sender = string.Empty,
                                                Text = TypeSafety.GetValue<string>(r["data"]),
                                                DateCreated = DateTime.Now,
                                                OriginId = 5, // Legacy System
                                                UniqueId = TypeSafety.GetValue<int>(r["customerid"], 0),
                                                EventId = TypeSafety.GetValue<int>(r["eventid"]),
                                                RowGuid = Guid.NewGuid(),
                                                Status = "UNKOWN " + TypeSafety.GetValue<string>(r["status"])
                                            };

                                        message = dataBinding.SetMessageForced(message);
                                    }

                                    if (message != null)
                                    {
                                        DeliveryReportEntity report = new DeliveryReportEntity();
                                        report.ChangeId = TypeSafety.GetValue<long>(r["changeid"]);
                                        report.MessageId = message.MessageId;
                                        report.SentId = TypeSafety.GetValue<long>(r["sentid"].ToString());
                                        report.Status = TypeSafety.GetValue<string>(r["status"]);
                                        report.StatusDate = TypeSafety.GetValue<DateTime>(r["statusdate"], DateTime.Now);

                                        dataBinding.InsertDeliveryReport(report);
                                    }
                                }
                                catch (Exception ex) 
                                {
                                    System.Threading.Thread.Sleep(1);
                                }
                            } );

                            recordsProcessed = results.Tables["data"].Rows.Count > 0;

                            if (recordsProcessed)
                            {
                                long latestID = results.Tables["data"].AsEnumerable().Max(s => TypeSafety.GetValue<long>(s["changeid"]));

                                dataBinding.UpdateSentSettingsLatestID(latestID, account.AccountId);
                            }
                        }
                    } while (recordsProcessed);
                }
            }
        }
Esempio n. 2
0
        private static void SaveInvalidMessages(ICollection<MessageEntity> messages)
        {
            using (var dataBinding = new MessageDataAccessor())
            {
                messages.Where(p => !p.IsValid).ForAll(message =>
                {
                    // If number is null, make it empty
                    if (string.IsNullOrEmpty(message.NumberTo))
                        message.NumberTo = string.Empty;
                    // If text is null, make it empty
                    if (string.IsNullOrEmpty(message.Text))
                        message.Text = string.Empty;
                    // If origin is 0, change it to 6 (Unkown)
                    if (message.OriginId == 0)
                        message.OriginId = 6;

                    message.RowGuid = Guid.NewGuid();
                    message.Status = "VALIDATION FAILED";
                    message.DateCreated = DateTime.Now;

                    // Create report first, because saving message clears the ValidationReason.
                    DeliveryReportEntity report = new DeliveryReportEntity()
                    {
                        ChangeId = 0,
                        SentId = 0,
                        Status = message.ValidationReason,
                        StatusDate = DateTime.Now
                    };

                    SaveMessage(message, report);
                });
            }
        }
Esempio n. 3
0
        private static void SaveMessage(MessageEntity message, DeliveryReportEntity deliveryReport)
        {
            using (var dataBinding = new MessageDataAccessor())
            {
                message = dataBinding.SetMessage(message);

                if (deliveryReport != null && message != null)
                {
                    deliveryReport.MessageId = message.MessageId;

                    dataBinding.InsertDeliveryReport(deliveryReport);
                }
            }
        }
Esempio n. 4
0
        public static void SendSMS(ICollection<MessageEntity> messages)
        {

            List<SendServiceCallEntity> accountList = new List<SendServiceCallEntity>();

            // Validate all messages first
            ValidateMessages(messages);

            SaveInvalidMessages(messages);

            // Get a list of accounts and add messages to these accounts
            accountList = GetAccountList(messages);

            // Get the default account and add all batches.
            accountList.AddRange(GetDefaultAccount(messages));


            foreach (var account in accountList)
            {
                DataSet dataSet = null;
                DataTable entriesTable = null;

                try
                {
                    using (var service = new MyMobileAPIService.APISoapClient())
                    {
                        dataSet = CreateSendSettings(account);
                        entriesTable = CreateEntriesTable();

                        foreach (var entry in account.Entries)
                        {
                            entriesTable.Rows.Add(GetEntryDataRow(entriesTable.NewRow(), entry));
                        }

                        dataSet.Tables.Add(entriesTable);

                        SaveXmlStringToFile(dataSet.GetXml(),"SEND_QUEUE");

                        // Send SMSs via web service.
                        DataSet resultSet = service.Send_DS_DS(account.Account.Username, account.Account.Password, dataSet);

                        dataSet = null;
                        entriesTable = null;

                        SaveXmlStringToFile(resultSet.GetXml(), "SEND_RESULTS");

                        if (resultSet != null && resultSet.Tables.Contains("call_result"))
                        {
                            int eventId = 0;

                            DataTable callResult = resultSet.Tables["call_result"];

                            if (resultSet.Tables.Contains("send_info") && resultSet.Tables["send_info"].Rows.Count > 0)
                            {
                                DataTable sendInfo = resultSet.Tables["send_info"];
                                eventId = TypeSafety.GetValue<int>(sendInfo.Rows[0]["eventid"]);
                            }

                            if (resultSet.Tables.Contains("entries_success") && resultSet.Tables["entries_success"].Rows.Count > 0)
                            {
                                var resultTable = resultSet.Tables["entries_success"];
                                foreach (DataRow row in resultTable.Rows)
                                {
                                    Guid rowGuid;
                                    if (Guid.TryParse(row["customerid"].ToString(), out rowGuid))
                                    {
                                        MessageEntity tempMessage = account.Entries.Where(p => p.RowGuid == rowGuid).FirstOrDefault();
                                        tempMessage.EventId = eventId;
                                        tempMessage.Status = "SENT";
                                        tempMessage.DateCreated = DateTime.Now;

                                        SaveMessage(tempMessage);
                                    }
                                }
                                resultTable = null;
                            }
                            if (resultSet.Tables.Contains("entries_failed") && resultSet.Tables["entries_failed"].Rows.Count > 0)
                            {
                                var resultTable = resultSet.Tables["entries_failed"];
                                foreach (DataRow row in resultTable.Rows)
                                {
                                    Guid rowGuid;
                                    if (Guid.TryParse(row["customerid"].ToString(), out rowGuid))
                                    {
                                        MessageEntity tempMessage = account.Entries.Where(p => p.RowGuid == rowGuid).FirstOrDefault();
                                        tempMessage.EventId = eventId;
                                        tempMessage.Status = "FAILED";
                                        tempMessage.DateCreated = DateTime.Now;

                                        // Create report first, because saving message clears the ValidationReason.
                                        DeliveryReportEntity report = new DeliveryReportEntity()
                                        {
                                            ChangeId = 0,
                                            SentId = 0,
                                            Status = TypeSafety.GetValue<string>(row["reason"]),
                                            StatusDate = DateTime.Now
                                        };

                                        SaveMessage(tempMessage, report);
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (entriesTable != null)
                    {
                        entriesTable.Dispose();
                    }
                    if (dataSet != null)
                    {
                        dataSet.Dispose();
                    }
                }
            }
        }
        private static DeliveryReport EntityToDeliveryReport(DeliveryReportEntity entity, DeliveryReport deliveryReport)
        {
            deliveryReport.MessageId = entity.MessageId;
            deliveryReport.ChangeId = entity.ChangeId;
            deliveryReport.SentId = entity.SentId;
            deliveryReport.Status = entity.Status;
            deliveryReport.StatusDate = entity.StatusDate;

            return deliveryReport;
        }
        public DeliveryReportEntity InsertDeliveryReport(DeliveryReportEntity entity)
        {
            using (var context = new CorpSMSEntities())
            {
                var report = context.DeliveryReports.Where(p => p.DeliveryReportId == entity.DeliveryReportId).FirstOrDefault();

                if (report != null)
                {
                    // Detach the entity first
                    context.Detach(report);

                    report = EntityToDeliveryReport(entity, report);

                    // Attach the entity again.
                    context.Attach(report);
                    context.ObjectStateManager.ChangeObjectState(report, System.Data.EntityState.Modified);
                }
                else
                {
                    report = EntityToDeliveryReport(entity, new DeliveryReport());
                    context.AddToDeliveryReports(report);
                }

                context.SaveChanges();

                return DeliveryReportToEntity(report);
            }
        }