| | | 1 | | using Microsoft.Crm.Sdk.Messages; |
| | | 2 | | using Microsoft.Xrm.Sdk; |
| | | 3 | | using Microsoft.Xrm.Sdk.Query; |
| | | 4 | | using System; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.ServiceModel; |
| | | 7 | | |
| | | 8 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | | 9 | | { |
| | | 10 | | public class InitializeFromRequestExecutor : IFakeMessageExecutor |
| | | 11 | | { |
| | | 12 | | public bool CanExecute(OrganizationRequest request) |
| | 42 | 13 | | { |
| | 42 | 14 | | return request is InitializeFromRequest; |
| | 42 | 15 | | } |
| | | 16 | | |
| | | 17 | | public Type GetResponsibleRequestType() |
| | 4270 | 18 | | { |
| | 4270 | 19 | | return typeof(InitializeFromRequest); |
| | 4270 | 20 | | } |
| | | 21 | | |
| | | 22 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| | 42 | 23 | | { |
| | 42 | 24 | | var req = request as InitializeFromRequest; |
| | 42 | 25 | | if (req == null) |
| | 0 | 26 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Cannot execute Initi |
| | | 27 | | |
| | | 28 | | //TODO: Implement logic to filter mapping attributes based on the req.TargetFieldType |
| | 42 | 29 | | if (req.TargetFieldType != TargetFieldType.All) |
| | 0 | 30 | | throw PullRequestException.PartiallyNotImplementedOrganizationRequest(req.GetType(), "logic for filterin |
| | | 31 | | |
| | 42 | 32 | | var service = ctx.GetOrganizationService(); |
| | 42 | 33 | | var fetchXml = string.Format(FetchMappingsByEntity, req.EntityMoniker.LogicalName, req.TargetEntityName); |
| | 42 | 34 | | var mapping = service.RetrieveMultiple(new FetchExpression(fetchXml)); |
| | 60 | 35 | | var sourceAttributes = mapping.Entities.Select(a => a.GetAttributeValue<AliasedValue>("attributemap.sourceat |
| | 42 | 36 | | var columnSet = sourceAttributes.Length == 0 ? new ColumnSet(true) : new ColumnSet(sourceAttributes); |
| | 42 | 37 | | var source = service.Retrieve(req.EntityMoniker.LogicalName, req.EntityMoniker.Id, columnSet); |
| | | 38 | | |
| | | 39 | | // If we are using proxy types, and the appropriate proxy type is found in |
| | | 40 | | // the assembly create an instance of the appropiate class |
| | | 41 | | // Othersise return a simple Entity |
| | 42 | 42 | | Entity entity = new Entity |
| | 42 | 43 | | { |
| | 42 | 44 | | LogicalName = req.TargetEntityName, |
| | 42 | 45 | | Id = Guid.Empty |
| | 42 | 46 | | }; |
| | | 47 | | |
| | 42 | 48 | | if (ctx.ProxyTypesAssembly != null) |
| | 36 | 49 | | { |
| | 36 | 50 | | var subClassType = ctx.FindReflectedType(req.TargetEntityName); |
| | 36 | 51 | | if (subClassType != null) |
| | 36 | 52 | | { |
| | 36 | 53 | | var instance = Activator.CreateInstance(subClassType); |
| | 36 | 54 | | entity = (Entity) instance; |
| | 36 | 55 | | } |
| | 36 | 56 | | } |
| | | 57 | | |
| | 42 | 58 | | if (mapping.Entities.Count > 0) |
| | 18 | 59 | | { |
| | 114 | 60 | | foreach (var attr in source.Attributes) |
| | 30 | 61 | | { |
| | 60 | 62 | | var mappingEntity = mapping.Entities.FirstOrDefault(e => e.GetAttributeValue<AliasedValue>("attribut |
| | 42 | 63 | | if (mappingEntity == null) continue; |
| | 18 | 64 | | var targetAttribute = mappingEntity.GetAttributeValue<AliasedValue>("attributemap.targetattributenam |
| | 18 | 65 | | entity[targetAttribute] = attr.Value; |
| | | 66 | | |
| | 18 | 67 | | var isEntityReference = string.Equals(attr.Key, source.LogicalName + "id", StringComparison.CurrentC |
| | 18 | 68 | | if (isEntityReference) |
| | 6 | 69 | | { |
| | 6 | 70 | | entity[targetAttribute] = new EntityReference(source.LogicalName, (Guid)attr.Value); |
| | 6 | 71 | | } |
| | | 72 | | else |
| | 12 | 73 | | { |
| | 12 | 74 | | entity[targetAttribute] = attr.Value; |
| | 12 | 75 | | } |
| | 18 | 76 | | } |
| | 18 | 77 | | } |
| | | 78 | | |
| | 42 | 79 | | var response = new InitializeFromResponse |
| | 42 | 80 | | { |
| | 42 | 81 | | Results = |
| | 42 | 82 | | { |
| | 42 | 83 | | ["Entity"] = entity |
| | 42 | 84 | | } |
| | 42 | 85 | | }; |
| | | 86 | | |
| | 42 | 87 | | return response; |
| | 42 | 88 | | } |
| | | 89 | | |
| | | 90 | | private const string FetchMappingsByEntity = @"<fetch version='1.0' mapping='logical' distinct='false'> |
| | | 91 | | <entity name='entitymap'> |
| | | 92 | | <attribute name='sourceentityname'/> |
| | | 93 | | <attribute name='targetentityname'/> |
| | | 94 | | <link-entity name='attributemap' alias='attributemap' to=' |
| | | 95 | | <attribute name='sourceattributename'/> |
| | | 96 | | <attribute name='targetattributename'/> |
| | | 97 | | </link-entity> |
| | | 98 | | <filter type='and'> |
| | | 99 | | <condition attribute='sourceentityname' operator='eq' v |
| | | 100 | | <condition attribute='targetentityname' operator='eq' v |
| | | 101 | | </filter> |
| | | 102 | | </entity> |
| | | 103 | | </fetch>"; |
| | | 104 | | } |
| | | 105 | | } |