Esempio n. 1
0
        /// <summary>
        /// Check that data is valid.
        /// </summary>
        /// <param name="searchCriteria">Species fact search criteria.</param>
        public static void CheckData(this WebSpeciesFactSearchCriteria searchCriteria)
        {
            Int32 index;

            if (searchCriteria.FieldSearchCriteria.IsNotEmpty())
            {
                if ((searchCriteria.FieldSearchCriteria.Count > 1) &&
                    (searchCriteria.FieldLogicalOperator != LogicalOperator.And) &&
                    (searchCriteria.FieldLogicalOperator != LogicalOperator.Or))
                {
                    // Not valid operator applied to field search criteria.
                    throw new ArgumentException("WebSpeciesFactSearchCriteria: FieldLogicalOperator must be And or Or. Current value = " + searchCriteria.FieldLogicalOperator);
                }

                foreach (WebSpeciesFactFieldSearchCriteria fieldSearchCriteria in searchCriteria.FieldSearchCriteria)
                {
                    fieldSearchCriteria.CheckData();
                }
            }

            if (searchCriteria.HostIds.IsNotEmpty())
            {
                for (index = searchCriteria.HostIds.Count - 1; index >= 0; index--)
                {
                    if (searchCriteria.HostIds[index] == 0)
                    {
                        // Host id equal to zero has a special meaning in the
                        // database and can not be used in species fact search.
                        // Host id equal to zero means that no value is specified.
                        searchCriteria.HostIds.RemoveAt(index);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get taxa count of taxa that matches search criteria.
        /// </summary>
        /// <param name="context">The web service context, information on user, requestId, connection etc.</param>
        /// <param name="searchCriteria">Species fact search criteria.</param>
        /// <returns>Taxa count of taxa that matches search criteria.</returns>
        public virtual Int32 GetTaxaCountBySearchCriteria(WebServiceContext context, WebSpeciesFactSearchCriteria searchCriteria)
        {
            WebClientInformation clientInformation;

            clientInformation = GetClientInformation(context, WebServiceId.TaxonAttributeService);
            return(WebServiceProxy.TaxonAttributeService.GetTaxaCountBySearchCriteria(clientInformation, searchCriteria));
        }
Esempio n. 3
0
        /// <summary>
        /// Get SQL query that matches search criteria.
        /// </summary>
        /// <param name="searchCriteria">Species fact search criteria.</param>
        /// <param name="selectPart">Select part of the query.</param>
        /// <returns>SQL query that matches search criteria.</returns>
        public static String GetQuery(this WebSpeciesFactSearchCriteria searchCriteria, QuerySelectPart selectPart)
        {
            Boolean       isFirstItem;
            StringBuilder query, where;

            query = new StringBuilder();
            where = new StringBuilder();

            switch (selectPart)
            {
            case QuerySelectPart.QueryDefault:
                query.Append(QueryDefault);
                break;

            case QuerySelectPart.QueryTaxonCount:
                query.Append(QueryTaxonCount);
                break;

            case QuerySelectPart.QueryTaxonIds:
                query.Append(QueryTaxonIds);
                break;

            default:
                throw new ArgumentOutOfRangeException("selectPart");
            }

            if (!searchCriteria.IncludeNotValidHosts)
            {
                query.Append(@" INNER JOIN Taxon AS ValidHosts WITH (NOLOCK) ON ValidHosts.TaxonId = af_data.host");
            }

            if (!searchCriteria.IncludeNotValidTaxa)
            {
                query.Append(@" INNER JOIN Taxon AS ValidTaxa WITH (NOLOCK) ON ValidTaxa.TaxonId = af_data.taxon");
            }

            if (searchCriteria.FactorDataTypeIds.IsNotEmpty() ||
                searchCriteria.FactorIds.IsNotEmpty())
            {
                query.Append(@" INNER JOIN #FactorIds AS InputFactors ON InputFactors.FactorId = af_data.faktor");
            }

            if (searchCriteria.FieldSearchCriteria.IsNotEmpty())
            {
                if (where.ToString().IsEmpty())
                {
                    where.Append(" WHERE ");
                }
                else
                {
                    where.Append(" AND ");
                }

                if (searchCriteria.FieldSearchCriteria.Count == 1)
                {
                    where.Append(searchCriteria.FieldSearchCriteria[0].GetQuery());
                }
                else
                {
                    where.Append("(");
                    isFirstItem = true;
                    foreach (WebSpeciesFactFieldSearchCriteria fieldSearchCriteria in searchCriteria.FieldSearchCriteria)
                    {
                        if (isFirstItem)
                        {
                            isFirstItem = false;
                        }
                        else
                        {
                            where.Append(" ");
                            where.Append(searchCriteria.FieldLogicalOperator);
                            where.Append(" ");
                        }

                        where.Append(fieldSearchCriteria.GetQuery());
                    }

                    where.Append(")");
                }
            }

            if (searchCriteria.HostIds.IsNotEmpty())
            {
                query.Append(@" INNER JOIN #HostIds AS InputHosts ON InputHosts.HostId = af_data.host");
            }

            if (searchCriteria.IndividualCategoryIds.IsNotEmpty())
            {
                if (where.ToString().IsEmpty())
                {
                    where.Append(" WHERE ");
                }
                else
                {
                    where.Append(" AND ");
                }

                if (searchCriteria.IndividualCategoryIds.Count == 1)
                {
                    where.Append("(af_data.IndividualCategoryId = ");
                    where.Append(searchCriteria.IndividualCategoryIds[0]);
                    where.Append(")");
                }
                else
                {
                    isFirstItem = true;
                    where.Append("(af_data.IndividualCategoryId IN (");
                    foreach (Int32 individualCategoryId in searchCriteria.IndividualCategoryIds)
                    {
                        if (isFirstItem)
                        {
                            isFirstItem = false;
                        }
                        else
                        {
                            where.Append(", ");
                        }

                        where.Append(individualCategoryId);
                    }

                    where.Append("))");
                }
            }

            if (searchCriteria.PeriodIds.IsNotEmpty())
            {
                if (where.ToString().IsEmpty())
                {
                    where.Append(" WHERE ");
                }
                else
                {
                    where.Append(" AND ");
                }

                where.Append("((af_data.period IS NULL) OR ");

                if (searchCriteria.PeriodIds.Count == 1)
                {
                    where.Append("(af_data.period = ");
                    where.Append(searchCriteria.PeriodIds[0]);
                    where.Append(")");
                }
                else
                {
                    isFirstItem = true;
                    where.Append("(af_data.period IN (");
                    foreach (Int32 periodId in searchCriteria.PeriodIds)
                    {
                        if (isFirstItem)
                        {
                            isFirstItem = false;
                        }
                        else
                        {
                            where.Append(", ");
                        }

                        where.Append(periodId);
                    }

                    where.Append("))");
                }
                where.Append(")");
            }

            if (searchCriteria.TaxonIds.IsNotEmpty())
            {
                query.Append(@" INNER JOIN #TaxonIds AS InputTaxa ON InputTaxa.TaxonId = af_data.taxon");
            }

            return(query.ToString() + where);
        }