/// <summary> /// Gets the entity keyword corresponding to the specified keyword. /// </summary> /// <param name="keyword">The keyword or null.</param> /// <param name="context">The context.</param> /// <returns>The entity or null.</returns> /// <exception cref="ArgumentNullException">context</exception> public static EfKeyword GetEfKeyword(Keyword keyword, BiblioDbContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (keyword == null) { return(null); } EfKeyword ef = context.Keywords.FirstOrDefault( k => k.Language == keyword.Language && k.Value == keyword.Value); if (ef == null) { ef = new EfKeyword { Language = keyword.Language, Value = keyword.Value, Valuex = StandardFilter.Apply(keyword.Value, true) }; context.Keywords.Add(ef); } return(ef); }
private static EfWorkType GetOrCreateWorkType(string id, string name, BiblioDbContext context) { // force a valid type if (id == null) { id = "-"; } if (name == null) { name = id; } // get it EfWorkType ef = context.WorkTypes.Find(id); if (ef == null) { ef = new EfWorkType { Id = id, Name = name }; context.WorkTypes.Add(ef); } else if (name != null) { ef.Name = name; } return(ef); }
/// <summary> /// Gets the author entity corresponding to the specified author. /// </summary> /// <param name="author">The author or null.</param> /// <param name="context">The context.</param> /// <returns>The entity or null.</returns> /// <exception cref="ArgumentNullException">context</exception> public static EfAuthor GetEfAuthor(Author author, BiblioDbContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (author == null) { return(null); } EfAuthor ef = author.Id != Guid.Empty ? context.Authors.Find(author.Id) : null; if (ef == null) { if (author.Last == null) { return(null); } ef = new EfAuthor(); context.Authors.Add(ef); } if (author.Last != null) { ef.First = author.First; ef.Last = author.Last; ef.Lastx = StandardFilter.Apply(author.Last, true); ef.Suffix = author.Suffix; } return(ef); }
/// <summary> /// Gets the work information corresponding to the specified work /// entity. /// </summary> /// <param name="ef">The entity or null.</param> /// <param name="context">The context.</param> /// <returns>The object or null.</returns> /// <exception cref="ArgumentNullException">context</exception> public static WorkInfo GetWorkInfo(EfWork ef, BiblioDbContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (ef == null) { return(null); } WorkInfo info = new WorkInfo { IsContainer = false, Id = ef.Id, Key = ef.Key, Type = ef.Type?.Name, Title = ef.Title, Language = ef.Language, Edition = ef.Edition, YearPub = ef.YearPub, PlacePub = ef.PlacePub, Number = ef.Number, FirstPage = ef.FirstPage, LastPage = ef.LastPage }; // authors if (ef.AuthorWorks?.Count > 0) { info.Authors = (from aw in ef.AuthorWorks select new WorkAuthor { Id = aw.AuthorId, First = aw.Author?.First, Last = aw.Author?.Last, Role = aw.Role, Ordinal = aw.Ordinal }).ToList(); } // keywords if (ef.KeywordWorks?.Count > 0) { info.Keywords = (from kw in ef.KeywordWorks select new Keyword { Language = kw.Keyword?.Language, Value = kw.Keyword?.Value }).ToList(); } // container if (ef.Container != null) { info.Container = GetWorkInfo(ef.Container, context); } return(info); }
private static void AddKeywords(IList <Keyword> keywords, EfWork work, BiblioDbContext context) { // collect the keywords to be assigned, adding the missing ones List <EfKeywordWork> requested = new List <EfKeywordWork>(); foreach (Keyword keyword in keywords) { // find the keyword by its content, as we have no ID EfKeyword efk = context.Keywords.FirstOrDefault(k => k.Value == keyword.Value && k.Language == keyword.Language); // if not found, add it if (efk == null) { efk = new EfKeyword { Language = keyword.Language, Value = keyword.Value, Valuex = StandardFilter.Apply(keyword.Value, true) }; context.Keywords.Add(efk); } requested.Add(new EfKeywordWork { Keyword = efk, Work = work }); } // remove all the keywords which are no more requested if (work.KeywordWorks != null) { foreach (EfKeywordWork kw in work.KeywordWorks) { if (requested.All(r => r.KeywordId != kw.KeywordId)) { context.KeywordWorks.Remove(kw); } } } else { work.KeywordWorks = new List <EfKeywordWork>(); } // add all those which are not yet present foreach (EfKeywordWork kw in requested) { if (work.KeywordWorks.All( r => r.KeywordId != kw.KeywordId)) { work.KeywordWorks.Add(kw); } } }
private static EfAuthor GetEfAuthorFor(WorkAuthor author, BiblioDbContext context) { // find the author EfAuthor efa = author.Id != Guid.Empty ? context.Authors.Find(author.Id) : null; // if not found, add a new author if (efa == null) { // if an existing author was required but was not found, // just ignore him (defensive) if (author.Last == null) { return(null); } // else we have a new author, add it efa = new EfAuthor { First = author.First, Last = author.Last, Suffix = author.Suffix }; author.Id = efa.Id; // update the received ID context.Authors.Add(efa); } else { // if found, supply data in the source author if empty if (author.Last == null) { author.First = efa.First; author.Last = efa.Last; author.Suffix = efa.Suffix; } // else update the target author else { efa.First = author.First; efa.Last = author.Last; efa.Suffix = author.Suffix; } } // update indexed last efa.Lastx = StandardFilter.Apply(efa.Last, true); return(efa); }
private static void AddAuthors(IList <WorkAuthor> authors, EfContainer container, BiblioDbContext context) { // collect the authors to be assigned, adding the missing ones List <EfAuthorContainer> requested = new List <EfAuthorContainer>(); foreach (WorkAuthor author in authors) { EfAuthor efa = GetEfAuthorFor(author, context); if (efa == null) { continue; } requested.Add(new EfAuthorContainer { Author = efa, Container = container }); } // for // remove all the no more requested authors if (container.AuthorContainers != null) { foreach (EfAuthorContainer ac in container.AuthorContainers) { if (requested.All(r => r.AuthorId != ac.AuthorId)) { context.AuthorContainers.Remove(ac); } } } else { container.AuthorContainers = new List <EfAuthorContainer>(); } // add all those which are not yet present foreach (EfAuthorContainer ac in requested) { if (container.AuthorContainers.All( r => r.AuthorId != ac.AuthorId)) { container.AuthorContainers.Add(ac); } } }
private static void AddAuthors(IList <WorkAuthor> authors, EfWork work, BiblioDbContext context) { // collect the authors to be assigned, adding the missing ones List <EfAuthorWork> requested = new List <EfAuthorWork>(); foreach (WorkAuthor author in authors) { EfAuthor efa = GetEfAuthorFor(author, context); if (efa == null) { continue; } requested.Add(new EfAuthorWork { Author = efa, Work = work }); } // for // remove all the no more requested authors if (work.AuthorWorks != null) { foreach (EfAuthorWork aw in work.AuthorWorks) { if (requested.All(r => r.AuthorId != aw.AuthorId)) { context.AuthorWorks.Remove(aw); } } } else { work.AuthorWorks = new List <EfAuthorWork>(); } // add all those which are not yet present foreach (EfAuthorWork aw in requested) { if (work.AuthorWorks.All( r => r.AuthorId != aw.AuthorId)) { work.AuthorWorks.Add(aw); } } }
private static char GetSuffixForKey(string key, BiblioDbContext context) { var existing = context.Works.FirstOrDefault(w => w.Key == key); if (existing != null) { Match m = Regex.Match(existing.Key, @"\d+([a-z])?$"); if (m.Success && m.Groups[1].Value.Length > 0) { char c = m.Groups[1].Value[0]; if (c < 'z') { c++; } return(c); } } return('\0'); }
/// <summary> /// Gets the work entity corresponding to the specified type. /// </summary> /// <param name="type">The type or null.</param> /// <param name="context">The context.</param> /// <returns>The entity or null.</returns> public static EfWorkType GetEfWorkType(WorkType type, BiblioDbContext context) { if (type == null) { return(null); } EfWorkType ef = type.Id != null ? context.WorkTypes.Find(type.Id) : null; if (ef == null) { ef = new EfWorkType { Id = type.Id }; context.WorkTypes.Add(ef); } ef.Name = type.Name; return(ef); }
/// <summary> /// Gets the work entity corresponding to the specified work. /// </summary> /// <param name="work">The work or null.</param> /// <param name="context">The context.</param> /// <returns>The entity or null.</returns> /// <exception cref="ArgumentNullException">context</exception> public static EfWork GetEfWork(Work work, BiblioDbContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (work == null) { return(null); } // find the work unless new EfWork ef = work.Id != Guid.Empty ? context.Works .Include(w => w.AuthorWorks) .Include(w => w.KeywordWorks) .FirstOrDefault(w => w.Id == work.Id) : null; // if new or not found, add it with a new ID if (ef == null) { ef = new EfWork(); context.Works.Add(ef); } // update the work ef.Type = GetOrCreateWorkType(work.Type, null, context); ef.Title = work.Title; ef.Titlex = StandardFilter.Apply(work.Title, true); ef.Language = work.Language; ef.Container = GetEfContainer(work.Container, context); ef.Edition = work.Edition; ef.Publisher = work.Publisher; ef.YearPub = work.YearPub; ef.PlacePub = work.PlacePub; ef.Location = work.Location; ef.AccessDate = work.AccessDate; ef.Number = work.Number; ef.FirstPage = work.FirstPage; ef.LastPage = work.LastPage; ef.Note = work.Note; // authors if (work.Authors?.Count > 0) { AddAuthors(work.Authors, ef, context); } // keywords if (work.Keywords?.Count > 0) { AddKeywords(work.Keywords, ef, context); } // key ef.Key = WorkKeyBuilder.PickKey(ef.Key, work); // add key suffix if required and possible if (ef.Key?.StartsWith(WorkKeyBuilder.MAN_KEY_PREFIX) != true) { char c = GetSuffixForKey(ef.Key, context); if (c != '\0') { ef.Key += c; } } return(ef); }
/// <summary> /// Gets the container entity corresponding to the specified container. /// </summary> /// <param name="container">The container or null.</param> /// <param name="context">The context.</param> /// <returns>The entity or null.</returns> /// <exception cref="ArgumentNullException">context</exception> public static EfContainer GetEfContainer(Container container, BiblioDbContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (container == null) { return(null); } // get the container unless it's new EfContainer ef = container.Id != Guid.Empty ? context.Containers .Include(c => c.AuthorContainers) .Include(c => c.KeywordContainers) .FirstOrDefault(c => c.Id == container.Id) : null; // if new or not found, add it with a new ID if (ef == null) { if (container.Title == null) { return(null); } ef = new EfContainer(); context.Containers.Add(ef); } // update the container unless empty if (container.Title != null) { ef.Type = GetOrCreateWorkType(container.Type, null, context); ef.Title = container.Title; ef.Titlex = StandardFilter.Apply(container.Title, true); ef.Language = container.Language; ef.Edition = container.Edition; ef.Publisher = container.Publisher; ef.YearPub = container.YearPub; ef.PlacePub = container.PlacePub; ef.Location = container.Location; ef.AccessDate = container.AccessDate; ef.Number = container.Number; ef.Note = container.Note; // authors if (container.Authors?.Count > 0) { AddAuthors(container.Authors, ef, context); } // keywords if (container.Keywords?.Count > 0) { AddKeywords(container.Keywords, ef, context); } // key ef.Key = WorkKeyBuilder.PickKey(ef.Key, container); // add key suffix if required and possible if (ef.Key?.StartsWith(WorkKeyBuilder.MAN_KEY_PREFIX) != true) { char c = GetSuffixForKey(ef.Key, context); if (c != '\0') { ef.Key += c; } } } return(ef); }