Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
 private static void PrepareKeywordFilter(KeywordFilter filter)
 {
     if (!string.IsNullOrEmpty(filter.Value))
     {
         filter.Value = StandardFilter.Apply(filter.Value, true);
     }
 }
Ejemplo n.º 4
0
 private static void PrepareAuthorFilter(AuthorFilter filter)
 {
     if (string.IsNullOrEmpty(filter.Last))
     {
         filter.Last = StandardFilter.Apply(filter.Last, true);
     }
 }
Ejemplo n.º 5
0
        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);
                }
            }
        }
Ejemplo n.º 6
0
        private void PrepareWorkFilter(WorkFilter filter)
        {
            if (!string.IsNullOrEmpty(filter.LastName))
            {
                filter.LastName = StandardFilter.Apply(filter.LastName, true);
            }

            if (!string.IsNullOrEmpty(filter.Title))
            {
                filter.Title = StandardFilter.Apply(filter.Title, true);
            }
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
0
        /// <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);
        }