/// <summary> Adds an single item ( as a <see cref="Single_Item"/> object) to the collections of items </summary>
 /// <param name="Item"> Single digital resource to add to the collection of items </param>
 /// <param name="BibID"> Bibliographic identifier for the title this volume belongs to </param>
 public void Add_Item(Single_Item Item, string BibID )
 {
     if (titleLookupByBib.ContainsKey(BibID))
     {
         titleLookupByBib[BibID].Add_Item(Item);
     }
     else
     {
         Multiple_Volume_Item newTitle = new Multiple_Volume_Item(BibID);
         newTitle.Add_Item(Item);
         Add_Title(newTitle);
     }
 }
        /// <summary> Adds an single item ( as a <see cref="SobekCM.Resource_Object.SobekCM_Item"/> object) to the collections of items </summary>
        /// <param name="Item"> Single digital resource to add to the collection of items </param>
        /// <param name="check_for_multiples"> Flag indicates whether to perform a multiple-check to see if volumes already exist for this title / item group </param>
        public void Add_SobekCM_Item(SobekCM_Item Item, bool check_for_multiples)
        {
            // Create this item
            Single_Item newItem = new Single_Item(Item.VID, Item.Behaviors.IP_Restriction_Membership, Item.Bib_Info.Main_Title.ToString());

            // Add this to the existing title, or add a new one
            string bibId = Item.BibID;
            if (titleLookupByBib.ContainsKey(bibId))
            {
                titleLookupByBib[bibId].Add_Item(newItem);
            }
            else
            {
                Multiple_Volume_Item newTitle = new Multiple_Volume_Item(bibId);
                newTitle.Add_Item(newItem);
                Add_Title(newTitle);
            }
        }
        /// <summary> Add a new title info object to this collection for searching </summary>
        /// <param name="BibID"> Bibliographic identifier for this title </param>
        /// <param name="ThumbnailUrl">URL for a custom thumbnail ( or null )</param>
        /// <param name="FlagByte">Byte contains a number of title-specific flags</param>
        /// <param name="LastFour"> Last four digits of the bibid, as a small integer </param>
        /// <param name="GroupTitle"> Group title </param>
        public void Add_Title(string BibID, string ThumbnailUrl, byte FlagByte, short LastFour, string GroupTitle)
        {
            // Create the multi-volume object
            Multiple_Volume_Item titleItem = new Multiple_Volume_Item
            {
                CustomThumbnail = ThumbnailUrl,
                FlagByte        = FlagByte,
                GroupTitle      = GroupTitle
            };

            // Add this to the search mechanism
            string short_bibid = (BibID.Length > 6) ? BibID.Substring(0, 6) : BibID;

            if (!lookupDictionary.ContainsKey(short_bibid))
            {
                Dictionary <short, Multiple_Volume_Item> innerDictionary = new Dictionary <short, Multiple_Volume_Item>();
                lookupDictionary[short_bibid] = innerDictionary;
                innerDictionary[LastFour]     = titleItem;
            }
            else
            {
                lookupDictionary[short_bibid][LastFour] = titleItem;
            }
        }
 /// <summary> Adds a new title to the lookup tables </summary>
 /// <param name="newTitle"> New title object </param>
 public void Add_Title(Multiple_Volume_Item newTitle)
 {
     titleLookupByBib[newTitle.BibID] = newTitle;
 }
        /// <summary> Gets a <see cref="Single_Item"/> object from the collection, by Bib ID and VID </summary>
        /// <param name="BibID"> Bibliographic identifier for the title / item group </param>
        /// <param name="VID"> Volume identifier for the individual volume within the title </param>
        /// <param name="Tracer"> Trace object keeps a list of each method executed and important milestones in rendering </param>
        /// <returns> Basic information about this item as a <see cref="Single_Item"/> object. </returns>
        public Single_Item Item_By_Bib_VID(string BibID, string VID, Custom_Tracer Tracer)
        {
            // Try to look this up in the database
            lock (thisLock)
            {
                if (titleLookupByBib.ContainsKey(BibID))
                {
                    if (titleLookupByBib[BibID].Contains_VID(VID))
                        return titleLookupByBib[BibID][VID];
                }

                // Try to pull this from the database
                DataRow itemRow = Engine_Database.Get_Item_Information(BibID, VID, Tracer);

                if (itemRow != null)
                {
                    // Get a reference to the item table first
                    DataTable itemTable = itemRow.Table;

                    // Get references to the datacolumn next
                    DataColumn vidColumn = itemTable.Columns["VID"];
                    DataColumn restrictionColumn = itemTable.Columns["IP_Restriction_Mask"];
                    DataColumn titleColumn = itemTable.Columns["Title"];

                    // Create this item object
                    Single_Item newItem = new Single_Item(itemRow[vidColumn].ToString(), Convert.ToInt16(itemRow[restrictionColumn]), itemRow[titleColumn].ToString());

                    // Add this to the existing title, or add a new one
                    if (titleLookupByBib.ContainsKey(BibID))
                    {
                        titleLookupByBib[BibID].Add_Item(newItem);
                    }
                    else
                    {
                        Multiple_Volume_Item newTitle = new Multiple_Volume_Item(BibID);
                        newTitle.Add_Item(newItem);
                        Add_Title(newTitle);
                    }

                    // Return the newly built item as well
                    return newItem;
                }

                return null;
            }
        }
        /// <summary> Populates the item lookup object with all the valid bibids and vids in the system </summary>
        /// <param name="IncludePrivate"> Flag indicates whether to include private items in this list </param>
        /// <param name="ItemLookupObject"> Item lookup object to directly populate from the database </param>
        /// <param name="Tracer"> Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <returns> TRUE if successful, otherwise FALSE </returns>
        /// <remarks> This calls the 'SobekCM_Item_List_Web' stored procedure </remarks> 
        public static bool Populate_Item_Lookup_Object(bool IncludePrivate, Item_Lookup_Object ItemLookupObject, Custom_Tracer Tracer)
        {
            if (Tracer != null)
            {
                Tracer.Add_Trace("Engine_Database.Populate_Item_Lookup_Object", String.Empty);
            }

            try
            {
                // Create the parameter list
                EalDbParameter[] parameters = new EalDbParameter[1];
                parameters[0] = new EalDbParameter("@include_private", IncludePrivate);

                // Get the data reader (wrapper)
                EalDbReaderWrapper readerWrapper = EalDbAccess.ExecuteDataReader(DatabaseType, Connection_String + "Connection Timeout=45", CommandType.StoredProcedure, "SobekCM_Item_List", parameters);

                // Pull out the database reader
                DbDataReader reader = readerWrapper.Reader;

                // Clear existing volumes
                ItemLookupObject.Clear();
                ItemLookupObject.Last_Updated = DateTime.Now;

                string currentBibid = String.Empty;
                Multiple_Volume_Item currentVolume = null;
                while (reader.Read())
                {
                    // Grab the values out
                    string newBib = reader.GetString(0);
                    string newVid = reader.GetString(1);
                    short newMask = reader.GetInt16(2);
                    string title = reader.GetString(3);

                    // Create a new multiple volume object?
                    if (newBib != currentBibid)
                    {
                        currentBibid = newBib;
                        currentVolume = new Multiple_Volume_Item(newBib);
                        ItemLookupObject.Add_Title(currentVolume);
                    }

                    // Add this volume
                    Single_Item newItem = new Single_Item(newVid, newMask, title);
                    if (currentVolume != null) currentVolume.Add_Item(newItem);
                }

                // Close the reader (which also closes the connection)
                readerWrapper.Close();

                // Return the first table from the returned dataset
                return true;
            }
            catch (Exception ee)
            {
                Last_Exception = ee;
                if (Tracer != null)
                {
                    Tracer.Add_Trace("Engine_Database.Populate_Item_Lookup_Object", "Exception caught during database work", Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("Engine_Database.Populate_Item_Lookup_Object", ee.Message, Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("Engine_Database.Populate_Item_Lookup_Object", ee.StackTrace, Custom_Trace_Type_Enum.Error);
                }
                return false;
            }
        }