private async Task <bool> LoadTickets(DateTime startDate, DateTime endDate) { return(await Task.Factory.StartNew(() => { if (_checkFoCancellation()) { ReportProgressEvent?.Invoke(_index, 100, true); return false; } var message = string.Format("Loading Tickets from {0:y} to {1:y}. Loaded {2} tickets.", startDate, endDate, _ticketCount); ReportStatusEvent?.Invoke(message); ReportProgressEvent?.Invoke(_index, 0, false); var tickets = new CommitCRM.ObjectQuery <CommitCRM.Ticket>(CommitCRM.LinkEnum.linkAND, ITEMS_COUNT_TO_LOAD); tickets.AddCriteria(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.OperatorEnum.opGreaterThan, startDate); tickets.AddCriteria(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.OperatorEnum.opLessThan, endDate); tickets.AddSortExpression(CommitCRM.Ticket.Fields.UpdateDate, CommitCRM.SortDirectionEnum.sortASC); foreach (var item in _ticketGroups.SelectMany(x => x)) { tickets.AddCriteria(CommitCRM.Ticket.Fields.TicketREC_ID, CommitCRM.OperatorEnum.opNotLike, item.TicketREC_ID.ToString()); } var ticketResult = tickets.FetchObjects(); if (ticketResult != null && ticketResult.Any()) { _ticketCount += ticketResult.Count; _ticketGroups.Add(ticketResult); } return ticketResult.Count == ITEMS_COUNT_TO_LOAD; })); }
private async Task ExportMultipleCustomers(IEnumerable <CommitCRM.Account> accounts, SQLiteConnection connection, string remoteHost) { Debug.WriteLine(accounts); foreach (CommitCRM.Account account in accounts) { try { if (_checkFoCancellation()) { ReportProgressEvent?.Invoke(_index, 100, true); return; } string customerId = string.Empty; //check cache and skip already exported using (SQLiteCommand cmdItemAlready = new SQLiteCommand(string.Format("SELECT CustomerId FROM Account WHERE AccountId='{0}'", account.AccountREC_ID), connection)) { using (SQLiteDataReader reader = cmdItemAlready.ExecuteReader()) { while (reader.Read()) { Debug.WriteLine(reader[0]); customerId = reader[0].ToString(); } } } if (!string.IsNullOrEmpty(customerId)) { _index++; continue; } var message = string.Format("Exporting ( {0}/{1} ) of Account", _index, _customerCount); ReportStatusEvent?.Invoke(message); ReportProgressEvent?.Invoke(_index, (100 * _index) / _customerCount, false); var customer = await ExportSingleCustomer(account, connection, remoteHost); if (customer == null) { message = string.Format("Unable to create Account with Name : {0}", account.LastName); ReportCustomerErrorEvent?.Invoke(account, message, null); } } catch (Exception ex) { var message = string.Format("Failed to Export a New Account {1}. Due to {0}", ex.Message, account.LastName); ReportCustomerErrorEvent?.Invoke(account, message, ex); } _index++; } }
private async Task ExportMultipleTickets(IEnumerable <CommitCRM.Ticket> tickets, SQLiteConnection connection, string remoteHost) { foreach (CommitCRM.Ticket ticket in tickets) { try { if (_checkFoCancellation()) { ReportProgressEvent?.Invoke(_index, 100, true); return; } string ticketId = string.Empty; using (SQLiteCommand cmdItemAlready = new SQLiteCommand(string.Format("SELECT RTicketId FROM Ticket WHERE ticketId='{0}'", ticket.TicketREC_ID), connection)) { using (SQLiteDataReader reader = cmdItemAlready.ExecuteReader()) { while (reader.Read()) { ticketId = reader[0].ToString(); } } } if (!string.IsNullOrEmpty(ticketId)) { _index++; continue; } string customerId = string.Empty; using (SQLiteCommand cmdItemAlready = new SQLiteCommand(string.Format("SELECT CustomerId FROM Account WHERE AccountId='{0}'", ticket.AccountREC_ID), connection)) { using (SQLiteDataReader reader = cmdItemAlready.ExecuteReader()) { while (reader.Read()) { customerId = reader[0].ToString(); } } } if (string.IsNullOrEmpty(customerId)) { var customer = await CreateCustomerForTicket(ticket, connection, remoteHost); if (customer != null) { customerId = customer.Id; } else { var customerIdMessage = string.Format("Unable to create Customer of Ticket: {0}", ticket.Description); ReportTicketErrorEvent?.Invoke(ticket, customerIdMessage, null); _index++; continue; } } var message = string.Format("Exporting ( {0}/{1} ) of Ticket", _index, _ticketCount); ReportStatusEvent?.Invoke(message); ReportProgressEvent?.Invoke(_index, (100 * _index) / _ticketCount, false); var result = await ExportSingleTicket(ticket, _defaultLocationId, customerId, connection, remoteHost); if (result == null) { message = string.Format("Unable to create Ticket: {0}", ticket.Description); ReportTicketErrorEvent?.Invoke(ticket, message, null); } } catch (Exception ex) { var message = string.Format("Failed to Export a New Ticket {1}. Due to {0}", ex.Message, ticket.Description); ReportTicketErrorEvent?.Invoke(ticket, message, ex); } _index++; } }