| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using Microsoft.Xrm.Sdk.Messages; |
| | 3 | | using Microsoft.Xrm.Sdk.Metadata; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Text; |
| | 7 | | using System.Linq; |
| | 8 | | using Microsoft.Xrm.Sdk.Client; |
| | 9 | |
|
| | 10 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 11 | | { |
| | 12 | | public class RetrieveEntityRequestExecutor : IFakeMessageExecutor |
| | 13 | | { |
| | 14 | |
|
| | 15 | | public bool CanExecute(OrganizationRequest request) |
| 30 | 16 | | { |
| 30 | 17 | | return request is RetrieveEntityRequest; |
| 30 | 18 | | } |
| | 19 | | public static Type GetEntityProxyType(string entityName, XrmFakedContext ctx) |
| 24 | 20 | | { |
| | 21 | | //Find the reflected type in the proxy types assembly |
| 24 | 22 | | var assembly = ctx.ProxyTypesAssembly; |
| 24 | 23 | | var subClassType = assembly.GetTypes() |
| 3114 | 24 | | .Where(t => typeof(Entity).IsAssignableFrom(t)) |
| 1908 | 25 | | .Where(t => t.GetCustomAttributes(typeof(EntityLogicalNameAttribute), true).Length > 0) |
| 1908 | 26 | | .Where(t => ((EntityLogicalNameAttribute)t.GetCustomAttributes(typeof(EntityLogicalNameAttribute), t |
| 24 | 27 | | .FirstOrDefault(); |
| | 28 | |
|
| 24 | 29 | | if (subClassType == null) |
| 0 | 30 | | { |
| 0 | 31 | | throw new Exception(string.Format("Entity {0} was not found in the proxy types", entityName)); |
| | 32 | | } |
| 24 | 33 | | return subClassType; |
| 24 | 34 | | } |
| | 35 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 30 | 36 | | { |
| 30 | 37 | | var req = request as RetrieveEntityRequest; |
| | 38 | |
|
| 30 | 39 | | if (string.IsNullOrWhiteSpace(req.LogicalName)) |
| 6 | 40 | | { |
| 6 | 41 | | throw new Exception("A logical name property must be specified in the request"); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | // HasFlag -> used to verify flag matches --> to verify EntityFilters.Entity | EntityFilters.Attributes |
| 24 | 45 | | if (req.EntityFilters.HasFlag(Microsoft.Xrm.Sdk.Metadata.EntityFilters.Entity) || |
| 24 | 46 | | req.EntityFilters.HasFlag(Microsoft.Xrm.Sdk.Metadata.EntityFilters.Attributes)) |
| 12 | 47 | | { |
| 12 | 48 | | if(!ctx.EntityMetadata.ContainsKey(req.LogicalName)) |
| 0 | 49 | | { |
| 0 | 50 | | throw new Exception($"Entity '{req.LogicalName}' is not found in the metadata cache"); |
| | 51 | | } |
| | 52 | |
|
| 12 | 53 | | var entityMetadata = ctx.GetEntityMetadataByName(req.LogicalName); |
| | 54 | |
|
| 12 | 55 | | var response = new RetrieveEntityResponse() |
| 12 | 56 | | { |
| 12 | 57 | | Results = new ParameterCollection |
| 12 | 58 | | { |
| 12 | 59 | | { "EntityMetadata", entityMetadata } |
| 12 | 60 | | } |
| 12 | 61 | | }; |
| | 62 | |
|
| 12 | 63 | | return response; |
| | 64 | | } |
| | 65 | |
|
| 12 | 66 | | throw new Exception("At least EntityFilters.Entity or EntityFilters.Attributes must be present on EntityFilt |
| 12 | 67 | | } |
| | 68 | |
|
| | 69 | | public Type GetResponsibleRequestType() |
| 4270 | 70 | | { |
| 4270 | 71 | | return typeof(RetrieveEntityRequest); |
| 4270 | 72 | | } |
| | 73 | | } |
| | 74 | | } |