public static void GenerateXmlReport(string filePath, StoreContext db)
        {
            var manufacturers = db.Manufacturers.
                                Join(db.Laptops,
                                m => m.Id, l => l.ManufacturerId,
                                (m, l) => new
                                {
                                    Manufacturer = m.Name,
                                    Model = l.Model,
                                    Class = l.Class.Name
                                }).ToList();

            var doc = new XDocument(new XElement(
                "manufacturers",
                from manufacturer in manufacturers
                select new XElement(
                    "manufacturer",
                new XElement("manufacturer", manufacturer.Manufacturer),
                new XElement("model", manufacturer.Model),
                new XElement("class", manufacturer.Class))));

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            string fileName = AddUniqueFilenameSuffix(@"\Manufacturers");
            var fullPath = filePath + fileName;
            doc.Save(fullPath);
        }
        public void ExportSalesToMySql()
        {
            var db = new StoreContext();
            var reports = db.Sales.ToList();

            foreach (var sale in reports)
            {
                Console.WriteLine(sale.Town.Name);
            }

            Console.WriteLine(reports.Count);
        }
        public static void Seed(MySqlData mySqlData)
        {
            var db = new StoreContext();

            var reports = db.Sales
                .Select(s => new SalesReport
                {
                    Town = s.Town.Name,
                    Date = s.Date,
                    LaptopId = s.LaptopId,
                    Quantity = s.Quantity,
                    Sum = s.Laptop.Price * s.Quantity
                })
                .ToList();

            Console.WriteLine("Seeding of Sales entries into MySql Db initialized.");
            //// mySqlData.SalesReport.DeleteAllReports();
            mySqlData.SalesReport.AddMany(reports);
            mySqlData.SalesReport.SaveChanges();
            Console.WriteLine("Seeding of Sales entries completed!");
        }
        public void Generate()
        {
            var seeder = new Seeder();
            var db = new StoreContext();
            var laptops = db.Laptops.ToList();
            var productsCount = laptops.Count;

            for (int report = 0; report < NumberOfReports; report++)
            {
                if (report % 10 == 0)
                {
                    Console.Write("...");
                }

                var date = new DateTime(this.random.Next(2002, 2016), this.random.Next(1, 13), this.random.Next(1, 29));
                var productsToBeAdded = new List<SalesInfo>();

                productsToBeAdded.AddRange(laptops.Select((x) => new SalesInfo(x.Id, x.Price, this.random.Next(1, 10))));

               seeder.Seed(this.towns[this.random.Next(0, this.towns.Length)], date, "Sales/", productsToBeAdded);
            }
        }
        public void GenerateComputersReports(string filePath, string fileName, StoreContext db)
        {
            fileName = AddUniqueFilenameSuffix(fileName);

            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

            var document = new Document(PageSize.A4, 50, 50, 25, 25);
            var output = new FileStream(filePath + fileName, FileMode.Create, FileAccess.Write);
            var writer = PdfWriter.GetInstance(document, output);  // never used opens process only

            PdfPTable table = this.CreateComputerReportsTable();
            this.AddComputerReportsTableHeader(table);
            this.AddComputerReportsTableColumns(table);
            this.FillComputerReportsTableData(table, db);

            document.Open();
            document.Add(table);
            document.Close();
        }
        public static void Main()
        {
            var db = new StoreContext();
            var databaseContext = new ComputersContextMongoDb();
            var mongoDb = new ComputerDataMongoDb(databaseContext);
            var dataForMongoDb = new List<Manufacturer>();

            var xmlManufacurersToImportWithGeneric = XmlDataImporterGeneric.ImportXmlData<Manufacturer>(@"..\..\LaptopManufacturers.xml");  // TODO make table with Unique values + remove ID?

            Console.WriteLine("Adding Manufacturers....");
            foreach (var manufacturer in xmlManufacurersToImportWithGeneric)
            {
                db.Manufacturers.Add(manufacturer);
                Console.WriteLine("{0} added successfully", manufacturer.Name);
            }

            var xmlManufacurersToImportWithSimple = XmlDataImporterSimple.ImportXmlData(@"..\..\LaptopManufacturers.xml");
            foreach (var manufacturer in xmlManufacurersToImportWithSimple)
            {
                db.Manufacturers.Add(manufacturer);
                dataForMongoDb.Add(manufacturer);

                Console.WriteLine("{0} added successfully", manufacturer.Name);
            }

            db.SaveChanges();
            Console.WriteLine("XML import to SQL complete!");

            mongoDb.Save("manufacturers", dataForMongoDb);
            Console.WriteLine("XML import to MongoDb complete!");

            Console.WriteLine();
            Console.WriteLine("XML Exporting from database.....");
            Console.WriteLine();
            XmlFileExporter.GenerateXmlReport(ExportDirectory, db);
            Console.WriteLine("Report successfully exported to {0}", ExportDirectory);
        }
        public void ExportToJson(string exportFolderPath)
        {
            var db = new StoreContext();
            var reports = db.Sales.ToList();

            if (!Directory.Exists(exportFolderPath))
            {
                Directory.CreateDirectory(exportFolderPath);
            }

            var groupedReports = reports
                .GroupBy(r => r.LaptopId)
                .ToList();

            foreach (var report in groupedReports)
            {
                var reportTemplate = report.First();

                var currentReport =
                    JObject.FromObject(
                        new
                        {
                            Id = reportTemplate.LaptopId,
                            reportTemplate.Laptop.Model,
                            Manufacturer = reportTemplate.Laptop.Manufacturer.Name,
                            Sold = report.Sum(r => r.Quantity),
                            Income = reportTemplate.Laptop.Price * report.Sum(r => r.Quantity)
                        });

                using (var writer = new StreamWriter(exportFolderPath + report.First().LaptopId + ".json", false))
                {
                    writer.Write(JsonConvert.SerializeObject(currentReport, Formatting.Indented));
                }

                Console.WriteLine("Created " + reportTemplate.LaptopId + ".json report");
            }
        }
        public void ExportToJson(string exportFolderPath)
        {
            var db = new StoreContext();
            var reports = db.Sales.ToList();

            if (!Directory.Exists(exportFolderPath))
            {
                Directory.CreateDirectory(exportFolderPath);
            }
            var counter = 0;

            foreach (var report in reports)
            {
                ++counter;
                if (counter % 5 == 0)
                {
                    Console.Write("...");
                }

                var currentReport =
                    JObject.FromObject(
                        new
                            {
                                Town = report.Town.Name,
                                Date = report.Date,
                                LaptopId = report.LaptopId,
                                Quantity = report.Quantity,
                                Sum = report.Laptop.Price * report.Quantity
                            });

                using (var writer = new StreamWriter(exportFolderPath + "/" + report.Id + ".json", false))
                {
                    writer.Write(JsonConvert.SerializeObject(currentReport, Formatting.Indented));
                }
            }
        }
        public static void Main()
        {
            var seedGenerator = new SeedGenerator();
            var seeder = new Seeder();
            seedGenerator.Generate();
            var zipPath = "sales-reports.zip";
            seeder.SendToZip("Sales/", zipPath);
            seeder.Unzip(zipPath, "Export/");

            var root = new DirectoryInfo(@"Export\sales-reports");
            var db = new StoreContext();
            var towns = new Dictionary<string, Town>();

            string pattern = "yyyy MMM dd";
            foreach (var directory in root.GetDirectories())
            {
                DateTime parsedDate;
                DateTime.TryParseExact(directory.Name, pattern, null, DateTimeStyles.None, out parsedDate);

                foreach (var file in directory.GetFiles())
                {
                    var townName = Path.GetFileNameWithoutExtension(file.FullName);
                    if (!towns.ContainsKey(townName))
                    {
                        if (db.Towns.Any(x => x.Name == townName))
                        {
                            towns.Add(townName, db.Towns.First(x => x.Name == townName));
                        }
                        else
                        {
                            towns.Add(townName, new Town { Name = townName });
                        }
                    }

                    using (Spreadsheet document = new Spreadsheet())
                    {
                        var row = 1;

                        document.LoadFromFile(file.FullName);
                        Worksheet worksheet = document.Workbook.Worksheets.ByName("SalesReport");

                        while (!worksheet.Cell(row, 0).Merged)
                        {
                            db.Sales.Add(new Sales
                                             {
                                                 Date = parsedDate,
                                                 Town = towns[townName],
                                                 LaptopId = worksheet.Cell(row, 0).ValueAsInteger,
                                                 Quantity = worksheet.Cell(row, 2).ValueAsInteger
                                             });

                            ++row;
                        }
                    }
                }
            }

            db.SaveChanges();

            // document.LoadFromFile(@"Export\sales-reports\2002 Feb 27\Bourgas.xls");
            // Worksheet worksheet = document.Workbook.Worksheets.ByName("SalesReport");
            // Console.WriteLine(worksheet.Cell(4, 0).Merged);
        }
        private void FillComputerReportsTableData(PdfPTable table, StoreContext db)
        {
            var computersReports = db.Laptops
                .Select(c =>
                    new
                    {
                        ManufacturerColumnHeader = c.Manufacturer.Name,
                        ModelColumnHeader = c.Model,
                        PriceColumnHeader = c.Price,
                        ClassColumnHeader = c.Class.Name
                    })
                .ToList();

            foreach (var computer in computersReports)
            {
                table.AddCell(computer.ManufacturerColumnHeader);
                table.AddCell(computer.ModelColumnHeader);
                table.AddCell(computer.PriceColumnHeader + " $");
                table.AddCell(computer.ClassColumnHeader);
            }
        }