<commit_before>﻿using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Skybrud.BorgerDk;
using Skybrud.Umbraco.BorgerDk.Extensions.JObject;
using Skybrud.Umbraco.BorgerDk.Models.Cached;

namespace Skybrud.Umbraco.BorgerDk.Models {

    /// <summary>
    /// Class representing a selection of a Borger.dk article as saved from either an Umbraco property editor or grid editor.
    /// </summary>
    public class BorgerDkArticleSelection {

        #region Properties

        /// <summary>
        /// Gets a reference to the underlying instance of <see cref="JObject"/>.
        /// </summary>
        [JsonIgnore]
        public JObject JObject { get; private set; }

        /// <summary>
        /// Gets the ID of the selected Borger.dk article, or <code>0</code> if no article has been selected.
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// Gets the URL of the selected Borger.dk article.
        /// </summary>
        public string Url { get; private set; }

        /// <summary>
        /// Gets the URL of the selected Borger.dk article.
        /// </summary>
        public string Domain { get; private set; }

        /// <summary>
        /// Gets the municipality ID of the selected Borger.dk article.
        /// </summary>
        public BorgerDkMunicipality Municipality { get; private set; }

        /// <summary>
        /// Gets the title of the selected Borger.dk article.
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        /// Gets the header of the selected Borger.dk article.
        /// </summary>
        public string Header { get; private set; }

        /// <summary>
        /// Gets an array of the IDs of the selected micro articles and blocks.
        /// </summary>
        public string[] Selected { get; private set; }

        /// <summary>
        /// Gets whether an article has been selected.
        /// </summary>
        public bool HasSelection {
            get { return Id > 0 && !String.IsNullOrWhiteSpace(Url); }
        }

        /// <summary>
        /// Gets a reference to the selected article.
        /// </summary>
        public BorgerDkCachedArticle Article { get; private set; }

        /// <summary>
        /// Gets an array of all selected text blocks.
        /// </summary>
        public BorgerDkCachedTextElement[] Blocks { get; private set; }

        /// <summary>
        /// Gets whether the selection has any text elements.
        /// </summary>
        public bool HasBlocks {
            get { return Blocks.Length > 0; }
        }

        /// <summary>
        /// Gets an array of all selected micro articles.
        /// </summary>
        public BorgerDkCachedMicroArticle[] MicroArticles { get; private set; }

        /// <summary>
        /// Gets whether the selection has any micro articles.
        /// </summary>
        public bool HasMicroArticles {
            get { return MicroArticles.Length > 0; }
        }

        #endregion

        #region Constructors

        private BorgerDkArticleSelection() {
            Selected = new string[0];
            Blocks = new BorgerDkCachedTextElement[0];
            MicroArticles = new BorgerDkCachedMicroArticle[0];
        }

        protected BorgerDkArticleSelection(JObject obj) {
            JObject = obj;
            Id = obj.GetInt32("id");
            Url = obj.GetString("url");
            Domain = obj.GetString("domain");
            Municipality = obj.GetInt32("municipality", BorgerDkMunicipality.GetFromCode);
            Title = obj.GetString("title");
            Header = obj.GetString("header");
            Selected = obj.GetStringArray("selected");
            Article = BorgerDkCachedArticle.Load(Municipality, Domain, Id);
            Blocks = (Article.Exists ? Article.Blocks.Where(IsSelected).ToArray() : new BorgerDkCachedTextElement[0]);
            MicroArticles = (Article.Exists ? Article.MicroArticles.Where(IsSelected).ToArray() : new BorgerDkCachedMicroArticle[0]);
        }

        #endregion

        #region Member methods

        public bool IsSelected(BorgerDkCachedTextElement text) {
            return text != null && Selected.Contains(text.Alias);
        }

        public bool IsSelected(BorgerDkCachedMicroArticle microArticle) {
            return microArticle != null && Selected.Contains(microArticle.Id);
        }
<commit_msg>Improved logic for checking whether an element is selected<commit_after>﻿using System;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Skybrud.BorgerDk;
using Skybrud.Umbraco.BorgerDk.Extensions.JObject;
using Skybrud.Umbraco.BorgerDk.Models.Cached;

namespace Skybrud.Umbraco.BorgerDk.Models {

    /// <summary>
    /// Class representing a selection of a Borger.dk article as saved from either an Umbraco property editor or grid editor.
    /// </summary>
    public class BorgerDkArticleSelection {

        #region Properties

        /// <summary>
        /// Gets a reference to the underlying instance of <see cref="JObject"/>.
        /// </summary>
        [JsonIgnore]
        public JObject JObject { get; private set; }

        /// <summary>
        /// Gets the ID of the selected Borger.dk article, or <code>0</code> if no article has been selected.
        /// </summary>
        public int Id { get; private set; }

        /// <summary>
        /// Gets the URL of the selected Borger.dk article.
        /// </summary>
        public string Url { get; private set; }

        /// <summary>
        /// Gets the URL of the selected Borger.dk article.
        /// </summary>
        public string Domain { get; private set; }

        /// <summary>
        /// Gets the municipality ID of the selected Borger.dk article.
        /// </summary>
        public BorgerDkMunicipality Municipality { get; private set; }

        /// <summary>
        /// Gets the title of the selected Borger.dk article.
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        /// Gets the header of the selected Borger.dk article.
        /// </summary>
        public string Header { get; private set; }

        /// <summary>
        /// Gets an array of the IDs of the selected micro articles and blocks.
        /// </summary>
        public string[] Selected { get; private set; }

        /// <summary>
        /// Gets whether an article has been selected.
        /// </summary>
        public bool HasSelection {
            get { return Id > 0 && !String.IsNullOrWhiteSpace(Url); }
        }

        /// <summary>
        /// Gets a reference to the selected article.
        /// </summary>
        public BorgerDkCachedArticle Article { get; private set; }

        /// <summary>
        /// Gets an array of all selected text blocks.
        /// </summary>
        public BorgerDkCachedTextElement[] Blocks { get; private set; }

        /// <summary>
        /// Gets whether the selection has any text elements.
        /// </summary>
        public bool HasBlocks {
            get { return Blocks.Length > 0; }
        }

        /// <summary>
        /// Gets an array of all selected micro articles.
        /// </summary>
        public BorgerDkCachedMicroArticle[] MicroArticles { get; private set; }

        /// <summary>
        /// Gets whether the selection has any micro articles.
        /// </summary>
        public bool HasMicroArticles {
            get { return MicroArticles.Length > 0; }
        }

        #endregion

        #region Constructors

        private BorgerDkArticleSelection() {
            Selected = new string[0];
            Blocks = new BorgerDkCachedTextElement[0];
            MicroArticles = new BorgerDkCachedMicroArticle[0];
        }

        protected BorgerDkArticleSelection(JObject obj) {
            JObject = obj;
            Id = obj.GetInt32("id");
            Url = obj.GetString("url");
            Domain = obj.GetString("domain");
            Municipality = obj.GetInt32("municipality", BorgerDkMunicipality.GetFromCode);
            Title = obj.GetString("title");
            Header = obj.GetString("header");
            Selected = obj.GetStringArray("selected");
            Article = BorgerDkCachedArticle.Load(Municipality, Domain, Id);
            Blocks = (Article.Exists ? Article.Blocks.Where(IsSelected).ToArray() : new BorgerDkCachedTextElement[0]);
            MicroArticles = (Article.Exists ? Article.MicroArticles.Where(IsSelected).ToArray() : new BorgerDkCachedMicroArticle[0]);
        }

        #endregion

        #region Member methods

        /// <summary>
        /// Gets whether an element with the specified <code>alias</code> has been selected.
        /// </summary>
        /// <param name="alias">The alias (or ID) of the element.</param>
        /// <returns>Returns <code>true</code> if a matching element has been selected, otherwise <code>false</code>.</returns>
        public bool IsSelected(string alias) {
            return !String.IsNullOrWhiteSpace(alias) && Selected.Contains(alias);
        }

        /// <summary>
        /// Gets whether the specified <code>text</code> element has been selected.
        /// </summary>
        /// <param name="text">An instance of <see cref="BorgerDkCachedTextElement"/> representing the text element.</param>
        /// <returns>Returns <code>true</code> if the element has been selected, otherwise <code>false</code>.</returns>
        public bool IsSelected(BorgerDkCachedTextElement text) {
            return text != null && Selected.Contains(text.Alias);
        }

        /// <summary>
        /// Gets whether the specified <code>microArticle</code> element has been selected.
        /// </summary>
        /// <param name="microArticle">An instance of <see cref="BorgerDkCachedMicroArticle"/> representing the micro article.</param>
        /// <returns>Returns <code>true</code> if the micro article has been selected, otherwise <code>false</code>.</returns>
        public bool IsSelected(BorgerDkCachedMicroArticle microArticle) {
            return microArticle != null && Selected.Contains(microArticle.Id);
        }

{'fraction_non_alphanumeric': 0.11455380705698368, 'fraction_numerical': 0.0025412960609911056, 'mean_word_length': 1.9883177570093458, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 113, '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': '936814', 'n_tokens_mistral': 2787, 'n_tokens_neox': 2654, 'n_words': 816}