| | 1 | | using System; |
| | 2 | | using System.ServiceModel; |
| | 3 | | using Microsoft.Crm.Sdk.Messages; |
| | 4 | | using Microsoft.Xrm.Sdk; |
| | 5 | | using Microsoft.Xrm.Sdk.Query; |
| | 6 | |
|
| | 7 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 8 | | { |
| | 9 | | public class BulkDeleteRequestExecutor : IFakeMessageExecutor |
| | 10 | | { |
| | 11 | | public bool CanExecute(OrganizationRequest request) |
| 12 | 12 | | { |
| 12 | 13 | | return request is BulkDeleteRequest; |
| 12 | 14 | | } |
| | 15 | |
|
| | 16 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 30 | 17 | | { |
| 30 | 18 | | var bulkDeleteRequest = (BulkDeleteRequest)request; |
| | 19 | |
|
| 30 | 20 | | if (string.IsNullOrEmpty(bulkDeleteRequest.JobName)) |
| 6 | 21 | | { |
| 6 | 22 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not Bulk delete |
| | 23 | | } |
| 24 | 24 | | if (bulkDeleteRequest.QuerySet == null) |
| 6 | 25 | | { |
| 6 | 26 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not Bulk delete |
| | 27 | | } |
| 18 | 28 | | if (bulkDeleteRequest.CCRecipients == null) |
| 6 | 29 | | { |
| 6 | 30 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not Bulk delete |
| | 31 | | } |
| 12 | 32 | | if (bulkDeleteRequest.ToRecipients == null) |
| 6 | 33 | | { |
| 6 | 34 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not Bulk delete |
| | 35 | | } |
| | 36 | |
|
| 6 | 37 | | var service = ctx.GetOrganizationService(); |
| | 38 | |
|
| | 39 | | // generate JobId |
| 6 | 40 | | var jobId = Guid.NewGuid(); |
| | 41 | |
|
| | 42 | | // create related asyncOperation |
| 6 | 43 | | Entity asyncOpertation = new Entity("asyncoperation") |
| 6 | 44 | | { |
| 6 | 45 | | Id = jobId |
| 6 | 46 | | }; |
| | 47 | |
|
| 6 | 48 | | service.Create(asyncOpertation); |
| | 49 | |
|
| | 50 | | // delete all records from all queries |
| 30 | 51 | | foreach (QueryExpression queryExpression in bulkDeleteRequest.QuerySet) |
| 6 | 52 | | { |
| 6 | 53 | | EntityCollection recordsToDelete = service.RetrieveMultiple(queryExpression); |
| 42 | 54 | | foreach (Entity record in recordsToDelete.Entities) |
| 12 | 55 | | { |
| 12 | 56 | | service.Delete(record.LogicalName, record.Id); |
| 12 | 57 | | } |
| 6 | 58 | | } |
| | 59 | |
|
| | 60 | | // set ayncoperation to completed |
| 6 | 61 | | asyncOpertation["statecode"] = new OptionSetValue(3); |
| 6 | 62 | | service.Update(asyncOpertation); |
| | 63 | |
|
| | 64 | | // return result |
| 6 | 65 | | return new BulkDeleteResponse { ResponseName = "BulkDeleteResponse", ["JobId"] = jobId}; |
| 6 | 66 | | } |
| | 67 | |
|
| | 68 | | public Type GetResponsibleRequestType() |
| 4270 | 69 | | { |
| 4270 | 70 | | return typeof(BulkDeleteRequest); |
| 4270 | 71 | | } |
| | 72 | | } |
| | 73 | | } |