| | | 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 AddMemberListRequestExecutor : IFakeMessageExecutor |
| | | 10 | | { |
| | | 11 | | public enum ListCreatedFromCode |
| | | 12 | | { |
| | | 13 | | Account = 1, |
| | | 14 | | Contact = 2, |
| | | 15 | | Lead = 4 |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public bool CanExecute(OrganizationRequest request) |
| | 48 | 19 | | { |
| | 48 | 20 | | return request is AddMemberListRequest; |
| | 48 | 21 | | } |
| | | 22 | | |
| | | 23 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| | 48 | 24 | | { |
| | 48 | 25 | | var req = (AddMemberListRequest)request; |
| | | 26 | | |
| | 48 | 27 | | if ( req.ListId == Guid.Empty) |
| | 6 | 28 | | { |
| | 6 | 29 | | FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "ListId parameter is required"); |
| | 0 | 30 | | } |
| | | 31 | | |
| | 42 | 32 | | if ( req.EntityId == Guid.Empty) |
| | 6 | 33 | | { |
| | 6 | 34 | | FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "EntityId parameter is required"); |
| | 0 | 35 | | } |
| | | 36 | | |
| | 36 | 37 | | var service = ctx.GetOrganizationService(); |
| | | 38 | | |
| | | 39 | | //Find the list |
| | 36 | 40 | | var list = ctx.CreateQuery("list") |
| | 36 | 41 | | .Where(e => e.Id == req.ListId) |
| | 36 | 42 | | .FirstOrDefault(); |
| | | 43 | | |
| | 36 | 44 | | if (list == null) |
| | 6 | 45 | | { |
| | 6 | 46 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("List with Id {0} wasn't found", req.Lis |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | //Find the member |
| | 30 | 50 | | if (!list.Attributes.ContainsKey("createdfromcode")) |
| | 6 | 51 | | { |
| | 6 | 52 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a Created |
| | 0 | 53 | | } |
| | | 54 | | |
| | 24 | 55 | | if (list["createdfromcode"] != null && !(list["createdfromcode"] is OptionSetValue)) |
| | 0 | 56 | | { |
| | 0 | 57 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a Created |
| | 0 | 58 | | } |
| | | 59 | | |
| | 24 | 60 | | var createdFromCodeValue = (list["createdfromcode"] as OptionSetValue).Value; |
| | 24 | 61 | | string memberEntityName = ""; |
| | 24 | 62 | | switch (createdFromCodeValue) |
| | | 63 | | { |
| | | 64 | | case (int)ListCreatedFromCode.Account: |
| | 12 | 65 | | memberEntityName = "account"; |
| | 12 | 66 | | break; |
| | | 67 | | |
| | | 68 | | case (int)ListCreatedFromCode.Contact: |
| | 6 | 69 | | memberEntityName = "contact"; |
| | 6 | 70 | | break; |
| | | 71 | | |
| | | 72 | | case (int)ListCreatedFromCode.Lead: |
| | 6 | 73 | | memberEntityName = "lead"; |
| | 6 | 74 | | break; |
| | | 75 | | |
| | | 76 | | default: |
| | 0 | 77 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a suppo |
| | 0 | 78 | | break; |
| | | 79 | | } |
| | | 80 | | |
| | 24 | 81 | | var member = ctx.CreateQuery(memberEntityName) |
| | 24 | 82 | | .Where(e => e.Id == req.EntityId) |
| | 24 | 83 | | .FirstOrDefault(); |
| | | 84 | | |
| | 24 | 85 | | if (member == null) |
| | 6 | 86 | | { |
| | 6 | 87 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("Member with Id {0} wasn't found", req.E |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | //create member list |
| | 18 | 91 | | var listmember = new Entity("listmember"); |
| | 18 | 92 | | listmember["listid"] = new EntityReference("list", req.ListId); |
| | 18 | 93 | | listmember["entityid"] = new EntityReference(memberEntityName, req.EntityId); |
| | 18 | 94 | | service.Create(listmember); |
| | | 95 | | |
| | 18 | 96 | | return new AddMemberListResponse(); |
| | 18 | 97 | | } |
| | | 98 | | |
| | | 99 | | public Type GetResponsibleRequestType() |
| | 4270 | 100 | | { |
| | 4270 | 101 | | return typeof(AddMemberListRequest); |
| | 4270 | 102 | | } |
| | | 103 | | } |
| | | 104 | | } |