Esempio n. 1
0
        private static void AddKeywords(IList <Keyword> keywords,
                                        EfContainer container, BiblioDbContext context)
        {
            // collect the keywords to be assigned, adding the missing ones
            List <EfKeywordContainer> requested = new List <EfKeywordContainer>();

            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 EfKeywordContainer
                {
                    Keyword   = efk,
                    Container = container
                });
            }

            // remove all the keywords which are no more requested
            if (container.KeywordContainers != null)
            {
                foreach (EfKeywordContainer kc in container.KeywordContainers)
                {
                    if (requested.All(r => r.KeywordId != kc.KeywordId))
                    {
                        context.KeywordContainers.Remove(kc);
                    }
                }
            }
            else
            {
                container.KeywordContainers = new List <EfKeywordContainer>();
            }

            // add all those which are not yet present
            foreach (EfKeywordContainer kc in requested)
            {
                if (container.KeywordContainers.All(
                        r => r.KeywordId != kc.KeywordId))
                {
                    container.KeywordContainers.Add(kc);
                }
            }
        }
Esempio n. 2
0
 /// <summary>
 /// Deletes the container.
 /// </summary>
 /// <param name="id">The identifier.</param>
 public void DeleteContainer(Guid id)
 {
     using (var db = GetContext())
     {
         EfContainer container = db.Containers.Find(id);
         if (container != null)
         {
             db.Containers.Remove(container);
             db.SaveChanges();
         }
     }
 }
Esempio n. 3
0
        /// <summary>
        /// Gets the work information corresponding to the specified container
        /// 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(EfContainer ef, BiblioDbContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (ef == null)
            {
                return(null);
            }
            WorkInfo info = new WorkInfo
            {
                IsContainer = true,
                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
            };

            // authors
            if (ef.AuthorContainers?.Count > 0)
            {
                info.Authors = (from aw in ef.AuthorContainers
                                select new WorkAuthor
                {
                    Id = aw.AuthorId,
                    First = aw.Author?.First,
                    Last = aw.Author?.Last,
                    Role = aw.Role,
                    Ordinal = aw.Ordinal
                }).ToList();
            }

            // keywords
            if (ef.KeywordContainers?.Count > 0)
            {
                info.Keywords = (from kw in ef.KeywordContainers
                                 select new Keyword
                {
                    Language = kw.Keyword?.Language,
                    Value = kw.Keyword?.Value
                }).ToList();
            }

            return(info);
        }
Esempio n. 4
0
        /// <summary>
        /// Adds or updates the specified container.
        /// Container type, 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.
        /// If new, <paramref name="container"/> ID gets updated; the key is
        /// always updated.
        /// </summary>
        /// <param name="container">The container.</param>
        /// <exception cref="ArgumentNullException">container</exception>
        public void AddContainer(Container container)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            using (var db = GetContext())
            {
                EfContainer ef = EfHelper.GetEfContainer(container, db);
                db.SaveChanges();
                container.Id  = ef.Id;
                container.Key = ef.Key;
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Gets the container by its ID.
 /// </summary>
 /// <param name="id">The container's identifier.</param>
 /// <returns>
 /// Container, or null if not found
 /// </returns>
 public Container GetContainer(Guid id)
 {
     using (var db = GetContext())
     {
         EfContainer container = db.Containers
                                 .AsNoTracking()
                                 .Include(w => w.Type)
                                 .Include(w => w.AuthorContainers)
                                 .ThenInclude(aw => aw.Author)
                                 .Include(w => w.KeywordContainers)
                                 .ThenInclude(kw => kw.Keyword)
                                 .FirstOrDefault(w => w.Id == id);
         return(EfHelper.GetContainer(container));
     }
 }
Esempio n. 6
0
        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);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Gets the container object corresponding to the specified container
        /// entity.
        /// </summary>
        /// <param name="ef">The entity or null.</param>
        /// <returns>The container or null.</returns>
        public static Container GetContainer(EfContainer ef)
        {
            if (ef == null)
            {
                return(null);
            }

            Container container = new Container
            {
                Id         = ef.Id,
                Key        = ef.Key,
                Type       = ef.Type?.Id,
                Title      = ef.Title,
                Language   = ef.Language,
                Edition    = ef.Edition,
                Publisher  = ef.Publisher,
                YearPub    = ef.YearPub,
                PlacePub   = ef.PlacePub,
                Location   = ef.Location,
                AccessDate = ef.AccessDate,
                Note       = ef.Note,
                Number     = ef.Number
            };

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

            // keywords
            if (ef.KeywordContainers?.Count > 0)
            {
                container.Keywords.AddRange(from ak in ef.KeywordContainers
                                            select GetKeyword(ak.Keyword));
            }

            return(container);
        }
Esempio n. 8
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);
        }