<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using Trifolia.Authorization;
using Trifolia.DB;
using Trifolia.Shared;
using Trifolia.Shared.Plugins;
using Trifolia.Shared.Validation;

namespace Trifolia.Plugins.Validation
{
    public abstract class BaseValidator : IValidator
    {
        protected IObjectRepository tdb;

        public BaseValidator(IObjectRepository tdb)
        {
            this.tdb = tdb;
        }

        public ValidationResults ValidateImplementationGuide(int implementationGuideId)
        {
            var implementationGuide = this.tdb.ImplementationGuides.Single(y => y.Id == implementationGuideId);
            var igSchema = implementationGuide.ImplementationGuideType.GetSimpleSchema();

            return this.ValidateImplementationGuide(implementationGuide, igSchema);
        }

        protected virtual ValidationResults ValidateImplementationGuide(ImplementationGuide implementationGuide, SimpleSchema igSchema)
        {
            ValidationResults results = new ValidationResults();

            var plugin = implementationGuide.ImplementationGuideType.GetPlugin();
            var childTemplateIds = implementationGuide.ChildTemplates.Select(y => y.Id);
            var containedTemplateIds = (from vtr in this.tdb.ViewTemplateRelationships.AsNoTracking()
                                      join pt in childTemplateIds on vtr.ParentTemplateId equals pt
                                      join ct in this.tdb.Templates.AsNoTracking() on vtr.ChildTemplateId equals ct.Id
                                      select ct.Id).Distinct();
            var containedTemplates = (from cti in containedTemplateIds
                                      join t in this.tdb.Templates.AsNoTracking() on cti equals t.Id
                                      select t).ToList();
            var valueSets = (from tc in this.tdb.TemplateConstraints
                             join t in this.tdb.Templates on tc.TemplateId equals t.Id
                             join vs in this.tdb.ValueSets on tc.ValueSetId equals vs.Id
                             join cti in childTemplateIds on t.Id equals cti
                             select vs);
            var valueSetCodesWithWhitespace = (from vsm in this.tdb.ViewValueSetMemberWhiteSpaces
                                               join vs in valueSets on vsm.ValueSetId equals vs.Id
                                               select new
                                               {
                                                   ValueSet = vs,
                                                   ValueSetMember = vsm
                                               });

            foreach (var template in implementationGuide.ChildTemplates)
            {
                var result = new TemplateValidationResult()
                {
                    Id = template.Id,
                    Name = template.Name,
                    Oid = template.Oid,
                    Items = this.ValidateTemplate(template, igSchema, containedTemplates)
                };

                if (result.Items.Count > 0)
                    results.TemplateResults.Add(result);
            }

            foreach (var vscww in valueSetCodesWithWhitespace)
            {
                var identifier = vscww.ValueSet.GetIdentifier(plugin);

                if (vscww.ValueSetMember.Code.Trim() != vscww.ValueSetMember.Code)
                    results.Messages.Add("Value set \"" + vscww.ValueSet.Name + "\" (" + identifier + ") has a code \"" + vscww.ValueSetMember.Code + "\" with white spaces.");

                if (vscww.ValueSetMember.DisplayName.Trim() != vscww.ValueSetMember.DisplayName)
                    results.Messages.Add("Value set \"" + vscww.ValueSet.Name + "\" (" + identifier + ") has a code \"" + vscww.ValueSetMember.Code + "\" whose display name \"" + vscww.ValueSetMember.DisplayName + "\" has white spaces.");
            }

            if (implementationGuide.HasImportedValueSets(this.tdb, ValueSetImportSources.VSAC))
            {
                User currentUser = CheckPoint.Instance.GetUser(this.tdb);

                if (!currentUser.HasValidUmlsLicense())
                {
                    results.RestrictDownload = true;
                    results.Messages.Add("This implementation guide contains VSAC content that you do not currently have a license to. <a href=\"/Account/MyProfile\">Update your profile</a> with your UMLS/VSAC credentials to export this implementation guide.");
                }
            }

            return results;
        }

        public List<ValidationResult> ValidateTemplate(int templateId)
        {
            Template template = this.tdb.Templates.Single(y => y.Id == templateId);<commit_msg>TRIF-1303: Value set validation during export for white spaces Modifying validation message to including "leading or trailing"<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using Trifolia.Authorization;
using Trifolia.DB;
using Trifolia.Shared;
using Trifolia.Shared.Plugins;
using Trifolia.Shared.Validation;

namespace Trifolia.Plugins.Validation
{
    public abstract class BaseValidator : IValidator
    {
        protected IObjectRepository tdb;

        public BaseValidator(IObjectRepository tdb)
        {
            this.tdb = tdb;
        }

        public ValidationResults ValidateImplementationGuide(int implementationGuideId)
        {
            var implementationGuide = this.tdb.ImplementationGuides.Single(y => y.Id == implementationGuideId);
            var igSchema = implementationGuide.ImplementationGuideType.GetSimpleSchema();

            return this.ValidateImplementationGuide(implementationGuide, igSchema);
        }

        protected virtual ValidationResults ValidateImplementationGuide(ImplementationGuide implementationGuide, SimpleSchema igSchema)
        {
            ValidationResults results = new ValidationResults();

            var plugin = implementationGuide.ImplementationGuideType.GetPlugin();
            var childTemplateIds = implementationGuide.ChildTemplates.Select(y => y.Id);
            var containedTemplateIds = (from vtr in this.tdb.ViewTemplateRelationships.AsNoTracking()
                                      join pt in childTemplateIds on vtr.ParentTemplateId equals pt
                                      join ct in this.tdb.Templates.AsNoTracking() on vtr.ChildTemplateId equals ct.Id
                                      select ct.Id).Distinct();
            var containedTemplates = (from cti in containedTemplateIds
                                      join t in this.tdb.Templates.AsNoTracking() on cti equals t.Id
                                      select t).ToList();
            var valueSets = (from tc in this.tdb.TemplateConstraints
                             join t in this.tdb.Templates on tc.TemplateId equals t.Id
                             join vs in this.tdb.ValueSets on tc.ValueSetId equals vs.Id
                             join cti in childTemplateIds on t.Id equals cti
                             select vs);
            var valueSetCodesWithWhitespace = (from vsm in this.tdb.ViewValueSetMemberWhiteSpaces
                                               join vs in valueSets on vsm.ValueSetId equals vs.Id
                                               select new
                                               {
                                                   ValueSet = vs,
                                                   ValueSetMember = vsm
                                               });

            foreach (var template in implementationGuide.ChildTemplates)
            {
                var result = new TemplateValidationResult()
                {
                    Id = template.Id,
                    Name = template.Name,
                    Oid = template.Oid,
                    Items = this.ValidateTemplate(template, igSchema, containedTemplates)
                };

                if (result.Items.Count > 0)
                    results.TemplateResults.Add(result);
            }

            foreach (var vscww in valueSetCodesWithWhitespace)
            {
                var identifier = vscww.ValueSet.GetIdentifier(plugin);

                if (vscww.ValueSetMember.Code.Trim() != vscww.ValueSetMember.Code)
                    results.Messages.Add("Value set \"" + vscww.ValueSet.Name + "\" (" + identifier + ") has a code \"" + vscww.ValueSetMember.Code + "\" with leading or trailing white spaces.");

                if (vscww.ValueSetMember.DisplayName.Trim() != vscww.ValueSetMember.DisplayName)
                    results.Messages.Add("Value set \"" + vscww.ValueSet.Name + "\" (" + identifier + ") has a code \"" + vscww.ValueSetMember.Code + "\" whose display name \"" + vscww.ValueSetMember.DisplayName + "\" has leading or trailing white spaces.");
            }

            if (implementationGuide.HasImportedValueSets(this.tdb, ValueSetImportSources.VSAC))
            {
                User currentUser = CheckPoint.Instance.GetUser(this.tdb);

                if (!currentUser.HasValidUmlsLicense())
                {
                    results.RestrictDownload = true;
                    results.Messages.Add("This implementation guide contains VSAC content that you do not currently have a license to. <a href=\"/Account/MyProfile\">Update your profile</a> with your UMLS/VSAC credentials to export this implementation guide.");
                }
            }

            return results;
        }

        public List<ValidationResult> ValidateTemplate(int templateId)
        {
            Template template = this.tdb.Templates.Single(y => y.Id == templateId);
{'fraction_non_alphanumeric': 0.0751834355211579, 'fraction_numerical': 0.0006030756859985928, 'mean_word_length': 1.7156113537117903, 'pattern_counts': {'":': 0, '<': 9, '<?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': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5315155', 'n_tokens_mistral': 2397, 'n_tokens_neox': 2282, 'n_words': 573}