<commit_before>﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Indexing;
using Orchard.Localization;
using Orchard.Search.Models;
using Orchard.Search.ViewModels;

namespace Orchard.Search.Drivers {

    public class SearchSettingsPartDriver : ContentPartDriver<SearchSettingsPart> {
        private readonly IIndexManager _indexManager;

        public SearchSettingsPartDriver(IIndexManager indexManager) {
            _indexManager = indexManager;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override string Prefix { get { return "SearchSettings"; } }

        protected override DriverResult Editor(SearchSettingsPart part, dynamic shapeHelper) {
            return Editor(part, null, shapeHelper);
            
        }

        protected override DriverResult Editor(SearchSettingsPart part, IUpdateModel updater, dynamic shapeHelper) {
            return Combined(
                ContentShape("Parts_Search_SiteSettings_Index", () => {
                    var model = new SearchSettingsIndexViewModel {
                        SelectedIndex = part.SearchIndex,
                        AvailableIndexes = _indexManager.GetSearchIndexProvider().List().ToList()
                    };

                    if (updater != null) {
                        if (updater.TryUpdateModel(model, Prefix, null, null)) {
                            part.SearchIndex = model.SelectedIndex;
                        }
                    }

                    return shapeHelper.EditorTemplate(TemplateName: "Parts/Search.SiteSettings.Index", Model: model, Prefix: Prefix);
                }).OnGroup("search"),
                ContentShape("Parts_Search_SiteSettings_Fields", () => {
                    var model = new SearchSettingsFieldsViewModel();
                    var searchFields = part.SearchFields;

                    if (updater != null) {
                        if (updater.TryUpdateModel(model, Prefix, null, null)) {
                            part.SearchFields = model.Entries.ToDictionary(x => x.Index, x => x.Fields.Where(e => e.Selected).Select(e => e.Field).ToArray());
                            part.FilterCulture = model.FilterCulture;
                        }
                    }
                    else if (_indexManager.HasIndexProvider()) {
                        model.FilterCulture = part.FilterCulture;
                        model.Entries = _indexManager.GetSearchIndexProvider().List().Select(x => {
                            var indexSettings = new IndexSettingsEntry {
                                Index = x,
                                Fields = new List<SearchSettingsEntry>()
                            };
                            foreach (var field in _indexManager.GetSearchIndexProvider().GetFields(x)) {
                                indexSettings.Fields.Add(new SearchSettingsEntry { Field = field, Selected = (searchFields.ContainsKey(x) && searchFields[x].Contains(field)) });
                            }

                            return indexSettings;
                        }).ToList();
                    }

                    return shapeHelper.EditorTemplate(TemplateName: "Parts/Search.SiteSettings.Fields", Model: model, Prefix: Prefix);
                }).OnGroup("search"));
        }

        protected override void Exporting(SearchSettingsPart part, ExportContentContext context) {
            context.Element(part.PartDefinition.Name).Add(new XAttribute("SearchFields", part.Retrieve<string>("SearchFields")));
        }

        protected override void Importing(SearchSettingsPart part, ImportContentContext context) {
            var xElement = context.Data.Element(part.PartDefinition.Name);
            if (xElement == null) return;
            
            var searchFields = xElement.Attribute("SearchFields");
            if (searchFields != null) {
                searchFields.Remove();

                part.Store("SearchFields", searchFields.Value); 
            }
        }
    }
}<commit_msg>Fixed potential NRE issue with SearchSettingsPart export handler.<commit_after>﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Indexing;
using Orchard.Localization;
using Orchard.Search.Models;
using Orchard.Search.ViewModels;

namespace Orchard.Search.Drivers {

    public class SearchSettingsPartDriver : ContentPartDriver<SearchSettingsPart> {
        private readonly IIndexManager _indexManager;

        public SearchSettingsPartDriver(IIndexManager indexManager) {
            _indexManager = indexManager;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        protected override string Prefix { get { return "SearchSettings"; } }

        protected override DriverResult Editor(SearchSettingsPart part, dynamic shapeHelper) {
            return Editor(part, null, shapeHelper);
            
        }

        protected override DriverResult Editor(SearchSettingsPart part, IUpdateModel updater, dynamic shapeHelper) {
            return Combined(
                ContentShape("Parts_Search_SiteSettings_Index", () => {
                    var model = new SearchSettingsIndexViewModel {
                        SelectedIndex = part.SearchIndex,
                        AvailableIndexes = _indexManager.GetSearchIndexProvider().List().ToList()
                    };

                    if (updater != null) {
                        if (updater.TryUpdateModel(model, Prefix, null, null)) {
                            part.SearchIndex = model.SelectedIndex;
                        }
                    }

                    return shapeHelper.EditorTemplate(TemplateName: "Parts/Search.SiteSettings.Index", Model: model, Prefix: Prefix);
                }).OnGroup("search"),
                ContentShape("Parts_Search_SiteSettings_Fields", () => {
                    var model = new SearchSettingsFieldsViewModel();
                    var searchFields = part.SearchFields;

                    if (updater != null) {
                        if (updater.TryUpdateModel(model, Prefix, null, null)) {
                            part.SearchFields = model.Entries.ToDictionary(x => x.Index, x => x.Fields.Where(e => e.Selected).Select(e => e.Field).ToArray());
                            part.FilterCulture = model.FilterCulture;
                        }
                    }
                    else if (_indexManager.HasIndexProvider()) {
                        model.FilterCulture = part.FilterCulture;
                        model.Entries = _indexManager.GetSearchIndexProvider().List().Select(x => {
                            var indexSettings = new IndexSettingsEntry {
                                Index = x,
                                Fields = new List<SearchSettingsEntry>()
                            };
                            foreach (var field in _indexManager.GetSearchIndexProvider().GetFields(x)) {
                                indexSettings.Fields.Add(new SearchSettingsEntry { Field = field, Selected = (searchFields.ContainsKey(x) && searchFields[x].Contains(field)) });
                            }

                            return indexSettings;
                        }).ToList();
                    }

                    return shapeHelper.EditorTemplate(TemplateName: "Parts/Search.SiteSettings.Fields", Model: model, Prefix: Prefix);
                }).OnGroup("search"));
        }

        protected override void Exporting(SearchSettingsPart part, ExportContentContext context) {
            context.Element(part.PartDefinition.Name).SetAttributeValue("SearchFields", part.Retrieve<string>("SearchFields"));
        }

        protected override void Importing(SearchSettingsPart part, ImportContentContext context) {
            var xElement = context.Data.Element(part.PartDefinition.Name);
            if (xElement == null) return;
            
            var searchFields = xElement.Attribute("SearchFields");
            if (searchFields != null) {
                searchFields.Remove();

                part.Store("SearchFields", searchFields.Value); 
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08666051234710362, 'fraction_numerical': 0.0, 'mean_word_length': 1.7271869100062933, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, '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': '6126358', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1886, 'n_words': 396}