<commit_before>﻿using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Xml.Linq;

namespace HarshPoint.Provisioning
{
    public sealed class HarshFieldProvisioner : HarshFieldProvisionerBase
    {
        private readonly HarshFieldSchemaXmlBuilder SchemaXmlBuilder;

        public HarshFieldProvisioner()
        {
            SchemaXmlBuilder = new HarshFieldSchemaXmlBuilder()
            {
                Transformers =
                {
                    new NonNullAttributeSetter(() => FieldTypeName, "Type"),
                    new NonNullAttributeSetter(() => InternalName, onFieldAddOnly: true),
                    new NonNullAttributeSetter(() => StaticName, onFieldAddOnly: true),
                }
            };
        }

        public AddFieldOptions AddFieldOptions
        {
            get;
            set;
        }

        public Boolean AddToDefaultView
        {
            get;
            set;
        }

        public String FieldTypeName
        {
            get;
            set;
        }

        public String InternalName
        {
            get;
            set;
        }

        public String StaticName
        {
            get;
            set;
        }

        public XElement SchemaXml
        {
            get;
            set;
        }

        public Collection<HarshFieldSchemaXmlTransformer> SchemaXmlTransformers
        {
            get { return SchemaXmlBuilder.Transformers; }
        }

        public Boolean FieldAdded
        {
            get;
            private set;
        }

        public Boolean FieldRemoved
        {
            get;
            private set;
        }

        public Boolean FieldUpdated
        {
            get;
            private set;
        }

        protected override void Initialize()
        {
            FieldAdded = false;
            FieldRemoved = false;
            FieldUpdated = false;

            base.Initialize();
        }

        protected override void OnProvisioning()
        {
            base.OnProvisioning();

            SchemaXml = SchemaXmlBuilder.Update(Field, SchemaXml);

            if (Field.IsNull())
            {
                Field = TargetFieldCollection.AddFieldAsXml(SchemaXml.ToString(), AddToDefaultView, AddFieldOptions);
                Context.ExecuteQuery();

                FieldAdded = true;
            }
            else
            {
                Field.SchemaXml = SchemaXml.ToString();
                Context.ExecuteQuery();

                FieldUpdated = true;
            }
        }

        protected override void OnUnprovisioningMayDeleteUserData()
        {
            if (!Field.IsNull())
            {
                Field.DeleteObject();
                Context.ExecuteQuery();

                FieldRemoved = true;
            }

            base.OnUnprovisioningMayDeleteUserData();
        }

        private sealed class NonNullAttributeSetter : HarshFieldSchemaXmlTransformer
        {
            private readonly XName _name;
            private readonly Func<Object> _valueAccessor;

            public NonNullAttributeSetter(Expression<Func<Object>> valueAccessorExpr, XName name = null, Boolean onFieldAddOnly = false)
            {
                if (name == null)
                {
                    _name = valueAccessorExpr.GetMemberName();
                }<commit_msg>dont update field schema if not different<commit_after>﻿using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Xml.Linq;

namespace HarshPoint.Provisioning
{
    public sealed class HarshFieldProvisioner : HarshFieldProvisionerBase
    {
        private readonly HarshFieldSchemaXmlBuilder SchemaXmlBuilder;
        private readonly XNodeEqualityComparer SchemaXmlComparer;

        public HarshFieldProvisioner()
        {
            SchemaXmlBuilder = new HarshFieldSchemaXmlBuilder()
            {
                Transformers =
                {
                    new NonNullAttributeSetter(() => FieldTypeName, "Type"),
                    new NonNullAttributeSetter(() => InternalName, onFieldAddOnly: true),
                    new NonNullAttributeSetter(() => StaticName, onFieldAddOnly: true),
                }
            };
            SchemaXmlComparer = new XNodeEqualityComparer();
        }

        public AddFieldOptions AddFieldOptions
        {
            get;
            set;
        }

        public Boolean AddToDefaultView
        {
            get;
            set;
        }

        public String FieldTypeName
        {
            get;
            set;
        }

        public String InternalName
        {
            get;
            set;
        }

        public String StaticName
        {
            get;
            set;
        }

        public XElement SchemaXml
        {
            get;
            set;
        }

        public Collection<HarshFieldSchemaXmlTransformer> SchemaXmlTransformers
        {
            get { return SchemaXmlBuilder.Transformers; }
        }

        public Boolean FieldAdded
        {
            get;
            private set;
        }

        public Boolean FieldRemoved
        {
            get;
            private set;
        }

        public Boolean FieldUpdated
        {
            get;
            private set;
        }

        protected override void Initialize()
        {
            FieldAdded = false;
            FieldRemoved = false;
            FieldUpdated = false;

            base.Initialize();
        }

        protected override void OnProvisioning()
        {
            base.OnProvisioning();

            SchemaXml = SchemaXmlBuilder.Update(Field, SchemaXml);

            if (Field.IsNull())
            {
                Field = TargetFieldCollection.AddFieldAsXml(SchemaXml.ToString(), AddToDefaultView, AddFieldOptions);
                Context.ExecuteQuery();

                FieldAdded = true;
            }
            else
            {
                var existingSchemaXml = SchemaXmlBuilder.GetExistingSchemaXml(Field);

                if (!SchemaXmlComparer.Equals(existingSchemaXml, SchemaXml))
                {
                    Field.SchemaXml = SchemaXml.ToString();
                    Context.ExecuteQuery();

                    FieldUpdated = true;
                }
            }
        }

        protected override void OnUnprovisioningMayDeleteUserData()
        {
            if (!Field.IsNull())
            {
                Field.DeleteObject();
                Context.ExecuteQuery();

                FieldRemoved = true;
            }

            base.OnUnprovisioningMayDeleteUserData();
        }

        private sealed class NonNullAttributeSetter : HarshFieldSchemaXmlTransformer
        {
            private readonly XName _name;
            private readonly Func<Object> _valueAccessor;

            public NonNullAttributeSetter(Expression<Func<Object>> valueAccessorExpr, XName name = null, Boolean onFieldAddOnly = false)
            {
                if (name == null)
                {
                    _name = valueAccessorExpr.GetMemberName();
                }
{'fraction_non_alphanumeric': 0.06530889341479973, 'fraction_numerical': 0.0, 'mean_word_length': 1.3892312682452157, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '521802', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1659, 'n_words': 361}