/// <summary>
        /// Get species facts that matches user parameter selection.
        /// </summary>
        /// <param name="context">Web service request context.</param>
        /// <param name="userParameterSelection">All parameters selected by user</param>
        /// <returns>Species facts.</returns>
        /// <exception cref="ArgumentException">Thrown if user parameter selection is null.</exception>
        public static List <WebSpeciesFact> GetSpeciesFactsByUserParameterSelection(WebServiceContext context,
                                                                                    WebUserParameterSelection userParameterSelection)
        {
            List <WebSpeciesFact> speciesFacts = null;

            // Check arguments.
            userParameterSelection.CheckNotNull("userParameterSelection");
            userParameterSelection.CheckData();

            // Insert species fact search parameters into database.
            AddUserSelectedParameters(context, userParameterSelection);

            // Get information about species facts from database.
            speciesFacts = new List <WebSpeciesFact>();
            using (DataReader dataReader = DataServer.GetSpeciesFactsByUserParameterSelection(context,
                                                                                              userParameterSelection.FactorIds.IsNotEmpty(),
                                                                                              userParameterSelection.TaxonIds.IsNotEmpty(),
                                                                                              userParameterSelection.IndividualCategoryIds.IsNotEmpty(),
                                                                                              userParameterSelection.PeriodIds.IsNotEmpty(),
                                                                                              userParameterSelection.HostIds.IsNotEmpty(),
                                                                                              userParameterSelection.ReferenceIds.IsNotEmpty()))
            {
                while (dataReader.Read())
                {
                    speciesFacts.Add(new WebSpeciesFact(dataReader));
                    if (speciesFacts.Count > MAX_SPECIES_FACTS)
                    {
                        // Exceeding max numbers of species facts that
                        // can be retrieved in one request.
                        throw new ApplicationException("Max " + MAX_SPECIES_FACTS + " species facts can be retrieved in one call.");
                    }
                }
            }

            return(speciesFacts);
        }
        /// <summary>
        /// Inserts a list of host ids into the database.
        /// </summary>
        /// <param name="context">Web service request context.</param>
        /// <param name="userParameterSelection">All parameters selected by user</param>
        public static void AddUserSelectedParameters(WebServiceContext context,
                                                     WebUserParameterSelection userParameterSelection)
        {
            DataColumn column;
            DataRow    row;
            DataTable  userSelectedParameter;
            Int32      rowCount, rowIndex;

            // Get max row count.
            rowCount = 0;
            if (userParameterSelection.FactorIds.IsNotEmpty() &&
                (rowCount < userParameterSelection.FactorIds.Count))
            {
                rowCount = userParameterSelection.FactorIds.Count;
            }
            if (userParameterSelection.HostIds.IsNotEmpty() &&
                (rowCount < userParameterSelection.HostIds.Count))
            {
                rowCount = userParameterSelection.HostIds.Count;
            }
            if (userParameterSelection.IndividualCategoryIds.IsNotEmpty() &&
                (rowCount < userParameterSelection.IndividualCategoryIds.Count))
            {
                rowCount = userParameterSelection.IndividualCategoryIds.Count;
            }
            if (userParameterSelection.PeriodIds.IsNotEmpty() &&
                (rowCount < userParameterSelection.PeriodIds.Count))
            {
                rowCount = userParameterSelection.PeriodIds.Count;
            }
            if (userParameterSelection.ReferenceIds.IsNotEmpty() &&
                (rowCount < userParameterSelection.ReferenceIds.Count))
            {
                rowCount = userParameterSelection.ReferenceIds.Count;
            }
            if (userParameterSelection.TaxonIds.IsNotEmpty() &&
                (rowCount < userParameterSelection.TaxonIds.Count))
            {
                rowCount = userParameterSelection.TaxonIds.Count;
            }

            if (rowCount > 0)
            {
                // Delete all user selected parameters that
                // belong to this request from the "temporary" tables.
                // This is done to avoid problem with restarts of the webservice.
                DeleteUserSelectedParameters(context);

                // Insert the new list of hosts.
                userSelectedParameter = new DataTable(UserSelectedParameterData.TABLE_NAME);
                column = new DataColumn(UserSelectedParameterData.REQUEST_ID, typeof(Int32));
                userSelectedParameter.Columns.Add(column);
                column = new DataColumn(UserSelectedParameterData.FACTOR_ID, typeof(Int32));
                userSelectedParameter.Columns.Add(column);
                column = new DataColumn(UserSelectedParameterData.HOST_ID, typeof(Int32));
                userSelectedParameter.Columns.Add(column);
                column = new DataColumn(UserSelectedParameterData.INDIVIDUAL_CATEGORY_ID, typeof(Int32));
                userSelectedParameter.Columns.Add(column);
                column = new DataColumn(UserSelectedParameterData.PERIOD_ID, typeof(Int32));
                userSelectedParameter.Columns.Add(column);
                column = new DataColumn(UserSelectedParameterData.REFERENCE_ID, typeof(Int32));
                userSelectedParameter.Columns.Add(column);
                column = new DataColumn(UserSelectedParameterData.TAXON_ID, typeof(Int32));
                userSelectedParameter.Columns.Add(column);
                for (rowIndex = 0; rowIndex < rowCount; rowIndex++)
                {
                    row    = userSelectedParameter.NewRow();
                    row[0] = context.RequestId;
                    if (userParameterSelection.FactorIds.IsNotEmpty() &&
                        (rowIndex < userParameterSelection.FactorIds.Count))
                    {
                        row[1] = userParameterSelection.FactorIds[rowIndex];
                    }
                    if (userParameterSelection.HostIds.IsNotEmpty() &&
                        (rowIndex < userParameterSelection.HostIds.Count))
                    {
                        row[2] = userParameterSelection.HostIds[rowIndex];
                    }
                    if (userParameterSelection.IndividualCategoryIds.IsNotEmpty() &&
                        (rowIndex < userParameterSelection.IndividualCategoryIds.Count))
                    {
                        row[3] = userParameterSelection.IndividualCategoryIds[rowIndex];
                    }
                    if (userParameterSelection.PeriodIds.IsNotEmpty() &&
                        (rowIndex < userParameterSelection.PeriodIds.Count))
                    {
                        row[4] = userParameterSelection.PeriodIds[rowIndex];
                    }
                    if (userParameterSelection.ReferenceIds.IsNotEmpty() &&
                        (rowIndex < userParameterSelection.ReferenceIds.Count))
                    {
                        row[5] = userParameterSelection.ReferenceIds[rowIndex];
                    }
                    if (userParameterSelection.TaxonIds.IsNotEmpty() &&
                        (rowIndex < userParameterSelection.TaxonIds.Count))
                    {
                        row[6] = userParameterSelection.TaxonIds[rowIndex];
                    }
                    userSelectedParameter.Rows.Add(row);
                }
                DataServer.AddUserSelectedParameters(context, userSelectedParameter);
            }
        }