| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- using GreenTree.Nachtragsmanagement.Core.Plugins;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using GreenTree.Nachtragsmanagement.Core.Domain.Misc;
- using GreenTree.Nachtragsmanagement.Services.User;
- using GreenTree.Nachtragsmanagement.Services.Configuration;
- using GreenTree.Nachtragsmanagement.Services.Appendix;
- using GreenTree.Nachtragsmanagement.Core.Domain.Appendix;
- using System.Net.Mail;
- using System.Net;
- namespace GreenTree.Nachtragsmanagement.Web.Scheduling
- {
- public class AppendixNotificationPlugin : INotificationPlugin
- {
- #region Services
- private readonly IUserService _userService;
- private readonly IConfigurationService _configurationService;
- private readonly IAppendixService _appendixService;
- #endregion
- #region Properties
- /// <summary>
- /// Id
- /// </summary>
- public Guid Id
- {
- get
- {
- return Guid.Parse("E99CA4A1-B3A9-4AA6-BBAD-9254CEED0A45");
- }
- }
- /// <summary>
- /// System name
- /// </summary>
- public string SystemName
- {
- get
- {
- return "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin";
- }
- }
- /// <summary>
- /// List of available notification jobs
- /// </summary>
- public List<NotificationJob> AvailableNotificationJobs
- {
- get
- {
- return new List<NotificationJob>
- {
- new NotificationJob
- (
- Guid.Parse("2F3642E0-259D-466D-8629-CB279F740313"),
- "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate",
- "Verhandlungstermine überprüfen",
- "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 8 Wochen nach Einreichung noch keinen " +
- "Verhandlungstermin gesetzt haben und ändert den entsprechenden Status ab"
- ),
- new NotificationJob
- (
- Guid.Parse("2E46B32A-1912-47F9-951E-C8188AA9BA50"),
- "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol",
- "Verhandlungsprotokolle überprüfen",
- "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 2 Wochen die zwar verhandelt sind, " +
- "jedoch noch kein Protokoll aufweisen."
- )
- };
- }
- }
- /// <summary>
- /// Displayed name
- /// </summary>
- public string Name
- {
- get
- {
- return "Nachtragsbenachrichtigung";
- }
- }
- /// <summary>
- /// Further description on how this plugin works
- /// </summary>
- public string Description
- {
- get
- {
- return
- "Erstellt automatisch Benachrichtigungen für Nachträge, die nach 8 Wochen nach Einreichung noch keinen " +
- "Verhandlungstermin gesetzt haben und ändert den entsprechenden Status ab. Außerdem werden alle 2 Wochen " +
- "Benachrichtigungen für Nachträge erstellt, die zwar verhandelt sind, jedoch noch kein Protokoll aufweisen.";
- }
- }
- #endregion
- /// <summary>
- /// Initializes a new instance of the AppendixNotificationPlugin class
- /// </summary>
- public AppendixNotificationPlugin() { }
- /// <summary>
- /// Initializes a new instance of the AppendixNotificationPlugin class
- /// </summary>
- public AppendixNotificationPlugin(
- IUserService userService,
- IConfigurationService configurationService,
- IAppendixService appendixService)
- {
- _userService = userService;
- _configurationService = configurationService;
- _appendixService = appendixService;
- }
- #region Processing
- /// <summary>
- /// Process all mail notifications registered for that plugin
- /// </summary>
- /// <param name="mailNotifications">The notifications which shall be generated.</param>
- public void ProcessNotifications(IEnumerable<MailNotification> mailNotifications)
- {
- if (mailNotifications == null || !mailNotifications.Any()) return;
- foreach (var notification in mailNotifications)
- {
- switch (notification.NotificationJobSystemName)
- {
- case "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate":
- {
- var conversionSuccess = false;
- var ageDaysConfigItem = _configurationService.GetConfigItemByName(
- "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.AgeDays");
- var ageDays = ageDaysConfigItem == null
- ? 56
- : _configurationService.TryGetConfigItemValue<int>(ageDaysConfigItem, out conversionSuccess);
- if (!conversionSuccess)
- ageDays = 56;
- var stateSetConfigItem = _configurationService.GetConfigItemByName(
- "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationDate.StateSet");
- var stateId = stateSetConfigItem == null
- ? 2
- : _configurationService.TryGetConfigItemValue<int>(stateSetConfigItem, out conversionSuccess);
- if (!conversionSuccess)
- stateId = 2;
- var appendices = _appendixService.GetAllAppendices()
- .Where(a => a.OfferingDate <= DateTime.Now.AddDays(ageDays) &&
- a.StateId != stateId &&
- a.NegotiationDate == null)
- .ToList();
- var mailBody = GenerateNegotiationDateMailBody(appendices);
- foreach (var appendix in appendices)
- {
- appendix.StateId = stateId;
- _appendixService.UpdateAppendix(appendix);
- }
- SendNotification(notification, "Autom. Übersicht nicht verhandelte Nachträge", mailBody);
- }
- break;
- case "GreenTree.Nachtragsmanagement.AppendixNotificationPlugin.ProcessNegotiationProtocol":
- break;
- default:
- continue;
- }
- }
- }
- /// <summary>
- /// Sets the corresponding status for appendices which offering date is older than 8 weeks and notifies the correspondig recipients
- /// </summary>
- private void ProcessNegotiationDateNotification()
- {
- }
- #endregion
- #region Mail body generation
- /// <summary>
- /// Generates a mail body with a list of all appendices with a offering date later than 8 weeks
- /// </summary>
- /// <param name="appendices">Appendices matching that criteria.</param>
- public string GenerateNegotiationDateMailBody(IEnumerable<Appendix> appendices)
- {
- var template =
- "<html>" +
- " <body>" +
- " <h3>Übersicht \"Nicht verhandelte Nachträge\"</h3>" +
- " <p>Folgende Nachträge haben ein Einreichdatum älter als 8 Wochen, jedoch noch kein Verhandlungstermin" +
- " gesetzt:</p>" +
- " <ul>" +
- " {0}" +
- " </ul>" +
- " </body>" +
- "</html>";
- if (!appendices.Any()) return String.Format(template, String.Empty);
- var appendicesList = String.Empty;
- foreach (var appendix in appendices)
- {
- appendicesList += String.Format(
- "<li>Nachtrag <b>\"{0}\"</b> in Baustelle <b>\"{1}\"</b> - Einreichdatum: <b>{2:dd.MM.yyyy}</b>",
- appendix.CustomNumber, appendix.Site.CustomNumber, appendix.OfferingDate);
- }
- return String.Format(template, appendicesList);
- }
- #endregion
- #region Mail sending
- /// <summary>
- /// Sends a generated mail body to the specified recipients in the mail notification
- /// </summary>
- /// <param name="mailNotification">The mail notification.</param>
- /// <param name="subject">The mail subject.</param>
- /// <param name="body">The mail body.</param>
- public void SendNotification(MailNotification mailNotification, string subject, string body)
- {
- if (mailNotification == null) return;
- var mailServerConfig = _configurationService.GetCurrentConfiguration().MailServerElement;
- var smptClient = new SmtpClient(mailServerConfig.SmtpServer, mailServerConfig.Port)
- {
- EnableSsl = mailServerConfig.UseSsl,
- Credentials = new NetworkCredential(
- mailServerConfig.Username,
- mailServerConfig.Password,
- mailServerConfig.Domain)
- };
- var recipients =
- mailNotification.Users
- .Select(u => u.MailAddress);
- var mailMessage = new MailMessage
- {
- IsBodyHtml = true,
- Subject = subject,
- Body = body,
- From = new MailAddress("Nachtragsbenachrichtigung@schweerbau.de")
- };
- foreach (var recipient in recipients)
- mailMessage.To.Add(recipient);
- smptClient.Send(mailMessage);
- }
- #endregion
- }
- }
|