| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using Microsoft.Xrm.Sdk.Messages; |
| | 3 | | using Microsoft.Xrm.Sdk.Metadata; |
| | 4 | | using System; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Linq; |
| | 7 | | using FakeXrmEasy.Extensions; |
| | 8 | |
|
| | 9 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 10 | | { |
| | 11 | | public class InsertStatusValueRequestExecutor : IFakeMessageExecutor |
| | 12 | | { |
| | 13 | | public bool CanExecute(OrganizationRequest request) |
| 6 | 14 | | { |
| 6 | 15 | | return request is InsertStatusValueRequest; |
| 6 | 16 | | } |
| | 17 | |
|
| | 18 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 6 | 19 | | { |
| 6 | 20 | | var req = request as InsertStatusValueRequest; |
| | 21 | |
|
| | 22 | | Debug.Assert(req != null, nameof(req) + " != null"); |
| 6 | 23 | | if (req.Label == null) |
| 0 | 24 | | throw new Exception("Label must not be null"); |
| | 25 | |
|
| 6 | 26 | | if (string.IsNullOrWhiteSpace(req.Label.LocalizedLabels[0].Label)) |
| 0 | 27 | | { |
| 0 | 28 | | throw new Exception("Label must not be empty"); |
| | 29 | | } |
| | 30 | |
|
| 6 | 31 | | if (string.IsNullOrEmpty(req.OptionSetName) |
| 6 | 32 | | && (string.IsNullOrEmpty(req.EntityLogicalName) |
| 6 | 33 | | || string.IsNullOrEmpty(req.AttributeLogicalName))) |
| 0 | 34 | | { |
| 0 | 35 | | throw new Exception("At least OptionSetName or both the EntityName and AttributeName must not be provide |
| | 36 | | } |
| | 37 | |
|
| 6 | 38 | | var key = !string.IsNullOrWhiteSpace(req.OptionSetName) ? req.OptionSetName : $"{req.EntityLogicalName}#{req |
| | 39 | |
|
| 6 | 40 | | if (!ctx.StatusAttributeMetadata.ContainsKey(key)) |
| 6 | 41 | | ctx.StatusAttributeMetadata.Add(key, new StatusAttributeMetadata()); |
| | 42 | |
|
| 6 | 43 | | var statusValuesMetadata = ctx.StatusAttributeMetadata[key]; |
| | 44 | | //statusValuesMetadata. |
| 6 | 45 | | statusValuesMetadata.OptionSet = new OptionSetMetadata(); |
| 6 | 46 | | statusValuesMetadata.OptionSet.Options.Add(new StatusOptionMetadata() |
| 6 | 47 | | { |
| 6 | 48 | | MetadataId = Guid.NewGuid(), |
| 6 | 49 | | Value = req.Value, |
| 6 | 50 | | Label = req.Label, |
| 6 | 51 | | State = req.StateCode, |
| 6 | 52 | | Description = req.Label |
| 6 | 53 | | }); |
| | 54 | |
|
| | 55 | |
|
| 6 | 56 | | if (!string.IsNullOrEmpty(req.EntityLogicalName)) |
| 6 | 57 | | { |
| 6 | 58 | | var entityMetadata = ctx.GetEntityMetadataByName(req.EntityLogicalName); |
| 6 | 59 | | if (entityMetadata != null) |
| 6 | 60 | | { |
| 6 | 61 | | var attribute = entityMetadata |
| 6 | 62 | | .Attributes |
| 12 | 63 | | .FirstOrDefault(a => a.LogicalName == req.AttributeLogicalName); |
| | 64 | |
|
| 6 | 65 | | if (attribute == null) |
| 0 | 66 | | { |
| 0 | 67 | | throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogica |
| | 68 | | } |
| | 69 | |
|
| 6 | 70 | | if (!(attribute is EnumAttributeMetadata)) |
| 0 | 71 | | { |
| 0 | 72 | | throw new Exception($"You are trying to insert an option set value for entity '{req.EntityLogica |
| | 73 | | } |
| | 74 | |
|
| 6 | 75 | | var enumAttribute = attribute as EnumAttributeMetadata; |
| | 76 | |
|
| 6 | 77 | | var options = enumAttribute.OptionSet == null ? new OptionMetadataCollection() : enumAttribute.Optio |
| | 78 | |
|
| 6 | 79 | | options.Add(new StatusOptionMetadata(){Value = req.Value, Label = req.Label, State = req.StateCode, |
| | 80 | |
|
| 6 | 81 | | enumAttribute.OptionSet = new OptionSetMetadata(options); |
| | 82 | |
|
| 6 | 83 | | entityMetadata.SetAttribute(enumAttribute); |
| 6 | 84 | | ctx.SetEntityMetadata(entityMetadata); |
| 6 | 85 | | } |
| 6 | 86 | | } |
| 6 | 87 | | return new InsertStatusValueResponse(); |
| 6 | 88 | | } |
| | 89 | |
|
| | 90 | | public Type GetResponsibleRequestType() |
| 4270 | 91 | | { |
| 4270 | 92 | | return typeof(InsertStatusValueRequest); |
| 4270 | 93 | | } |
| | 94 | | } |
| | 95 | | } |