<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Environment;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.Framework.Harvesters;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Models;
using Orchard.Layouts.Services;

namespace Orchard.Layouts.Providers {
    public class BlueprintElementHarvester : Component, IElementHarvester {
        private readonly Work<IElementBlueprintService> _elementBlueprintService;
        private readonly Work<IElementManager> _elementManager;
        private bool _isHarvesting;

        public BlueprintElementHarvester(Work<IElementBlueprintService> elementBlueprintService, Work<IElementManager> elementManager) {
            _elementBlueprintService = elementBlueprintService;
            _elementManager = elementManager;
        }

        public IEnumerable<ElementDescriptor> HarvestElements(HarvestElementsContext context) {
            if (_isHarvesting)
                return Enumerable.Empty<ElementDescriptor>();

            _isHarvesting = true;
            var blueprints = _elementBlueprintService.Value.GetBlueprints().ToArray();

            var query = 
                from blueprint in blueprints 
                let describeContext = DescribeElementsContext.Empty 
                let baseElementDescriptor = _elementManager.Value.GetElementDescriptorByTypeName(describeContext, blueprint.BaseElementTypeName)
                let baseElement = _elementManager.Value.ActivateElement(baseElementDescriptor)
                select new ElementDescriptor(
                    baseElement.Descriptor.ElementType, 
                    blueprint.ElementTypeName, 
                    T(blueprint.ElementDisplayName),
                    GetCategory(blueprint)) {
                        EnableEditorDialog = false,
                        IsSystemElement = false,
                        CreatingDisplay = creatingDisplayContext => CreatingDisplay(creatingDisplayContext, blueprint),
                        Display = displayContext => Displaying(displayContext, baseElement),
                        StateBag = new Dictionary<string, object> {
                            {"ElementTypeName", baseElement.Descriptor.TypeName}
                        }
                    };

            var descriptors = query.ToArray();
            _isHarvesting = false;
            return descriptors;
        }

        private static string GetCategory(ElementBlueprint blueprint) {
            return !String.IsNullOrWhiteSpace(blueprint.ElementCategory) ? blueprint.ElementCategory : "Blueprints";
        }

        private void CreatingDisplay(ElementCreatingDisplayShapeContext context, ElementBlueprint blueprint) {
            var bluePrintState = ElementStateHelper.Deserialize(blueprint.BaseElementState);
            var elementState = context.Element.State.Where(x => !String.IsNullOrWhiteSpace(x.Value)).ToDictionary(x => x.Key, x => x.Value);
            context.Element.State = bluePrintState.Combine(new StateDictionary(elementState));
        }

        private void Displaying(ElementDisplayContext context, IElement element) {
            var drivers = _elementManager.Value.GetDrivers(element);

            foreach (var driver in drivers) {
                driver.Displaying(context);
            }
        }
    }
}<commit_msg>Fixing blueprint state inheritance.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Orchard.Environment;
using Orchard.Layouts.Framework.Display;
using Orchard.Layouts.Framework.Elements;
using Orchard.Layouts.Framework.Harvesters;
using Orchard.Layouts.Helpers;
using Orchard.Layouts.Models;
using Orchard.Layouts.Services;

namespace Orchard.Layouts.Providers {
    public class BlueprintElementHarvester : Component, IElementHarvester {
        private readonly Work<IElementBlueprintService> _elementBlueprintService;
        private readonly Work<IElementManager> _elementManager;
        private bool _isHarvesting;

        public BlueprintElementHarvester(Work<IElementBlueprintService> elementBlueprintService, Work<IElementManager> elementManager) {
            _elementBlueprintService = elementBlueprintService;
            _elementManager = elementManager;
        }

        public IEnumerable<ElementDescriptor> HarvestElements(HarvestElementsContext context) {
            if (_isHarvesting)
                return Enumerable.Empty<ElementDescriptor>();

            _isHarvesting = true;
            var blueprints = _elementBlueprintService.Value.GetBlueprints().ToArray();

            var query = 
                from blueprint in blueprints 
                let describeContext = DescribeElementsContext.Empty 
                let baseElementDescriptor = _elementManager.Value.GetElementDescriptorByTypeName(describeContext, blueprint.BaseElementTypeName)
                let baseElement = _elementManager.Value.ActivateElement(baseElementDescriptor)
                select new ElementDescriptor(
                    baseElement.Descriptor.ElementType, 
                    blueprint.ElementTypeName, 
                    T(blueprint.ElementDisplayName),
                    GetCategory(blueprint)) {
                        EnableEditorDialog = false,
                        IsSystemElement = false,
                        CreatingDisplay = creatingDisplayContext => CreatingDisplay(creatingDisplayContext, blueprint),
                        Display = displayContext => Displaying(displayContext, baseElement),
                        StateBag = new Dictionary<string, object> {
                            {"ElementTypeName", baseElement.Descriptor.TypeName}
                        }
                    };

            var descriptors = query.ToArray();
            _isHarvesting = false;
            return descriptors;
        }

        private static string GetCategory(ElementBlueprint blueprint) {
            return !String.IsNullOrWhiteSpace(blueprint.ElementCategory) ? blueprint.ElementCategory : "Blueprints";
        }

        private void CreatingDisplay(ElementCreatingDisplayShapeContext context, ElementBlueprint blueprint) {
            var bluePrintState = ElementStateHelper.Deserialize(blueprint.BaseElementState);
            context.Element.State = bluePrintState;
        }

        private void Displaying(ElementDisplayContext context, IElement element) {
            var drivers = _elementManager.Value.GetDrivers(element);

            foreach (var driver in drivers) {
                driver.Displaying(context);
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.06622516556291391, 'fraction_numerical': 0.0, 'mean_word_length': 2.5233297985153764, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 24, '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': '1697474', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1472, 'n_words': 297}