/// <summary> Adds a single item to this title's collection of child items </summary>
 /// <param name="NewItem"> New single item information to add to this title </param>
 public void Add_Item(Single_Item NewItem)
 {
     items[NewItem.VID] = NewItem;
     if (String.IsNullOrEmpty(singleVid))
     {
         singleVid = NewItem.VID;
     }
 }
 /// <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="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> 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 = SobekCM_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> 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> 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> Populates the item lookup object with all the valid bibids and vids in the system </summary>
        /// <param name="Include_Private"> 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 Include_Private, Item_Lookup_Object ItemLookupObject, Custom_Tracer Tracer)
        {
            if (Tracer != null)
            {
                Tracer.Add_Trace("SobekCM_Database.Populate_Item_Lookup_Object", String.Empty);
            }

            try
            {

                // Create the connection
                using (SqlConnection connect = new SqlConnection(connectionString + "Connection Timeout=45"))
                {

                    SqlCommand executeCommand = new SqlCommand("SobekCM_Item_List_Web", connect)
                                                    {CommandTimeout = 45, CommandType = CommandType.StoredProcedure};
                    executeCommand.Parameters.AddWithValue("@include_private", Include_Private);

                    // Create the data reader
                    connect.Open();
                    using (SqlDataReader reader = executeCommand.ExecuteReader())
                    {
                        // 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);
                        }
                        reader.Close();
                    }
                    connect.Close();
                }

                // Return the first table from the returned dataset
                return true;
            }
            catch (Exception ee)
            {
                lastException = ee;
                if (Tracer != null)
                {
                    Tracer.Add_Trace("SobekCM_Database.Populate_Item_Lookup_Object", "Exception caught during database work", Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("SobekCM_Database.Populate_Item_Lookup_Object", ee.Message, Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("SobekCM_Database.Populate_Item_Lookup_Object", ee.StackTrace, Custom_Trace_Type_Enum.Error);
                }
                return false;
            }
        }
        /// <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 = SobekCM_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> Adds a single item to this title's collection of child items </summary>
 /// <param name="NewItem"> New single item information to add to this title </param>
 public void Add_Item(Single_Item NewItem)
 {
     items[NewItem.VID] = NewItem;
     if (String.IsNullOrEmpty(singleVid))
         singleVid = NewItem.VID;
 }