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;
            }));
        }
示例#2
0
        public virtual void ReportProgress(string progress)
        {
            engineLogger.Information(progress);
            ReportProgressEventArgs args = new ReportProgressEventArgs();

            args.ProgressUpdate = progress;
            ReportProgressEvent?.Invoke(this, args);
        }
        public virtual void ReportProgress(string progress)
        {
            engineLogger.Information(progress);
            ReportProgressEventArgs args = new ReportProgressEventArgs();

            args.ProgressUpdate = progress;
            //send log to server
            Core.Server.SocketClient.SendExecutionLog(progress);

            //invoke event
            ReportProgressEvent?.Invoke(this, args);
        }
示例#4
0
        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++;
            }
        }
示例#5
0
        public async Task Export(DateTime fromDate, string remoteHost)
        {
            DateTime startDate = fromDate;
            DateTime endDate   = startDate.AddMonths(DATE_RANGE_TO_LOAD);

            using (var connection = new SQLiteConnection(_connectionString))
            {
                connection.Open();

                while (startDate < DateTime.Today)
                {
                    _index         = 0;
                    _customerCount = 0;
                    _customerGroups.Clear();

                    if (_checkFoCancellation())
                    {
                        ReportProgressEvent?.Invoke(_index, 100, true);
                        connection.Close();
                        return;
                    }

                    var loadCustomers = true;
                    while (loadCustomers)
                    {
                        //divide in groups of 10
                        loadCustomers = await LoadCustomers(startDate, endDate);
                    }

                    var tasks = new List <Task>();
                    foreach (var group in _customerGroups)
                    {
                        var task = ExportMultipleCustomers(group, connection, remoteHost);
                        tasks.Add(task);

                        if (tasks.Count == MAX_THREADS_COUNT)
                        {
                            await Task.WhenAll(tasks);

                            tasks.Clear();
                        }
                    }

                    await Task.WhenAll(tasks);

                    startDate = endDate;
                    endDate   = startDate.AddMonths(DATE_RANGE_TO_LOAD);
                }

                connection.Close();
            }
        }
        public virtual void ReportProgress(string progress, LogEventLevel eventLevel = LogEventLevel.Information)
        {
            ReportProgressEventArgs args = new ReportProgressEventArgs();

            switch (eventLevel)
            {
            case LogEventLevel.Verbose:
                EngineLogger.Verbose(progress);
                args.LoggerColor = Color.Purple;
                break;

            case LogEventLevel.Debug:
                EngineLogger.Debug(progress);
                args.LoggerColor = Color.Green;
                break;

            case LogEventLevel.Information:
                EngineLogger.Information(progress);
                args.LoggerColor = SystemColors.Highlight;
                break;

            case LogEventLevel.Warning:
                EngineLogger.Warning(progress);
                args.LoggerColor = Color.Goldenrod;
                break;

            case LogEventLevel.Error:
                EngineLogger.Error(progress);
                args.LoggerColor = Color.Red;
                break;

            case LogEventLevel.Fatal:
                EngineLogger.Fatal(progress);
                args.LoggerColor = Color.Black;
                break;
            }

            if (progress.StartsWith("Skipping"))
            {
                args.LoggerColor = Color.Green;
            }

            args.ProgressUpdate = progress;

            //send log to server
            SocketClient.SendExecutionLog(progress);

            //invoke event
            ReportProgressEvent?.Invoke(this, args);
        }
示例#7
0
        public virtual void ReportProgress(string progress, string eventLevel = "Information")
        {
            ReportProgressEventArgs args          = new ReportProgressEventArgs();
            LogEventLevel           logEventLevel = (LogEventLevel)Enum.Parse(typeof(LogEventLevel), eventLevel);

            switch (logEventLevel)
            {
            case LogEventLevel.Verbose:
                Log.Verbose(progress);
                args.LoggerColor = Color.Purple;
                break;

            case LogEventLevel.Debug:
                Log.Debug(progress);
                args.LoggerColor = Color.Green;
                break;

            case LogEventLevel.Information:
                Log.Information(progress);
                args.LoggerColor = SystemColors.Highlight;
                break;

            case LogEventLevel.Warning:
                Log.Warning(progress);
                args.LoggerColor = Color.Goldenrod;
                break;

            case LogEventLevel.Error:
                Log.Error(progress);
                args.LoggerColor = Color.Red;
                break;

            case LogEventLevel.Fatal:
                Log.Fatal(progress);
                args.LoggerColor = Color.Black;
                break;
            }

            if (progress.StartsWith("Skipping"))
            {
                args.LoggerColor = Color.Green;
            }

            args.ProgressUpdate = progress;

            //invoke event
            ReportProgressEvent?.Invoke(this, args);
        }
        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++;
            }
        }