Exemplo n.º 1
0
        private static FilterCriterion BuildCriterion(List <Token> tokens, Type dataType, Type defaultCriterionType, CultureInfo culture)
        {
            FilterCriterion filterCriterion = null;
            ReferenceList <FilterCriterion> initializedCriteria = new ReferenceList <FilterCriterion>();
            List <int> precedences = new List <int>();

            FilterParser.FillPrecedenceList(precedences);

            for (int index = 0; index < precedences.Count; index++)
            {
                int curOperatorPrecedence = precedences[index];

                foreach (Type type in FilterParser.RegisteredFilterCriterionTypes)
                {
                    CriterionDescriptorAttribute attribute = ( CriterionDescriptorAttribute )type.GetCustomAttributes(typeof(CriterionDescriptorAttribute), true)[0];

                    if (( int )attribute.OperatorPrecedence == curOperatorPrecedence)
                    {
                        if (!string.IsNullOrEmpty(attribute.Pattern))
                        {
                            bool tokenConsumed;

                            do
                            {
                                tokenConsumed = false;

                                for (int i = 0; i < tokens.Count; i++)
                                {
                                    FilterCriterionToken filterToken = tokens[i] as FilterCriterionToken;

                                    if ((filterToken != null) && (filterToken.FilterCriterion.GetType() == type) && (!initializedCriteria.Contains(filterToken.FilterCriterion)))
                                    {
                                        List <object> parameters = new List <object>();

                                        if (attribute.Pattern.StartsWith("@"))
                                        {
                                            if (i <= 0)
                                            {
                                                throw new DataGridException(string.Format(FilterParser.MissingLeftOperandErrorText, type.Name));
                                            }

                                            if (tokens[i - 1] is ValueToken)
                                            {
                                                FilterParser.AddValueToParameters(parameters, ( ValueToken )tokens[i - 1], dataType, culture);
                                            }
                                            else
                                            {
                                                parameters.Add((( FilterCriterionToken )tokens[i - 1]).FilterCriterion);
                                            }

                                            tokens.RemoveAt(i - 1);
                                            i--;
                                        }

                                        if (attribute.Pattern.EndsWith("@"))
                                        {
                                            if (i + 1 >= tokens.Count)
                                            {
                                                throw new DataGridException(string.Format(FilterParser.MissingRightOperandErrorText, type.Name));
                                            }

                                            if (tokens[i + 1] is ValueToken)
                                            {
                                                FilterParser.AddValueToParameters(parameters, ( ValueToken )tokens[i + 1], dataType, culture);
                                            }
                                            else
                                            {
                                                parameters.Add((( FilterCriterionToken )tokens[i + 1]).FilterCriterion);
                                            }

                                            tokens.RemoveAt(i + 1);
                                            i--;
                                        }

                                        filterToken.FilterCriterion.InitializeFrom(parameters.ToArray(), defaultCriterionType);
                                        initializedCriteria.Add(filterToken.FilterCriterion);
                                        tokenConsumed = true;
                                        break;
                                    }
                                }
                            }while(tokenConsumed);
                        }
                        else
                        {
                            if (type != typeof(ContainsFilterCriterion))
                            {
                                throw new DataGridInternalException("Missing pattern in attribute: " + type.Name);
                            }
                        }
                    }
                }
            }

            if ((tokens.Count == 1) && (tokens[0] is FilterCriterionToken))
            {
                filterCriterion = (( FilterCriterionToken )tokens[0]).FilterCriterion;
            }
            else
            {
                throw new DataGridException(FilterParser.InvalidExpressionErrorText);
            }

            return(filterCriterion);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Convert a WebSpeciesFact instance into
        /// an ISpeciesFact instance.
        /// </summary>
        /// <param name="userContext">
        /// Information about the user that makes this method call.
        /// </param>
        /// <param name="webSpeciesFact">A WebSpeciesFact instance.</param>
        /// <param name="factors">List of factors.</param>
        /// <param name="individualCategories">List of individual categories.</param>
        /// <param name="periods">List of periods.</param>
        /// <param name="references">List of references.</param>
        /// <param name="speciesFactQualities">List of species fact qualities.</param>
        /// <param name="taxa">Taxa that are used in the species facts.</param>
        /// <returns>An ISpeciesFact instance.</returns>
        private ISpeciesFact GetSpeciesFact(IUserContext userContext,
                                            WebSpeciesFact webSpeciesFact,
                                            FactorList factors,
                                            IndividualCategoryList individualCategories,
                                            PeriodList periods,
                                            ReferenceList references,
                                            SpeciesFactQualityList speciesFactQualities,
                                            TaxonList taxa)
        {
            IFactor      factor;
            IPeriod      period;
            ISpeciesFact speciesFact;
            ITaxon       host, taxon;

            factor = factors.Get(webSpeciesFact.FactorId);
            if (webSpeciesFact.IsHostSpecified)
            {
#if EXAMINE_PROBLEM
                if (!(taxa.Contains(webSpeciesFact.HostId)))
                {
                    throw new Exception("Host with id = " + webSpeciesFact.HostId + " is not in taxon list." + webSpeciesFact.GetString());
                }
#endif

                host = taxa.Get(webSpeciesFact.HostId);
            }
            else
            {
                if (factor.IsTaxonomic)
                {
                    host = CoreData.TaxonManager.GetTaxon(userContext, 0);
                }
                else
                {
                    host = null;
                }
            }

            if (webSpeciesFact.IsPeriodSpecified)
            {
#if EXAMINE_PROBLEM
                if (!(periods.Contains(webSpeciesFact.PeriodId)))
                {
                    throw new Exception("Period with id = " + webSpeciesFact.PeriodId + " is not in period list." + webSpeciesFact.GetString());
                }
#endif

                period = periods.Get(webSpeciesFact.PeriodId);
            }
            else
            {
                period = null;
            }

#if EXAMINE_PROBLEM
            if (!(taxa.Contains(webSpeciesFact.TaxonId)))
            {
                throw new Exception("Taxon with id = " + webSpeciesFact.TaxonId + " is not in taxon list." + webSpeciesFact.GetString());
            }
#endif

            taxon = taxa.Get(webSpeciesFact.TaxonId);
#if EXAMINE_PROBLEM
            if (!(individualCategories.Contains(webSpeciesFact.IndividualCategoryId)))
            {
                throw new Exception("Individual category with id = " + webSpeciesFact.IndividualCategoryId + " is not in individual category list." + webSpeciesFact.GetString());
            }

            if (!(speciesFactQualities.Contains(webSpeciesFact.QualityId)))
            {
                throw new Exception("Quality with id = " + webSpeciesFact.QualityId + " is not in quality list." + webSpeciesFact.GetString());
            }

            if (!(references.Contains(webSpeciesFact.ReferenceId)))
            {
                throw new Exception("Reference with id = " + webSpeciesFact.ReferenceId + " is not in reference list." + webSpeciesFact.GetString());
            }
#endif

            speciesFact = CoreData.SpeciesFactManager.GetSpeciesFact(userContext,
                                                                     webSpeciesFact.Id,
                                                                     taxon,
                                                                     individualCategories.Get(webSpeciesFact.IndividualCategoryId),
                                                                     factor,
                                                                     host,
                                                                     period,
                                                                     webSpeciesFact.FieldValue1,
                                                                     webSpeciesFact.IsFieldValue1Specified,
                                                                     webSpeciesFact.FieldValue2,
                                                                     webSpeciesFact.IsFieldValue2Specified,
                                                                     webSpeciesFact.FieldValue3,
                                                                     webSpeciesFact.IsFieldValue3Specified,
                                                                     webSpeciesFact.FieldValue4,
                                                                     webSpeciesFact.IsFieldValue4Specified,
                                                                     webSpeciesFact.FieldValue5,
                                                                     webSpeciesFact.IsFieldValue5Specified,
                                                                     speciesFactQualities.Get(webSpeciesFact.QualityId),
                                                                     references.Get(webSpeciesFact.ReferenceId),
                                                                     webSpeciesFact.ModifiedBy,
                                                                     webSpeciesFact.ModifiedDate);

            return(speciesFact);
        }