<commit_before>/*
* Copyright (c) 2014 All Rights Reserved by the SDL Group.
* 
* 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.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using Trisoft.ISHRemote.Interfaces;
using Trisoft.ISHRemote.Objects;
using Trisoft.ISHRemote.Objects.Public;

namespace Trisoft.ISHRemote.HelperClasses
{
    internal class TriDKXmlSetupHelper
    {
        #region Private Members
        /// <summary>
        /// Logger
        /// </summary>
        private readonly ILogger _logger;
        /// <summary>
        /// Returning list of definitions
        /// </summary>
        private SortedDictionary<string, IshTypeFieldDefinition> _ishTypeFieldDefinitions;
        /// <summary>
        /// cardTypeElementName and list of fieldElementNames
        /// </summary>
        private Dictionary<string, List<CardTypeFieldDefinition>> _cardTypeFieldElementNames = new Dictionary<string, List<CardTypeFieldDefinition>>();
        /// <summary>
        /// fieldElementNames and FieldDefinition matching TriDKXmlSetup tridk:field
        /// </summary>
        private Dictionary<string, FieldDefinition> _fieldDefinitionElementNames = new Dictionary<string, FieldDefinition>();
        /// <summary>
        /// Loaded TriDKXmlSetup Full Export 
        /// </summary>
        XPathDocument _xpathDocument;
        #endregion

        internal class CardTypeFieldDefinition
        {
            internal Enumerations.ISHType Type { get; set; }
            internal Enumerations.Level Level { get; set; }
            internal string FieldName { get; set; }
            public List<Enumerations.ISHType> ReferenceType { get; }
            internal CardTypeFieldDefinition(Enumerations.ISHType ishType, Enumerations.Level ishLevel, string ishFieldName)
            {
                Type = ishType;
                Level = ishLevel;
                FieldName = ishFieldName;
                ReferenceType = new List<Enumerations.ISHType>();
            }
            internal static string Key(Enumerations.ISHType ishType, Enumerations.Level ishLevel)
            {
                return Key(ishType, ishLevel, "");
            }
            internal static string Key(Enumerations.ISHType ishType, Enumerations.Level ishLevel, string ishFieldName)
            {
                return ishType + "=" + (int)ishLevel + ishLevel + "=" + ishFieldName;
            }
            internal static string Key(CardTypeFieldDefinition cardTypeFieldDefinition)
            {
                return Key(cardTypeFieldDefinition.Type, cardTypeFieldDefinition.Level, cardTypeFieldDefinition.FieldName);
            }
        }

        internal class FieldDefinition
        {
            internal string Name { get; set; }
            internal string Label { get; set; }
            internal string Description { get; set; }
            internal string ReferenceLov { get; set; }
            internal Enumerations.DataType DataType { get; set; }
            internal bool IsMandatory { get; set; }
            internal bool IsMultiValue { get; set; }
            internal bool IsSystem { get; set; }
            internal bool IsBasic { get; set; }
            public bool IsDescriptive { get; set; }
            public bool AllowOnRead { get; set; }
            public bool AllowOnCreate { get; set; }
            public bool AllowOnUpdate { get; set; }
            public bool AllowOnSearch { get; set; }


            /// <summary>
            /// Private temporary container that initializes that custom fields are okay, system/standard fields will need correction later
            /// </summary>
            internal FieldDefinition(string name, string label, string description, string dataType, string referenceLov, long min, long max, string isPublic, string isSystem, List<string> classes)
            {
                Name = name;
                Label = label;
                Description = description;
                IsMandatory = (min >= 1);
                IsMultiValue = (max > 1);
                switch (dataType)
                {
                    case "typedate":
                        DataType = Enumerations.DataType.DateTime;
                        break;
                    case "typelov":
                        DataType = Enumerations.DataType.ISHLov;
                        ReferenceLov = referenceLov;
                        break;
                    case "typestring":
                    case "typelanguagedependentstring":
                        DataType = Enumerations.DataType.String;
                        break;
                    case "typecard":
                    case "typecardreference":
                        DataType = Enumerations.DataType.ISHType;
                        break;
                    case "typelongtext":
                        DataType = Enumerations.DataType.LongText;
                        IsMultiValue = false;
                        break;
                    case "typenumber":
                        DataType = Enumerations.DataType.Number;
                        break;
                }
                IsBasic = classes.Contains("SECURITY") ? false : isPublic.Equals("yes", StringComparison.InvariantCulture);
                IsSystem = isSystem.Equals("yes", StringComparison.InvariantCulture);
                string[] descriptiveFields = { "VERSION", "DOC-LANGUAGE", "FRESOLUTION", "FISHPUBLNGCOMBINATION", "FISHOUTPUTFORMATREF", "FISHOUTPUTFORMATNAME", "FISHEDTNAME", "FNAME", "USERNAME", "FISHUSERROLENAME", "FISHUSERGROUPNAME" };
                IsDescriptive = descriptiveFields.Contains<string>(name);
                string[] notAllowOnReadFields = { "PASSWORD" };
                AllowOnRead = !notAllowOnReadFields.Contains<string>(name);
                AllowOnCreate = classes.Contains("NEW");
                AllowOnUpdate = classes.Contains("MODIFY");
                AllowOnSearch = classes.Contains("INDEX");
            }

            internal void Correct(bool allowOnCreate, bool allowOnRead, bool allowOnUpdate, bool allowOnSearch)
            {
                AllowOnCreate = allowOnCreate;
                AllowOnRead = AllowOnRead;
                AllowOnUpdate = allowOnUpdate;
                AllowOnSearch = allowOnSearch;
            }

            internal void Correct(bool allowOnCreate, bool allowOnRead, bool allowOnUpdate, bool allowOnSearch, bool isDescriptive, bool isBasic)
            {
                AllowOnRead = AllowOnRead;
                AllowOnCreate = allowOnCreate;<commit_msg>#23 Added FISHDITADLVRCLIENTSECRET as alternative PASSWORD field, forbidden to read<commit_after>/*
* Copyright (c) 2014 All Rights Reserved by the SDL Group.
* 
* 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.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using Trisoft.ISHRemote.Interfaces;
using Trisoft.ISHRemote.Objects;
using Trisoft.ISHRemote.Objects.Public;

namespace Trisoft.ISHRemote.HelperClasses
{
    internal class TriDKXmlSetupHelper
    {
        #region Private Members
        /// <summary>
        /// Logger
        /// </summary>
        private readonly ILogger _logger;
        /// <summary>
        /// Returning list of definitions
        /// </summary>
        private SortedDictionary<string, IshTypeFieldDefinition> _ishTypeFieldDefinitions;
        /// <summary>
        /// cardTypeElementName and list of fieldElementNames
        /// </summary>
        private Dictionary<string, List<CardTypeFieldDefinition>> _cardTypeFieldElementNames = new Dictionary<string, List<CardTypeFieldDefinition>>();
        /// <summary>
        /// fieldElementNames and FieldDefinition matching TriDKXmlSetup tridk:field
        /// </summary>
        private Dictionary<string, FieldDefinition> _fieldDefinitionElementNames = new Dictionary<string, FieldDefinition>();
        /// <summary>
        /// Loaded TriDKXmlSetup Full Export 
        /// </summary>
        XPathDocument _xpathDocument;
        #endregion

        internal class CardTypeFieldDefinition
        {
            internal Enumerations.ISHType Type { get; set; }
            internal Enumerations.Level Level { get; set; }
            internal string FieldName { get; set; }
            public List<Enumerations.ISHType> ReferenceType { get; }
            internal CardTypeFieldDefinition(Enumerations.ISHType ishType, Enumerations.Level ishLevel, string ishFieldName)
            {
                Type = ishType;
                Level = ishLevel;
                FieldName = ishFieldName;
                ReferenceType = new List<Enumerations.ISHType>();
            }
            internal static string Key(Enumerations.ISHType ishType, Enumerations.Level ishLevel)
            {
                return Key(ishType, ishLevel, "");
            }
            internal static string Key(Enumerations.ISHType ishType, Enumerations.Level ishLevel, string ishFieldName)
            {
                return ishType + "=" + (int)ishLevel + ishLevel + "=" + ishFieldName;
            }
            internal static string Key(CardTypeFieldDefinition cardTypeFieldDefinition)
            {
                return Key(cardTypeFieldDefinition.Type, cardTypeFieldDefinition.Level, cardTypeFieldDefinition.FieldName);
            }
        }

        internal class FieldDefinition
        {
            internal string Name { get; set; }
            internal string Label { get; set; }
            internal string Description { get; set; }
            internal string ReferenceLov { get; set; }
            internal Enumerations.DataType DataType { get; set; }
            internal bool IsMandatory { get; set; }
            internal bool IsMultiValue { get; set; }
            internal bool IsSystem { get; set; }
            internal bool IsBasic { get; set; }
            public bool IsDescriptive { get; set; }
            public bool AllowOnRead { get; set; }
            public bool AllowOnCreate { get; set; }
            public bool AllowOnUpdate { get; set; }
            public bool AllowOnSearch { get; set; }


            /// <summary>
            /// Private temporary container that initializes that custom fields are okay, system/standard fields will need correction later
            /// </summary>
            internal FieldDefinition(string name, string label, string description, string dataType, string referenceLov, long min, long max, string isPublic, string isSystem, List<string> classes)
            {
                Name = name;
                Label = label;
                Description = description;
                IsMandatory = (min >= 1);
                IsMultiValue = (max > 1);
                switch (dataType)
                {
                    case "typedate":
                        DataType = Enumerations.DataType.DateTime;
                        break;
                    case "typelov":
                        DataType = Enumerations.DataType.ISHLov;
                        ReferenceLov = referenceLov;
                        break;
                    case "typestring":
                    case "typelanguagedependentstring":
                        DataType = Enumerations.DataType.String;
                        break;
                    case "typecard":
                    case "typecardreference":
                        DataType = Enumerations.DataType.ISHType;
                        break;
                    case "typelongtext":
                        DataType = Enumerations.DataType.LongText;
                        IsMultiValue = false;
                        break;
                    case "typenumber":
                        DataType = Enumerations.DataType.Number;
                        break;
                }
                IsBasic = classes.Contains("SECURITY") ? false : isPublic.Equals("yes", StringComparison.InvariantCulture);
                IsSystem = isSystem.Equals("yes", StringComparison.InvariantCulture);
                string[] descriptiveFields = { "VERSION", "DOC-LANGUAGE", "FRESOLUTION", "FISHPUBLNGCOMBINATION", "FISHOUTPUTFORMATREF", "FISHOUTPUTFORMATNAME", "FISHEDTNAME", "FNAME", "USERNAME", "FISHUSERROLENAME", "FISHUSERGROUPNAME" };
                IsDescriptive = descriptiveFields.Contains<string>(name);
                string[] notAllowOnReadFields = { "PASSWORD", "<PASSWORD>" };
                AllowOnRead = !notAllowOnReadFields.Contains<string>(name);
                AllowOnCreate = classes.Contains("NEW");
                AllowOnUpdate = classes.Contains("MODIFY");
                AllowOnSearch = classes.Contains("INDEX");
            }

            internal void Correct(bool allowOnCreate, bool allowOnRead, bool allowOnUpdate, bool allowOnSearch)
            {
                AllowOnCreate = allowOnCreate;
                AllowOnRead = AllowOnRead;
                AllowOnUpdate = allowOnUpdate;
                AllowOnSearch = allowOnSearch;
            }

            internal void Correct(bool allowOnCreate, bool allowOnRead, bool allowOnUpdate, bool allowOnSearch, bool isDescriptive, bool isBasic)
            {
                AllowOnRead = AllowOnRead;
                AllowOnCreate = allowOnCreate;
{'fraction_non_alphanumeric': 0.0803087192323738, 'fraction_numerical': 0.0015296898901404534, 'mean_word_length': 1.9251576164327842, 'pattern_counts': {'":': 16, '<': 52, '<?xml version=': 0, '>': 56, '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': '7151985', 'n_tokens_mistral': 3535, 'n_tokens_neox': 3383, 'n_words': 963}