<commit_before>﻿//******************************************************************************************************
//  HTMLWriter.cs - Gbtc
//
//  Copyright © 2014, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.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.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  11/14/2014 - <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;
using FaultData.DataAnalysis;
using FaultData.Database;
using FaultData.Database.FaultLocationDataTableAdapters;
using FaultData.Database.MeterDataTableAdapters;
using DataPoint = FaultData.DataAnalysis.DataPoint;
using Series = System.Windows.Forms.DataVisualization.Charting.Series;

namespace FaultData.DataWriters
{
    /// <summary>
    /// Writes the results of fault analysis to an email.
    /// </summary>
    public class EmailWriter
    {
        #region [ Members ]

        // Nested Types
        private class FaultRecordInfo
        {
            public Meter Meter;
            public Line Line;

            public MeterData.EventRow Event;
            public List<FaultLocationData.FaultCurveRow> FaultCurves;
            public FaultLocationData.FaultSummaryRow FaultSummary;

            public List<Recipient> Recipients;
        }

        // Fields
        private string m_connectionString;

        private string m_smtpServer;
        private string m_fromAddress;
        private double m_maxFaultDistanceMultiplier;
        private double m_minFaultDistanceMultiplier;
        private string m_lengthUnits;

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// Creates a new instance of the <see cref="XMLWriter"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string used to connect to the database containing fault location data.</param>
        public EmailWriter(string connectionString)
        {
            m_connectionString = connectionString;
        }

        #endregion

        #region [ Properties ]

        public string SMTPServer
        {
            get
            {
                return m_smtpServer;
            }
            set
            {
                m_smtpServer = value;
            }
        }

        public string FromAddress
        {
            get
            {
                return m_fromAddress;
            }
            set
            {
                m_fromAddress = value;
            }
        }

        public double MaxFaultDistanceMultiplier
        {
            get
            {
                return m_maxFaultDistanceMultiplier;
            }
            set
            {
                m_maxFaultDistanceMultiplier = value;
            }
        }

        public double MinFaultDistanceMultiplier
        {
            get
            {
                return m_minFaultDistanceMultiplier;
            }
            set
            {
                m_minFaultDistanceMultiplier = value;
            }
        }

        public string LengthUnits
        {
            get
            {
                return m_lengthUnits;
            }
            set
            {
                m_lengthUnits = value;
            }
        }

        #endregion

        #region [ Methods ]

        public void WriteResults(int eventID)
        {
            FaultRecordInfo faultRecordInfo = new FaultRecordInfo();

            using (SystemInfoDataContext systemInfo = new SystemInfoDataContext(m_connectionString))
            using (MeterInfoDataContext meterInfo = new MeterInfoDataContext(m_connectionString))
            using (EventTableAdapter eventAdapter = new EventTableAdapter())
            using (CycleDataTableAdapter cycleDataAdapter = new CycleDataTableAdapter())
            using (FaultCurveTableAdapter faultCurveAdapter = new FaultCurveTableAdapter())
            using (FaultSummaryTableAdapter faultSummaryAdapter = new FaultSummaryTableAdapter())
            {
                eventAdapter.Connection.ConnectionString = m_connectionString;
                cycleDataAdapter.Connection.ConnectionString = m_connectionString;
                faultCurveAdapter.Connection.ConnectionString = m_connectionString;
                faultSummaryAdapter.Connection.ConnectionString = m_connectionString;

                faultRecordInfo.Event = eventAdapter.GetDataByID(eventID).First();

                faultRecordInfo.Meter = meterInfo.Meters.Single(m => faultRecordInfo.Event.MeterID == m.ID);
                faultRecordInfo.Line = meterInfo.Lines.Single(l => faultRecordInfo.Event.LineID == l.ID);

                faultRecordInfo.FaultCurves = faultCurveAdapter.GetDataBy(eventID).ToList();
                faultRecordInfo.FaultSummary = faultSummaryAdapter.GetDataBy(eventID).FirstOrDefault();

                faultRecordInfo.Recipients = systemInfo.Recipients.ToList();

                WriteResults(faultRecordInfo);
            }
        }

        private void WriteResults(FaultRecordInfo faultRecordInfo)
        {
            string template;
            string subject;
            string body;
            string table;

            List<DataSeries> faultCurves;

            template = null;

            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("FaultData.DataWriters.FaultResultsTemplate.html"))
            {<commit_msg>Fixed error that occurs when SMTP host is not defined and the list of recipients is empty.<commit_after>﻿//******************************************************************************************************
//  HTMLWriter.cs - Gbtc
//
//  Copyright © 2014, Grid Protection Alliance.  All Rights Reserved.
//
//  Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
//  the NOTICE file distributed with this work for additional information regarding copyright ownership.
//  The GPA licenses this file to you under the Eclipse Public License -v 1.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.opensource.org/licenses/eclipse-1.0.php
//
//  Unless agreed to in writing, the subject software distributed under the License is distributed on an
//  "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
//  License for the specific language governing permissions and limitations.
//
//  Code Modification History:
//  ----------------------------------------------------------------------------------------------------
//  11/14/2014 - Stephen <NAME>
//       Generated original version of source code.
//
//******************************************************************************************************

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using System.Windows.Forms.DataVisualization.Charting;
using FaultData.DataAnalysis;
using FaultData.Database;
using FaultData.Database.FaultLocationDataTableAdapters;
using FaultData.Database.MeterDataTableAdapters;
using DataPoint = FaultData.DataAnalysis.DataPoint;
using Series = System.Windows.Forms.DataVisualization.Charting.Series;

namespace FaultData.DataWriters
{
    /// <summary>
    /// Writes the results of fault analysis to an email.
    /// </summary>
    public class EmailWriter
    {
        #region [ Members ]

        // Nested Types
        private class FaultRecordInfo
        {
            public Meter Meter;
            public Line Line;

            public MeterData.EventRow Event;
            public List<FaultLocationData.FaultCurveRow> FaultCurves;
            public FaultLocationData.FaultSummaryRow FaultSummary;

            public List<Recipient> Recipients;
        }

        // Fields
        private string m_connectionString;

        private string m_smtpServer;
        private string m_fromAddress;
        private double m_maxFaultDistanceMultiplier;
        private double m_minFaultDistanceMultiplier;
        private string m_lengthUnits;

        #endregion

        #region [ Constructors ]

        /// <summary>
        /// Creates a new instance of the <see cref="XMLWriter"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string used to connect to the database containing fault location data.</param>
        public EmailWriter(string connectionString)
        {
            m_connectionString = connectionString;
        }

        #endregion

        #region [ Properties ]

        public string SMTPServer
        {
            get
            {
                return m_smtpServer;
            }
            set
            {
                m_smtpServer = value;
            }
        }

        public string FromAddress
        {
            get
            {
                return m_fromAddress;
            }
            set
            {
                m_fromAddress = value;
            }
        }

        public double MaxFaultDistanceMultiplier
        {
            get
            {
                return m_maxFaultDistanceMultiplier;
            }
            set
            {
                m_maxFaultDistanceMultiplier = value;
            }
        }

        public double MinFaultDistanceMultiplier
        {
            get
            {
                return m_minFaultDistanceMultiplier;
            }
            set
            {
                m_minFaultDistanceMultiplier = value;
            }
        }

        public string LengthUnits
        {
            get
            {
                return m_lengthUnits;
            }
            set
            {
                m_lengthUnits = value;
            }
        }

        #endregion

        #region [ Methods ]

        public void WriteResults(int eventID)
        {
            FaultRecordInfo faultRecordInfo = new FaultRecordInfo();

            using (SystemInfoDataContext systemInfo = new SystemInfoDataContext(m_connectionString))
            using (MeterInfoDataContext meterInfo = new MeterInfoDataContext(m_connectionString))
            using (EventTableAdapter eventAdapter = new EventTableAdapter())
            using (CycleDataTableAdapter cycleDataAdapter = new CycleDataTableAdapter())
            using (FaultCurveTableAdapter faultCurveAdapter = new FaultCurveTableAdapter())
            using (FaultSummaryTableAdapter faultSummaryAdapter = new FaultSummaryTableAdapter())
            {
                faultRecordInfo.Recipients = systemInfo.Recipients.ToList();

                if (faultRecordInfo.Recipients.Count > 0)
                {
                    eventAdapter.Connection.ConnectionString = m_connectionString;
                    cycleDataAdapter.Connection.ConnectionString = m_connectionString;
                    faultCurveAdapter.Connection.ConnectionString = m_connectionString;
                    faultSummaryAdapter.Connection.ConnectionString = m_connectionString;

                    faultRecordInfo.Event = eventAdapter.GetDataByID(eventID).First();

                    faultRecordInfo.Meter = meterInfo.Meters.Single(m => faultRecordInfo.Event.MeterID == m.ID);
                    faultRecordInfo.Line = meterInfo.Lines.Single(l => faultRecordInfo.Event.LineID == l.ID);

                    faultRecordInfo.FaultCurves = faultCurveAdapter.GetDataBy(eventID).ToList();
                    faultRecordInfo.FaultSummary = faultSummaryAdapter.GetDataBy(eventID).FirstOrDefault();

                    WriteResults(faultRecordInfo);
                }
            }
        }

        private void WriteResults(FaultRecordInfo faultRecordInfo)
        {
            string template;
            string subject;
            string body;
            string table;

            List<DataSeries> faultCurves;

            template = null;

            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("FaultData.DataWriters.FaultResultsTemplate.html"))
            {
{'fraction_non_alphanumeric': 0.11078497779784752, 'fraction_numerical': 0.002483630616391962, 'mean_word_length': 1.9535452322738387, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 30, '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': '1884203', 'n_tokens_mistral': 3173, 'n_tokens_neox': 3063, 'n_words': 802}