| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using Microsoft.Xrm.Sdk.Messages; |
| | 3 | | using Microsoft.Xrm.Sdk.Query; |
| | 4 | | using System; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 8 | | { |
| | 9 | | public class DisassociateRequestExecutor : IFakeMessageExecutor |
| | 10 | | { |
| | 11 | | public bool CanExecute(OrganizationRequest request) |
| 6 | 12 | | { |
| 6 | 13 | | return request is DisassociateRequest; |
| 6 | 14 | | } |
| | 15 | |
|
| | 16 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 24 | 17 | | { |
| 24 | 18 | | var disassociateRequest = request as DisassociateRequest; |
| 24 | 19 | | var service = ctx.GetFakedOrganizationService(); |
| | 20 | |
|
| 24 | 21 | | if (disassociateRequest == null) |
| 0 | 22 | | { |
| 0 | 23 | | throw new Exception("Only disassociate request can be processed!"); |
| | 24 | | } |
| | 25 | |
|
| 24 | 26 | | var relationShipName = disassociateRequest.Relationship.SchemaName; |
| 24 | 27 | | var relationShip = ctx.GetRelationship(relationShipName); |
| | 28 | |
|
| 24 | 29 | | if (relationShip == null) |
| 0 | 30 | | { |
| 0 | 31 | | throw new Exception(string.Format("Relationship {0} does not exist in the metadata cache", relationShipN |
| | 32 | | } |
| | 33 | |
|
| 24 | 34 | | if (disassociateRequest.Target == null) |
| 0 | 35 | | { |
| 0 | 36 | | throw new Exception("Disassociation without target is invalid!"); |
| | 37 | | } |
| | 38 | |
|
| 144 | 39 | | foreach (var relatedEntity in disassociateRequest.RelatedEntities) |
| 36 | 40 | | { |
| 36 | 41 | | var isFrom1to2 = disassociateRequest.Target.LogicalName == relationShip.Entity1LogicalName |
| 36 | 42 | | || relatedEntity.LogicalName != relationShip.Entity1LogicalName |
| 36 | 43 | | || String.IsNullOrWhiteSpace(disassociateRequest.Target.LogicalName); |
| 36 | 44 | | var fromAttribute = isFrom1to2 ? relationShip.Entity1Attribute : relationShip.Entity2Attribute; |
| 36 | 45 | | var toAttribute = isFrom1to2 ? relationShip.Entity2Attribute : relationShip.Entity1Attribute; |
| | 46 | |
|
| 36 | 47 | | var query = new QueryExpression(relationShip.IntersectEntity) |
| 36 | 48 | | { |
| 36 | 49 | | ColumnSet = new ColumnSet(true), |
| 36 | 50 | | Criteria = new FilterExpression(LogicalOperator.And) |
| 36 | 51 | | }; |
| | 52 | |
|
| 36 | 53 | | query.Criteria.AddCondition(new ConditionExpression(fromAttribute, |
| 36 | 54 | | ConditionOperator.Equal, disassociateRequest.Target.Id)); |
| 36 | 55 | | query.Criteria.AddCondition(new ConditionExpression(toAttribute, |
| 36 | 56 | | ConditionOperator.Equal, relatedEntity.Id)); |
| | 57 | |
|
| 36 | 58 | | var results = service.RetrieveMultiple(query); |
| | 59 | |
|
| 36 | 60 | | if (results.Entities.Count == 1) |
| 36 | 61 | | { |
| 36 | 62 | | service.Delete(relationShip.IntersectEntity, results.Entities.First().Id); |
| 36 | 63 | | } |
| 36 | 64 | | } |
| | 65 | |
|
| 24 | 66 | | return new DisassociateResponse(); |
| 24 | 67 | | } |
| | 68 | |
|
| | 69 | | public Type GetResponsibleRequestType() |
| 4270 | 70 | | { |
| 4270 | 71 | | return typeof(DisassociateRequest); |
| 4270 | 72 | | } |
| | 73 | | } |
| | 74 | | } |