Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Deletes the work with the specified ID.
 /// </summary>
 /// <param name="id">The work's identifier.</param>
 public void DeleteWork(Guid id)
 {
     using (var db = GetContext())
     {
         EfWork work = db.Works.Find(id);
         if (work != null)
         {
             db.Works.Remove(work);
             db.SaveChanges();
         }
     }
 }
Esempio n. 4
0
        /// <summary>
        /// Gets the work corresponding to the specified work entity.
        /// </summary>
        /// <param name="ef">The entity or null.</param>
        /// <returns>The work.</returns>
        public static Work GetWork(EfWork ef)
        {
            if (ef == null)
            {
                return(null);
            }

            Work work = new Work
            {
                Id         = ef.Id,
                Type       = ef.Type?.Name,
                Title      = ef.Title,
                Language   = ef.Language,
                Container  = GetContainer(ef.Container),
                Edition    = ef.Edition,
                Publisher  = ef.Publisher,
                YearPub    = ef.YearPub,
                PlacePub   = ef.PlacePub,
                Location   = ef.Location,
                AccessDate = ef.AccessDate,
                Number     = ef.Number,
                FirstPage  = ef.FirstPage,
                LastPage   = ef.LastPage,
                Key        = ef.Key,
                Note       = ef.Note,
            };

            // authors
            if (ef.AuthorWorks?.Count > 0)
            {
                work.Authors.AddRange(from ac in ef.AuthorWorks
                                      select new WorkAuthor
                {
                    Id      = ac.AuthorId,
                    Last    = ac.Author.Last,
                    First   = ac.Author.First,
                    Role    = ac.Role,
                    Ordinal = ac.Ordinal
                });
            }

            // keywords
            if (ef.KeywordWorks?.Count > 0)
            {
                work.Keywords.AddRange(from aw in ef.KeywordWorks
                                       select GetKeyword(aw.Keyword));
            }

            return(work);
        }
Esempio n. 5
0
        /// <summary>
        /// Adds or updates the specified work.
        /// Work type, container, authors, and keywords are stored too.
        /// As for authors, you can specify only the author's ID to assign
        /// the work to an existing author; otherwise, the author will be
        /// either added or updated as required. Keywords are added or
        /// updated as required. Type is added if not found, even this should
        /// not happen, as types are a predefined set. As for the container,
        /// you can specify only its ID to assign the work to it; otherwise,
        /// the container will be added or updated as required.
        /// If new, <paramref name="work"/> ID gets updated; the key is
        /// always updated.
        /// </summary>
        /// <param name="work">The work.</param>
        /// <exception cref="ArgumentNullException">work</exception>
        public void AddWork(Work work)
        {
            if (work == null)
            {
                throw new ArgumentNullException(nameof(work));
            }

            using (var db = GetContext())
            {
                EfWork ef = EfHelper.GetEfWork(work, db);
                db.SaveChanges();
                work.Id  = ef.Id;
                work.Key = ef.Key;
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Gets the work by its ID.
 /// </summary>
 /// <param name="id">The work's identifier.</param>
 /// <returns>
 /// Work, or null if not found
 /// </returns>
 public Work GetWork(Guid id)
 {
     using (var db = GetContext())
     {
         EfWork work = db.Works
                       .AsNoTracking()
                       .Include(w => w.Type)
                       .Include(w => w.Container)
                       .Include(w => w.AuthorWorks)
                       .ThenInclude(aw => aw.Author)
                       .Include(w => w.KeywordWorks)
                       .ThenInclude(kw => kw.Keyword)
                       .FirstOrDefault(w => w.Id == id);
         return(EfHelper.GetWork(work));
     }
 }
Esempio n. 7
0
        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);
                }
            }
        }
Esempio 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);
        }