| | | 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 AddListMembersListRequestExecutor : IFakeMessageExecutor |
| | | 10 | | { |
| | | 11 | | public enum ListCreatedFromCode |
| | | 12 | | { |
| | | 13 | | Account = 1, |
| | | 14 | | Contact = 2, |
| | | 15 | | Lead = 4 |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public bool CanExecute(OrganizationRequest request) |
| | 54 | 19 | | { |
| | 54 | 20 | | return request is AddListMembersListRequest; |
| | 54 | 21 | | } |
| | | 22 | | |
| | | 23 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| | 54 | 24 | | { |
| | 54 | 25 | | var req = (AddListMembersListRequest)request; |
| | | 26 | | |
| | 54 | 27 | | if (req.MemberIds == null) |
| | 0 | 28 | | { |
| | 0 | 29 | | FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "Required field 'MemberIds' is missing"); |
| | 0 | 30 | | } |
| | | 31 | | |
| | 54 | 32 | | if (req.ListId == Guid.Empty) |
| | 6 | 33 | | { |
| | 6 | 34 | | FakeOrganizationServiceFault.Throw(ErrorCodes.InvalidArgument, "Expected non-empty Guid."); |
| | 0 | 35 | | } |
| | | 36 | | |
| | 48 | 37 | | var service = ctx.GetOrganizationService(); |
| | | 38 | | |
| | | 39 | | //Find the list |
| | 48 | 40 | | var list = ctx.CreateQuery("list") |
| | 48 | 41 | | .Where(e => e.Id == req.ListId) |
| | 48 | 42 | | .FirstOrDefault(); |
| | | 43 | | |
| | 48 | 44 | | if (list == null) |
| | 12 | 45 | | { |
| | 12 | 46 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("List with Id {0} wasn't found", req.Lis |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | //Find the member |
| | 36 | 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 | | |
| | 30 | 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 | | |
| | 30 | 60 | | var createdFromCodeValue = (list["createdfromcode"] as OptionSetValue).Value; |
| | 30 | 61 | | string memberEntityName = ""; |
| | 30 | 62 | | switch (createdFromCodeValue) |
| | | 63 | | { |
| | | 64 | | case (int)ListCreatedFromCode.Account: |
| | 18 | 65 | | memberEntityName = "account"; |
| | 18 | 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 | | default: |
| | 0 | 76 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvUnExpected, string.Format("List with Id {0} must have a suppo |
| | 0 | 77 | | break; |
| | | 78 | | } |
| | | 79 | | |
| | 150 | 80 | | foreach (var memberId in req.MemberIds) |
| | 36 | 81 | | { |
| | 36 | 82 | | var member = ctx.CreateQuery(memberEntityName) |
| | 36 | 83 | | .Where(e => e.Id == memberId) |
| | 36 | 84 | | .FirstOrDefault(); |
| | | 85 | | |
| | 36 | 86 | | if (member == null) |
| | 12 | 87 | | { |
| | 12 | 88 | | FakeOrganizationServiceFault.Throw(ErrorCodes.IsvAborted, string.Format("Member of type {0} with Id {1} wasn't |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | //create member list |
| | 24 | 92 | | var listmember = new Entity("listmember"); |
| | 24 | 93 | | listmember["listid"] = new EntityReference("list", req.ListId); |
| | 24 | 94 | | listmember["entityid"] = new EntityReference(memberEntityName, memberId); |
| | | 95 | | |
| | 24 | 96 | | service.Create(listmember); |
| | 24 | 97 | | } |
| | | 98 | | |
| | 18 | 99 | | return new AddListMembersListResponse(); |
| | 18 | 100 | | } |
| | | 101 | | |
| | | 102 | | public Type GetResponsibleRequestType() |
| | 4270 | 103 | | { |
| | 4270 | 104 | | return typeof(AddListMembersListRequest); |
| | 4270 | 105 | | } |
| | | 106 | | } |
| | | 107 | | } |