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