| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Text; |
| | 5 | | using System.Globalization; |
| | 6 | | using Microsoft.Xrm.Sdk.Messages; |
| | 7 | | using System.Linq; |
| | 8 | | using Microsoft.Xrm.Sdk.Metadata; |
| | 9 | |
|
| | 10 | | namespace Xrm.Oss.XTL.Interpreter |
| | 11 | | { |
| | 12 | | public static class PropertyStringifier |
| | 13 | | { |
| | 14 | | private static string StringifyProperty(string field, object value, Entity record, IOrganizationService service, |
| 148 | 15 | | { |
| 148 | 16 | | var entityReference = value as EntityReference; |
| 148 | 17 | | var optionSet = value as OptionSetValue; |
| 148 | 18 | | var money = value as Money; |
| 148 | 19 | | var aliasedValue = value as AliasedValue; |
| | 20 | |
|
| 148 | 21 | | if (optionSet != null) |
| 6 | 22 | | { |
| 6 | 23 | | var textValue = optionSet.Value.ToString(CultureInfo.InvariantCulture); |
| | 24 | |
|
| 6 | 25 | | if (config == null) |
| 1 | 26 | | { |
| 1 | 27 | | return textValue; |
| | 28 | | } |
| | 29 | |
|
| 5 | 30 | | var configLanguage = config.GetValue<int>("optionSetLcid", "optionSetLcid must be an int!"); |
| | 31 | |
|
| 5 | 32 | | if (configLanguage == 0) |
| 2 | 33 | | { |
| 2 | 34 | | return textValue; |
| | 35 | | } |
| | 36 | |
|
| 3 | 37 | | var request = new RetrieveAttributeRequest |
| 3 | 38 | | { |
| 3 | 39 | | EntityLogicalName = record.LogicalName, |
| 3 | 40 | | RetrieveAsIfPublished = true, |
| 3 | 41 | | LogicalName = field |
| 3 | 42 | | }; |
| | 43 | |
|
| 3 | 44 | | var response = service.Execute(request) as RetrieveAttributeResponse; |
| 3 | 45 | | var metadata = (EnumAttributeMetadata)response.AttributeMetadata; |
| | 46 | |
|
| 6 | 47 | | var fieldMetadata = metadata.OptionSet.Options.First(f => f.Value == optionSet.Value); |
| | 48 | |
|
| 6 | 49 | | var label = fieldMetadata.Label.LocalizedLabels.FirstOrDefault(l => l.LanguageCode == configLanguage)?.L |
| | 50 | |
|
| 3 | 51 | | if (label != null) |
| 3 | 52 | | { |
| 3 | 53 | | return label; |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | return fieldMetadata.Label.UserLocalizedLabel.Label; |
| | 57 | | } |
| | 58 | |
|
| 142 | 59 | | if (record.FormattedValues.ContainsKey(field)) |
| 2 | 60 | | { |
| 2 | 61 | | return record.FormattedValues[field]; |
| | 62 | | } |
| | 63 | |
|
| 140 | 64 | | if (entityReference != null) |
| 12 | 65 | | { |
| 12 | 66 | | return entityReference.Name ?? entityReference.Id.ToString(); |
| | 67 | | } |
| | 68 | |
|
| 128 | 69 | | if (money != null) |
| 2 | 70 | | { |
| 2 | 71 | | return money.Value.ToString(CultureInfo.InvariantCulture); |
| | 72 | | } |
| | 73 | |
|
| 126 | 74 | | if (aliasedValue != null) |
| 1 | 75 | | { |
| 1 | 76 | | return StringifyProperty(field, aliasedValue.Value, record, service, config); |
| | 77 | | } |
| | 78 | |
|
| 125 | 79 | | return value.ToString(); |
| 148 | 80 | | } |
| | 81 | |
|
| | 82 | | public static string Stringify(string field, Entity record, IOrganizationService service, ConfigHandler config = |
| 148 | 83 | | { |
| 148 | 84 | | var value = record.GetAttributeValue<object>(field); |
| | 85 | |
|
| 148 | 86 | | if (value == null) |
| 1 | 87 | | { |
| 1 | 88 | | return null; |
| | 89 | | } |
| | 90 | |
|
| 147 | 91 | | return StringifyProperty(field, value, record, service, config); |
| 148 | 92 | | } |
| | 93 | | } |
| | 94 | | } |