Exemple #1
0
        public string GetRentalPdf(SearchFilters searchFilters)
        {
            DateTime sDate = (DateTime)searchFilters.PuDate;
            DateTime eDate = searchFilters.DoDate;

            Rental s = new Rental(Const.Locations[searchFilters.Location].Rental);

            s.SetTime(searchFilters.PuTime.Hours, searchFilters.PuTime.Minutes, searchFilters.DoTime.Hours, searchFilters.DoTime.Minutes);
            s.InitDate(sDate);
            int numOfIterations = (eDate - sDate).Days;

            List <string> links     = s.GetGeneratedLinksByDate(sDate, eDate);
            List <JOffer> minOffers = new List <JOffer>();

            Dictionary <string, Dictionary <string, JOffer> > offerMap = new Dictionary <string, Dictionary <string, JOffer> >();

            for (int i = 0; i < links.Count; i++)
            {
                offerMap.Add(links[i], new Dictionary <string, JOffer>());
            }

            List <Thread> threads = new List <Thread>();

            //--- Start all threads
            for (int index = 0; index < links.Count; index++)
            {
                Thread thread = new Thread(() =>
                {
                    JSourceReader reader = new JSourceReader();
                    offerMap[Thread.CurrentThread.Name == null ? links.ElementAt(0) : Thread.CurrentThread.Name] =
                        reader.GetMap(reader.ExtractOffers(reader.GetResultGroup(Thread.CurrentThread.Name)));
                });
                thread.Name = links.ElementAt(index);
                threads.Add(thread);
                thread.Start();
            }

            //check if thread has done
            Boolean allCompleted = false;

            while (!allCompleted)
            {
                int completed = links.Count;
                for (int i = 0; i < links.Count; i++)
                {
                    if (!threads.ElementAt(i).IsAlive)
                    {
                        --completed;
                    }
                    else
                    {
                        Thread.Sleep(300);
                        break;
                    }
                }
                if (completed == 0)
                {
                    break;
                }
            }
            return(CreatePdf(s, offerMap));
        }
Exemple #2
0
        public string GetRentalExcel(SearchFilters searchFilters)
        {
            DateTime sDate = (DateTime)searchFilters.PuDate;
            DateTime eDate = searchFilters.DoDate;

            Rental s = new Rental(Const.Locations[searchFilters.Location].Rental);

            s.SetTime(searchFilters.PuTime.Hours, searchFilters.PuTime.Minutes, searchFilters.DoTime.Hours, searchFilters.DoTime.Minutes);
            s.InitDate(sDate);

            int numOfIterations = (eDate - sDate).Days;

            List <string> links     = s.GetGeneratedLinksByDate(sDate, eDate);
            List <JOffer> minOffers = new List <JOffer>();

            Dictionary <string, Dictionary <string, JOffer> > offerMap = new Dictionary <string, Dictionary <string, JOffer> >();

            for (int i = 0; i < links.Count; i++)
            {
                offerMap.Add(links[i], new Dictionary <string, JOffer>());
            }

            List <Thread> threads = new List <Thread>();

            //--- Start all threads
            for (int index = 0; index < links.Count; index++)
            {
                Thread thread = new Thread(() =>
                {
                    JSourceReader reader = new JSourceReader();
                    offerMap[Thread.CurrentThread.Name == null ? links.ElementAt(0) : Thread.CurrentThread.Name] =
                        reader.GetMap(reader.ExtractOffers(reader.GetResultGroup(Thread.CurrentThread.Name)));
                });
                thread.Name = links.ElementAt(index);
                threads.Add(thread);
                thread.Start();
            }

            //check if thread has done
            Boolean allCompleted = false;

            while (!allCompleted)
            {
                int completed = links.Count;
                for (int i = 0; i < links.Count; i++)
                {
                    if (!threads.ElementAt(i).IsAlive)
                    {
                        --completed;
                    }
                    else
                    {
                        Thread.Sleep(300);
                        break;
                    }
                }
                if (completed == 0)
                {
                    break;
                }
            }
            FileInfo template = new FileInfo(Server.MapPath(@"\Content\ExcelPackageTemplate.xlsx"));
            string   filename = @"\excel\" + s.GetTitle() + s.GetPuMonth() + "-" + s.GetPuDay() + s.GetCity() + ".xlsx";
            FileInfo newFile  = new FileInfo(Server.MapPath(filename));

            using (ExcelPackage excelPackage = new ExcelPackage(newFile, template))
            {
                // Getting the complete workbook...
                ExcelWorkbook myWorkbook = excelPackage.Workbook;

                // Getting the worksheet by its name...
                ExcelWorksheet myWorksheet = myWorkbook.Worksheets["Rates"];
                int            rowNum      = 2;
                DateTime       doDate      = new DateTime(Convert.ToInt32(s.GetPuYear()), Convert.ToInt32(s.GetPuMonth()), Convert.ToInt32(s.GetPuDay()));
                foreach (string link in links)
                {
                    Dictionary <string, JOffer> map = offerMap[link];
                    List <JOffer> offers            = new List <JOffer>();
                    if (map.Count > 0)
                    {
                        foreach (Category item in Const.categories)
                        {
                            if (map.ContainsKey(item.Name))
                            {
                                if (map[item.Name] != null)
                                {
                                    map[item.Name].SetSiteName(link);
                                    offers.Add(map[item.Name]);
                                }
                            }
                            else
                            {
                                offers.Add(new JOffer());
                            }
                        }
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("Map count 0");
                    }

                    myWorksheet.Cells[rowNum, 1].Value = s.GetPuMonth() + "-" + s.GetPuDay() + "/" + doDate.AddDays(rowNum - 1).Day + "\n" + (rowNum - 1);
                    for (int i = 0; i < offers.Count; i++)
                    {
                        myWorksheet.Cells[rowNum, i + 2].Value = offers.ElementAt(i).GetOffer();
                        myWorksheet.Row(rowNum).Height         = 35;
                    }
                    ++rowNum;
                }
                excelPackage.Save();// Saving the change...
                return(filename);
            }
        }