| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using Microsoft.Xrm.Sdk.Messages; |
| | 3 | | using System; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 7 | | { |
| | 8 | | public class RetrieveRelationshipRequestExecutor : IFakeMessageExecutor |
| | 9 | | { |
| | 10 | | public bool CanExecute(OrganizationRequest request) |
| 18 | 11 | | { |
| 18 | 12 | | return request is RetrieveRelationshipRequest; |
| 18 | 13 | | } |
| | 14 | |
|
| | 15 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 18 | 16 | | { |
| 18 | 17 | | var retrieveRequest = request as RetrieveRelationshipRequest; |
| 18 | 18 | | if (retrieveRequest == null) |
| 0 | 19 | | { |
| 0 | 20 | | throw new Exception("Only RetrieveRelationshipRequest can be processed!"); |
| | 21 | | } |
| | 22 | |
|
| 18 | 23 | | var service = ctx.GetFakedOrganizationService(); |
| 18 | 24 | | var fakeRelationShip = ctx.GetRelationship(retrieveRequest.Name); |
| 18 | 25 | | if (fakeRelationShip == null) |
| 0 | 26 | | { |
| 0 | 27 | | throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", retrieveReque |
| | 28 | | } |
| | 29 | |
|
| | 30 | |
|
| 18 | 31 | | var response = new RetrieveRelationshipResponse(); |
| 18 | 32 | | response.Results = new ParameterCollection(); |
| 18 | 33 | | response.Results.Add("RelationshipMetadata", GetRelationshipMetadata(fakeRelationShip)); |
| 18 | 34 | | response.ResponseName = "RetrieveRelationship"; |
| | 35 | |
|
| 18 | 36 | | return response; |
| 18 | 37 | | } |
| | 38 | |
|
| | 39 | | private static object GetRelationshipMetadata(XrmFakedRelationship fakeRelationShip) |
| 18 | 40 | | { |
| 18 | 41 | | if (fakeRelationShip.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.ManyToMany) |
| 6 | 42 | | { |
| 6 | 43 | | var mtm = new Microsoft.Xrm.Sdk.Metadata.ManyToManyRelationshipMetadata(); |
| 6 | 44 | | mtm.Entity1LogicalName = fakeRelationShip.Entity1LogicalName; |
| 6 | 45 | | mtm.Entity1IntersectAttribute = fakeRelationShip.Entity1Attribute; |
| 6 | 46 | | mtm.Entity2LogicalName = fakeRelationShip.Entity2LogicalName; |
| 6 | 47 | | mtm.Entity2IntersectAttribute = fakeRelationShip.Entity2Attribute; |
| 6 | 48 | | mtm.SchemaName = fakeRelationShip.IntersectEntity; |
| 6 | 49 | | mtm.IntersectEntityName = fakeRelationShip.IntersectEntity.ToLower(); |
| 6 | 50 | | return mtm; |
| 12 | 51 | | } else { |
| | 52 | |
|
| 12 | 53 | | var otm = new Microsoft.Xrm.Sdk.Metadata.OneToManyRelationshipMetadata(); |
| | 54 | | #if FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9 |
| 6 | 55 | | otm.ReferencedEntityNavigationPropertyName = fakeRelationShip.IntersectEntity; |
| | 56 | | #endif |
| 12 | 57 | | otm.ReferencingAttribute = fakeRelationShip.Entity1Attribute; |
| 12 | 58 | | otm.ReferencingEntity = fakeRelationShip.Entity1LogicalName; |
| 12 | 59 | | otm.ReferencedAttribute = fakeRelationShip.Entity2Attribute; |
| 12 | 60 | | otm.ReferencedEntity = fakeRelationShip.Entity2LogicalName; |
| 12 | 61 | | otm.SchemaName = fakeRelationShip.IntersectEntity; |
| 12 | 62 | | return otm; |
| | 63 | | } |
| 18 | 64 | | } |
| | 65 | |
|
| | 66 | | public Type GetResponsibleRequestType() |
| 4270 | 67 | | { |
| 4270 | 68 | | return typeof(RetrieveRelationshipRequest); |
| 4270 | 69 | | } |
| | 70 | | } |
| | 71 | | } |