/// <summary> Adds information about a child item under this title result </summary>
 /// <param name="Item_Result"> Display and identification information for a single item within this title </param>
 internal void Add_Item_Result(Database_Item_Result Item_Result)
 {
     itemList.Add(Item_Result);
 }
 /// <summary> Adds information about a child item under this title result </summary>
 /// <param name="Item_Result"> Display and identification information for a single item within this title </param>
 internal void Add_Item_Result(Database_Item_Result Item_Result)
 {
     itemList.Add(Item_Result);
 }
        private static List<iSearch_Title_Result> DataReader_To_Simple_Result_List2(SqlDataReader Reader, List<string> Metadata_Field_Names)
        {
            // Create return list
            List<iSearch_Title_Result> returnValue = new List<iSearch_Title_Result>();

            // Create some lists used during the construction
            Dictionary<int, Database_Title_Result> titleLookupByRowNumber = new Dictionary<int, Database_Title_Result>();
            Dictionary<int, Database_Item_Result> itemLookupByItemID = new Dictionary<int, Database_Item_Result>();
            Dictionary<int, int> rowNumberLookupByItemID = new Dictionary<int, int>();

            // May have not values returned
            if (Reader.FieldCount < 5)
                return null;

            // Get all the main title values first
            int minimumRownumber = -1;
            while (Reader.Read())
            {
                // Create new database title object for this
                Database_Title_Result result = new Database_Title_Result
                {
                    RowNumber = Reader.GetInt32(0),
                    BibID = Reader.GetString(1),
                    GroupTitle = Reader.GetString(2),
                    OPAC_Number = Reader.GetInt32(3),
                    OCLC_Number = Reader.GetInt64(4),
                    GroupThumbnail = Reader.GetString(5),
                    MaterialType = Reader.GetString(6),
                    Primary_Identifier_Type = Reader.GetString(7),
                    Primary_Identifier = Reader.GetString(8)
                };

                titleLookupByRowNumber.Add(result.RowNumber, result);

                if (minimumRownumber == -1)
                {
                    minimumRownumber = result.RowNumber;
                }
            }

            // Move to the item system-required information table
            Reader.NextResult();

            // If there were no titles, then there are no results
            if (titleLookupByRowNumber.Count == 0)
                return returnValue;

            // Step through all the item rows, build the item, and add to the title
            Database_Title_Result titleResult = titleLookupByRowNumber[minimumRownumber];
            returnValue.Add(titleResult);
            int lastRownumber = titleResult.RowNumber;
            while (Reader.Read())
            {
                // Ensure this is the right title for this item
                int thisRownumber = Reader.GetInt32(0);
                if (thisRownumber != lastRownumber)
                {
                    titleResult = titleLookupByRowNumber[thisRownumber];
                    lastRownumber = thisRownumber;

                    // Add this title to the list
                    returnValue.Add(titleResult);
                }

                // Create new database item object for this
                Database_Item_Result result = new Database_Item_Result
                {
                    ItemID = Reader.GetInt32(1),
                    VID = Reader.GetString(2),
                    Title = Reader.GetString(3),
                    IP_Restriction_Mask = Reader.GetInt16(4),
                    MainThumbnail = Reader.GetString(5),
                    Level1_Index = (short)Reader.GetInt32(6),
                    Level1_Text = Reader.GetString(7),
                    Level2_Index = (short)Reader.GetInt32(8),
                    Level2_Text = Reader.GetString(9),
                    Level3_Index = (short)Reader.GetInt32(10),
                    Level3_Text = Reader.GetString(11),
                    PubDate = Reader.GetString(12),
                    PageCount = Reader.GetInt32(13),
                    Link = Reader.GetString(14),
                    Spatial_KML = Reader.GetString(15),
                    COinS_OpenURL = Reader.GetString(16)
                };

                // Save to the hash lookup for adding display metadata
                itemLookupByItemID[result.ItemID] = result;
                rowNumberLookupByItemID[result.ItemID] = thisRownumber;

                // Add this to the title object
                titleResult.Add_Item_Result(result);
            }

            // Move to the item aggregation-configured display information table
            Reader.NextResult();

            // Set some values for checking for uniformity of values
            const int ITEMS_TO_CHECK_IN_EACH_TITLE = 20;
            bool first_item_analyzed = true;
            List<bool> checking_fields = new List<bool>();
            int display_fields_count = 0;
            int itemcount = 0;
            int lastRowNumber = -1;
            while (Reader.Read())
            {
                // Get the item id and then work back to the local title id
                int itemId = Reader.GetInt32(0);
                int rowNumber = rowNumberLookupByItemID[itemId];

                // If this is the very first item analyzed, need to do some work first
                if (first_item_analyzed)
                {
                    // Save the number of display fields
                    display_fields_count = Reader.FieldCount - 1;

                    // Add a boolean for each display field
                    for (int i = 0; i < display_fields_count; i++)
                    {
                        // Add the default boolean value here
                        checking_fields.Add(true);

                        // Save the metadata label
                        Metadata_Field_Names.Add(Reader.GetName(i + 1));
                    }

                    // Done with the first row analysis, so ensure it does not repeat
                    first_item_analyzed = false;
                }

                // Is this is the start of a new title row?
                if (lastRowNumber != rowNumber)
                {
                    // Get this title object
                    titleResult = titleLookupByRowNumber[rowNumber];

                    // Set items analyzed for this title to zero
                    itemcount = 0;

                    // Back to checking each metadata field since this is a new title
                    for (int i = 0; i < display_fields_count; i++)
                        checking_fields[i] = true;

                    // Save this row numbe as the last row number analyzed
                    lastRowNumber = rowNumber;
                }

                if (itemcount == 0)
                {
                    // Set all the initial display values (at the title level) from
                    // this item's display information
                    titleResult.Metadata_Display_Values = new string[display_fields_count];
                    for (int i = 0; i < display_fields_count; i++)
                    {
                        titleResult.Metadata_Display_Values[i] = Reader.GetString(i + 1);
                    }
                }
                else if (itemcount < ITEMS_TO_CHECK_IN_EACH_TITLE)
                {
                    // Compare the values attached with each display piece of metadata
                    // from the title with this additional, individual item.  If the
                    // values are the same, it should display at the title level, but
                    // if they are different, we will not display the values at that level
                    for (int i = 0; i < display_fields_count; i++)
                    {
                        // If we already found a mismatch for this metadata field, then
                        // no need to continue checking
                        if (checking_fields[i])
                        {
                            if (String.Compare(titleResult.Metadata_Display_Values[i], Reader.GetString(i + 1), StringComparison.InvariantCultureIgnoreCase) != 0)
                            {
                                titleResult.Metadata_Display_Values[i] = "*";
                                checking_fields[i] = false;
                            }
                        }
                    }
                }
            }

            return returnValue;
        }
        private static List<iSearch_Title_Result> DataReader_To_Simple_Result_List(SqlDataReader reader)
        {
            // Create return list
            List<iSearch_Title_Result> returnValue = new List<iSearch_Title_Result>();

            Dictionary<int, int> lookup = new Dictionary<int, int>();

            // Get all the main title values first
            while (reader.Read())
            {
                // Create new database title object for this
                Database_Title_Result result = new Database_Title_Result
                                                   {
                                                       RowNumber = (short) reader.GetInt32(0),
                                                       BibID = reader.GetString(1),
                                                       GroupTitle = reader.GetString(2),
                                                       ALEPH_Number = reader.GetInt32(3),
                                                       OCLC_Number = reader.GetInt64(4),
                                                       GroupThumbnail = reader.GetString(5),
                                                       MaterialType = reader.GetString(6)
                                                   };
                if (reader.FieldCount > 7)
                {
                    result.Primary_Identifier_Type = reader.GetString(7);
                    result.Primary_Identifier = reader.GetString(8);
                }
                else
                {
                    result.Primary_Identifier = String.Empty;
                    result.Primary_Identifier_Type = String.Empty;
                }
                returnValue.Add(result);

                lookup.Add(result.RowNumber, returnValue.Count - 1);
            }

            // Move to the item table
            reader.NextResult();

            // If there were no titles, then there are no results
            if (returnValue.Count == 0)
                return returnValue;

            // Set some values for checking for uniformity of values
            const int itemsToCheckInEachTitle = 20;
            bool checkingPublisher = true;
            bool checkingAuthor = true;
            bool checkingFormat = true;
            bool checkingSpatial = true;
            bool checkingEdition = true;
            bool checkingInstitution = true;
            bool checkingMaterial = true;
            bool checkingMeasurement = true;
            bool checkingStyleperiod = true;
            bool checkingTechnique = true;
            bool checkingDonor = true;
            bool checkingSubjects = true;
            bool checkingCoordinates = true;

            // Step through all the item rows, build the item, and add to the title
            Database_Title_Result titleResult = (Database_Title_Result)returnValue[0];
            int lastRowNumber = titleResult.RowNumber;
            int itemcount = 0;
            while (reader.Read())
            {
                // Ensure this is the right title for this item
                int thisRowNumber = reader.GetInt32(0);
                if (thisRowNumber != lastRowNumber)
                {
                    titleResult = (Database_Title_Result)returnValue[lookup[thisRowNumber]];
                    lastRowNumber = thisRowNumber;
                    itemcount = 0;

                    // Reset some values
                    checkingPublisher = true;
                    checkingAuthor = true;
                    checkingFormat = true;
                    checkingSpatial = true;
                    checkingEdition = true;
                    checkingInstitution = true;
                    checkingMaterial = true;
                    checkingMeasurement = true;
                    checkingStyleperiod = true;
                    checkingTechnique = true;
                    checkingDonor = true;
                    checkingSubjects = true;
                    checkingCoordinates = true;
                }

                // Create new database item object for this
                Database_Item_Result result = new Database_Item_Result
                                                  {
                                                      VID = reader.GetString(1),
                                                      Title = reader.GetString(2),
                                                      IP_Restriction_Mask = reader.GetInt16(3),
                                                      MainThumbnail = reader.GetString(4),
                                                      Level1_Index = (short) reader.GetInt32(5),
                                                      Level1_Text = reader.GetString(6),
                                                      Level2_Index = (short) reader.GetInt32(7),
                                                      Level2_Text = reader.GetString(8),
                                                      Level3_Index = (short) reader.GetInt32(9),
                                                      Level3_Text = reader.GetString(10),
                                                      PubDate = reader.GetString(11),
                                                      PageCount = reader.GetInt32(12),
                                                      Link = reader.GetString(13)
                                                  };

                if (itemcount == 0)
                {
                    titleResult.Publisher = reader.GetString(14);
                    titleResult.Author = reader.GetString(15);
                    titleResult.Format = reader.GetString(16);
                    titleResult.Donor = reader.GetString(17);
                    titleResult.Spatial_Coverage = reader.GetString(18);
                    titleResult.Edition = reader.GetString(19);
                    titleResult.Institution = reader.GetString(20);
                    titleResult.Material = reader.GetString(21);
                    titleResult.Measurement = reader.GetString(22);
                    titleResult.Style_Period = reader.GetString(23);
                    titleResult.Technique = reader.GetString(24);
                    titleResult.Subjects = reader.GetString(25);
                    titleResult.Spatial_Coordinates = reader.GetString(26);

                    if (reader.FieldCount > 27)
                        titleResult.UserNotes = reader.GetString(27);
                }
                else if ( itemcount < itemsToCheckInEachTitle )
                {
                    if (checkingPublisher)
                    {
                        if (titleResult.Publisher != reader.GetString(14))
                        {
                            titleResult.Publisher = "*";
                            checkingPublisher = false;
                        }
                    }

                    if (checkingAuthor)
                    {
                        if (titleResult.Author != reader.GetString(15))
                        {
                            titleResult.Author = "*";
                            checkingAuthor = false;
                        }
                    }

                    if (checkingFormat)
                    {
                        if (titleResult.Format != reader.GetString(16))
                        {
                            titleResult.Format = "*";
                            checkingFormat = false;
                        }
                    }

                    if (checkingDonor)
                    {
                        if (titleResult.Donor != reader.GetString(17))
                        {
                            titleResult.Donor = "*";
                            checkingDonor = false;
                        }
                    }

                    if (checkingSpatial)
                    {
                        if (titleResult.Spatial_Coverage != reader.GetString(18))
                        {
                            titleResult.Spatial_Coverage = "*";
                            checkingSpatial = false;
                        }
                    }

                    if (checkingEdition)
                    {
                        if (titleResult.Edition != reader.GetString(19))
                        {
                            titleResult.Edition = "*";
                            checkingEdition = false;
                        }
                    }

                    if (checkingInstitution)
                    {
                        if (titleResult.Institution != reader.GetString(20))
                        {
                            titleResult.Institution = "*";
                            checkingInstitution = false;
                        }
                    }

                    if (checkingMaterial)
                    {
                        if (titleResult.Material != reader.GetString(21))
                        {
                            titleResult.Material = "*";
                            checkingMaterial = false;
                        }
                    }

                    if (checkingMeasurement)
                    {
                        if (titleResult.Measurement != reader.GetString(22))
                        {
                            titleResult.Measurement = "*";
                            checkingMeasurement = false;
                        }
                    }

                    if (checkingStyleperiod)
                    {
                        if (titleResult.Style_Period != reader.GetString(23))
                        {
                            titleResult.Style_Period = "*";
                            checkingStyleperiod = false;
                        }
                    }

                    if (checkingTechnique)
                    {
                        if (titleResult.Technique != reader.GetString(24))
                        {
                            titleResult.Technique = "*";
                            checkingTechnique = false;
                        }
                    }

                    if (checkingSubjects)
                    {
                        if (titleResult.Subjects != reader.GetString(25))
                        {
                            titleResult.Subjects = "*";
                            checkingSubjects = false;
                        }
                    }

                    if (checkingCoordinates)
                    {
                        if (titleResult.Spatial_Coordinates != reader.GetString(26))
                        {
                            titleResult.Spatial_Coordinates = "*";
                            checkingCoordinates = false;
                        }
                    }
                }

                // Add this to the title object
                titleResult.Add_Item_Result(result);

                // Increment the item count
                itemcount++;
            }

            return returnValue;
        }