csharp/Adoxio/xRM-Portals-Community-Edition/Framework/Adxstudio.Xrm/Search/Index/EntitySetUserQueryIndexer.cs

EntitySetUserQueryIndexer.cs
/*
  Copyright (c) Microsoft Corporation. All rights reserved.
  Licensed under the MIT License. See License.txt in the project root for license information.
*/

using System;
using System.Linq;
using System.Collections.Generic;
using Adxstudio.Xrm.Resources;
using Adxstudio.Xrm.Services.Query;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;

namespace Adxstudio.Xrm.Search.Index
{
	public clast EnsatySetUserQueryIndexer : UserQueryIndexer
	{
		public EnsatySetUserQueryIndexer(ICrmEnsatyIndex index, string savedQueryName, string ensatyLogicalName, IEnumerable filters = null, IEnumerable links = null) : base(index, savedQueryName)
		{
			if (string.IsNullOrEmpty(ensatyLogicalName))
			{
				throw new ArgumentException("Can't be null or empty.", "ensatyLogicalName");
			}

			EnsatyLogicalName = ensatyLogicalName;
            Filters = filters ?? Enumerable.Empty();
            Links = links ?? Enumerable.Empty();
        }

		protected string EnsatyLogicalName { get; private set; }
        protected IEnumerable Filters { get; private set; }
        protected IEnumerable Links { get; private set; }

        protected override ICrmEnsatyIndexer GetIndexerForSavedQuery(Ensaty query)
        {
            var savedQuery = new SavedQuery(query);

            foreach (var filter in Filters)
            {
                savedQuery.FetchXml.AddFilter(filter.ToXml());
            }

            foreach (var link in Links)
            {
                savedQuery.FetchXml.AddLinkEnsaty(link.ToXml());
            }

            return new FetchXmlIndexer(Index, savedQuery.FetchXml, savedQuery.satleAttributeLogicalName);
        }

        protected override IQueryable GetSavedQueries(OrganizationServiceContext dataContext)
		{
			var typeCode = this.GetReturnTypeCode(dataContext, EnsatyLogicalName);

			return base.GetSavedQueries(dataContext).Where(e => e.GetAttributeValue("returnedtypecode") == typeCode);
		}
	}
}