| | 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 AssociateRequestExecutor : IFakeMessageExecutor |
| | 9 | | { |
| | 10 | | public bool CanExecute(OrganizationRequest request) |
| 36 | 11 | | { |
| 36 | 12 | | return request is AssociateRequest; |
| 36 | 13 | | } |
| | 14 | |
|
| | 15 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 180 | 16 | | { |
| 180 | 17 | | var associateRequest = request as AssociateRequest; |
| 180 | 18 | | var service = ctx.GetFakedOrganizationService(); |
| | 19 | |
|
| 180 | 20 | | if (associateRequest == null) |
| 6 | 21 | | { |
| 6 | 22 | | throw new Exception("Only associate request can be processed!"); |
| | 23 | | } |
| | 24 | |
|
| 174 | 25 | | var associateRelationship = associateRequest.Relationship; |
| 174 | 26 | | var relationShipName = associateRelationship.SchemaName; |
| 174 | 27 | | var fakeRelationShip = ctx.GetRelationship(relationShipName); |
| | 28 | |
|
| 174 | 29 | | if (fakeRelationShip == null) |
| 6 | 30 | | { |
| 6 | 31 | | throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipN |
| | 32 | | } |
| | 33 | |
|
| 168 | 34 | | if (associateRequest.Target == null) |
| 6 | 35 | | { |
| 6 | 36 | | throw new Exception("Association without target is invalid!"); |
| | 37 | | } |
| | 38 | |
|
| 1494 | 39 | | foreach (var relatedEntityReference in associateRequest.RelatedEntities) |
| 510 | 40 | | { |
| 510 | 41 | | if (fakeRelationShip.RelationshipType == XrmFakedRelationship.enmFakeRelationshipType.ManyToMany) |
| 498 | 42 | | { |
| 498 | 43 | | var isFrom1to2 = associateRequest.Target.LogicalName == fakeRelationShip.Entity1LogicalName |
| 498 | 44 | | || relatedEntityReference.LogicalName != fakeRelationShip.Entity1LogicalName |
| 498 | 45 | | || String.IsNullOrWhiteSpace(associateRequest.Target.LogicalName); |
| 498 | 46 | | var fromAttribute = isFrom1to2 ? fakeRelationShip.Entity1Attribute : fakeRelationShip.Entity2Attribu |
| 498 | 47 | | var toAttribute = isFrom1to2 ? fakeRelationShip.Entity2Attribute : fakeRelationShip.Entity1Attribute |
| 498 | 48 | | var fromEntityName = isFrom1to2 ? fakeRelationShip.Entity1LogicalName : fakeRelationShip.Entity2Logi |
| 498 | 49 | | var toEntityName = isFrom1to2 ? fakeRelationShip.Entity2LogicalName : fakeRelationShip.Entity1Logica |
| | 50 | |
|
| | 51 | | //Check records exist |
| 498 | 52 | | var targetExists = ctx.CreateQuery(fromEntityName) |
| 498 | 53 | | .Where(e => e.Id == associateRequest.Target.Id) |
| 498 | 54 | | .FirstOrDefault() != null; |
| | 55 | |
|
| 498 | 56 | | if (!targetExists) |
| 6 | 57 | | { |
| 6 | 58 | | throw new Exception(string.Format("{0} with Id {1} doesn't exist", fromEntityName, associateRequ |
| | 59 | | } |
| | 60 | |
|
| 492 | 61 | | var relatedExists = ctx.CreateQuery(toEntityName) |
| 492 | 62 | | .Where(e => e.Id == relatedEntityReference.Id) |
| 492 | 63 | | .FirstOrDefault() != null; |
| | 64 | |
|
| 492 | 65 | | if (!relatedExists) |
| 6 | 66 | | { |
| 6 | 67 | | throw new Exception(string.Format("{0} with Id {1} doesn't exist", toEntityName, relatedEntityRe |
| | 68 | | } |
| | 69 | |
|
| 486 | 70 | | var association = new Entity(fakeRelationShip.IntersectEntity) |
| 486 | 71 | | { |
| 486 | 72 | | Attributes = new AttributeCollection |
| 486 | 73 | | { |
| 486 | 74 | | { fromAttribute, associateRequest.Target.Id }, |
| 486 | 75 | | { toAttribute, relatedEntityReference.Id } |
| 486 | 76 | | } |
| 486 | 77 | | }; |
| | 78 | |
|
| 486 | 79 | | service.Create(association); |
| 486 | 80 | | } |
| | 81 | | else |
| 12 | 82 | | { |
| | 83 | | //One to many |
| | 84 | | //Get entity to update |
| 12 | 85 | | var entityToUpdate = new Entity(relatedEntityReference.LogicalName) |
| 12 | 86 | | { |
| 12 | 87 | | Id = relatedEntityReference.Id |
| 12 | 88 | | }; |
| | 89 | |
|
| 12 | 90 | | entityToUpdate[fakeRelationShip.Entity2Attribute] = associateRequest.Target; |
| 12 | 91 | | service.Update(entityToUpdate); |
| 12 | 92 | | } |
| 498 | 93 | | } |
| | 94 | |
|
| 150 | 95 | | return new AssociateResponse(); |
| 150 | 96 | | } |
| | 97 | |
|
| | 98 | | public Type GetResponsibleRequestType() |
| 4270 | 99 | | { |
| 4270 | 100 | | return typeof(AssociateRequest); |
| 4270 | 101 | | } |
| | 102 | | } |
| | 103 | | } |