<commit_before>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using log4net;
using Microsoft.SharePoint.Client;
using Sherpa.Library.SiteHierarchy.Model;
using System.Collections.Generic;

namespace Sherpa.Library.SiteHierarchy
{
    public class SiteSetupManager : ISiteSetupManager
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public readonly ShSiteCollection ConfigurationSiteCollection;
        private ClientContext ClientContext { get; set; }
        private FeatureManager FeatureManager { get; set; }
        private QuicklaunchManager QuicklaunchManager { get; set; }
        private PropertyManager PropertyManager { get; set; }
        private ListManager ListManager { get; set; }
        private ContentUploadManager ContentUploadManager { get; set; }

        public SiteSetupManager(ClientContext clientContext, ShSiteCollection configurationSiteCollection, string rootConfigurationPath)
        {
            ConfigurationSiteCollection = configurationSiteCollection;
            ClientContext = clientContext;

            FeatureManager = new FeatureManager();
            QuicklaunchManager = new QuicklaunchManager();
            PropertyManager = new PropertyManager();
            ListManager = new ListManager();

            var contentConfigurationPath = Path.Combine(rootConfigurationPath, "content");
            ContentUploadManager = new ContentUploadManager(contentConfigurationPath);
        }
        public void SetupSites()
        {
            Log.Debug("Starting SetupSites - setting up site collection");
            //FeatureManager.ActivateSiteCollectionFeatures(ClientContext, ConfigurationSiteCollection.SiteFeatures);
            //EnsureAndConfigureWebAndActivateFeatures(ClientContext, null, ConfigurationSiteCollection.RootWeb);
            SetUpCustomPermissionLevels(ClientContext, ConfigurationSiteCollection.PermissionLevels);
        }

        public void SetUpCustomPermissionLevels(ClientContext context, List<ShPermissionLevel> permissionLevels)
        {
            foreach (var permissionLevel in permissionLevels)
            {
                context.Load(context.Site.RootWeb.RoleDefinitions);
                context.ExecuteQuery();

                var existing = context.Site.RootWeb.RoleDefinitions.FirstOrDefault(x => x.Name.Equals(permissionLevel.Name));
                context.Load(existing);
                context.ExecuteQuery();

                if (existing == null)
                {
                    BasePermissions permissions = new BasePermissions();
                    foreach (var basePermission in permissionLevel.BasePermissions)
                    {
                        permissions.Set(basePermission);<commit_msg>Fix when permission level doesnt exist<commit_after>﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;
using log4net;
using Microsoft.SharePoint.Client;
using Sherpa.Library.SiteHierarchy.Model;
using System.Collections.Generic;

namespace Sherpa.Library.SiteHierarchy
{
    public class SiteSetupManager : ISiteSetupManager
    {
        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public readonly ShSiteCollection ConfigurationSiteCollection;
        private ClientContext ClientContext { get; set; }
        private FeatureManager FeatureManager { get; set; }
        private QuicklaunchManager QuicklaunchManager { get; set; }
        private PropertyManager PropertyManager { get; set; }
        private ListManager ListManager { get; set; }
        private ContentUploadManager ContentUploadManager { get; set; }

        public SiteSetupManager(ClientContext clientContext, ShSiteCollection configurationSiteCollection, string rootConfigurationPath)
        {
            ConfigurationSiteCollection = configurationSiteCollection;
            ClientContext = clientContext;

            FeatureManager = new FeatureManager();
            QuicklaunchManager = new QuicklaunchManager();
            PropertyManager = new PropertyManager();
            ListManager = new ListManager();

            var contentConfigurationPath = Path.Combine(rootConfigurationPath, "content");
            ContentUploadManager = new ContentUploadManager(contentConfigurationPath);
        }
        public void SetupSites()
        {
            Log.Debug("Starting SetupSites - setting up site collection");
            //FeatureManager.ActivateSiteCollectionFeatures(ClientContext, ConfigurationSiteCollection.SiteFeatures);
            //EnsureAndConfigureWebAndActivateFeatures(ClientContext, null, ConfigurationSiteCollection.RootWeb);
            SetUpCustomPermissionLevels(ClientContext, ConfigurationSiteCollection.PermissionLevels);
        }

        public void SetUpCustomPermissionLevels(ClientContext context, List<ShPermissionLevel> permissionLevels)
        {
            foreach (var permissionLevel in permissionLevels)
            {
                context.Load(context.Site.RootWeb.RoleDefinitions);
                context.ExecuteQuery();

                var existingPermissionLevel = context.Site.RootWeb.RoleDefinitions.FirstOrDefault(x => x.Name.Equals(permissionLevel.Name));
                if (existingPermissionLevel == null)
                {
                    BasePermissions permissions = new BasePermissions();
                    foreach (var basePermission in permissionLevel.BasePermissions)
                    {
                        permissions.Set(basePermission);
{'fraction_non_alphanumeric': 0.0667618707604427, 'fraction_numerical': 0.0003570153516601214, 'mean_word_length': 2.9485553206483437, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '521846', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1192, 'n_words': 282}