コード例 #1
0
ファイル: Import.cs プロジェクト: WilliamSlabbaert/Project-B3
        public static void import(string location)
        {
            UnitOfWork        uow = new UnitOfWork();
            ComicStripManager CM  = new ComicStripManager(uow);
            AuthorManager     AM  = new AuthorManager(uow);
            PublisherManager  PM  = new PublisherManager(uow);

            //Publisher , Author en comicStrip aan de databank toe voegen
            foreach (ComicStrip comicStrip in ReadJson(location))
            {
                try
                {
                    // Publisher toe voegen
                    if (!PM.Exist(comicStrip.Publisher, true))
                    {
                        comicStrip.SetPublisher(PM.Add(comicStrip.Publisher));
                    }
                    else
                    {
                        comicStrip.SetPublisher(PM.GetByName(comicStrip.Publisher.Name));
                    }

                    // Authors toe voegen
                    List <Author> tempAuthor = new List <Author>();
                    foreach (Author author in comicStrip.Authors)
                    {
                        if (AM.Exist(author, true))
                        {
                            tempAuthor.Add(AM.GetByName(author.Firstname, author.Surname));
                        }
                        else
                        {
                            tempAuthor.Add(AM.Add(author));
                        }
                    }
                    // Reeks toe voegen
                    if (!CM.ExistSerie(comicStrip.Serie, true))
                    {
                        comicStrip.SetSerie(CM.AddSerie(comicStrip.Serie));
                    }
                    else
                    {
                        comicStrip.SetSerie(CM.GetSerieByName(comicStrip.Serie.Name));
                    }
                    // Commiting Changes
                    comicStrip.SetAuthors(tempAuthor);
                    CM.Add(comicStrip);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            //get file dir
            List <string> split = location.Split("\\").ToList();

            split.RemoveAt(split.Count - 1);
            string newLocation = "";

            foreach (string s in split)
            {
                newLocation += s + "\\";
            }

            //get all bad comics
            List <Strip> rejectstrips = new List <Strip>();

            foreach (var key in Errors.Keys)
            {
                foreach (Strip strip in Errors[key])
                {
                    rejectstrips.Add(strip);
                }
            }

            //dump data
            string        newrawJson = JsonConvert.SerializeObject(rejectstrips);
            DirectoryInfo dir        = new DirectoryInfo(newLocation);

            File.WriteAllText(dir + "\\RejectDump.json", newrawJson);
        }