public void CreateLanguages()
        {
            char apostrophe                 = "'".ToCharArray()[0];
            Dictionary <string, int> ld     = new Dictionary <string, int>();
            List <string>            audios = new List <string>();
            List <string>            subts  = new List <string>();

            if (languagesRAW != null && languagesRAW.Trim().Length > 0) //Only create relations if the origin of the data if from Raw (WebService/AniDB)
            {
                string[] langs = languagesRAW.Split(apostrophe);
                foreach (string language in langs)
                {
                    string rlan = language.Trim().ToLower();
                    if (rlan.Length > 0)
                    {
                        if (!ld.ContainsKey(rlan))
                        {
                            Language lan = Repo.Instance.Language.GetByLanguageName(rlan) ?? Repo.Instance.Language.BeginAdd(new Language {
                                LanguageName = rlan
                            }).Commit();
                            ld.Add(rlan, lan.LanguageID);
                        }

                        audios.Add(rlan);
                    }
                }
            }

            if (subtitlesRAW != null && subtitlesRAW.Trim().Length > 0)
            {
                string[] subs = subtitlesRAW.Split(apostrophe);
                foreach (string language in subs)
                {
                    string rlan = language.Trim().ToLower();
                    if (rlan.Length > 0)
                    {
                        if (!ld.ContainsKey(rlan))
                        {
                            Language lan = Repo.Instance.Language.GetByLanguageName(rlan) ?? Repo.Instance.Language.BeginAdd(new Language {
                                LanguageName = rlan
                            }).Commit();
                            ld.Add(rlan, lan.LanguageID);
                        }
                        subts.Add(rlan);
                    }
                }
            }

            if (audios.Count > 0)
            {
                using (var upd = Repo.Instance.CrossRef_Languages_AniDB_File.BeginBatchUpdate(() => Repo.Instance.CrossRef_Languages_AniDB_File.GetByFileID(FileID), true))
                {
                    foreach (string str in audios)
                    {
                        int langid = ld[str];
                        CrossRef_Languages_AniDB_File cross = upd.FindOrCreate(a => a.LanguageID == langid);
                        cross.FileID     = FileID;
                        cross.LanguageID = langid;
                        upd.Update(cross);
                    }
                    upd.Commit();
                }
            }

            if (subts.Count > 0)
            {
                using (var upd = Repo.Instance.CrossRef_Subtitles_AniDB_File.BeginBatchUpdate(() => Repo.Instance.CrossRef_Subtitles_AniDB_File.GetByFileID(FileID), true))
                {
                    foreach (string str in subts)
                    {
                        int langid = ld[str];
                        CrossRef_Subtitles_AniDB_File cross = upd.FindOrCreate(a => a.LanguageID == langid);
                        cross.FileID     = FileID;
                        cross.LanguageID = langid;
                        upd.Update(cross);
                    }
                    upd.Commit();
                }
            }
        }
Esempio n. 2
0
        public void CreateLanguages()
        {
            char apostrophe = "'".ToCharArray()[0];

            if (languagesRAW != null) //Only create relations if the origin of the data if from Raw (WebService/AniDB)
            {
                if (languagesRAW.Trim().Length == 0)
                {
                    return;
                }
                // Delete old if changed

                List <CrossRef_Languages_AniDB_File> fileLanguages =
                    RepoFactory.CrossRef_Languages_AniDB_File.GetByFileID(this.FileID);
                foreach (CrossRef_Languages_AniDB_File fLan in fileLanguages)
                {
                    RepoFactory.CrossRef_Languages_AniDB_File.Delete(fLan.CrossRef_Languages_AniDB_FileID);
                }


                string[] langs = languagesRAW.Split(apostrophe);
                foreach (string language in langs)
                {
                    string rlan = language.Trim().ToLower();
                    if (rlan.Length > 0)
                    {
                        Language lan = RepoFactory.Language.GetByLanguageName(rlan);
                        if (lan == null)
                        {
                            lan = new Language();
                            lan.LanguageName = rlan;
                            RepoFactory.Language.Save(lan);
                        }
                        CrossRef_Languages_AniDB_File cross = new CrossRef_Languages_AniDB_File();
                        cross.LanguageID = lan.LanguageID;
                        cross.FileID     = FileID;
                        RepoFactory.CrossRef_Languages_AniDB_File.Save(cross);
                    }
                }
            }

            if (subtitlesRAW != null)
            {
                if (subtitlesRAW.Trim().Length == 0)
                {
                    return;
                }

                // Delete old if changed
                List <CrossRef_Subtitles_AniDB_File> fileSubtitles =
                    RepoFactory.CrossRef_Subtitles_AniDB_File.GetByFileID(this.FileID);

                foreach (CrossRef_Subtitles_AniDB_File fSub in fileSubtitles)
                {
                    RepoFactory.CrossRef_Subtitles_AniDB_File.Delete(fSub.CrossRef_Subtitles_AniDB_FileID);
                }

                string[] subs = subtitlesRAW.Split(apostrophe);
                foreach (string language in subs)
                {
                    string rlan = language.Trim().ToLower();
                    if (rlan.Length > 0)
                    {
                        Language lan = RepoFactory.Language.GetByLanguageName(rlan);
                        if (lan == null)
                        {
                            lan = new Language();
                            lan.LanguageName = rlan;
                            RepoFactory.Language.Save(lan);
                        }
                        CrossRef_Subtitles_AniDB_File cross = new CrossRef_Subtitles_AniDB_File();
                        cross.LanguageID = lan.LanguageID;
                        cross.FileID     = FileID;
                        RepoFactory.CrossRef_Subtitles_AniDB_File.Save(cross);
                    }
                }
            }
        }