Ejemplo n.º 1
0
        private static void Main(string[] args)
        {
            Stopwatch getProductsStopwatch = new Stopwatch();
            getProductsStopwatch.Start();

            //lcboStoresService = new LcboStoresServiceAgent(new TestPageRetrieverFactory());
            lcboStoresService = new LcboStoresServiceAgent(new LivePageRetrieverFactory());
            unitOfWork = new SqlUnitOfWork();
            var storesInDb = unitOfWork.Stores.FindAll();
            Dictionary<int, Store> dbStoreDictionary = new Dictionary<int, Store>();
            int storesInDbCount = 0;
            foreach (Store store in storesInDb)
            {
                storesInDbCount++;
                dbStoreDictionary.Add(store.Id, store);
            }

            Console.WriteLine(String.Format("Found {0} stores in the DB", storesInDbCount));

            var lcboStores = lcboStoresService.GetAllStores();

            Dictionary<int, LcboStoreInformation> lcboStoresDictionary = new Dictionary<int, LcboStoreInformation>();

            List<int> lcboStoreIds = new List<int>();
            foreach (LcboStoreInformation lcboStore in lcboStores)
            {
                lcboStoresDictionary.Add(lcboStore.StoreNumber, lcboStore);
                lcboStoreIds.Add(lcboStore.StoreNumber);
            }

            lcboStoreIds.Sort();
            //Console.WriteLine("LCBO ctore numbers are:");
            //foreach (int lcboStoreId in lcboStoreIds)
            //{
            //    Console.WriteLine(lcboStoreId);
            //}

            // Figure out changed stores and update them in the unitOfWork
            int newStoreCount = 0;
            foreach (int lcboStoreId in lcboStoresDictionary.Keys)
            {
                LcboStoreInformation lcboStore = lcboStoresDictionary[lcboStoreId];
                if (dbStoreDictionary.ContainsKey(lcboStoreId))
                {
                    Store dbStore = dbStoreDictionary[lcboStoreId];
                    dbStore.Address = lcboStore.AddressLine;
                    dbStore.City = lcboStore.City;
                    dbStore.Latitude = lcboStore.Latitude;
                    dbStore.Longitude = lcboStore.Longitude;
                }
                else
                {
                    Store newDbStore = new Store() { Address = lcboStore.AddressLine,
                    City = lcboStore.City,
                    Id = lcboStore.StoreNumber,
                    Latitude = lcboStore.Latitude,
                    Longitude = lcboStore.Longitude };

                    newStoreCount++;
                    unitOfWork.Stores.Add(newDbStore);
                }
            }

            unitOfWork.Commit();

            getProductsStopwatch.Stop();
            Console.WriteLine(String.Format("Found {0} stores on the LCBO website", lcboStores.Count));
            Console.WriteLine(String.Format("Found {0} new stores on the LCBO website", newStoreCount));
            Console.WriteLine(String.Format("Elapsed time in seconds: {0:0.00}", getProductsStopwatch.ElapsedMilliseconds / 1000.00));
        }
Ejemplo n.º 2
0
        public string ScanStores(IConnection signalrConnection)
        {
            StringBuilder result = new StringBuilder();

            Stopwatch getProductsStopwatch = new Stopwatch();
            getProductsStopwatch.Start();

            var storesInDb = unitOfWork.Stores.FindAll();
            Dictionary<int, Store> dbStoreDictionary = new Dictionary<int, Store>();
            int storesInDbCount = 0;
            foreach (Store store in storesInDb)
            {
                storesInDbCount++;
                dbStoreDictionary.Add(store.Id, store);
            }

            signalrConnection.Broadcast(String.Format("Found {0} stores in the DB", storesInDbCount));
            result.AppendFormat("Found {0} stores in the DB\r\n", storesInDbCount);

            var lcboStores = lcboStoresService.GetAllStores();

            signalrConnection.Broadcast(String.Format("Found {0} stores on the LCBO website", lcboStores.Count));
            result.AppendFormat("Found {0} stores on the LCBO website\r\n", lcboStores.Count);

            Dictionary<int, LcboStoreInformation> lcboStoresDictionary = new Dictionary<int, LcboStoreInformation>();

            List<int> lcboStoreIds = new List<int>();
            foreach (LcboStoreInformation lcboStore in lcboStores)
            {
                lcboStoresDictionary.Add(lcboStore.StoreNumber, lcboStore);
                lcboStoreIds.Add(lcboStore.StoreNumber);
            }

            lcboStoreIds.Sort();
            //Console.WriteLine("LCBO ctore numbers are:");
            //foreach (int lcboStoreId in lcboStoreIds)
            //{
            //    Console.WriteLine(lcboStoreId);
            //}

            // Figure out new and changed stores and update them in the unitOfWork
            int newStoreCount = 0;
            foreach (int lcboStoreId in lcboStoresDictionary.Keys)
            {

                LcboStoreInformation lcboStore = lcboStoresDictionary[lcboStoreId];
                if (dbStoreDictionary.ContainsKey(lcboStoreId))
                {
                    Store dbStore = dbStoreDictionary[lcboStoreId];
                    dbStore.Address = lcboStore.AddressLine;
                    dbStore.City = lcboStore.City;
                    dbStore.Latitude = lcboStore.Latitude;
                    dbStore.Longitude = lcboStore.Longitude;
                }
                else
                {
                    Store newDbStore = new Store()
                    {
                        Address = lcboStore.AddressLine,
                        City = lcboStore.City,
                        Id = lcboStore.StoreNumber,
                        Latitude = lcboStore.Latitude,
                        Longitude = lcboStore.Longitude,
                        IsDeleted = false
                    };

                    newStoreCount++;
                    unitOfWork.Stores.Add(newDbStore);
                }
            }

            // Identify closed stores i.e. in the DB but not on the LCBO site
            int deletedStores = 0;
            foreach (int dbStoreId in dbStoreDictionary.Keys)
            {
                if (!lcboStoresDictionary.ContainsKey(dbStoreId))
                {
                    Store storeToDelete = dbStoreDictionary[dbStoreId];
                    unitOfWork.Stores.Remove(storeToDelete);
                    deletedStores++;
                }
            }

            signalrConnection.Broadcast(String.Format("Deleted {0} stores", deletedStores));
            unitOfWork.Commit();

            getProductsStopwatch.Stop();
            signalrConnection.Broadcast(String.Format("Found {0} new stores on the LCBO website", newStoreCount));
            result.AppendFormat("Found {0} new stores on the LCBO website\r\n", newStoreCount);
            signalrConnection.Broadcast(String.Format("Elapsed time in seconds: {0:0.00}", getProductsStopwatch.ElapsedMilliseconds / 1000.00));
            result.AppendFormat("Elapsed time in seconds: {0:0.00}\r\n", getProductsStopwatch.ElapsedMilliseconds / 1000.00);
            return result.ToString();
        }