Example #1
0
        public void BuildResponse(XmlElement responseNode)
        {
            MailboxSearchProvider.BuildResponseState buildResponseState = MailboxSearchProvider.BuildResponseState.Seeking;
            int  num  = -1;
            bool flag = false;

            this.totalRowCount   = 0;
            this.numberResponses = 0;
            XmlNode xmlNode = responseNode.OwnerDocument.CreateElement("Status", "Search:");

            responseNode.AppendChild(xmlNode);
            xmlNode.InnerText = 1.ToString(CultureInfo.InvariantCulture);
            foreach (MailboxSearchCriteriaBuilder.SchemaCacheItem schemaCacheItem in this.schemaCache.Values)
            {
                schemaCacheItem.SchemaState.Options.Clear();
                this.mailboxSchemaOptions.PopulateOptionsCollection(this.context.Request.DeviceIdentity.DeviceType, schemaCacheItem.SchemaState.Options);
            }
            bool flag2 = false;
            int  num2  = 0;
            bool hasBodyPartPreferences = this.mailboxSchemaOptions.HasBodyPartPreferences;

            if (this.queryFilter != null)
            {
                using (QueryResult queryResult = this.searchFolder.ItemQuery(ItemQueryType.None, null, this.defaultSortBy, MailboxSearchProvider.queryColumns))
                {
                    this.uncappedResultCount = queryResult.EstimatedRowCount;
                    num2 = this.uncappedResultCount;
                    while (!flag2 && num <= this.maxRange)
                    {
                        object[][] rows = queryResult.GetRows(this.maxRange + 1);
                        flag2 = (rows.Length == 0);
                        for (int i = 0; i < rows.Length; i++)
                        {
                            StoreObjectId objectId       = (rows[i][0] as VersionedId).ObjectId;
                            string        itemClass      = rows[i][1] as string;
                            StoreObjectId parentFolderId = rows[i][2] as StoreObjectId;
                            string        innerText      = null;
                            try
                            {
                                AirSyncDataObject airSyncDataObject = null;
                                IServerDataObject serverDataObject  = null;
                                foreach (string text in this.schemaCache.Keys)
                                {
                                    MailboxSearchCriteriaBuilder.SchemaCacheItem schemaCacheItem2 = this.schemaCache[text];
                                    if (schemaCacheItem2.MailboxDataObject.CanConvertItemClassUsingCurrentSchema(itemClass))
                                    {
                                        innerText         = text;
                                        airSyncDataObject = schemaCacheItem2.AirSyncDataObject;
                                        serverDataObject  = schemaCacheItem2.MailboxDataObject;
                                        break;
                                    }
                                }
                                if (airSyncDataObject != null)
                                {
                                    if (this.searchCriteriaBuilder.DoesMatchCriteria(parentFolderId, objectId))
                                    {
                                        num++;
                                        if (MailboxSearchProvider.BuildResponseState.BuildingResponse == buildResponseState || (buildResponseState == MailboxSearchProvider.BuildResponseState.Seeking && num >= this.minRange))
                                        {
                                            buildResponseState = MailboxSearchProvider.BuildResponseState.BuildingResponse;
                                            buildResponseState = MailboxSearchProvider.BuildResponseState.BuildingResponse;
                                            Item item = null;
                                            try
                                            {
                                                item = this.BindToItemWithItemClass(objectId, itemClass, serverDataObject.GetPrefetchProperties());
                                                if (this.searchCriteriaBuilder.ExcludedFolders.Contains(item.ParentId))
                                                {
                                                    num--;
                                                }
                                                else
                                                {
                                                    XmlNode xmlNode2 = responseNode.OwnerDocument.CreateElement("Result", "Search:");
                                                    XmlNode xmlNode3 = responseNode.OwnerDocument.CreateElement("Properties", "Search:");
                                                    XmlNode xmlNode4 = responseNode.OwnerDocument.CreateElement("Class", "AirSync:");
                                                    xmlNode4.InnerText = innerText;
                                                    XmlNode xmlNode5 = responseNode.OwnerDocument.CreateElement("LongId", "Search:");
                                                    XmlNode xmlNode6 = responseNode.OwnerDocument.CreateElement("CollectionId", "AirSync:");
                                                    xmlNode5.InnerText = HttpUtility.UrlEncode(item.Id.ObjectId.ToBase64String());
                                                    if (hasBodyPartPreferences)
                                                    {
                                                        if (this.searchCriteriaBuilder.Conversation == null)
                                                        {
                                                            throw new InvalidOperationException("Conversation object should not be null here when body part is requested!");
                                                        }
                                                        this.searchCriteriaBuilder.Conversation.LoadItemParts(new List <StoreObjectId>
                                                        {
                                                            item.StoreObjectId
                                                        });
                                                    }
                                                    if (this.mailboxSchemaOptions.RightsManagementSupport)
                                                    {
                                                        Command.CurrentCommand.DecodeIrmMessage(item, false);
                                                    }
                                                    try
                                                    {
                                                        serverDataObject.Bind(item);
                                                        airSyncDataObject.Bind(xmlNode3);
                                                        airSyncDataObject.CopyFrom(serverDataObject);
                                                    }
                                                    finally
                                                    {
                                                        airSyncDataObject.Unbind();
                                                        serverDataObject.Unbind();
                                                    }
                                                    if (this.mailboxSchemaOptions.RightsManagementSupport)
                                                    {
                                                        Command.CurrentCommand.SaveLicense(item);
                                                    }
                                                    xmlNode2.AppendChild(xmlNode4);
                                                    xmlNode2.AppendChild(xmlNode5);
                                                    if (this.FolderIdMappingSyncState == null)
                                                    {
                                                        throw new SearchNeedToFolderSyncException();
                                                    }
                                                    string text2 = ((FolderIdMapping)this.FolderIdMappingSyncState[CustomStateDatumType.IdMapping])[MailboxSyncItemId.CreateForNewItem(item.ParentId)];
                                                    if (text2 == null)
                                                    {
                                                        throw new SearchNeedToFolderSyncException();
                                                    }
                                                    xmlNode6.InnerText = text2;
                                                    xmlNode2.AppendChild(xmlNode6);
                                                    xmlNode2.AppendChild(xmlNode3);
                                                    responseNode.AppendChild(xmlNode2);
                                                    this.numberResponses++;
                                                    if (this.minRange + this.numberResponses > this.maxRange)
                                                    {
                                                        flag = true;
                                                        buildResponseState = MailboxSearchProvider.BuildResponseState.CountingTotal;
                                                    }
                                                }
                                            }
                                            catch
                                            {
                                                num--;
                                                throw;
                                            }
                                            finally
                                            {
                                                if (item != null)
                                                {
                                                    item.Dispose();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                if (ex is SearchNeedToFolderSyncException)
                                {
                                    throw;
                                }
                                if (!SyncCommand.IsItemSyncTolerableException(ex) && !SyncCommand.IsObjectNotFound(ex))
                                {
                                    throw;
                                }
                            }
                        }
                    }
                    if (!flag && this.uncappedResultCount > queryResult.EstimatedRowCount)
                    {
                        this.haveUnretrievableResults = true;
                    }
                }
            }
            if (this.numberResponses == 0)
            {
                XmlNode newChild = responseNode.OwnerDocument.CreateElement("Result", "Search:");
                responseNode.AppendChild(newChild);
                if (this.minRange >= GlobalSettings.MaxMailboxSearchResults)
                {
                    xmlNode.InnerText = 12.ToString(CultureInfo.InvariantCulture);
                    return;
                }
            }
            else if (this.rangeSpecified)
            {
                XmlNode xmlNode7 = responseNode.OwnerDocument.CreateElement("Range", "Search:");
                XmlNode xmlNode8 = responseNode.OwnerDocument.CreateElement("Total", "Search:");
                xmlNode7.InnerText = this.minRange.ToString(CultureInfo.InvariantCulture) + "-" + (this.minRange + this.numberResponses - 1).ToString(CultureInfo.InvariantCulture);
                responseNode.AppendChild(xmlNode7);
                if (this.haveUnretrievableResults)
                {
                    this.totalRowCount = this.uncappedResultCount;
                    xmlNode.InnerText  = 12.ToString(CultureInfo.InvariantCulture);
                }
                else if (this.uncappedResultCount > num2 || !flag2)
                {
                    this.totalRowCount = this.uncappedResultCount;
                }
                else
                {
                    this.totalRowCount = num + 1;
                    if (this.minRange + this.numberResponses == GlobalSettings.MaxMailboxSearchResults)
                    {
                        xmlNode.InnerText = 12.ToString(CultureInfo.InvariantCulture);
                    }
                }
                xmlNode8.InnerText = this.totalRowCount.ToString(CultureInfo.InvariantCulture);
                responseNode.AppendChild(xmlNode8);
            }
        }