| | 1 | | using Microsoft.Crm.Sdk.Messages; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using System; |
| | 4 | | using System.Linq; |
| | 5 | | using System.ServiceModel; |
| | 6 | |
|
| | 7 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 8 | | { |
| | 9 | | public class CloseIncidentRequestExecutor : IFakeMessageExecutor |
| | 10 | | { |
| | 11 | | private const string AttributeIncidentId = "incidentid"; |
| | 12 | | private const string AttributeSubject = "subject"; |
| | 13 | | private const string IncidentLogicalName = "incident"; |
| | 14 | | private const string IncidentResolutionLogicalName = "incidentresolution"; |
| | 15 | | private const int StateResolved = 1; |
| | 16 | |
|
| | 17 | | public bool CanExecute(OrganizationRequest request) |
| 6 | 18 | | { |
| 6 | 19 | | return request is CloseIncidentRequest; |
| 6 | 20 | | } |
| | 21 | |
|
| | 22 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 24 | 23 | | { |
| 24 | 24 | | var service = ctx.GetOrganizationService(); |
| 24 | 25 | | var closeIncidentRequest = (CloseIncidentRequest)request; |
| | 26 | |
|
| 24 | 27 | | var incidentResolution = closeIncidentRequest.IncidentResolution; |
| 24 | 28 | | if (incidentResolution == null) |
| 6 | 29 | | { |
| 6 | 30 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Cannot close inciden |
| | 31 | | } |
| | 32 | |
|
| 18 | 33 | | var status = closeIncidentRequest.Status; |
| 18 | 34 | | if (status == null) |
| 6 | 35 | | { |
| 6 | 36 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Cannot close inciden |
| | 37 | | } |
| | 38 | |
|
| 12 | 39 | | var incidentId = (EntityReference)incidentResolution[AttributeIncidentId]; |
| 12 | 40 | | if (ctx.Data.ContainsKey(IncidentLogicalName) && |
| 24 | 41 | | ctx.Data[IncidentLogicalName].Values.SingleOrDefault(p => p.Id == incidentId.Id) == null) |
| 6 | 42 | | { |
| 6 | 43 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), |
| 6 | 44 | | string.Format("Incident with id {0} not found.", incidentId.Id)); |
| | 45 | | } |
| | 46 | |
|
| 6 | 47 | | var newIncidentResolution = new Entity |
| 6 | 48 | | { |
| 6 | 49 | | LogicalName = IncidentResolutionLogicalName, |
| 6 | 50 | | Attributes = new AttributeCollection |
| 6 | 51 | | { |
| 6 | 52 | | { "description", incidentResolution[AttributeSubject] }, |
| 6 | 53 | | { AttributeSubject, incidentResolution[AttributeSubject] }, |
| 6 | 54 | | { AttributeIncidentId, incidentId } |
| 6 | 55 | | } |
| 6 | 56 | | }; |
| 6 | 57 | | service.Create(newIncidentResolution); |
| | 58 | |
|
| 6 | 59 | | var setState = new SetStateRequest |
| 6 | 60 | | { |
| 6 | 61 | | EntityMoniker = incidentId, |
| 6 | 62 | | Status = status, |
| 6 | 63 | | State = new OptionSetValue(StateResolved) |
| 6 | 64 | | }; |
| | 65 | |
|
| 6 | 66 | | service.Execute(setState); |
| | 67 | |
|
| 6 | 68 | | return new CloseIncidentResponse(); |
| 6 | 69 | | } |
| | 70 | |
|
| | 71 | | public Type GetResponsibleRequestType() |
| 4270 | 72 | | { |
| 4270 | 73 | | return typeof(CloseIncidentResponse); |
| 4270 | 74 | | } |
| | 75 | | } |
| | 76 | | } |