<commit_before>﻿using HarshPoint.Provisioning.Implementation;
using HarshPoint.Provisioning.Resolvers;
using Microsoft.SharePoint.Client;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace HarshPoint.Provisioning
{
    public class HarshFieldRef : HarshProvisioner
    {
        public HarshFieldRef()
        {
            ExistingFieldLinks = DeferredResolveBuilder.Create(
                () => new ResolveContentTypeFieldLink(ContentType.ValidateIsClientObjectResolveBuilder())
            );
        }

        [DefaultFromContext]
        [Parameter]
        public IResolveSingle<ContentType> ContentType
        {
            get;
            set;
        }

        [Parameter(Mandatory = true)]
        public IResolve<Field> Fields
        {
            get;
            set;
        }

        [Parameter]
        public Boolean Hidden
        {
            get;
            set;
        }

        [Parameter]
        public Boolean Required
        {
            get;
            set;
        }

        protected override void InitializeResolveContext(ClientObjectResolveContext context)
        {
            if (context == null)
            {
                throw Logger.Fatal.ArgumentNull(nameof(context));
            }

            base.InitializeResolveContext(context);

            context.Include<Field>(
                f => f.Id
            );

            context.Include<FieldLink>(
                fl => fl.Id,
                fl => fl.Name
            );
        }

        protected override async Task OnProvisioningAsync()
        {
            var links = from field in Fields
                        select
                            ExistingFieldLinks.FirstOrDefault(fl => fl.Id == field.Id)
                            ??
                            ContentType.Value.FieldLinks.Add(
                                new FieldLinkCreationInformation() { Field = field }
                            );

            foreach (var link in links)
            {
                link.Hidden = Hidden;
                link.Required = Required;
            }

            ContentType.Value.Update(updateChildren: true);
            await ClientContext.ExecuteQueryAsync();
        }

        internal IResolve<FieldLink> ExistingFieldLinks { get; set; }
    }
}
<commit_msg>fieldref use mapping<commit_after>﻿using HarshPoint.Provisioning.Implementation;
using HarshPoint.Provisioning.Resolvers;
using Microsoft.SharePoint.Client;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace HarshPoint.Provisioning
{
    public class HarshFieldRef : HarshProvisioner
    {
        private readonly LazyObjectMapping<HarshFieldRef, FieldLink> _map;
        private readonly RecordWriter<HarshProvisionerContext, FieldLink> _recordWriter;

        public HarshFieldRef()
        {
            ExistingFieldLinks = DeferredResolveBuilder.Create(
                () => new ResolveContentTypeFieldLink(ContentType.ValidateIsClientObjectResolveBuilder())
            );

            _map = new LazyObjectMapping<HarshFieldRef, FieldLink>(Metadata);

            _map.Map(fl => fl.Hidden);
            _map.Map(fl => fl.Required);

            _recordWriter = CreateRecordWriter<FieldLink>();
        }

        [Parameter]
        [DefaultFromContext]
        public IResolveSingle<ContentType> ContentType { get; set; }

        [Parameter(Mandatory = true)]
        public IResolve<Field> Fields { get; set; }

        [Parameter]
        public Boolean? Hidden { get; set; }

        [Parameter]
        public Boolean? Required { get; set; }

        protected override void InitializeResolveContext(ClientObjectResolveContext context)
        {
            if (context == null)
            {
                throw Logger.Fatal.ArgumentNull(nameof(context));
            }

            base.InitializeResolveContext(context);

            context.Include<Field>(
                f => f.Id
            );

            context.Include<FieldLink>(
                fl => fl.Id,
                fl => fl.Name
            );

            context.Include(
                _map.GetTargetExpressions()
            );
        }

        protected override async Task OnProvisioningAsync()
        {
            var links = from field in Fields
                        select
                            ExistingFieldLinks.FirstOrDefault(fl => fl.Id == field.Id)
                            ??
                            ContentType.Value.FieldLinks.Add(
                                new FieldLinkCreationInformation() { Field = field }
                            );

            var changed = false;

            foreach (var link in links)
            {
                changed |= _map.Apply(_recordWriter, this, link);
            }

            if (changed)
            {
                ContentType.Value.Update(updateChildren: true);
                await ClientContext.ExecuteQueryAsync();
            }
        }

        internal IResolve<FieldLink> ExistingFieldLinks { get; set; }
    }
}

{'fraction_non_alphanumeric': 0.07829181494661921, 'fraction_numerical': 0.0, 'mean_word_length': 1.5069375619425174, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7140618', 'n_tokens_mistral': 1244, 'n_tokens_neox': 1210, 'n_words': 243}