| | | 1 | | using Microsoft.Xrm.Sdk.Query; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Text; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Xml.Linq; |
| | | 7 | | using Microsoft.Xrm.Sdk; |
| | | 8 | | using System.Globalization; |
| | | 9 | | |
| | | 10 | | namespace FakeXrmEasy.Extensions.FetchXml |
| | | 11 | | { |
| | | 12 | | public static class XmlExtensionsForFetchXml |
| | | 13 | | { |
| | 6 | 14 | | private static IEnumerable<ConditionOperator> OperatorsNotToConvertArray = new[] |
| | 6 | 15 | | { |
| | 6 | 16 | | #if FAKE_XRM_EASY_2015 || FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9 |
| | 6 | 17 | | ConditionOperator.OlderThanXWeeks, |
| | 6 | 18 | | ConditionOperator.OlderThanXYears, |
| | 6 | 19 | | ConditionOperator.OlderThanXDays, |
| | 6 | 20 | | ConditionOperator.OlderThanXHours, |
| | 6 | 21 | | ConditionOperator.OlderThanXMinutes, |
| | 6 | 22 | | #endif |
| | 6 | 23 | | ConditionOperator.OlderThanXMonths, |
| | 6 | 24 | | ConditionOperator.LastXDays, |
| | 6 | 25 | | ConditionOperator.LastXHours, |
| | 6 | 26 | | ConditionOperator.LastXMonths, |
| | 6 | 27 | | ConditionOperator.LastXWeeks, |
| | 6 | 28 | | ConditionOperator.LastXYears, |
| | 6 | 29 | | ConditionOperator.NextXHours, |
| | 6 | 30 | | ConditionOperator.NextXDays, |
| | 6 | 31 | | ConditionOperator.NextXWeeks, |
| | 6 | 32 | | ConditionOperator.NextXMonths, |
| | 6 | 33 | | ConditionOperator.NextXYears, |
| | 6 | 34 | | ConditionOperator.NextXWeeks, |
| | 6 | 35 | | ConditionOperator.InFiscalYear |
| | 6 | 36 | | }; |
| | | 37 | | |
| | | 38 | | public static bool IsAttributeTrue(this XElement elem, string attributeName) |
| | 4069 | 39 | | { |
| | 4069 | 40 | | var val = elem.GetAttribute(attributeName)?.Value; |
| | | 41 | | |
| | 4069 | 42 | | return "true".Equals(val, StringComparison.InvariantCultureIgnoreCase) |
| | 4069 | 43 | | || "1".Equals(val, StringComparison.InvariantCultureIgnoreCase); |
| | 4069 | 44 | | } |
| | | 45 | | |
| | | 46 | | public static bool IsAggregateFetchXml(this XDocument doc) |
| | 2251 | 47 | | { |
| | 2251 | 48 | | return doc.Root.IsAttributeTrue("aggregate"); |
| | 2251 | 49 | | } |
| | | 50 | | |
| | | 51 | | public static bool IsDistincFetchXml(this XDocument doc) |
| | 1268 | 52 | | { |
| | 1268 | 53 | | return doc.Root.IsAttributeTrue("distinct"); |
| | 1268 | 54 | | } |
| | | 55 | | |
| | | 56 | | public static bool IsFetchXmlNodeValid(this XElement elem) |
| | 8319 | 57 | | { |
| | 8319 | 58 | | switch (elem.Name.LocalName) |
| | | 59 | | { |
| | | 60 | | case "filter": |
| | 1052 | 61 | | return true; |
| | | 62 | | |
| | | 63 | | case "value": |
| | | 64 | | case "fetch": |
| | 1388 | 65 | | return true; |
| | | 66 | | |
| | | 67 | | case "entity": |
| | 1298 | 68 | | return elem.GetAttribute("name") != null; |
| | | 69 | | |
| | | 70 | | case "all-attributes": |
| | 18 | 71 | | return true; |
| | | 72 | | |
| | | 73 | | case "attribute": |
| | 2888 | 74 | | return elem.GetAttribute("name") != null; |
| | | 75 | | |
| | | 76 | | case "link-entity": |
| | 182 | 77 | | return elem.GetAttribute("name") != null |
| | 182 | 78 | | && elem.GetAttribute("from") != null |
| | 182 | 79 | | && elem.GetAttribute("to") != null; |
| | | 80 | | |
| | | 81 | | case "order": |
| | 346 | 82 | | if (elem.Document.IsAggregateFetchXml()) |
| | 24 | 83 | | { |
| | 24 | 84 | | return elem.GetAttribute("alias") != null |
| | 24 | 85 | | && elem.GetAttribute("attribute") == null; |
| | | 86 | | } |
| | | 87 | | else |
| | 322 | 88 | | { |
| | 322 | 89 | | return elem.GetAttribute("attribute") != null; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | case "condition": |
| | 1141 | 93 | | return elem.GetAttribute("attribute") != null |
| | 1141 | 94 | | && elem.GetAttribute("operator") != null; |
| | | 95 | | |
| | | 96 | | default: |
| | 6 | 97 | | throw new Exception(string.Format("Node {0} is not a valid FetchXml node or it doesn't have the requ |
| | | 98 | | } |
| | 8313 | 99 | | } |
| | | 100 | | |
| | | 101 | | public static XAttribute GetAttribute(this XElement elem, string sAttributeName) |
| | 30698 | 102 | | { |
| | 96434 | 103 | | return elem.Attributes().FirstOrDefault((a => a.Name.LocalName.Equals(sAttributeName))); |
| | 30698 | 104 | | } |
| | | 105 | | |
| | | 106 | | public static ColumnSet ToColumnSet(this XElement el) |
| | 1450 | 107 | | { |
| | 1450 | 108 | | var allAttributes = el.Elements() |
| | 5876 | 109 | | .Where(e => e.Name.LocalName.Equals("all-attributes")) |
| | 1450 | 110 | | .FirstOrDefault(); |
| | | 111 | | |
| | 1450 | 112 | | if (allAttributes != null) |
| | 18 | 113 | | { |
| | 18 | 114 | | return new ColumnSet(true); |
| | | 115 | | } |
| | | 116 | | |
| | 1432 | 117 | | var attributes = el.Elements() |
| | 5840 | 118 | | .Where(e => e.Name.LocalName.Equals("attribute")) |
| | 4308 | 119 | | .Select(e => e.GetAttribute("name").Value) |
| | 1432 | 120 | | .ToArray(); |
| | | 121 | | |
| | | 122 | | |
| | 1432 | 123 | | return new ColumnSet(attributes); |
| | 1450 | 124 | | } |
| | | 125 | | |
| | | 126 | | public static int? ToTopCount(this XElement el) |
| | 1238 | 127 | | { |
| | 1238 | 128 | | var countAttr = el.GetAttribute("top"); |
| | 2460 | 129 | | if (countAttr == null) return null; |
| | | 130 | | |
| | | 131 | | int iCount; |
| | 16 | 132 | | if (!int.TryParse(countAttr.Value, out iCount)) |
| | 0 | 133 | | throw new Exception("Top attribute in fetch node must be an integer"); |
| | | 134 | | |
| | 16 | 135 | | return iCount; |
| | 1238 | 136 | | } |
| | | 137 | | |
| | | 138 | | public static int? ToCount(this XElement el) |
| | 1238 | 139 | | { |
| | 1238 | 140 | | var countAttr = el.GetAttribute("count"); |
| | 2440 | 141 | | if (countAttr == null) return null; |
| | | 142 | | |
| | | 143 | | int iCount; |
| | 36 | 144 | | if (!int.TryParse(countAttr.Value, out iCount)) |
| | 6 | 145 | | throw new Exception("Count attribute in fetch node must be an integer"); |
| | | 146 | | |
| | 30 | 147 | | return iCount; |
| | 1232 | 148 | | } |
| | | 149 | | |
| | | 150 | | |
| | | 151 | | public static bool ToReturnTotalRecordCount(this XElement el) |
| | 1232 | 152 | | { |
| | 1232 | 153 | | var returnTotalRecordCountAttr = el.GetAttribute("returntotalrecordcount"); |
| | 2446 | 154 | | if (returnTotalRecordCountAttr == null) return false; |
| | | 155 | | |
| | | 156 | | bool bReturnCount; |
| | 18 | 157 | | if (!bool.TryParse(returnTotalRecordCountAttr.Value, out bReturnCount)) |
| | 0 | 158 | | throw new Exception("returntotalrecordcount attribute in fetch node must be an boolean"); |
| | | 159 | | |
| | 18 | 160 | | return bReturnCount; |
| | 1232 | 161 | | } |
| | | 162 | | |
| | | 163 | | public static int? ToPageNumber(this XElement el) |
| | 1232 | 164 | | { |
| | 1232 | 165 | | var pageAttr = el.GetAttribute("page"); |
| | 2440 | 166 | | if (pageAttr == null) return null; |
| | | 167 | | |
| | | 168 | | int iPage; |
| | 24 | 169 | | if (!int.TryParse(pageAttr.Value, out iPage)) |
| | 0 | 170 | | throw new Exception("Count attribute in fetch node must be an integer"); |
| | | 171 | | |
| | 24 | 172 | | return iPage; |
| | 1232 | 173 | | } |
| | | 174 | | |
| | | 175 | | public static ColumnSet ToColumnSet(this XDocument xlDoc) |
| | 1268 | 176 | | { |
| | | 177 | | //Check if all-attributes exist |
| | 1268 | 178 | | return xlDoc.Elements() //fetch |
| | 1268 | 179 | | .Elements() |
| | 1268 | 180 | | .FirstOrDefault() |
| | 1268 | 181 | | .ToColumnSet(); |
| | 1268 | 182 | | } |
| | | 183 | | |
| | | 184 | | |
| | | 185 | | public static int? ToTopCount(this XDocument xlDoc) |
| | 1238 | 186 | | { |
| | | 187 | | //Check if all-attributes exist |
| | 1238 | 188 | | return xlDoc.Elements() //fetch |
| | 1238 | 189 | | .FirstOrDefault() |
| | 1238 | 190 | | .ToTopCount(); |
| | 1238 | 191 | | } |
| | | 192 | | |
| | | 193 | | public static int? ToCount(this XDocument xlDoc) |
| | 1238 | 194 | | { |
| | | 195 | | //Check if all-attributes exist |
| | 1238 | 196 | | return xlDoc.Elements() //fetch |
| | 1238 | 197 | | .FirstOrDefault() |
| | 1238 | 198 | | .ToCount(); |
| | 1232 | 199 | | } |
| | | 200 | | |
| | | 201 | | public static bool ToReturnTotalRecordCount(this XDocument xlDoc) |
| | 1232 | 202 | | { |
| | 1232 | 203 | | return xlDoc.Elements() //fetch |
| | 1232 | 204 | | .FirstOrDefault() |
| | 1232 | 205 | | .ToReturnTotalRecordCount(); |
| | 1232 | 206 | | } |
| | | 207 | | |
| | | 208 | | |
| | | 209 | | public static int? ToPageNumber(this XDocument xlDoc) |
| | 1232 | 210 | | { |
| | | 211 | | //Check if all-attributes exist |
| | 1232 | 212 | | return xlDoc.Elements() //fetch |
| | 1232 | 213 | | .FirstOrDefault() |
| | 1232 | 214 | | .ToPageNumber(); |
| | 1232 | 215 | | } |
| | | 216 | | |
| | | 217 | | public static FilterExpression ToCriteria(this XDocument xlDoc, XrmFakedContext ctx) |
| | 1268 | 218 | | { |
| | 1268 | 219 | | return xlDoc.Elements() //fetch |
| | 1268 | 220 | | .Elements() //entity |
| | 1268 | 221 | | .Elements() //child nodes of entity |
| | 5382 | 222 | | .Where(el => el.Name.LocalName.Equals("filter")) |
| | 2230 | 223 | | .Select(el => el.ToFilterExpression(ctx)) |
| | 1268 | 224 | | .FirstOrDefault(); |
| | 1238 | 225 | | } |
| | | 226 | | |
| | | 227 | | public static string GetAssociatedEntityNameForConditionExpression(this XElement el) |
| | 1141 | 228 | | { |
| | | 229 | | |
| | 2342 | 230 | | while (el != null) |
| | 2342 | 231 | | { |
| | 2342 | 232 | | var parent = el.Parent; |
| | 2342 | 233 | | if (parent.Name.LocalName.Equals("entity") || parent.Name.LocalName.Equals("link-entity")) |
| | 1141 | 234 | | { |
| | 1141 | 235 | | return parent.GetAttribute("name").Value; |
| | | 236 | | } |
| | 1201 | 237 | | el = parent; |
| | 1201 | 238 | | } |
| | | 239 | | |
| | 0 | 240 | | return null; |
| | 1141 | 241 | | } |
| | | 242 | | |
| | | 243 | | public static LinkEntity ToLinkEntity(this XElement el, XrmFakedContext ctx) |
| | 182 | 244 | | { |
| | | 245 | | //Create this node |
| | 182 | 246 | | var linkEntity = new LinkEntity(); |
| | | 247 | | |
| | 182 | 248 | | linkEntity.LinkFromEntityName = el.Parent.GetAttribute("name").Value; |
| | 182 | 249 | | linkEntity.LinkFromAttributeName = el.GetAttribute("to").Value; |
| | 182 | 250 | | linkEntity.LinkToAttributeName = el.GetAttribute("from").Value; |
| | 182 | 251 | | linkEntity.LinkToEntityName = el.GetAttribute("name").Value; |
| | | 252 | | |
| | 182 | 253 | | if (el.GetAttribute("alias") != null) |
| | 159 | 254 | | { |
| | 159 | 255 | | linkEntity.EntityAlias = el.GetAttribute("alias").Value; |
| | 159 | 256 | | } |
| | | 257 | | |
| | | 258 | | //Join operator |
| | 182 | 259 | | if (el.GetAttribute("link-type") != null) |
| | 110 | 260 | | { |
| | 110 | 261 | | switch (el.GetAttribute("link-type").Value) |
| | | 262 | | { |
| | | 263 | | case "outer": |
| | 30 | 264 | | linkEntity.JoinOperator = JoinOperator.LeftOuter; |
| | 30 | 265 | | break; |
| | | 266 | | default: |
| | 80 | 267 | | linkEntity.JoinOperator = JoinOperator.Inner; |
| | 80 | 268 | | break; |
| | | 269 | | } |
| | 110 | 270 | | } |
| | | 271 | | |
| | | 272 | | //Process other link entities recursively |
| | 182 | 273 | | var convertedLinkEntityNodes = el.Elements() |
| | 460 | 274 | | .Where(e => e.Name.LocalName.Equals("link-entity")) |
| | 200 | 275 | | .Select(e => e.ToLinkEntity(ctx)) |
| | 182 | 276 | | .ToList(); |
| | | 277 | | |
| | 582 | 278 | | foreach (var le in convertedLinkEntityNodes) |
| | 18 | 279 | | { |
| | 18 | 280 | | linkEntity.LinkEntities.Add(le); |
| | 18 | 281 | | } |
| | | 282 | | |
| | | 283 | | //Process column sets |
| | 182 | 284 | | linkEntity.Columns = el.ToColumnSet(); |
| | | 285 | | |
| | | 286 | | //Process filter |
| | 182 | 287 | | linkEntity.LinkCriteria = el.Elements() |
| | 460 | 288 | | .Where(e => e.Name.LocalName.Equals("filter")) |
| | 242 | 289 | | .Select(e => e.ToFilterExpression(ctx)) |
| | 182 | 290 | | .FirstOrDefault(); |
| | | 291 | | |
| | 182 | 292 | | return linkEntity; |
| | 182 | 293 | | } |
| | | 294 | | |
| | | 295 | | public static List<LinkEntity> ToLinkEntities(this XDocument xlDoc, XrmFakedContext ctx) |
| | 1232 | 296 | | { |
| | 1232 | 297 | | return xlDoc.Elements() //fetch |
| | 1232 | 298 | | .Elements() //entity |
| | 1232 | 299 | | .Elements() //child nodes of entity |
| | 5308 | 300 | | .Where(el => el.Name.LocalName.Equals("link-entity")) |
| | 1396 | 301 | | .Select(el => el.ToLinkEntity(ctx)) |
| | 1232 | 302 | | .ToList(); |
| | 1232 | 303 | | } |
| | | 304 | | |
| | | 305 | | public static List<OrderExpression> ToOrderExpressionList(this XDocument xlDoc) |
| | 1124 | 306 | | { |
| | 1124 | 307 | | var orderByElements = xlDoc.Elements() //fetch |
| | 1124 | 308 | | .Elements() //entity |
| | 1124 | 309 | | .Elements() //child nodes of entity |
| | 5044 | 310 | | .Where(el => el.Name.LocalName.Equals("order")) |
| | 1124 | 311 | | .Select(el => |
| | 1434 | 312 | | new OrderExpression |
| | 1434 | 313 | | { |
| | 1434 | 314 | | AttributeName = el.GetAttribute("attribute").Value, |
| | 1434 | 315 | | OrderType = el.IsAttributeTrue("descending") ? OrderType.Descending : OrderT |
| | 1434 | 316 | | }) |
| | 1124 | 317 | | .ToList(); |
| | | 318 | | |
| | 1124 | 319 | | return orderByElements; |
| | 1124 | 320 | | } |
| | | 321 | | |
| | | 322 | | public static FilterExpression ToFilterExpression(this XElement elem, XrmFakedContext ctx) |
| | 1052 | 323 | | { |
| | 1052 | 324 | | var filterExpression = new FilterExpression(); |
| | | 325 | | |
| | 1052 | 326 | | var filterType = elem.GetAttribute("type"); |
| | 1052 | 327 | | if (filterType == null) |
| | 58 | 328 | | { |
| | 58 | 329 | | filterExpression.FilterOperator = LogicalOperator.And; //By default |
| | 58 | 330 | | } |
| | | 331 | | else |
| | 994 | 332 | | { |
| | 994 | 333 | | filterExpression.FilterOperator = filterType.Value.Equals("and") ? |
| | 994 | 334 | | LogicalOperator.And : LogicalOperator.Or; |
| | 994 | 335 | | } |
| | | 336 | | |
| | | 337 | | //Process other filters recursively |
| | 1052 | 338 | | var otherFilters = elem |
| | 1052 | 339 | | .Elements() //child nodes of this filter |
| | 2223 | 340 | | .Where(el => el.Name.LocalName.Equals("filter")) |
| | 1082 | 341 | | .Select(el => el.ToFilterExpression(ctx)) |
| | 1052 | 342 | | .ToList(); |
| | | 343 | | |
| | | 344 | | |
| | | 345 | | //Process conditions |
| | 1052 | 346 | | var conditions = elem |
| | 1052 | 347 | | .Elements() //child nodes of this filter |
| | 2223 | 348 | | .Where(el => el.Name.LocalName.Equals("condition")) |
| | 2193 | 349 | | .Select(el => el.ToConditionExpression(ctx)) |
| | 1052 | 350 | | .ToList(); |
| | | 351 | | |
| | 5288 | 352 | | foreach (var c in conditions) |
| | 1111 | 353 | | filterExpression.AddCondition(c); |
| | | 354 | | |
| | 3126 | 355 | | foreach (var f in otherFilters) |
| | 30 | 356 | | filterExpression.AddFilter(f); |
| | | 357 | | |
| | 1022 | 358 | | return filterExpression; |
| | 1022 | 359 | | } |
| | | 360 | | |
| | | 361 | | public static object ToValue(this XElement elem, XrmFakedContext ctx, string sEntityName, string sAttributeName, |
| | 96 | 362 | | { |
| | 96 | 363 | | return GetConditionExpressionValueCast(elem.Value, ctx, sEntityName, sAttributeName, op); |
| | 84 | 364 | | } |
| | | 365 | | |
| | | 366 | | public static ConditionExpression ToConditionExpression(this XElement elem, XrmFakedContext ctx) |
| | 1141 | 367 | | { |
| | 1141 | 368 | | var conditionExpression = new ConditionExpression(); |
| | | 369 | | |
| | 1141 | 370 | | var conditionEntityName = ""; |
| | | 371 | | |
| | 1141 | 372 | | var attributeName = elem.GetAttribute("attribute").Value; |
| | 1141 | 373 | | ConditionOperator op = ConditionOperator.Equal; |
| | | 374 | | |
| | 1141 | 375 | | string value = null; |
| | 1141 | 376 | | if (elem.GetAttribute("value") != null) |
| | 897 | 377 | | { |
| | 897 | 378 | | value = elem.GetAttribute("value").Value; |
| | 897 | 379 | | } |
| | 1141 | 380 | | if (elem.GetAttribute("entityname") != null) |
| | 20 | 381 | | { |
| | 20 | 382 | | conditionEntityName = elem.GetAttribute("entityname").Value; |
| | 20 | 383 | | } |
| | | 384 | | |
| | 1141 | 385 | | switch (elem.GetAttribute("operator").Value) |
| | | 386 | | { |
| | | 387 | | case "eq": |
| | 363 | 388 | | op = ConditionOperator.Equal; |
| | 363 | 389 | | break; |
| | | 390 | | case "ne": |
| | | 391 | | case "neq": |
| | 24 | 392 | | op = ConditionOperator.NotEqual; |
| | 24 | 393 | | break; |
| | | 394 | | case "begins-with": |
| | 6 | 395 | | op = ConditionOperator.BeginsWith; |
| | 6 | 396 | | break; |
| | | 397 | | case "not-begin-with": |
| | 6 | 398 | | op = ConditionOperator.DoesNotBeginWith; |
| | 6 | 399 | | break; |
| | | 400 | | case "ends-with": |
| | 6 | 401 | | op = ConditionOperator.EndsWith; |
| | 6 | 402 | | break; |
| | | 403 | | case "not-end-with": |
| | 6 | 404 | | op = ConditionOperator.DoesNotEndWith; |
| | 6 | 405 | | break; |
| | | 406 | | case "in": |
| | 7 | 407 | | op = ConditionOperator.In; |
| | 7 | 408 | | break; |
| | | 409 | | case "not-in": |
| | 7 | 410 | | op = ConditionOperator.NotIn; |
| | 7 | 411 | | break; |
| | | 412 | | case "null": |
| | 16 | 413 | | op = ConditionOperator.Null; |
| | 16 | 414 | | break; |
| | | 415 | | case "not-null": |
| | 12 | 416 | | op = ConditionOperator.NotNull; |
| | 12 | 417 | | break; |
| | | 418 | | case "like": |
| | 36 | 419 | | op = ConditionOperator.Like; |
| | | 420 | | |
| | 36 | 421 | | if (value != null) |
| | 36 | 422 | | { |
| | 36 | 423 | | if (value.StartsWith("%") && !value.EndsWith("%")) |
| | 6 | 424 | | op = ConditionOperator.EndsWith; |
| | 30 | 425 | | else if (!value.StartsWith("%") && value.EndsWith("%")) |
| | 6 | 426 | | op = ConditionOperator.BeginsWith; |
| | 24 | 427 | | else if (value.StartsWith("%") && value.EndsWith("%")) |
| | 24 | 428 | | op = ConditionOperator.Contains; |
| | | 429 | | |
| | 36 | 430 | | value = value.Replace("%", ""); |
| | 36 | 431 | | } |
| | 36 | 432 | | break; |
| | | 433 | | case "not-like": |
| | 48 | 434 | | op = ConditionOperator.NotLike; |
| | 48 | 435 | | if (value != null) |
| | 48 | 436 | | { |
| | 48 | 437 | | if (value.StartsWith("%") && !value.EndsWith("%")) |
| | 36 | 438 | | op = ConditionOperator.DoesNotEndWith; |
| | 12 | 439 | | else if (!value.StartsWith("%") && value.EndsWith("%")) |
| | 6 | 440 | | op = ConditionOperator.DoesNotBeginWith; |
| | 6 | 441 | | else if (value.StartsWith("%") && value.EndsWith("%")) |
| | 6 | 442 | | op = ConditionOperator.DoesNotContain; |
| | | 443 | | |
| | 48 | 444 | | value = value.Replace("%", ""); |
| | 48 | 445 | | } |
| | 48 | 446 | | break; |
| | | 447 | | case "gt": |
| | 30 | 448 | | op = ConditionOperator.GreaterThan; |
| | 30 | 449 | | break; |
| | | 450 | | case "ge": |
| | 12 | 451 | | op = ConditionOperator.GreaterEqual; |
| | 12 | 452 | | break; |
| | | 453 | | case "lt": |
| | 36 | 454 | | op = ConditionOperator.LessThan; |
| | 36 | 455 | | break; |
| | | 456 | | case "le": |
| | 18 | 457 | | op = ConditionOperator.LessEqual; |
| | 18 | 458 | | break; |
| | | 459 | | case "on": |
| | 18 | 460 | | op = ConditionOperator.On; |
| | 18 | 461 | | break; |
| | | 462 | | case "on-or-before": |
| | 12 | 463 | | op = ConditionOperator.OnOrBefore; |
| | 12 | 464 | | break; |
| | | 465 | | case "on-or-after": |
| | 6 | 466 | | op = ConditionOperator.OnOrAfter; |
| | 6 | 467 | | break; |
| | | 468 | | case "today": |
| | 6 | 469 | | op = ConditionOperator.Today; |
| | 6 | 470 | | break; |
| | | 471 | | case "yesterday": |
| | 6 | 472 | | op = ConditionOperator.Yesterday; |
| | 6 | 473 | | break; |
| | | 474 | | case "tomorrow": |
| | 6 | 475 | | op = ConditionOperator.Tomorrow; |
| | 6 | 476 | | break; |
| | | 477 | | case "between": |
| | 24 | 478 | | op = ConditionOperator.Between; |
| | 24 | 479 | | break; |
| | | 480 | | case "not-between": |
| | 24 | 481 | | op = ConditionOperator.NotBetween; |
| | 24 | 482 | | break; |
| | | 483 | | case "eq-userid": |
| | 12 | 484 | | op = ConditionOperator.EqualUserId; |
| | 12 | 485 | | break; |
| | | 486 | | case "ne-userid": |
| | 12 | 487 | | op = ConditionOperator.NotEqualUserId; |
| | 12 | 488 | | break; |
| | | 489 | | case "olderthan-x-months": |
| | 30 | 490 | | op = ConditionOperator.OlderThanXMonths; |
| | 30 | 491 | | break; |
| | | 492 | | case "last-seven-days": |
| | 12 | 493 | | op = ConditionOperator.Last7Days; |
| | 12 | 494 | | break; |
| | | 495 | | case "eq-businessid": |
| | 12 | 496 | | op = ConditionOperator.EqualBusinessId; |
| | 12 | 497 | | break; |
| | | 498 | | case "neq-businessid": |
| | 0 | 499 | | op = ConditionOperator.NotEqualBusinessId; |
| | 0 | 500 | | break; |
| | | 501 | | case "next-x-weeks": |
| | 12 | 502 | | op = ConditionOperator.NextXWeeks; |
| | 12 | 503 | | break; |
| | | 504 | | case "next-seven-days": |
| | 12 | 505 | | op = ConditionOperator.Next7Days; |
| | 12 | 506 | | break; |
| | | 507 | | case "this-year": |
| | 6 | 508 | | op = ConditionOperator.ThisYear; |
| | 6 | 509 | | break; |
| | | 510 | | case "last-year": |
| | 6 | 511 | | op = ConditionOperator.LastYear; |
| | 6 | 512 | | break; |
| | | 513 | | case "next-year": |
| | 6 | 514 | | op = ConditionOperator.NextYear; |
| | 6 | 515 | | break; |
| | | 516 | | case "last-x-hours": |
| | 18 | 517 | | op = ConditionOperator.LastXHours; |
| | 18 | 518 | | break; |
| | | 519 | | case "last-x-days": |
| | 18 | 520 | | op = ConditionOperator.LastXDays; |
| | 18 | 521 | | break; |
| | | 522 | | case "last-x-weeks": |
| | 18 | 523 | | op = ConditionOperator.LastXWeeks; |
| | 18 | 524 | | break; |
| | | 525 | | case "last-x-months": |
| | 18 | 526 | | op = ConditionOperator.LastXMonths; |
| | 18 | 527 | | break; |
| | | 528 | | case "last-x-years": |
| | 18 | 529 | | op = ConditionOperator.LastXYears; |
| | 18 | 530 | | break; |
| | | 531 | | case "next-x-hours": |
| | 18 | 532 | | op = ConditionOperator.NextXHours; |
| | 18 | 533 | | break; |
| | | 534 | | case "next-x-days": |
| | 18 | 535 | | op = ConditionOperator.NextXDays; |
| | 18 | 536 | | break; |
| | | 537 | | case "next-x-months": |
| | 18 | 538 | | op = ConditionOperator.NextXMonths; |
| | 18 | 539 | | break; |
| | | 540 | | case "next-x-years": |
| | 18 | 541 | | op = ConditionOperator.NextXYears; |
| | 18 | 542 | | break; |
| | | 543 | | case "this-month": |
| | 6 | 544 | | op = ConditionOperator.ThisMonth; |
| | 6 | 545 | | break; |
| | | 546 | | case "last-month": |
| | 6 | 547 | | op = ConditionOperator.LastMonth; |
| | 6 | 548 | | break; |
| | | 549 | | case "next-month": |
| | 6 | 550 | | op = ConditionOperator.NextMonth; |
| | 6 | 551 | | break; |
| | | 552 | | case "last-week": |
| | 12 | 553 | | op = ConditionOperator.LastWeek; |
| | 12 | 554 | | break; |
| | | 555 | | case "this-week": |
| | 12 | 556 | | op = ConditionOperator.ThisWeek; |
| | 12 | 557 | | break; |
| | | 558 | | case "next-week": |
| | 12 | 559 | | op = ConditionOperator.NextWeek; |
| | 12 | 560 | | break; |
| | | 561 | | case "in-fiscal-year": |
| | 6 | 562 | | op = ConditionOperator.InFiscalYear; |
| | 6 | 563 | | break; |
| | | 564 | | #if !FAKE_XRM_EASY && !FAKE_XRM_EASY_2013 |
| | | 565 | | case "olderthan-x-minutes": |
| | 12 | 566 | | op = ConditionOperator.OlderThanXMinutes; |
| | 12 | 567 | | break; |
| | | 568 | | case "olderthan-x-hours": |
| | 12 | 569 | | op = ConditionOperator.OlderThanXHours; |
| | 12 | 570 | | break; |
| | | 571 | | case "olderthan-x-days": |
| | 12 | 572 | | op = ConditionOperator.OlderThanXDays; |
| | 12 | 573 | | break; |
| | | 574 | | case "olderthan-x-weeks": |
| | 12 | 575 | | op = ConditionOperator.OlderThanXWeeks; |
| | 12 | 576 | | break; |
| | | 577 | | case "olderthan-x-years": |
| | 12 | 578 | | op = ConditionOperator.OlderThanXYears; |
| | 12 | 579 | | break; |
| | | 580 | | #endif |
| | | 581 | | #if FAKE_XRM_EASY_9 |
| | | 582 | | case "contain-values": |
| | 2 | 583 | | op = ConditionOperator.ContainValues; |
| | 2 | 584 | | break; |
| | | 585 | | case "not-contain-values": |
| | 2 | 586 | | op = ConditionOperator.DoesNotContainValues; |
| | 2 | 587 | | break; |
| | | 588 | | #endif |
| | | 589 | | default: |
| | 0 | 590 | | throw PullRequestException.FetchXmlOperatorNotImplemented(elem.GetAttribute("operator").Value); |
| | | 591 | | } |
| | | 592 | | |
| | | 593 | | //Process values |
| | 1141 | 594 | | object[] values = null; |
| | | 595 | | |
| | | 596 | | |
| | 1141 | 597 | | var entityName = GetAssociatedEntityNameForConditionExpression(elem); |
| | | 598 | | |
| | | 599 | | //Find values inside the condition expression, if apply |
| | 1141 | 600 | | values = elem |
| | 1141 | 601 | | .Elements() //child nodes of this filter |
| | 1237 | 602 | | .Where(el => el.Name.LocalName.Equals("value")) |
| | 1237 | 603 | | .Select(el => el.ToValue(ctx, entityName, attributeName, op)) |
| | 1141 | 604 | | .ToArray(); |
| | | 605 | | |
| | | 606 | | |
| | | 607 | | //Otherwise, a single value was used |
| | 1129 | 608 | | if (value != null) |
| | 897 | 609 | | { |
| | | 610 | | #if FAKE_XRM_EASY_2013 || FAKE_XRM_EASY_2015 || FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9 |
| | 760 | 611 | | if (string.IsNullOrWhiteSpace(conditionEntityName)) |
| | 750 | 612 | | { |
| | 750 | 613 | | return new ConditionExpression(attributeName, op, GetConditionExpressionValueCast(value, ctx, entity |
| | | 614 | | } |
| | | 615 | | else |
| | 10 | 616 | | { |
| | 10 | 617 | | return new ConditionExpression(conditionEntityName, attributeName, op, GetConditionExpressionValueCa |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | #else |
| | 137 | 621 | | return new ConditionExpression(attributeName, op, GetConditionExpressionValueCast(value, ctx, entityName |
| | | 622 | | |
| | | 623 | | #endif |
| | | 624 | | } |
| | | 625 | | |
| | | 626 | | #if FAKE_XRM_EASY_2013 || FAKE_XRM_EASY_2015 || FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9 |
| | | 627 | | |
| | 196 | 628 | | if (string.IsNullOrWhiteSpace(conditionEntityName)) |
| | 186 | 629 | | { |
| | 186 | 630 | | return new ConditionExpression(attributeName, op, values); |
| | | 631 | | } |
| | | 632 | | else |
| | 10 | 633 | | { |
| | 10 | 634 | | return new ConditionExpression(conditionEntityName, attributeName, op, values); |
| | | 635 | | } |
| | | 636 | | #else |
| | 36 | 637 | | return new ConditionExpression(attributeName, op, values); |
| | | 638 | | #endif |
| | | 639 | | |
| | | 640 | | |
| | | 641 | | |
| | 1111 | 642 | | } |
| | | 643 | | |
| | | 644 | | |
| | | 645 | | public static object GetValueBasedOnType(Type t, string value) |
| | 563 | 646 | | { |
| | 563 | 647 | | if (t == typeof(int) |
| | 563 | 648 | | || t == typeof(int?) |
| | 563 | 649 | | || t.IsOptionSet() |
| | 563 | 650 | | #if FAKE_XRM_EASY_9 |
| | 563 | 651 | | || t.IsOptionSetValueCollection() |
| | 563 | 652 | | #endif |
| | 563 | 653 | | ) |
| | 72 | 654 | | { |
| | 72 | 655 | | int intValue = 0; |
| | | 656 | | |
| | 72 | 657 | | if (int.TryParse(value, out intValue)) |
| | 72 | 658 | | { |
| | 72 | 659 | | if (t.IsOptionSet()) |
| | 54 | 660 | | { |
| | 54 | 661 | | return new OptionSetValue(intValue); |
| | | 662 | | } |
| | 18 | 663 | | return intValue; |
| | | 664 | | } |
| | | 665 | | else |
| | 0 | 666 | | { |
| | 0 | 667 | | throw new Exception("Integer value expected"); |
| | | 668 | | } |
| | | 669 | | } |
| | | 670 | | |
| | 491 | 671 | | else if (t == typeof(Guid) |
| | 491 | 672 | | || t == typeof(Guid?) |
| | 491 | 673 | | || t == typeof(EntityReference) |
| | 491 | 674 | | #if FAKE_XRM_EASY |
| | 491 | 675 | | || t == typeof(Microsoft.Xrm.Client.CrmEntityReference) |
| | 491 | 676 | | #endif |
| | 491 | 677 | | ) |
| | 59 | 678 | | { |
| | 59 | 679 | | Guid gValue = Guid.Empty; |
| | | 680 | | |
| | 59 | 681 | | if (Guid.TryParse(value, out gValue)) |
| | 59 | 682 | | { |
| | 59 | 683 | | if (t == typeof(EntityReference) |
| | 59 | 684 | | #if FAKE_XRM_EASY |
| | 59 | 685 | | || t == typeof(Microsoft.Xrm.Client.CrmEntityReference) |
| | 59 | 686 | | #endif |
| | 59 | 687 | | ) |
| | 29 | 688 | | { |
| | 29 | 689 | | return new EntityReference() { Id = gValue }; |
| | | 690 | | } |
| | 30 | 691 | | return gValue; |
| | | 692 | | } |
| | | 693 | | else |
| | 0 | 694 | | { |
| | 0 | 695 | | throw new Exception("Guid value expected"); |
| | | 696 | | } |
| | | 697 | | } |
| | 432 | 698 | | else if (t == typeof(decimal) |
| | 432 | 699 | | || t == typeof(decimal?) |
| | 432 | 700 | | || t == typeof(Money)) |
| | 12 | 701 | | { |
| | 12 | 702 | | decimal decValue = 0; |
| | 12 | 703 | | if (decimal.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out decValue)) |
| | 12 | 704 | | { |
| | 12 | 705 | | if (t == typeof(Money)) |
| | 12 | 706 | | { |
| | 12 | 707 | | return new Money(decValue); |
| | | 708 | | } |
| | 0 | 709 | | return decValue; |
| | | 710 | | } |
| | | 711 | | else |
| | 0 | 712 | | { |
| | 0 | 713 | | throw new Exception("Decimal value expected"); |
| | | 714 | | } |
| | | 715 | | } |
| | | 716 | | |
| | 420 | 717 | | else if (t == typeof(double) |
| | 420 | 718 | | || t == typeof(double?)) |
| | 54 | 719 | | { |
| | 54 | 720 | | double dblValue = 0; |
| | 54 | 721 | | if (double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out dblValue)) |
| | 54 | 722 | | { |
| | 54 | 723 | | return dblValue; |
| | | 724 | | } |
| | | 725 | | else |
| | 0 | 726 | | { |
| | 0 | 727 | | throw new Exception("Double value expected"); |
| | | 728 | | } |
| | | 729 | | } |
| | | 730 | | |
| | 366 | 731 | | else if (t == typeof(float) |
| | 366 | 732 | | || t == typeof(float?)) |
| | 0 | 733 | | { |
| | 0 | 734 | | float fltValue = 0; |
| | 0 | 735 | | if (float.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out fltValue)) |
| | 0 | 736 | | { |
| | 0 | 737 | | return fltValue; |
| | | 738 | | } |
| | | 739 | | else |
| | 0 | 740 | | { |
| | 0 | 741 | | throw new Exception("Float value expected"); |
| | | 742 | | } |
| | | 743 | | } |
| | | 744 | | |
| | 366 | 745 | | else if (t == typeof(DateTime) |
| | 366 | 746 | | || t == typeof(DateTime?)) |
| | 84 | 747 | | { |
| | 84 | 748 | | DateTime dtValue = DateTime.MinValue; |
| | 84 | 749 | | if (DateTime.TryParse(value, out dtValue)) |
| | 84 | 750 | | { |
| | 84 | 751 | | return dtValue; |
| | | 752 | | } |
| | | 753 | | else |
| | 0 | 754 | | { |
| | 0 | 755 | | throw new Exception("DateTime value expected"); |
| | | 756 | | } |
| | | 757 | | } |
| | | 758 | | //fix Issue #141 |
| | 282 | 759 | | else if (t == typeof(bool) |
| | 282 | 760 | | || t == typeof(bool?)) |
| | 36 | 761 | | { |
| | 36 | 762 | | bool boolValue = false; |
| | 36 | 763 | | if (bool.TryParse(value, out boolValue)) |
| | 6 | 764 | | { |
| | 6 | 765 | | return boolValue; |
| | | 766 | | } |
| | | 767 | | else |
| | 30 | 768 | | { |
| | 30 | 769 | | switch (value) |
| | | 770 | | { |
| | 18 | 771 | | case "0": return false; |
| | 0 | 772 | | case "1": return true; |
| | | 773 | | default: |
| | 12 | 774 | | throw new Exception("Boolean value expected"); |
| | | 775 | | } |
| | | 776 | | } |
| | | 777 | | } |
| | | 778 | | |
| | | 779 | | //Otherwise, return the string |
| | 246 | 780 | | return value; |
| | 551 | 781 | | } |
| | | 782 | | |
| | | 783 | | public static bool ValueNeedsConverting(ConditionOperator conditionOperator) |
| | 833 | 784 | | { |
| | 833 | 785 | | return !OperatorsNotToConvertArray.Contains(conditionOperator); |
| | 833 | 786 | | } |
| | | 787 | | |
| | | 788 | | public static object GetConditionExpressionValueCast(string value, XrmFakedContext ctx, string sEntityName, stri |
| | 993 | 789 | | { |
| | 993 | 790 | | if (ctx.ProxyTypesAssembly != null) |
| | 833 | 791 | | { |
| | | 792 | | //We have proxy types so get appropiate type value based on entity name and attribute type |
| | 833 | 793 | | var reflectedType = ctx.FindReflectedType(sEntityName); |
| | 833 | 794 | | if (reflectedType != null) |
| | 833 | 795 | | { |
| | 833 | 796 | | var attributeType = ctx.FindReflectedAttributeType(reflectedType, sEntityName, sAttributeName); |
| | 833 | 797 | | if (attributeType != null) |
| | 833 | 798 | | { |
| | | 799 | | try |
| | 833 | 800 | | { |
| | 833 | 801 | | if (ValueNeedsConverting(op)) |
| | 563 | 802 | | { |
| | 563 | 803 | | return GetValueBasedOnType(attributeType, value); |
| | | 804 | | } |
| | | 805 | | |
| | | 806 | | else |
| | 270 | 807 | | { |
| | 270 | 808 | | return int.Parse(value); |
| | | 809 | | } |
| | | 810 | | } |
| | 12 | 811 | | catch (Exception e) |
| | 12 | 812 | | { |
| | 12 | 813 | | throw new Exception(string.Format("When trying to parse value for entity {0} and attribute { |
| | | 814 | | } |
| | | 815 | | |
| | | 816 | | } |
| | 0 | 817 | | } |
| | 0 | 818 | | } |
| | | 819 | | |
| | | 820 | | |
| | | 821 | | //Try parsing a guid |
| | 160 | 822 | | Guid gOut = Guid.Empty; |
| | 160 | 823 | | if (Guid.TryParse(value, out gOut)) |
| | 6 | 824 | | return gOut; |
| | | 825 | | |
| | | 826 | | //Try checking if it is a numeric value, cause, from the fetchxml it |
| | | 827 | | //would be impossible to know the real typed based on the string value only |
| | | 828 | | // ex: "123" might compared as a string, or, as an int, it will depend on the attribute |
| | | 829 | | // data type, therefore, in this case we do need to use proxy types |
| | | 830 | | |
| | 154 | 831 | | bool bIsNumeric = false; |
| | 154 | 832 | | bool bIsDateTime = false; |
| | 154 | 833 | | double dblValue = 0.0; |
| | 154 | 834 | | decimal decValue = 0.0m; |
| | 154 | 835 | | int intValue = 0; |
| | | 836 | | |
| | 154 | 837 | | if (double.TryParse(value, out dblValue)) |
| | 6 | 838 | | bIsNumeric = true; |
| | | 839 | | |
| | 154 | 840 | | if (decimal.TryParse(value, out decValue)) |
| | 6 | 841 | | bIsNumeric = true; |
| | | 842 | | |
| | 154 | 843 | | if (int.TryParse(value, out intValue)) |
| | 0 | 844 | | bIsNumeric = true; |
| | | 845 | | |
| | 154 | 846 | | DateTime dtValue = DateTime.MinValue; |
| | 154 | 847 | | if (DateTime.TryParse(value, out dtValue)) |
| | 18 | 848 | | bIsDateTime = true; |
| | | 849 | | |
| | 154 | 850 | | if (bIsNumeric || bIsDateTime) |
| | 18 | 851 | | { |
| | 18 | 852 | | throw new Exception("When using arithmetic values in Fetch a ProxyTypesAssembly must be used in order to |
| | | 853 | | } |
| | | 854 | | |
| | | 855 | | //Default value |
| | 136 | 856 | | return value; |
| | 963 | 857 | | } |
| | | 858 | | } |
| | | 859 | | } |