Esempio n. 1
0
        private void Importer_DoWork(object sender, DoWorkEventArgs e)
        {
            var worker             = (BackgroundWorker)sender;
            var opts               = (Options)e.Argument;
            var lookup             = new ShoreTelCustomDataImporter.CSVFileCustomerLookup(opts.ImportFile);
            var found              = 0;
            var updated            = 0;
            var lastProgressUpdate = 0;

            using (var cdr = new ShoreTelCustomDataImporter.ShoreTelCDRConnection(opts.ServerAddress))
            {
                var total = cdr.GetPotentialCallCount(opts.StartTime, opts.EndTime, opts.UpdateExisting);
                foreach (var call in cdr.GetCalls(opts.StartTime, opts.EndTime, opts.UpdateExisting))
                {
                    found++;
                    var info = lookup.LookupCallerId(call.CallerId);
                    if (info != null)
                    {
                        cdr.UpdateCustomerInfo(call.CallId, info);
                        updated++;
                    }
                    var progress = found * 100 / total;
                    if (progress > lastProgressUpdate)
                    {
                        lastProgressUpdate = progress;
                        worker.ReportProgress(progress, updated);
                    }
                }
            }
            e.Result = Tuple.Create(found, updated);
        }
        static void Main(string[] args)
        {
            try
            {
                DateTime?startDate       = null;
                DateTime?endDate         = null;
                var      updateExisting  = false;
                var      verbose         = false;
                var      providerArgs    = new List <object>();
                var      lookupType      = typeof(ShoreTelCustomDataImporter.CSVFileCustomerLookup);
                var      stServerAddress = "localhost";

                for (var i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-lookupProvider")
                    {
                        lookupType = Type.GetType(args[++i], true);
                    }

                    else if (args[i] == "-startDate")
                    {
                        startDate = DateTime.Parse(args[++i]);
                    }

                    else if (args[i] == "-endDate")
                    {
                        endDate = DateTime.Parse(args[++i]);
                    }

                    else if (args[i] == "-updateExisting")
                    {
                        updateExisting = true;
                    }

                    else if (args[i] == "-verbose")
                    {
                        verbose = true;
                    }

                    else if (args[i] == "-stserver")
                    {
                        stServerAddress = args[++i];
                    }

                    else if (args[i] == "-h")
                    {
                        ShowUsage();
                        return;
                    }
                    else
                    {
                        providerArgs.Add(args[i]);
                    }
                }

                ShoreTelCustomDataImporter.ICustomerLookup lookup;
                try
                {
                    lookup = (ShoreTelCustomDataImporter.ICustomerLookup)Activator.CreateInstance(lookupType, providerArgs.ToArray());
                }
                catch (System.Reflection.TargetInvocationException ex)
                {
                    InitializationError(ex.InnerException);
                    return;
                }
                catch (Exception ex)
                {
                    InitializationError(ex);
                    return;
                }

                int found   = 0;
                int updated = 0;

                using (var cdr = new ShoreTelCustomDataImporter.ShoreTelCDRConnection(stServerAddress))
                {
                    foreach (var call in cdr.GetCalls(startDate, endDate, updateExisting))
                    {
                        found++;
                        var info = lookup.LookupCallerId(call.CallerId);
                        if (info != null)
                        {
                            cdr.UpdateCustomerInfo(call.CallId, info);
                            updated++;
                            if (verbose)
                            {
                                Console.WriteLine("Call {0} with CallerID {1} was set to customer id / name {2} / {3}",
                                                  call.CallId, call.CallerId, info.CustomerId, info.CustomerName);
                            }
                        }
                    }
                }

                Console.WriteLine("{0} calls were updated out of {1} found", updated, found);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }