<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Archetype.Models;
using Archetype.PropertyConverters;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Courier.Core;
using Umbraco.Courier.DataResolvers;
using Umbraco.Courier.ItemProviders;

namespace Archetype.Courier.DataResolvers
{
	public class ArchetypeDataResolver : PropertyDataResolverProvider
	{
		private enum Direction
		{
			Extracting,
			Packaging
		}

		public override string EditorAlias
		{
			get
			{
				return Archetype.Constants.PropertyEditorAlias;
			}
		}

		public override void ExtractingDataType(DataType item)
		{
			// No longer need to extract the DataType (int) Ids as they now reference the Guid [LK]
		}

		public override void ExtractingProperty(Item item, ContentProperty propertyData)
		{
			ReplacePropertyDataIds(item, propertyData, Direction.Extracting);
		}

		public override void PackagingDataType(DataType item)
		{
			AddDataTypeDependencies(item);
		}

		public override void PackagingProperty(Item item, ContentProperty propertyData)
		{
			ReplacePropertyDataIds(item, propertyData, Direction.Packaging);
		}

		private void AddDataTypeDependencies(DataType item)
		{
			if (item.Prevalues != null && item.Prevalues.Count > 0)
			{
				var prevalue = item.Prevalues[0];
				if (prevalue.Alias.InvariantEquals(Archetype.Constants.PreValueAlias) && !string.IsNullOrWhiteSpace(prevalue.Value))
				{
					var config = JsonConvert.DeserializeObject<ArchetypePreValue>(prevalue.Value);

					if (config != null && config.Fieldsets != null)
					{
						foreach (var property in config.Fieldsets.SelectMany(x => x.Properties))
						{
							item.Dependencies.Add(property.DataTypeGuid.ToString(), ProviderIDCollection.dataTypeItemProviderGuid);
						}

						item.Prevalues[0].Value = JsonConvert.SerializeObject(config, Formatting.None);
					}
				}
			}
		}

		private void ReplacePropertyDataIds(Item item, ContentProperty propertyData, Direction direction)
		{
			if (propertyData != null && propertyData.Value != null)
			{
				// just look at the amount of dancing around we have to do in order to fake a `PublishedPropertyType`?!
				var dataTypeId = PersistenceManager.Default.GetNodeId(propertyData.DataType, NodeObjectTypes.DataType);
				var fakePropertyType = this.CreateDummyPropertyType(dataTypeId, this.EditorAlias);

				var converter = new ArchetypeValueConverter();
				var archetype = (ArchetypeModel)converter.ConvertDataToSource(fakePropertyType, propertyData.Value, false);

				if (archetype != null)
				{
					// create a 'fake' provider, as ultimately only the 'Packaging' enum will be referenced.
					var fakeItemProvider = new PropertyItemProvider();

					foreach (var property in archetype.Fieldsets.SelectMany(x => x.Properties))
					{
						if (property == null || string.IsNullOrWhiteSpace(property.PropertyEditorAlias))
							continue;

						// create a 'fake' item for Courier to process
						var fakeItem = new ContentPropertyData()
						{
							ItemId = item.ItemId,
							Name = string.Format("{0} [{1}: Nested {2} ({3})]", new[] { item.Name, this.EditorAlias, property.PropertyEditorAlias, property.Alias }),
							Data = new List<ContentProperty>
							{
								new ContentProperty
								{
									Alias = property.Alias,
									DataType = PersistenceManager.Default.GetUniqueId(property.DataTypeId, NodeObjectTypes.DataType),
									PropertyEditorAlias = property.PropertyEditorAlias,
									Value = property.Value
								}
							}
						};

						if (direction == Direction.Packaging)
						{
							try
							{
								// run the 'fake' item through Courier's data resolvers
								ResolutionManager.Instance.PackagingItem(fakeItem, fakeItemProvider);
							}
							catch (Exception ex)
							{
								LogHelper.Error<ArchetypeDataResolver>(string.Concat("Error packaging data value: ", fakeItem.Name), ex);
							}

							// pass up the dependencies and resources
							item.Dependencies.AddRange(fakeItem.Dependencies);
							item.Resources.AddRange(fakeItem.Resources);
						}
						else if (direction == Direction.Extracting)
						{
							try
							{
								// run the 'fake' item through Courier's data resolvers
								ResolutionManager.Instance.ExtractingItem(fakeItem, fakeItemProvider);
							}
							catch (Exception ex)<commit_msg>`PersistenceManager.Default` was obsoleted, replaced calls with `ExecutionContext.DatabasePersistence`<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Archetype.Models;
using Archetype.PropertyConverters;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Courier.Core;
using Umbraco.Courier.DataResolvers;
using Umbraco.Courier.ItemProviders;

namespace Archetype.Courier.DataResolvers
{
	public class ArchetypeDataResolver : PropertyDataResolverProvider
	{
		private enum Direction
		{
			Extracting,
			Packaging
		}

		public override string EditorAlias
		{
			get
			{
				return Archetype.Constants.PropertyEditorAlias;
			}
		}

		public override void ExtractingDataType(DataType item)
		{
			// No longer need to extract the DataType (int) Ids as they now reference the Guid [LK]
		}

		public override void ExtractingProperty(Item item, ContentProperty propertyData)
		{
			ReplacePropertyDataIds(item, propertyData, Direction.Extracting);
		}

		public override void PackagingDataType(DataType item)
		{
			AddDataTypeDependencies(item);
		}

		public override void PackagingProperty(Item item, ContentProperty propertyData)
		{
			ReplacePropertyDataIds(item, propertyData, Direction.Packaging);
		}

		private void AddDataTypeDependencies(DataType item)
		{
			if (item.Prevalues != null && item.Prevalues.Count > 0)
			{
				var prevalue = item.Prevalues[0];
				if (prevalue.Alias.InvariantEquals(Archetype.Constants.PreValueAlias) && !string.IsNullOrWhiteSpace(prevalue.Value))
				{
					var config = JsonConvert.DeserializeObject<ArchetypePreValue>(prevalue.Value);

					if (config != null && config.Fieldsets != null)
					{
						foreach (var property in config.Fieldsets.SelectMany(x => x.Properties))
						{
							item.Dependencies.Add(property.DataTypeGuid.ToString(), ProviderIDCollection.dataTypeItemProviderGuid);
						}

						item.Prevalues[0].Value = JsonConvert.SerializeObject(config, Formatting.None);
					}
				}
			}
		}

		private void ReplacePropertyDataIds(Item item, ContentProperty propertyData, Direction direction)
		{
			if (propertyData != null && propertyData.Value != null)
			{
				// just look at the amount of dancing around we have to do in order to fake a `PublishedPropertyType`?!
				var dataTypeId = ExecutionContext.DatabasePersistence.GetNodeId(propertyData.DataType, NodeObjectTypes.DataType);
				var fakePropertyType = this.CreateDummyPropertyType(dataTypeId, this.EditorAlias);

				var converter = new ArchetypeValueConverter();
				var archetype = (ArchetypeModel)converter.ConvertDataToSource(fakePropertyType, propertyData.Value, false);

				if (archetype != null)
				{
					// create a 'fake' provider, as ultimately only the 'Packaging' enum will be referenced.
					var fakeItemProvider = new PropertyItemProvider();

					foreach (var property in archetype.Fieldsets.SelectMany(x => x.Properties))
					{
						if (property == null || string.IsNullOrWhiteSpace(property.PropertyEditorAlias))
							continue;

						// create a 'fake' item for Courier to process
						var fakeItem = new ContentPropertyData()
						{
							ItemId = item.ItemId,
							Name = string.Format("{0} [{1}: Nested {2} ({3})]", new[] { item.Name, this.EditorAlias, property.PropertyEditorAlias, property.Alias }),
							Data = new List<ContentProperty>
							{
								new ContentProperty
								{
									Alias = property.Alias,
									DataType = ExecutionContext.DatabasePersistence.GetUniqueId(property.DataTypeId, NodeObjectTypes.DataType),
									PropertyEditorAlias = property.PropertyEditorAlias,
									Value = property.Value
								}
							}
						};

						if (direction == Direction.Packaging)
						{
							try
							{
								// run the 'fake' item through Courier's data resolvers
								ResolutionManager.Instance.PackagingItem(fakeItem, fakeItemProvider);
							}
							catch (Exception ex)
							{
								LogHelper.Error<ArchetypeDataResolver>(string.Concat("Error packaging data value: ", fakeItem.Name), ex);
							}

							// pass up the dependencies and resources
							item.Dependencies.AddRange(fakeItem.Dependencies);
							item.Resources.AddRange(fakeItem.Resources);
						}
						else if (direction == Direction.Extracting)
						{
							try
							{
								// run the 'fake' item through Courier's data resolvers
								ResolutionManager.Instance.ExtractingItem(fakeItem, fakeItemProvider);
							}
							catch (Exception ex)
{'fraction_non_alphanumeric': 0.09211544841313724, 'fraction_numerical': 0.0015481587968594493, 'mean_word_length': 3.475012370113805, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, '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': '1793166', 'n_tokens_mistral': 3358, 'n_tokens_neox': 2526, 'n_words': 589}