<commit_before>﻿using System;
using System.Linq;
using Microsoft.Xrm.Sdk;

namespace KpdApps.Common.MsCrm2013.Extensions
{
	public static class AttributeCollectionExtensions
	{
		public static object GetValue(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name))
				return null;

			object crmValue = properties[name];
			if (crmValue == null)
				return null;

			Type fieldType = crmValue.GetType();
			Type[] endTypes = { typeof(DateTime), typeof(int), typeof(double) };

			EntityReference reference = crmValue as EntityReference;
			if (reference != null)
				return reference.Id;

			if (fieldType == typeof(OptionSetValue))
			{
				OptionSetValue field = (OptionSetValue)crmValue;
				return field.Value;
			}

			if (fieldType == typeof(Money))
			{
				Money field = (Money)crmValue;
				return field.Value;
			}

			if (endTypes.Contains(fieldType))
				return crmValue;

			if (fieldType == typeof(AliasedValue))
				return ((AliasedValue)crmValue).Value;

			return crmValue;
		}

		public static string GetStringValue(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name))
				return null;

			object property = properties[name];
			return property?.ToString();
		}

		public static void SetStringValue(this AttributeCollection properties, string name, string value)
		{
			if (!properties.Contains(name))
				properties.Add(name, value);
			else
				properties[name] = value;
		}

		public static int GetStatusValue(this AttributeCollection properties)
		{
			return GetPicklistValue(properties, "statuscode");
		}

		public static int GetStateValue(this AttributeCollection properties)
		{
			return GetPicklistValue(properties, "statecode");
		}

		public static int GetPicklistValue(this AttributeCollection properties, string name)
		{
			return GetPicklistValue(properties, name, -1);
		}

		public static int GetPicklistValue(this AttributeCollection properties, string name, int defaultValue)
		{
			if (!properties.Contains(name))
				return defaultValue;

			OptionSetValue property = properties[name] as OptionSetValue;
			return property?.Value ?? defaultValue;
		}

		public static void SetPicklistValue(this AttributeCollection properties, string name, int value)
		{
			if (!properties.Contains(name))
				properties.Add(name, new OptionSetValue(value));
			else
				properties[name] = new OptionSetValue(value);
		}

		public static void SetStatusValue(this AttributeCollection properties, string name, int value)
		{
			SetPicklistValue(properties, name, value);
		}

		public static DateTime GetDateTimeValue(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name))
				return DateTime.MinValue;

			object property = properties[name];
			if (property != null)
				return Convert.ToDateTime(property);
			return DateTime.MinValue;
		}

		public static void SetDateTimeValue(this AttributeCollection properties, string name, DateTime value)
		{
			if (!properties.Contains(name))
			{
				properties.Add(name, value);
			}
			else
			{
				properties[name] = value;
			}
		}
	}
}<commit_msg>GetLookup, GetLookupValue, SetLookupValue<commit_after>﻿using System;
using System.Linq;
using Microsoft.Xrm.Sdk;

namespace KpdApps.Common.MsCrm2013.Extensions
{
	public static class AttributeCollectionExtensions
	{
		public static object GetValue(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name))
				return null;

			object crmValue = properties[name];
			if (crmValue == null)
				return null;

			Type fieldType = crmValue.GetType();
			Type[] endTypes = { typeof(DateTime), typeof(int), typeof(double) };

			EntityReference reference = crmValue as EntityReference;
			if (reference != null)
				return reference.Id;

			if (fieldType == typeof(OptionSetValue))
			{
				OptionSetValue field = (OptionSetValue)crmValue;
				return field.Value;
			}

			if (fieldType == typeof(Money))
			{
				Money field = (Money)crmValue;
				return field.Value;
			}

			if (endTypes.Contains(fieldType))
				return crmValue;

			if (fieldType == typeof(AliasedValue))
				return ((AliasedValue)crmValue).Value;

			return crmValue;
		}

		public static string GetStringValue(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name))
				return null;

			object property = properties[name];
			return property?.ToString();
		}

		public static void SetStringValue(this AttributeCollection properties, string name, string value)
		{
			if (!properties.Contains(name))
				properties.Add(name, value);
			else
				properties[name] = value;
		}

		public static int GetStatusValue(this AttributeCollection properties)
		{
			return GetPicklistValue(properties, "statuscode");
		}

		public static int GetStateValue(this AttributeCollection properties)
		{
			return GetPicklistValue(properties, "statecode");
		}

		public static int GetPicklistValue(this AttributeCollection properties, string name)
		{
			return GetPicklistValue(properties, name, -1);
		}

		public static int GetPicklistValue(this AttributeCollection properties, string name, int defaultValue)
		{
			if (!properties.Contains(name))
				return defaultValue;

			OptionSetValue property = properties[name] as OptionSetValue;
			return property?.Value ?? defaultValue;
		}

		public static void SetPicklistValue(this AttributeCollection properties, string name, int value)
		{
			if (!properties.Contains(name))
				properties.Add(name, new OptionSetValue(value));
			else
				properties[name] = new OptionSetValue(value);
		}

		public static void SetStatusValue(this AttributeCollection properties, string name, int value)
		{
			SetPicklistValue(properties, name, value);
		}

		public static DateTime GetDateTimeValue(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name))
				return DateTime.MinValue;

			object property = properties[name];
			if (property != null)
				return Convert.ToDateTime(property);
			return DateTime.MinValue;
		}

		public static void SetDateTimeValue(this AttributeCollection properties, string name, DateTime value)
		{
			if (!properties.Contains(name))
			{
				properties.Add(name, value);
			}
			else
			{
				properties[name] = value;
			}
		}

		public static EntityReference GetLookup(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name)) return null;

			var @ref = properties[name];
			if (@ref != null)
				return (EntityReference)properties[name];

			return null;
		}

		public static Guid GetLookupValue(this AttributeCollection properties, string name)
		{
			if (!properties.Contains(name))
				return Guid.Empty;

			object property = properties[name];
			EntityReference @ref = property as EntityReference;
			if (@ref != null)
				return @ref.Id;

			return Guid.Empty;
		}

		public static void SetLookupValue(this AttributeCollection properties, string name, string type, Guid value)
		{
			if (!properties.Contains(name))
			{
				properties.Add(name, new EntityReference(type, value));
			}
			else
			{
				properties[name] = new EntityReference(type, value);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.0953312831982108, 'fraction_numerical': 0.0013978194017332962, 'mean_word_length': 4.1327116212338595, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4031389', 'n_tokens_mistral': 2444, 'n_tokens_neox': 1971, 'n_words': 542}