| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Text; |
| | | 5 | | using Microsoft.Crm.Sdk.Messages; |
| | | 6 | | using Microsoft.Xrm.Sdk; |
| | | 7 | | using Microsoft.Xrm.Sdk.Messages; |
| | | 8 | | using Microsoft.Xrm.Sdk.Query; |
| | | 9 | | |
| | | 10 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | | 11 | | { |
| | | 12 | | public class ReviseQuoteRequestExecutor : IFakeMessageExecutor |
| | | 13 | | { |
| | | 14 | | public bool CanExecute(OrganizationRequest request) |
| | 6 | 15 | | { |
| | 6 | 16 | | return request is ReviseQuoteRequest; |
| | 6 | 17 | | } |
| | | 18 | | |
| | | 19 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| | 6 | 20 | | { |
| | 6 | 21 | | var service = ctx.GetOrganizationService(); |
| | | 22 | | |
| | 6 | 23 | | var reviseQuoteRequest = request as ReviseQuoteRequest; |
| | | 24 | | |
| | 6 | 25 | | if (reviseQuoteRequest == null) |
| | 0 | 26 | | { |
| | 0 | 27 | | throw new Exception("You did not pass a ReviseQuoteRequest!"); |
| | | 28 | | } |
| | | 29 | | |
| | 6 | 30 | | var oldQuoteId = reviseQuoteRequest.QuoteId; |
| | | 31 | | |
| | 6 | 32 | | if (oldQuoteId == Guid.Empty) |
| | 0 | 33 | | { |
| | 0 | 34 | | throw new Exception("QuoteId needs to be set!"); |
| | | 35 | | } |
| | | 36 | | |
| | 6 | 37 | | var oldQuote = service.Retrieve("quote", oldQuoteId, new ColumnSet(true)); |
| | | 38 | | |
| | 6 | 39 | | var revisedQuote = new Entity |
| | 6 | 40 | | { |
| | 6 | 41 | | LogicalName = "quote", |
| | 6 | 42 | | Id = Guid.NewGuid() |
| | 6 | 43 | | }; |
| | | 44 | | |
| | 6 | 45 | | var columnSet = reviseQuoteRequest.ColumnSet; |
| | 6 | 46 | | var quoteBlackList = new List<string> { "quoteid", "statuscode", "statecode", "createdon", "createdby" }; |
| | | 47 | | |
| | 126 | 48 | | foreach (var attribute in oldQuote.Attributes) |
| | 54 | 49 | | { |
| | 54 | 50 | | if (quoteBlackList.Contains(attribute.Key)) |
| | 30 | 51 | | { |
| | 30 | 52 | | continue; |
| | | 53 | | } |
| | | 54 | | |
| | 24 | 55 | | if (columnSet.AllColumns || columnSet.Columns.Contains(attribute.Key)) |
| | 24 | 56 | | { |
| | 24 | 57 | | if(attribute.Key != "statecode") //Skip statecode on create |
| | 24 | 58 | | revisedQuote[attribute.Key] = attribute.Value; |
| | 24 | 59 | | } |
| | 24 | 60 | | } |
| | | 61 | | |
| | 6 | 62 | | service.Create(revisedQuote); |
| | | 63 | | |
| | 6 | 64 | | var quoteLines = service.RetrieveMultiple(new QueryExpression("quotedetail") |
| | 6 | 65 | | { |
| | 6 | 66 | | ColumnSet = new ColumnSet(true), |
| | 6 | 67 | | Criteria = new FilterExpression(LogicalOperator.And) |
| | 6 | 68 | | { |
| | 6 | 69 | | Conditions = { new ConditionExpression("quoteid", ConditionOperator.Equal, oldQuote.ToEntityReferenc |
| | 6 | 70 | | } |
| | 6 | 71 | | }).Entities.ToList(); |
| | | 72 | | |
| | 30 | 73 | | foreach (var quoteDetail in quoteLines) |
| | 6 | 74 | | { |
| | 6 | 75 | | var revisedDetail = new Entity |
| | 6 | 76 | | { |
| | 6 | 77 | | LogicalName = "quotedetail", |
| | 6 | 78 | | Id = Guid.NewGuid(), |
| | 6 | 79 | | Attributes = new AttributeCollection |
| | 6 | 80 | | { |
| | 6 | 81 | | { "quoteid", revisedQuote.ToEntityReference() } |
| | 6 | 82 | | } |
| | 6 | 83 | | }; |
| | | 84 | | |
| | 6 | 85 | | var quoteDetailBlackList = new List<string> { "quoteid", "quotedetailid", "createdon", "createdby" }; |
| | | 86 | | |
| | 126 | 87 | | foreach (var attribute in quoteDetail.Attributes) |
| | 54 | 88 | | { |
| | 54 | 89 | | if (quoteDetailBlackList.Contains(attribute.Key)) |
| | 24 | 90 | | { |
| | 24 | 91 | | continue; |
| | | 92 | | } |
| | 30 | 93 | | if (attribute.Key != "statecode") //Skip statecode on create |
| | 24 | 94 | | revisedDetail[attribute.Key] = attribute.Value; |
| | 30 | 95 | | } |
| | | 96 | | |
| | 6 | 97 | | service.Create(revisedDetail); |
| | 6 | 98 | | } |
| | | 99 | | |
| | 6 | 100 | | var response = new ReviseQuoteResponse(); |
| | | 101 | | |
| | 6 | 102 | | revisedQuote = service.Retrieve(revisedQuote.LogicalName, revisedQuote.Id, new ColumnSet(true)); |
| | | 103 | | |
| | 6 | 104 | | response.Results["Entity"] = revisedQuote; |
| | | 105 | | |
| | 6 | 106 | | return response; |
| | 6 | 107 | | } |
| | | 108 | | |
| | | 109 | | public Type GetResponsibleRequestType() |
| | 4270 | 110 | | { |
| | 4270 | 111 | | return typeof(ReviseQuoteRequest); |
| | 4270 | 112 | | } |
| | | 113 | | } |
| | | 114 | | } |