<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Reflection;

using UmbracoVault.Attributes;
using UmbracoVault.Caching;
using UmbracoVault.Extensions;
using UmbracoVault.Transformations;
using UmbracoVault.TypeHandlers;

using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Web;

namespace UmbracoVault
{
    /// <summary>
    /// Implementation of the IUmbracoContext
    /// </summary>
    public class UmbracoWebContext : IUmbracoContext
    {
        private readonly ClassConstructor _classConstructor;
        private readonly IList<ITransformation> _transformations;
        private readonly CacheManager _cacheManager;

        private readonly TypeHandlerFactory _typeHandlerFactory;

        public UmbracoWebContext()
        {
            //TODO: fetch classes from configuration and populate this list based on type and assembly strings
            //TODO: Document Default Transformations
            //TODO: Re-add transformations once there's a way to opt-in to them instead of having it be global
            _transformations = new List<ITransformation>
                                   {
                                       //new SuperScriptTransformation()
                                   };

            _classConstructor = new ClassConstructor();
            _typeHandlerFactory = TypeHandlerFactory.Instance;
            _cacheManager = new CacheManager();
        }

        private UmbracoHelper _helper;
        protected UmbracoHelper Helper
        {
            get
            {
                if (_helper == null)
                {
                    _helper = new UmbracoHelper(UmbracoContext.Current);
                }
                return _helper;
            }
        }

        private IPublishedContent GetCurrentUmbracoContent()
        {
            if (UmbracoContext.Current.PageId != null)
            {
                return GetUmbracoContent(UmbracoContext.Current.PageId.Value);
            }
            return null;
        }

        private IPublishedContent GetUmbracoContent(int id)
        {
            var umbracoItem = Helper.TypedContent(id);
            return umbracoItem;
        }

        private IMedia GetUmbracoMedia(int id)
        {
            var mediaItem = ApplicationContext.Current.Services.MediaService.GetById(id);
            return mediaItem;
        }

        private IMember GetUmbracoMember(int id)
        {
            var member = ApplicationContext.Current.Services.MemberService.GetById(id);
            return member;
        }

        #region IUmbracoContext Members

        /// <summary>
        /// Retrieves a data item for the current node.
        /// </summary>
        /// <typeparam name="T">The object type to cast the item to</typeparam>
        /// <returns>a strongly typed version (T) of the current umbraco item.</returns>
        public T GetCurrent<T>()
        {

            var umbracoItem = GetCurrentUmbracoContent();
            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>("Could not retrieve current umbraco item.", null);
                return default(T);
            }


            return GetItem<T>(umbracoItem);
        }

        /// <summary>
        /// Retrieves a data item for the current node, instantiating and hydrating a type as defined by the passed-in type parameter.
        /// This method is for use primarily when the desired return type is not known at compile time (i.e. in instances where a type is built from a string at runtime).
        /// If the desired return type is known at compile time, use GetCurrent&lt;T&gt;() instead.
        /// </summary>
        /// <param name="type">The desired Type to be returned</param>
        /// <returns>A fully-hydrated type (as defined by the Type parameter) containing data mapped from the current umbraco item</returns>
        public object GetCurrent(Type type)
        {
            // ReSharper disable once PossibleInvalidOperationException
            var id = UmbracoContext.Current.PageId.Value;
            return GetContentById(type, id.ToString());
        }

        public object GetContentById(Type type, string idString)
        {
            var methodInfo = typeof(UmbracoWebContext).GetMethod("GetContentById", new[]{typeof(string)});
            var genericMethod = methodInfo.MakeGenericMethod(new[] { type });
            var result = genericMethod.Invoke(Vault.Context, new[] { idString });
            return result;
        }

        public T GetContentById<T>(string idString)
        {
            var id = GetIdFromString(idString);
            return GetContentById<T>(id);
        }

        public T GetMediaById<T>(string idString)
        {
            var id = GetIdFromString(idString);
            return GetMediaById<T>(id);
        }

        public T GetContentById<T>(int id)
        {
            var umbracoItem = GetUmbracoContent(id);

            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>(string.Format("Could not locate umbraco item with Id of '{0}'.", id), null);
                return default(T);
            }

            return GetItem<T>(umbracoItem);
        }

        public T GetMediaById<T>(int id)
        {
            var umbracoItem = GetUmbracoMedia(id);

            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>(string.Format("Could not locate umbraco media item with Id of '{0}'.", id), null);
                return default(T);
            }

            return GetMediaItem<T>(umbracoItem);
        }

        public T GetMemberById<T>(string idString)
        {
            var id = GetIdFromString(idString);
            return GetMemberById<T>(id);
        }

        public T GetMemberById<T>(int id)
        {
            var umbracoItem = GetUmbracoMember(id);

            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>(string.Format("Could not locate umbraco media item with Id of '{0}'.", id), null);
                return default(T);
            }

            return GetMemberItem<T>(umbracoItem);
        }

        public IEnumerable<T> GetContentByCsv<T>(string csv)
        {
            return Helper.GetTypedContentByCsv(csv).Select(GetItem<T>);
        }

        public IEnumerable<T> GetByDocumentType<T>()
        {
            var items = new List<T>();
            var type = typeof(T);

            foreach (var alias in GetUmbracoEntityAliasesFromType(type))
            {
                var contents = Helper.GetContentByAlias(alias);
                items.AddRange(contents.Select(GetItem<T>));
            }

            return items;
        }
<commit_msg>fix member not found error message<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Reflection;

using UmbracoVault.Attributes;
using UmbracoVault.Caching;
using UmbracoVault.Extensions;
using UmbracoVault.Transformations;
using UmbracoVault.TypeHandlers;

using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Web;

namespace UmbracoVault
{
    /// <summary>
    /// Implementation of the IUmbracoContext
    /// </summary>
    public class UmbracoWebContext : IUmbracoContext
    {
        private readonly ClassConstructor _classConstructor;
        private readonly IList<ITransformation> _transformations;
        private readonly CacheManager _cacheManager;

        private readonly TypeHandlerFactory _typeHandlerFactory;

        public UmbracoWebContext()
        {
            //TODO: fetch classes from configuration and populate this list based on type and assembly strings
            //TODO: Document Default Transformations
            //TODO: Re-add transformations once there's a way to opt-in to them instead of having it be global
            _transformations = new List<ITransformation>
                                   {
                                       //new SuperScriptTransformation()
                                   };

            _classConstructor = new ClassConstructor();
            _typeHandlerFactory = TypeHandlerFactory.Instance;
            _cacheManager = new CacheManager();
        }

        private UmbracoHelper _helper;
        protected UmbracoHelper Helper
        {
            get
            {
                if (_helper == null)
                {
                    _helper = new UmbracoHelper(UmbracoContext.Current);
                }
                return _helper;
            }
        }

        private IPublishedContent GetCurrentUmbracoContent()
        {
            if (UmbracoContext.Current.PageId != null)
            {
                return GetUmbracoContent(UmbracoContext.Current.PageId.Value);
            }
            return null;
        }

        private IPublishedContent GetUmbracoContent(int id)
        {
            var umbracoItem = Helper.TypedContent(id);
            return umbracoItem;
        }

        private IMedia GetUmbracoMedia(int id)
        {
            var mediaItem = ApplicationContext.Current.Services.MediaService.GetById(id);
            return mediaItem;
        }

        private IMember GetUmbracoMember(int id)
        {
            var member = ApplicationContext.Current.Services.MemberService.GetById(id);
            return member;
        }

        #region IUmbracoContext Members

        /// <summary>
        /// Retrieves a data item for the current node.
        /// </summary>
        /// <typeparam name="T">The object type to cast the item to</typeparam>
        /// <returns>a strongly typed version (T) of the current umbraco item.</returns>
        public T GetCurrent<T>()
        {

            var umbracoItem = GetCurrentUmbracoContent();
            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>("Could not retrieve current umbraco item.", null);
                return default(T);
            }


            return GetItem<T>(umbracoItem);
        }

        /// <summary>
        /// Retrieves a data item for the current node, instantiating and hydrating a type as defined by the passed-in type parameter.
        /// This method is for use primarily when the desired return type is not known at compile time (i.e. in instances where a type is built from a string at runtime).
        /// If the desired return type is known at compile time, use GetCurrent&lt;T&gt;() instead.
        /// </summary>
        /// <param name="type">The desired Type to be returned</param>
        /// <returns>A fully-hydrated type (as defined by the Type parameter) containing data mapped from the current umbraco item</returns>
        public object GetCurrent(Type type)
        {
            // ReSharper disable once PossibleInvalidOperationException
            var id = UmbracoContext.Current.PageId.Value;
            return GetContentById(type, id.ToString());
        }

        public object GetContentById(Type type, string idString)
        {
            var methodInfo = typeof(UmbracoWebContext).GetMethod("GetContentById", new[]{typeof(string)});
            var genericMethod = methodInfo.MakeGenericMethod(new[] { type });
            var result = genericMethod.Invoke(Vault.Context, new[] { idString });
            return result;
        }

        public T GetContentById<T>(string idString)
        {
            var id = GetIdFromString(idString);
            return GetContentById<T>(id);
        }

        public T GetMediaById<T>(string idString)
        {
            var id = GetIdFromString(idString);
            return GetMediaById<T>(id);
        }

        public T GetContentById<T>(int id)
        {
            var umbracoItem = GetUmbracoContent(id);

            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>(string.Format("Could not locate umbraco item with Id of '{0}'.", id), null);
                return default(T);
            }

            return GetItem<T>(umbracoItem);
        }

        public T GetMediaById<T>(int id)
        {
            var umbracoItem = GetUmbracoMedia(id);

            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>(string.Format("Could not locate umbraco media item with Id of '{0}'.", id), null);
                return default(T);
            }

            return GetMediaItem<T>(umbracoItem);
        }

        public T GetMemberById<T>(string idString)
        {
            var id = GetIdFromString(idString);
            return GetMemberById<T>(id);
        }

        public T GetMemberById<T>(int id)
        {
            var umbracoItem = GetUmbracoMember(id);

            if (umbracoItem == null || umbracoItem.Id <= 0)
            {
                LogHelper.Error<T>(string.Format("Could not locate umbraco member with Id of '{0}'.", id), null);
                return default(T);
            }

            return GetMemberItem<T>(umbracoItem);
        }

        public IEnumerable<T> GetContentByCsv<T>(string csv)
        {
            return Helper.GetTypedContentByCsv(csv).Select(GetItem<T>);
        }

        public IEnumerable<T> GetByDocumentType<T>()
        {
            var items = new List<T>();
            var type = typeof(T);

            foreach (var alias in GetUmbracoEntityAliasesFromType(type))
            {
                var contents = Helper.GetContentByAlias(alias);
                items.AddRange(contents.Select(GetItem<T>));
            }

            return items;
        }

{'fraction_non_alphanumeric': 0.08756592731739037, 'fraction_numerical': 0.0010114876092767863, 'mean_word_length': 1.8605083694978302, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4307268', 'n_tokens_mistral': 3705, 'n_tokens_neox': 3530, 'n_words': 956}