<commit_before>// Copyright Naked Objects Group Ltd, 45 Station Road, Henley on Thames, UK, RG9 1AT
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Microsoft.AspNetCore.Http;
using NakedFramework.Facade.Contexts;
using NakedFramework.Facade.Interface;
using NakedFramework.Facade.Translation;
using NakedFramework.Rest.Snapshot.Constants;
using NakedFramework.Rest.Snapshot.RelTypes;
using NakedFramework.Rest.Snapshot.Utility;

namespace NakedFramework.Rest.Snapshot.Representation {
    [DataContract]
    public class ActionResultRepresentation : Representation {
        protected ActionResultRepresentation(IFrameworkFacade frameworkFacade, HttpRequest req, ActionResultContextFacade actionResult, RestControlFlags flags)
            : base(frameworkFacade.OidStrategy, flags) {
            SelfRelType = new ActionResultRelType(RelValues.Self, new UriMtHelper(OidStrategy, req, actionResult.ActionContext));
            SetResultType(actionResult);
            SetLinks(frameworkFacade, req, actionResult);
            SetExtensions(actionResult);
            SetHeader(actionResult);
        }

        [DataMember(Name = JsonPropertyNames.Links)]
        public LinkRepresentation[] Links { get; set; }

        [DataMember(Name = JsonPropertyNames.Extensions)]
        public MapRepresentation Extensions { get; set; }

        [DataMember(Name = JsonPropertyNames.ResultType)]
        public string ResultType { get; set; }

        private void SetHeader(ActionResultContextFacade actionResult) {
            Caching = CacheType.Transactional;

            if (actionResult.Specification.IsObject && actionResult.Result != null) {
                if (actionResult.Result.Target.IsTransient) {
                    SetEtag(actionResult.TransientSecurityHash);
                }
                else {
                    SetEtag(actionResult.Result.Target);
                }
            }
        }

        private static void AddIfPresent(Dictionary<string, object> exts, string[] warningOrMessage, string type) {
            if (warningOrMessage?.Length > 0) {
                exts.Add(type, warningOrMessage);
            }
        }

        private void SetExtensions(ActionResultContextFacade actionResult) {
            var exts = new Dictionary<string, object>();

            AddIfPresent(exts, actionResult.Warnings(), JsonPropertyNames.CustomWarnings);
            AddIfPresent(exts, actionResult.Messages(), JsonPropertyNames.CustomMessages);

            Extensions = exts.Count > 0 ? RestUtils.CreateMap(exts) : new MapRepresentation();
        }

        private void SetLinks(IFrameworkFacade frameworkFacade, HttpRequest req, ActionResultContextFacade actionResult) {
            Links = actionResult.ActionContext.Action.IsQueryOnly ? new[] {LinkRepresentation.Create(OidStrategy, SelfRelType, Flags, new OptionalProperty(JsonPropertyNames.Arguments, CreateArguments(frameworkFacade, req, actionResult)))} : Array.Empty<LinkRepresentation>();
        }

        private void SetResultType(ActionResultContextFacade actionResult) {
            if (actionResult.Specification.IsParseable) {
                ResultType = ResultTypes.Scalar;
            }
            else if (actionResult.Specification.IsCollection) {
                ResultType = ResultTypes.List;
            }
            else {
                ResultType = actionResult.Specification.IsVoid ? ResultTypes.Void : ResultTypes.Object;
            }
        }

        private IObjectFacade GetFacade(IFrameworkFacade frameworkFacade, object obj) => frameworkFacade.GetObject(obj);

        private MapRepresentation CreateArguments(IFrameworkFacade frameworkFacade, HttpRequest req, ActionResultContextFacade actionResult) {
            var optionalProperties = new List<OptionalProperty>();

            foreach (var visibleParamContext in actionResult.ActionContext.VisibleParameters) {
                IRepresentation value;

                if (visibleParamContext.Specification.IsParseable) {
                    var proposedObj = visibleParamContext.ProposedObjectFacade ?? GetFacade(frameworkFacade, visibleParamContext.ProposedValue);
                    var valueObj = proposedObj != null ? RestUtils.ObjectToPredefinedType(proposedObj, false) : null;
                    value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueObj));
                }
                else if (visibleParamContext.Specification.IsCollection) {
                    if (visibleParamContext.ElementSpecification.IsParseable) {
                        var proposedEnumerable = (visibleParamContext.ProposedObjectFacade == null
                            ? visibleParamContext.ProposedValue
                            : visibleParamContext.ProposedObjectFacade?.Object) as IEnumerable;

                        var proposedCollection = proposedEnumerable == null ? Array.Empty<object>() : proposedEnumerable.Cast<object>();

                        var valueObjs = proposedCollection.Select(i => RestUtils.ObjectToPredefinedType(GetFacade(frameworkFacade, i), false)).ToArray();
                        value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueObjs));
                    }
                    else {
                        var refNos = visibleParamContext.ProposedObjectFacade.ToEnumerable().Select(no => no).ToArray();
                        var refs = refNos.Select(no => RefValueRepresentation.Create(OidStrategy, new ObjectRelType(RelValues.Self, new UriMtHelper(OidStrategy, req, no)), Flags)).ToArray();

                        value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, refs));
                    }<commit_msg>Fix bug in last change<commit_after>// Copyright Naked Objects Group Ltd, 45 Station Road, <NAME>, UK, RG9 1AT
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using Microsoft.AspNetCore.Http;
using NakedFramework.Facade.Contexts;
using NakedFramework.Facade.Interface;
using NakedFramework.Facade.Translation;
using NakedFramework.Rest.Snapshot.Constants;
using NakedFramework.Rest.Snapshot.RelTypes;
using NakedFramework.Rest.Snapshot.Utility;

namespace NakedFramework.Rest.Snapshot.Representation {
    [DataContract]
    public class ActionResultRepresentation : Representation {
        protected ActionResultRepresentation(IFrameworkFacade frameworkFacade, HttpRequest req, ActionResultContextFacade actionResult, RestControlFlags flags)
            : base(frameworkFacade.OidStrategy, flags) {
            SelfRelType = new ActionResultRelType(RelValues.Self, new UriMtHelper(OidStrategy, req, actionResult.ActionContext));
            SetResultType(actionResult);
            SetLinks(frameworkFacade, req, actionResult);
            SetExtensions(actionResult);
            SetHeader(actionResult);
        }

        [DataMember(Name = JsonPropertyNames.Links)]
        public LinkRepresentation[] Links { get; set; }

        [DataMember(Name = JsonPropertyNames.Extensions)]
        public MapRepresentation Extensions { get; set; }

        [DataMember(Name = JsonPropertyNames.ResultType)]
        public string ResultType { get; set; }

        private void SetHeader(ActionResultContextFacade actionResult) {
            Caching = CacheType.Transactional;

            if (actionResult.Specification.IsObject && actionResult.Result != null) {
                if (actionResult.Result.Target.IsTransient) {
                    SetEtag(actionResult.TransientSecurityHash);
                }
                else {
                    SetEtag(actionResult.Result.Target);
                }
            }
        }

        private static void AddIfPresent(Dictionary<string, object> exts, string[] warningOrMessage, string type) {
            if (warningOrMessage?.Length > 0) {
                exts.Add(type, warningOrMessage);
            }
        }

        private void SetExtensions(ActionResultContextFacade actionResult) {
            var exts = new Dictionary<string, object>();

            AddIfPresent(exts, actionResult.Warnings(), JsonPropertyNames.CustomWarnings);
            AddIfPresent(exts, actionResult.Messages(), JsonPropertyNames.CustomMessages);

            Extensions = exts.Count > 0 ? RestUtils.CreateMap(exts) : new MapRepresentation();
        }

        private void SetLinks(IFrameworkFacade frameworkFacade, HttpRequest req, ActionResultContextFacade actionResult) {
            Links = actionResult.ActionContext.Action.IsQueryOnly ? new[] {LinkRepresentation.Create(OidStrategy, SelfRelType, Flags, new OptionalProperty(JsonPropertyNames.Arguments, CreateArguments(frameworkFacade, req, actionResult)))} : Array.Empty<LinkRepresentation>();
        }

        private void SetResultType(ActionResultContextFacade actionResult) {
            if (actionResult.Specification.IsParseable) {
                ResultType = ResultTypes.Scalar;
            }
            else if (actionResult.Specification.IsCollection) {
                ResultType = ResultTypes.List;
            }
            else {
                ResultType = actionResult.Specification.IsVoid ? ResultTypes.Void : ResultTypes.Object;
            }
        }

        private IObjectFacade GetFacade(IFrameworkFacade frameworkFacade, object obj) => obj is not null ? frameworkFacade.GetObject(obj): null;

        private MapRepresentation CreateArguments(IFrameworkFacade frameworkFacade, HttpRequest req, ActionResultContextFacade actionResult) {
            var optionalProperties = new List<OptionalProperty>();

            foreach (var visibleParamContext in actionResult.ActionContext.VisibleParameters) {
                IRepresentation value;

                if (visibleParamContext.Specification.IsParseable) {
                    var proposedObj = visibleParamContext.ProposedObjectFacade ?? GetFacade(frameworkFacade, visibleParamContext.ProposedValue);
                    var valueObj = proposedObj is not null ? RestUtils.ObjectToPredefinedType(proposedObj, false) : null;
                    value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueObj));
                }
                else if (visibleParamContext.Specification.IsCollection) {
                    if (visibleParamContext.ElementSpecification.IsParseable) {
                        var proposedEnumerable = (visibleParamContext.ProposedObjectFacade == null
                            ? visibleParamContext.ProposedValue
                            : visibleParamContext.ProposedObjectFacade?.Object) as IEnumerable;

                        var proposedCollection = proposedEnumerable == null ? Array.Empty<object>() : proposedEnumerable.Cast<object>();

                        var valueObjs = proposedCollection.Select(i => RestUtils.ObjectToPredefinedType(GetFacade(frameworkFacade, i), false)).ToArray();
                        value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, valueObjs));
                    }
                    else {
                        var refNos = visibleParamContext.ProposedObjectFacade.ToEnumerable().Select(no => no).ToArray();
                        var refs = refNos.Select(no => RefValueRepresentation.Create(OidStrategy, new ObjectRelType(RelValues.Self, new UriMtHelper(OidStrategy, req, no)), Flags)).ToArray();

                        value = MapRepresentation.Create(new OptionalProperty(JsonPropertyNames.Value, refs));
                    }
{'fraction_non_alphanumeric': 0.07844372686461369, 'fraction_numerical': 0.0015751752382452547, 'mean_word_length': 2.850212249848393, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '987194', 'n_tokens_mistral': 3056, 'n_tokens_neox': 2959, 'n_words': 711}