private TResult ApplyScalarMethods <TResult, TDocument>(SolrCompositeQuery compositeQuery, SolrSearchResults <TDocument> processedResults, SolrQueryResults <Dictionary <string, object> > results)
        {
            var    queryMethod = compositeQuery.Methods.First();
            object obj;

            switch (queryMethod.MethodType)
            {
            case QueryMethodType.All:
                obj = true;
                break;

            case QueryMethodType.Any:
                obj = processedResults.Any() ? 1 : 0;
                break;

            case QueryMethodType.Count:
                obj = !compositeQuery.Methods.Any(i =>
                {
                    if (i.MethodType != QueryMethodType.Take)
                    {
                        return(i.MethodType == QueryMethodType.Skip);
                    }
                    return(true);
                }) ? processedResults.Count() : (object)results.Count();
                break;

            case QueryMethodType.ElementAt:
                obj = !((ElementAtMethod)queryMethod).AllowDefaultValue ? processedResults.ElementAt(((ElementAtMethod)queryMethod).Index) : (object)processedResults.ElementAtOrDefault(((ElementAtMethod)queryMethod).Index);
                break;

            case QueryMethodType.First:
                obj = !((FirstMethod)queryMethod).AllowDefaultValue ? processedResults.First() : (object)processedResults.FirstOrDefault();
                break;

            case QueryMethodType.Last:
                obj = !((LastMethod)queryMethod).AllowDefaultValue ? processedResults.Last() : (object)processedResults.LastOrDefault();
                break;

            case QueryMethodType.Single:
                obj = !((SingleMethod)queryMethod).AllowDefaultValue ? processedResults.Single() : (object)processedResults.SingleOrDefault();
                break;

            case QueryMethodType.GetResults:
                var searchHits   = processedResults.GetSearchHits();
                var facetResults = this.FormatFacetResults(processedResults.GetFacets(), compositeQuery.FacetQueries);
                obj = ReflectionUtility.CreateInstance(typeof(TResult), (object)searchHits, (object)processedResults.NumberFound, (object)facetResults);
                break;

            case QueryMethodType.GetFacets:
                obj = this.FormatFacetResults(processedResults.GetFacets(), compositeQuery.FacetQueries);
                break;

            default:
                throw new InvalidOperationException("Invalid query method");
            }
            return((TResult)Convert.ChangeType(obj, typeof(TResult)));
        }
        private void BuildMenuObject <TMenuObject>(string englishTitle,
                                                   string chinesTitle)
            where TMenuObject : class, new()
        {
            var finalTitle = englishTitle ?? typeof(TMenuObject).Name;

            AddItemAndCacheIndex(OdinMenuTree, MultiLanguageString.Create
                                     (finalTitle, chinesTitle).Text, ReflectionUtility.CreateInstance <TMenuObject>());
        }
Пример #3
0
        /// <summary> Creates an instance of the specified class using the NodeTypeTable. </summary>
        public object CreateInstance(string className)
        {
            NodeTypeEntry entry;

            if (!_nodeTypes.TryGetValue(className, out entry))
            {
                throw new Exception(String.Format("CreateInstance: Class ({0}) not found in node type list.", className));
            }
            return(ReflectionUtility.CreateInstance(entry.Namespace, className, entry.Assembly));
        }
        public override TResult Execute <TResult>(SolrCompositeQuery compositeQuery)
        {
            var  queryWithHighlighting = compositeQuery as SolrCompositeQueryWithHighlights;
            bool resWithHighlights     = queryWithHighlighting != null && typeof(TResult).GetGenericTypeDefinition() == typeof(SearchResultsWithHighlights <>);

            // TODO Check this condition in more details
            if (typeof(TResult).IsGenericType && (typeof(TResult).GetGenericTypeDefinition() == typeof(SearchResults <>) || resWithHighlights))
            {
                var documentType = typeof(TResult).GetGenericArguments()[0];
                var results      = this.Execute(compositeQuery, documentType);

                var solrSearchResultsType        = typeof(SolrSearchResults <>);
                var solrSearchResultsGenericType = solrSearchResultsType.MakeGenericType(documentType);

                var applyScalarMethodsMethod = this.GetType().GetMethod("ApplyScalarMethods", BindingFlags.Instance | BindingFlags.NonPublic);

                // We need to handle the search result for the GetResultsWithHighlights as for the default GetResults case:
                Type returnType = (resWithHighlights) ? typeof(SearchResults <>).MakeGenericType(documentType) : typeof(TResult);
                var  applyScalarMethodsGenericMethod = applyScalarMethodsMethod.MakeGenericMethod(returnType, documentType);

                var selectMethod = GetSelectMethod(compositeQuery);

                // Execute query methods
                var processedResults = ReflectionUtility.CreateInstance(solrSearchResultsGenericType, this.context, results, selectMethod, compositeQuery.ExecutionContexts, compositeQuery.VirtualFieldProcessors);

                var searchResult = applyScalarMethodsGenericMethod.Invoke(this, new object[] { compositeQuery, processedResults, results });

                if (resWithHighlights)
                {
                    return((TResult)ReflectionUtility.CreateInstance(typeof(TResult), searchResult, results.Highlights));
                }

                return((TResult)searchResult);
            }
            else
            {
                var results = this.Execute(compositeQuery, typeof(TResult));

                var selectMethod = GetSelectMethod(compositeQuery);

                var processedResults = new SolrSearchResults <TResult>(this.context, results, selectMethod, compositeQuery.ExecutionContexts, compositeQuery.VirtualFieldProcessors);

                return(this.ApplyScalarMethods <TResult, TResult>(compositeQuery, processedResults, results));
            }
        }
        public override TResult Execute <TResult>(SolrCompositeQuery compositeQuery)
        {
            if (EnumerableLinq.ShouldExecuteEnumerableLinqQuery(compositeQuery))
            {
                return(EnumerableLinq.ExecuteEnumerableLinqQuery <TResult>(compositeQuery));
            }
            if (typeof(TResult).IsGenericType &&
                typeof(TResult).GetGenericTypeDefinition() == typeof(SearchResults <>))
            {
                var resultType       = typeof(TResult).GetGenericArguments()[0];
                var solrQueryResults = this.Execute(compositeQuery, resultType);
                var type             = typeof(SolrSearchResults <>).MakeGenericType(resultType);
                var methodInfo       = this.GetType()
                                       .GetMethod("ApplyScalarMethods", BindingFlags.Instance | BindingFlags.NonPublic)
                                       .MakeGenericMethod(typeof(TResult), resultType);
                var selectMethod = GetSelectMethod(compositeQuery);

                var instance = ReflectionUtility.CreateInstance(type, (object)this._context, (object)solrQueryResults,
                                                                (object)selectMethod, (object)compositeQuery.ExecutionContexts,
                                                                (object)compositeQuery.VirtualFieldProcessors);
                return((TResult)methodInfo.Invoke(this, new object[3]
                {
                    compositeQuery,
                    instance,
                    solrQueryResults
                }));
            }

            compositeQuery.Methods.Add(new GetFacetsMethod());

            var solrQueryResults1 = this.Execute(compositeQuery, typeof(TResult));
            var selectMethod1     = GetSelectMethod(compositeQuery);
            var processedResults  = new SolrSearchResults <TResult>(this._context, solrQueryResults1, selectMethod1, compositeQuery.ExecutionContexts, compositeQuery.VirtualFieldProcessors);

            return(this.ApplyScalarMethods <TResult, TResult>(compositeQuery, processedResults, solrQueryResults1));
        }
        private TResult ApplyScalarMethods <TResult, TDocument>(SolrCompositeQuery compositeQuery, SolrSearchResults <TDocument> processedResults, SolrQueryResults <Dictionary <string, object> > results)
        {
            var method = compositeQuery.Methods.First();

            object result;

            switch (method.MethodType)
            {
            case QueryMethodType.All:
                result = true;
                break;

            case QueryMethodType.Any:
                result = processedResults.Any();
                break;

            case QueryMethodType.Count:
                result = processedResults.Count();
                break;

            case QueryMethodType.ElementAt:
                if (((ElementAtMethod)method).AllowDefaultValue)
                {
                    result = processedResults.ElementAtOrDefault(((ElementAtMethod)method).Index);
                }
                else
                {
                    result = processedResults.ElementAt(((ElementAtMethod)method).Index);
                }

                break;

            case QueryMethodType.First:
                if (((FirstMethod)method).AllowDefaultValue)
                {
                    result = processedResults.FirstOrDefault();
                }
                else
                {
                    result = processedResults.First();
                }

                break;

            case QueryMethodType.Last:
                if (((LastMethod)method).AllowDefaultValue)
                {
                    result = processedResults.LastOrDefault();
                }
                else
                {
                    result = processedResults.Last();
                }

                break;

            case QueryMethodType.Single:
                if (((SingleMethod)method).AllowDefaultValue)
                {
                    result = processedResults.SingleOrDefault();
                }
                else
                {
                    result = processedResults.Single();
                }

                break;

            case QueryMethodType.GetResults:
                var resultList = processedResults.GetSearchHits();
                var facets     = this.FormatFacetResults(processedResults.GetFacets(), compositeQuery.FacetQueries);
                result = ReflectionUtility.CreateInstance(typeof(TResult), resultList, processedResults.NumberFound, facets);     // Create instance of SearchResults<TDocument>
                break;


            case QueryMethodType.GetFacets:
                result = this.FormatFacetResults(processedResults.GetFacets(), compositeQuery.FacetQueries);
                break;

            default:
                throw new InvalidOperationException("Invalid query method");
            }

            return((TResult)System.Convert.ChangeType(result, typeof(TResult)));
        }