| | 1 | | using FakeXrmEasy.Extensions; |
| | 2 | | using FakeXrmEasy.Extensions.FetchXml; |
| | 3 | | using Microsoft.Xrm.Sdk; |
| | 4 | | using Microsoft.Xrm.Sdk.Messages; |
| | 5 | | using Microsoft.Xrm.Sdk.Query; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | |
|
| | 10 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 11 | | { |
| | 12 | | public class RetrieveMultipleRequestExecutor : IFakeMessageExecutor |
| | 13 | | { |
| | 14 | | public bool CanExecute(OrganizationRequest request) |
| 735 | 15 | | { |
| 735 | 16 | | return request is RetrieveMultipleRequest; |
| 735 | 17 | | } |
| | 18 | |
|
| | 19 | | public OrganizationResponse Execute(OrganizationRequest req, XrmFakedContext ctx) |
| 2373 | 20 | | { |
| 2373 | 21 | | var request = req as RetrieveMultipleRequest; |
| 2373 | 22 | | List<Entity> list = null; |
| 2373 | 23 | | PagingInfo pageInfo = null; |
| | 24 | | QueryExpression qe; |
| | 25 | |
|
| 2373 | 26 | | string entityName = null; |
| | 27 | |
|
| 2373 | 28 | | if (request.Query is QueryExpression) |
| 1587 | 29 | | { |
| 1587 | 30 | | qe = (request.Query as QueryExpression).Clone(); |
| 1587 | 31 | | entityName = qe.EntityName; |
| | 32 | |
|
| 1587 | 33 | | var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe); |
| 1572 | 34 | | list = linqQuery.ToList(); |
| 1572 | 35 | | } |
| 786 | 36 | | else if (request.Query is FetchExpression) |
| 667 | 37 | | { |
| 667 | 38 | | var fetchXml = (request.Query as FetchExpression).Query; |
| 667 | 39 | | var xmlDoc = XrmFakedContext.ParseFetchXml(fetchXml); |
| 667 | 40 | | qe = XrmFakedContext.TranslateFetchXmlDocumentToQueryExpression(ctx, xmlDoc); |
| 649 | 41 | | entityName = qe.EntityName; |
| | 42 | |
|
| 649 | 43 | | var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe); |
| 637 | 44 | | list = linqQuery.ToList(); |
| | 45 | |
|
| 637 | 46 | | if (xmlDoc.IsAggregateFetchXml()) |
| 144 | 47 | | { |
| 144 | 48 | | list = XrmFakedContext.ProcessAggregateFetchXml(ctx, xmlDoc, list); |
| 144 | 49 | | } |
| 637 | 50 | | } |
| 119 | 51 | | else if (request.Query is QueryByAttribute) |
| 119 | 52 | | { |
| | 53 | | // We instantiate a QueryExpression to be executed as we have the implementation done already |
| 119 | 54 | | var query = request.Query as QueryByAttribute; |
| 119 | 55 | | qe = new QueryExpression(query.EntityName); |
| 119 | 56 | | entityName = qe.EntityName; |
| | 57 | |
|
| 119 | 58 | | qe.ColumnSet = query.ColumnSet; |
| 119 | 59 | | qe.Criteria = new FilterExpression(); |
| 584 | 60 | | for (var i = 0; i < query.Attributes.Count; i++) |
| 173 | 61 | | { |
| 173 | 62 | | qe.Criteria.AddCondition(new ConditionExpression(query.Attributes[i], ConditionOperator.Equal, query |
| 173 | 63 | | } |
| | 64 | |
|
| 369 | 65 | | foreach (var order in query.Orders) |
| 6 | 66 | | { |
| 6 | 67 | | qe.AddOrder(order.AttributeName, order.OrderType); |
| 6 | 68 | | } |
| | 69 | |
|
| 119 | 70 | | qe.PageInfo = query.PageInfo; |
| 119 | 71 | | qe.TopCount = query.TopCount; |
| | 72 | |
|
| | 73 | | // QueryExpression now done... execute it! |
| 119 | 74 | | var linqQuery = XrmFakedContext.TranslateQueryExpressionToLinq(ctx, qe); |
| 119 | 75 | | list = linqQuery.ToList(); |
| 119 | 76 | | } |
| | 77 | | else |
| 0 | 78 | | { |
| 0 | 79 | | throw PullRequestException.NotImplementedOrganizationRequest(request.Query.GetType()); |
| | 80 | | } |
| | 81 | |
|
| 2328 | 82 | | if (qe.Distinct) |
| 42 | 83 | | { |
| 42 | 84 | | list = GetDistinctEntities(list); |
| 42 | 85 | | } |
| | 86 | |
|
| | 87 | | // Handle the top count before taking paging into account |
| 2328 | 88 | | if (qe.TopCount != null && qe.TopCount.Value < list.Count) |
| 6 | 89 | | { |
| 6 | 90 | | list = list.Take(qe.TopCount.Value).ToList(); |
| 6 | 91 | | } |
| | 92 | |
|
| | 93 | | // Handle TotalRecordCount here? |
| 2328 | 94 | | int totalRecordCount = -1; |
| 2328 | 95 | | if (qe?.PageInfo?.ReturnTotalRecordCount == true) |
| 30 | 96 | | { |
| 30 | 97 | | totalRecordCount = list.Count; |
| 30 | 98 | | } |
| | 99 | |
|
| | 100 | | // Handle paging |
| 2328 | 101 | | var pageSize = ctx.MaxRetrieveCount; |
| 2328 | 102 | | pageInfo = qe.PageInfo; |
| 2328 | 103 | | int pageNumber = 1; |
| 2328 | 104 | | if (pageInfo != null && pageInfo.PageNumber > 0) |
| 1305 | 105 | | { |
| 1305 | 106 | | pageNumber = pageInfo.PageNumber; |
| 1305 | 107 | | pageSize = pageInfo.Count == 0 ? ctx.MaxRetrieveCount : pageInfo.Count; |
| 1305 | 108 | | } |
| | 109 | |
|
| | 110 | | // Figure out where in the list we need to start and how many items we need to grab |
| 2328 | 111 | | int numberToGet = pageSize; |
| 2328 | 112 | | int startPosition = 0; |
| | 113 | |
|
| 2328 | 114 | | if (pageNumber != 1) |
| 36 | 115 | | { |
| 36 | 116 | | startPosition = (pageNumber - 1) * pageSize; |
| 36 | 117 | | } |
| | 118 | |
|
| 2328 | 119 | | if (list.Count < pageSize) |
| 2124 | 120 | | { |
| 2124 | 121 | | numberToGet = list.Count; |
| 2124 | 122 | | } |
| 204 | 123 | | else if (list.Count - pageSize * (pageNumber - 1) < pageSize) |
| 18 | 124 | | { |
| 18 | 125 | | numberToGet = list.Count - (pageSize * (pageNumber - 1)); |
| 18 | 126 | | } |
| | 127 | |
|
| 2328 | 128 | | var recordsToReturn = startPosition + numberToGet > list.Count ? new List<Entity>() : list.GetRange(startPos |
| | 129 | |
|
| 18083 | 130 | | recordsToReturn.ForEach(e => e.ApplyDateBehaviour(ctx)); |
| 18083 | 131 | | recordsToReturn.ForEach(e => PopulateFormattedValues(e)); |
| | 132 | |
|
| 2328 | 133 | | var response = new RetrieveMultipleResponse |
| 2328 | 134 | | { |
| 2328 | 135 | | Results = new ParameterCollection |
| 2328 | 136 | | { |
| 2328 | 137 | | { "EntityCollection", new EntityCollection(recordsToReturn) } |
| 2328 | 138 | | } |
| 2328 | 139 | | }; |
| 2328 | 140 | | response.EntityCollection.EntityName = entityName; |
| 2328 | 141 | | response.EntityCollection.MoreRecords = (list.Count - pageSize * pageNumber) > 0; |
| 2328 | 142 | | response.EntityCollection.TotalRecordCount = totalRecordCount; |
| | 143 | |
|
| 2328 | 144 | | if (response.EntityCollection.MoreRecords) |
| 66 | 145 | | { |
| 66 | 146 | | var first = response.EntityCollection.Entities.First(); |
| 66 | 147 | | var last = response.EntityCollection.Entities.Last(); |
| 66 | 148 | | response.EntityCollection.PagingCookie = $"<cookie page=\"{pageNumber}\"><{first.LogicalName}id last=\"{ |
| 66 | 149 | | } |
| | 150 | |
|
| 2328 | 151 | | return response; |
| 2328 | 152 | | } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Populates the formmated values property of this entity record based on the proxy types |
| | 156 | | /// </summary> |
| | 157 | | /// <param name="e"></param> |
| | 158 | | protected void PopulateFormattedValues(Entity e) |
| 15755 | 159 | | { |
| | 160 | | // Iterate through attributes and retrieve formatted values based on type |
| 80763 | 161 | | foreach (var attKey in e.Attributes.Keys) |
| 16749 | 162 | | { |
| 16749 | 163 | | var value = e[attKey]; |
| 16749 | 164 | | string formattedValue = ""; |
| 16749 | 165 | | if (!e.FormattedValues.ContainsKey(attKey) && (value != null)) |
| 16731 | 166 | | { |
| | 167 | | bool bShouldAdd; |
| 16731 | 168 | | formattedValue = this.GetFormattedValueForValue(value, out bShouldAdd); |
| 16731 | 169 | | if (bShouldAdd) |
| 6 | 170 | | { |
| 6 | 171 | | e.FormattedValues.Add(attKey, formattedValue); |
| 6 | 172 | | } |
| 16731 | 173 | | } |
| 16749 | 174 | | } |
| 15755 | 175 | | } |
| | 176 | |
|
| | 177 | | protected string GetFormattedValueForValue(object value, out bool bShouldAddFormattedValue) |
| 20827 | 178 | | { |
| 20827 | 179 | | bShouldAddFormattedValue = false; |
| 20827 | 180 | | var sFormattedValue = string.Empty; |
| | 181 | |
|
| 20827 | 182 | | if (value is Enum) |
| 6 | 183 | | { |
| | 184 | | // Retrieve the enum type |
| 6 | 185 | | sFormattedValue = Enum.GetName(value.GetType(), value); |
| 6 | 186 | | bShouldAddFormattedValue = true; |
| 6 | 187 | | } |
| 20821 | 188 | | else if (value is AliasedValue) |
| 4096 | 189 | | { |
| 4096 | 190 | | return this.GetFormattedValueForValue((value as AliasedValue)?.Value, out bShouldAddFormattedValue); |
| | 191 | | } |
| | 192 | |
|
| 16731 | 193 | | return sFormattedValue; |
| 20827 | 194 | | } |
| | 195 | |
|
| | 196 | | public Type GetResponsibleRequestType() |
| 4270 | 197 | | { |
| 4270 | 198 | | return typeof(RetrieveMultipleRequest); |
| 4270 | 199 | | } |
| | 200 | |
|
| | 201 | | private static List<Entity> GetDistinctEntities(IEnumerable<Entity> input) |
| 42 | 202 | | { |
| 42 | 203 | | var output = new List<Entity>(); |
| | 204 | |
|
| 258 | 205 | | foreach (var entity in input) |
| 66 | 206 | | { |
| 90 | 207 | | if (!output.Any(i => i.LogicalName == entity.LogicalName && i.Attributes.SequenceEqual(entity.Attributes |
| 48 | 208 | | { |
| 48 | 209 | | output.Add(entity); |
| 48 | 210 | | } |
| 66 | 211 | | } |
| | 212 | |
|
| 42 | 213 | | return output; |
| 42 | 214 | | } |
| | 215 | | } |
| | 216 | | } |