public void PickKey_NewNullVsNonManual_New()
        {
            Work work = GetSampleWork();

            string key = WorkKeyBuilder.PickKey("old", work);

            Assert.Equal("Doe 2020", key);
        }
        public void PickKey_NewNullVsManual_Old()
        {
            Work work = GetSampleWork();

            string key = WorkKeyBuilder.PickKey("!old", work);

            Assert.Equal("!old", key);
        }
        public void PickKey_NewManual_New()
        {
            Work work = GetSampleWork();

            work.Key = "!new";

            string key = WorkKeyBuilder.PickKey("old", work);

            Assert.Equal("!new", key);
        }
Exemple #4
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);
        }
Exemple #5
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);
        }