<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using Sherpa.Library.Taxonomy.Model;

namespace Sherpa.Library.Taxonomy
{
    public class TaxonomyManager
    {
        private readonly ShTermGroup _termGroup;

        public TaxonomyManager(){}
        public TaxonomyManager(ShTermGroup termGroup)
        {
            _termGroup = termGroup;
        }

        public ShTermGroup ExportTaxonomyGroupToConfig(ClientContext context, string groupName)
        {
            TermStore termStore = GetTermStore(context);

            var spTermGroup = termStore.Groups.ToList().FirstOrDefault(g => g.Name == groupName);
            context.Load(spTermGroup, x => x.TermSets);
            context.ExecuteQuery();

            if (spTermGroup == null)
            {
                Console.WriteLine("Couldn't find a taxonomy group with the name " + groupName);
                return null;
            }
            var shTermGroup = new ShTermGroup(spTermGroup.Id, spTermGroup.Name);

            foreach (var spTermSet in spTermGroup.TermSets)
            {
                var shTermSet = new ShTermSet(spTermSet.Id, spTermSet.Name);
                AddTermsToConfig(context, spTermSet, shTermSet);
                shTermGroup.TermSets.Add(shTermSet);
            }
            return shTermGroup;
        }

        private void AddTermsToConfig(ClientContext context, TermSetItem spTerm, ShTermItemBase termItem)
        {
            context.Load(spTerm, t => t.Terms);
            context.ExecuteQuery();

            foreach (var spChildTerm in spTerm.Terms)
            {
                var shTerm = new ShTerm(spChildTerm.Id, spChildTerm.Name);
                AddTermsToConfig(context, spChildTerm, shTerm);
                termItem.Terms.Add(shTerm);
            }
        }

        public void WriteTaxonomyToTermStore(ClientContext context)
        {
            ValidateConfiguration(_termGroup);
            TermStore termStore = GetTermStore(context);

            if (!IsCurrentUserTermStoreAdministrator(context, termStore))
            {
                throw new Exception("Couldn't verify admin access. You must be a term store administrator to perform this operation");
            }

            var termGroup = termStore.Groups.ToList().FirstOrDefault(g => g.Id == _termGroup.Id) ??
                            termStore.CreateGroup(_termGroup.Title, _termGroup.Id);
                
            context.Load(termGroup, x => x.TermSets);
            context.ExecuteQuery();

            foreach (var termSet in _termGroup.TermSets)
            {
                var spTermSet = termStore.GetTermSet(termSet.Id);
                context.Load(spTermSet, x => x.Terms);
                context.ExecuteQuery();
                if (spTermSet.ServerObjectIsNull != null && spTermSet.ServerObjectIsNull.Value)
                {
                    spTermSet = termGroup.CreateTermSet(termSet.Title, termSet.Id, termStore.DefaultLanguage);
                    if (!string.IsNullOrEmpty(termSet.CustomSortOrder))
                        spTermSet.CustomSortOrder = termSet.CustomSortOrder;
                    context.Load(spTermSet, x => x.Terms);
                    context.ExecuteQuery();
                }

                foreach (var term in termSet.Terms)
                {
                    CreateTerm(context, termStore, term, spTermSet);
                }
            }
        }

        private void CreateTerm(ClientContext context, TermStore termStore, ShTerm shTerm, TermSetItem parentTerm)
        {
            var spTerm = termStore.GetTerm(shTerm.Id);
            context.Load(spTerm, t => t.Terms);
            context.ExecuteQuery();
            if (spTerm.ServerObjectIsNull != null && spTerm.ServerObjectIsNull.Value)
            {
                spTerm = parentTerm.CreateTerm(shTerm.Title, termStore.DefaultLanguage, shTerm.Id);
                if (!string.IsNullOrEmpty(shTerm.CustomSortOrder)) spTerm.CustomSortOrder = shTerm.CustomSortOrder;
                spTerm.IsAvailableForTagging = shTerm.NotAvailableForTagging;
                context.Load(spTerm);
                context.ExecuteQuery();
            }
            foreach (ShTerm childTerm in shTerm.Terms)
            {<commit_msg>Updated logic after rename from IsAvailable to NotAvailable<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using Sherpa.Library.Taxonomy.Model;

namespace Sherpa.Library.Taxonomy
{
    public class TaxonomyManager
    {
        private readonly ShTermGroup _termGroup;

        public TaxonomyManager(){}
        public TaxonomyManager(ShTermGroup termGroup)
        {
            _termGroup = termGroup;
        }

        public ShTermGroup ExportTaxonomyGroupToConfig(ClientContext context, string groupName)
        {
            TermStore termStore = GetTermStore(context);

            var spTermGroup = termStore.Groups.ToList().FirstOrDefault(g => g.Name == groupName);
            context.Load(spTermGroup, x => x.TermSets);
            context.ExecuteQuery();

            if (spTermGroup == null)
            {
                Console.WriteLine("Couldn't find a taxonomy group with the name " + groupName);
                return null;
            }
            var shTermGroup = new ShTermGroup(spTermGroup.Id, spTermGroup.Name);

            foreach (var spTermSet in spTermGroup.TermSets)
            {
                var shTermSet = new ShTermSet(spTermSet.Id, spTermSet.Name);
                AddTermsToConfig(context, spTermSet, shTermSet);
                shTermGroup.TermSets.Add(shTermSet);
            }
            return shTermGroup;
        }

        private void AddTermsToConfig(ClientContext context, TermSetItem spTerm, ShTermItemBase termItem)
        {
            context.Load(spTerm, t => t.Terms);
            context.ExecuteQuery();

            foreach (var spChildTerm in spTerm.Terms)
            {
                var shTerm = new ShTerm(spChildTerm.Id, spChildTerm.Name);
                AddTermsToConfig(context, spChildTerm, shTerm);
                termItem.Terms.Add(shTerm);
            }
        }

        public void WriteTaxonomyToTermStore(ClientContext context)
        {
            ValidateConfiguration(_termGroup);
            TermStore termStore = GetTermStore(context);

            if (!IsCurrentUserTermStoreAdministrator(context, termStore))
            {
                throw new Exception("Couldn't verify admin access. You must be a term store administrator to perform this operation");
            }

            var termGroup = termStore.Groups.ToList().FirstOrDefault(g => g.Id == _termGroup.Id) ??
                            termStore.CreateGroup(_termGroup.Title, _termGroup.Id);
                
            context.Load(termGroup, x => x.TermSets);
            context.ExecuteQuery();

            foreach (var termSet in _termGroup.TermSets)
            {
                var spTermSet = termStore.GetTermSet(termSet.Id);
                context.Load(spTermSet, x => x.Terms);
                context.ExecuteQuery();
                if (spTermSet.ServerObjectIsNull != null && spTermSet.ServerObjectIsNull.Value)
                {
                    spTermSet = termGroup.CreateTermSet(termSet.Title, termSet.Id, termStore.DefaultLanguage);
                    if (!string.IsNullOrEmpty(termSet.CustomSortOrder))
                        spTermSet.CustomSortOrder = termSet.CustomSortOrder;
                    context.Load(spTermSet, x => x.Terms);
                    context.ExecuteQuery();
                }

                foreach (var term in termSet.Terms)
                {
                    CreateTerm(context, termStore, term, spTermSet);
                }
            }
        }

        private void CreateTerm(ClientContext context, TermStore termStore, ShTerm shTerm, TermSetItem parentTerm)
        {
            var spTerm = termStore.GetTerm(shTerm.Id);
            context.Load(spTerm, t => t.Terms);
            context.ExecuteQuery();
            if (spTerm.ServerObjectIsNull != null && spTerm.ServerObjectIsNull.Value)
            {
                spTerm = parentTerm.CreateTerm(shTerm.Title, termStore.DefaultLanguage, shTerm.Id);
                if (!string.IsNullOrEmpty(shTerm.CustomSortOrder)) spTerm.CustomSortOrder = shTerm.CustomSortOrder;
                spTerm.IsAvailableForTagging = !shTerm.NotAvailableForTagging;
                context.Load(spTerm);
                context.ExecuteQuery();
            }
            foreach (ShTerm childTerm in shTerm.Terms)
            {
{'fraction_non_alphanumeric': 0.08395202741290692, 'fraction_numerical': 0.0, 'mean_word_length': 1.907038512616202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '6259568', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2214, 'n_words': 449}