<commit_before>﻿using System;

using Castle.DynamicProxy;

using umbraco.NodeFactory;
using umbraco;

namespace Vega.USiteBuilder
{
    public class DocumentTypeActivator
    {
        private ProxyGenerator _generator;
        private ProxyGenerationOptions _options;
        private IInterceptor[] _interceptors;

        public DocumentTypeActivator()
        {
            _generator = new ProxyGenerator();
            _options = new ProxyGenerationOptions(new DocumentTypePropertiesProxyGenerationHook());
            _interceptors = new IInterceptor[] 
                               {
                                   new DocumentTypePropertyInterceptor()
                               };
        }

        public virtual T CreateAndPopulateTypedInstance<T>(Node node) where T : DocumentTypeBase
        {
            T retVal = null;
            if (node != null)
            {
                Type typeDocType = DocumentTypeManager.GetDocumentTypeType(node.NodeTypeAlias);
                T typedPage = (T)CreateInstance(typeDocType);
                if (ContentHelper.PopuplateInstance<T>(node, typeDocType, typedPage))
                {
                    retVal = typedPage;
                }
            }

            return retVal;
        }

        public virtual T CreateAndPopulateTypedInstance<T>(int nodeId) where T : DocumentTypeBase
        {
            Node node = uQuery.GetNode(nodeId);
            return CreateAndPopulateTypedInstance<T>(node);
        }

        public virtual object CreateInstance(Type typeDocType)
        {
            return CreateInstance(typeDocType, new object[] {});
        }

        protected virtual object CreateInstance(Type typeDocType, object[] ctorArguments)
        {
            return _generator.CreateClassProxy(typeDocType, new Type[] { }, _options, ctorArguments, _interceptors);
        }
    }
}<commit_msg>added descriptive exception when document type exists in db but not in code<commit_after>﻿using System;

using Castle.DynamicProxy;

using umbraco.NodeFactory;
using umbraco;

namespace Vega.USiteBuilder
{
    public class DocumentTypeActivator
    {
        private ProxyGenerator _generator;
        private ProxyGenerationOptions _options;
        private IInterceptor[] _interceptors;

        public DocumentTypeActivator()
        {
            _generator = new ProxyGenerator();
            _options = new ProxyGenerationOptions(new DocumentTypePropertiesProxyGenerationHook());
            _interceptors = new IInterceptor[] 
                               {
                                   new DocumentTypePropertyInterceptor()
                               };
        }

        public virtual T CreateAndPopulateTypedInstance<T>(Node node) where T : DocumentTypeBase
        {
            T retVal = null;
            if (node != null)
            {
                Type typeDocType = DocumentTypeManager.GetDocumentTypeType(node.NodeTypeAlias);
                if (typeDocType == null)
                {
                    var message =
                        string.Format(
                            "Error processing document with id {0} of type '{1}' - no code for the document type found. ",
                            node.Id, node.NodeTypeAlias);

                    throw new Exception(message);
                }

                T typedPage = (T)CreateInstance(typeDocType);
                if (ContentHelper.PopuplateInstance<T>(node, typeDocType, typedPage))
                {
                    retVal = typedPage;
                }
            }

            return retVal;
        }

        public virtual T CreateAndPopulateTypedInstance<T>(int nodeId) where T : DocumentTypeBase
        {
            Node node = uQuery.GetNode(nodeId);
            return CreateAndPopulateTypedInstance<T>(node);
        }

        public virtual object CreateInstance(Type typeDocType)
        {
            return CreateInstance(typeDocType, new object[] {});
        }

        protected virtual object CreateInstance(Type typeDocType, object[] ctorArguments)
        {
            return _generator.CreateClassProxy(typeDocType, new Type[] { }, _options, ctorArguments, _interceptors);
        }
    }
}
{'fraction_non_alphanumeric': 0.06876926725160067, 'fraction_numerical': 0.00047427080863172874, 'mean_word_length': 1.7804878048780488, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7153951', 'n_tokens_mistral': 1022, 'n_tokens_neox': 980, 'n_words': 239}