File: D:/HostingSpaces/TDijk1/erp-apps.eu/wwwroot/App_Code/ERPApps/Tasks/PublishingTask.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.Scheduler;
using CMS.EventLog;
using CMS.DataEngine;
using System.Data;
using CMS.GlobalHelper;
using CMS.SettingsProvider;
using CMS.EmailEngine;
using CMS.CMSHelper;
using CMS.SiteProvider;
using CMS.TreeEngine;
using CMS.DocumentEngine;
using CMS.Helpers;
using CMS.MacroEngine;
using CMS;
using ERP.Application;
using CMS.Membership;
using ERP.Ad;
/// <summary>
/// Summary description for PublicationTask
/// </summary>
[assembly: RegisterCustomClass("ERP.PublishingTask", typeof(ERP.PublishingTask))]
namespace ERP
{
public class PublishingTask : ITask
{
/// <summary>
/// Executes the task.
/// </summary>
/// <param name="ti">Task info</param>
public string Execute(TaskInfo ti)
{
EventLogProvider.LogEvent(EventType.INFORMATION, "PublishingTask", "Start", null, "This task was executed from '~/App_Code/ERPApps/Tasks/PublicationTask.cs'.");
LinkedList<int> unpublishedAds = PublishAds();
LinkedList<int> unpublishedApps = PublishApps();
SendExpiringNotificationsAds();
SendExpiringNotificationsApps();
EventLogProvider.LogEvent(EventType.INFORMATION, "PublishingTask", "Finished", null, "This task was executed from '~/App_Code/ERPApps/Tasks/PublicationTask.cs'. Unpublished ads: " + string.Join(", ", unpublishedAds) + ". Unpublished apps: " + string.Join(", ", unpublishedApps));
return null;
}
#region "Publishing methods"
protected LinkedList<int> PublishApps()
{
// unpublish expired commercial profiles of applications
LinkedList<int> unpublishedIds = new LinkedList<int>();
var applicationNodes = ApplicationTreeNodeProvider.GetApplications().WhereEquals("AppShowCommercialProfile", true)
.WhereLessThan("AppShowCommercialProfile", DateTime.Today).ToArray();
foreach (var applicationNode in applicationNodes)
{
applicationNode.AppShowCommercialProfile = false;
applicationNode.AppPaymentLog = applicationNode.AppPaymentLog + string.Format("Profile unpublished on {0}<br>", DateTime.Now);
applicationNode.Update();
unpublishedIds.AddLast(applicationNode.AppID);
// send e-mail to vendor
SendUnpublishedAppEmail(applicationNode.AppID);
}
return unpublishedIds;
}
protected LinkedList<int> PublishAds()
{
// unpublish expired ads
LinkedList<int> unpublishedIds = new LinkedList<int>();
var applications = ApplicationTreeNodeProvider.GetApplications().WhereEquals("AdIsPublished", true).WhereLessThan("AdEndDate", DateTime.Today).ToArray();
var ads = AdTreeNodeProvider.GetAds().WhereEquals("AdIsPublished", true).WhereLessThan("AdEndDate", DateTime.Today).ToArray();
foreach (var ad in ads)
{
ad.AdIsPublished = false;
ad.AdPaymentLog = ad.AdPaymentLog + string.Format("Unpublished on {0}<br>", DateTime.Now);
ad.Update();
// ad unpublished
unpublishedIds.AddLast(ad.AdID);
// sent e-mail to vendor
SendUnpublishedAdEmail(ad.AdID);
}
return unpublishedIds;
}
#endregion
#region "Expiration info messages"
private void SendUnpublishedAppEmail(int appId)
{
// create email message from template
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("ERP.App.Expired", SiteContext.CurrentSiteID);
if (eti == null)
{
//elp.LogEvent(EventType.ERROR, DateTime.Now, "PublishingTask", "EMAILTEMPLATE", null, "Email template ERP.App.Expired not found!");
return;
}
// find application node
var application = ApplicationTreeNodeProvider.GetApplications().WhereEquals("AppId", appId).FirstObject;
if (application == null)
{
// application not found ?
EventLogProvider.LogEvent(EventType.ERROR, "PublishingTask", "APP NOT FOUND", null, "App ID " + appId + " not found (during sending expiration notification email)");
return;
}
// find vendors users accounts for the application
var vendorUsers = UserInfoProvider.GetUsers().WhereEquals("UserVendorID", application.AppVendorID).ToArray();
if (!vendorUsers.Any())
{
// no vendor user account found, noone to send the email to
EventLogProvider.LogEvent(EventType.WARNING, "PublishingTask", "VENDOR NOT FOUND", null, "Users accounts for vendor ID " + application.AppVendorID + " not found (during sending expiration notification email)");
return;
}
MacroResolver mcr = MacroResolver.GetInstance();
mcr.SetNamedSourceData("appname", application.GetValue("AppName").ToString());
// send email
ERPDataHelper.SendEmailToVendor(eti, mcr, new LinkedList<string>(vendorUsers.Select(x => x.Email).Distinct()));
}
private void SendUnpublishedAdEmail(int adId)
{
// create email message from template
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("ERP.Ad.Expired", SiteContext.CurrentSiteID);
if (eti == null)
{
//elp.LogEvent(EventType.ERROR, DateTime.Now, "PublishingTask", "EMAILTEMPLATE", null, "Email template ERP.Ad.Expired not found!");
return;
}
GeneralConnection con = ConnectionHelper.GetConnection();
// find ad node
var ad = AdTreeNodeProvider.GetAds().WhereEquals("AdID", adId).FirstObject;
if (ad == null)
{
// ad not found ?
EventLogProvider.LogEvent(EventType.ERROR, "PublishingTask", "AD NOT FOUND", null, "Ad ID " + adId + " not found (during sending expiration notification email)");
return;
}
// find vendors users accounts for the advertisement
var adVendors = UserInfoProvider.GetUsers().WhereEquals("UserVendorID", ad.AdVendorID).ToArray();
if (!adVendors.Any())
{
// no vendor user account found, noone to send the email to
EventLogProvider.LogEvent(EventType.WARNING, "PublishingTask", "VENDOR NOT FOUND", null, "Users accounts for vendor ID " + ad.AdVendorID + " not found (during sending expiration notification email)");
return;
}
MacroResolver mcr = MacroResolver.GetInstance();
mcr.SetNamedSourceData("adname", ad.AdName);
// send email
ERPDataHelper.SendEmailToVendor(eti, mcr, new LinkedList<string>(adVendors.Select(x => x.Email)));
}
#endregion
#region "Coming expiration notification messages"
private void SendExpiringNotificationsAds()
{
int days = SettingsKeyInfoProvider.GetIntValue(SiteContext.CurrentSiteName + ".ERPExpirationInterval");
if (days <= 0)
{
// notifications disabled
return;
}
// create email message from template
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("ERP.Ad.AboutExpire", SiteContext.CurrentSiteID);
if (eti == null)
{
// template not found
//ev.LogEvent(EventType.ERROR, DateTime.Now, "PublishingTask", "EMAILTEMPLATE", null, "Email template ERP.Ad.AboutExpire not found!");
return;
}
// select ads near expiration
var adsNearExpiration = AdTreeNodeProvider.GetAds()
.WhereTrue("AdIsPublished")
.WhereGreaterOrEquals("AdEndDate", DateTime.Today.AddDays(days))
.Where(
new WhereCondition()
.WhereNull("AdExpirationNotificationSent")
.Or()
.WhereLessOrEquals("AdExpirationNotificationSent", DateTime.Today.AddDays(-days - 4)))
.ToArray();
if (!adsNearExpiration.Any())
{
// no ads to expire
return;
}
foreach (var adNearExpiration in adsNearExpiration)
{
// find vendors users accounts for the advertisement
int vendorID = adNearExpiration.AdVendorID;
if (vendorID == 0)
{
EventLogProvider.LogEvent(EventType.ERROR, "PublishingTask", "NotifAd", null, "Vendor ID " + adNearExpiration.AdVendorID + " from Ad ID " + adNearExpiration.AdID + " can not be retrieved.");
continue;
}
var usersVendors = UserInfoProvider.GetUsers().WhereEquals("UserVendorID", vendorID).ToArray();
if (!usersVendors.Any())
{
// no vendor user account found, noone to send the email to
EventLogProvider.LogEvent(EventType.WARNING, "PublishingTask", "NotifAd", null, "Users accounts for vendor ID " + vendorID + " not found (during sending expiration notification email)");
return;
}
MacroResolver mcr = MacroResolver.GetInstance();
mcr.SetNamedSourceData("adname", adNearExpiration.AdName);
mcr.SetNamedSourceData("enddate", adNearExpiration.AdEndDate.ToShortDateString());
// mark node as notified, so no more notification emails are sent in the future
adNearExpiration.AdExpirationNotificationSent = DateTime.Now;
adNearExpiration.Update();
// send email
ERPDataHelper.SendEmailToVendor(eti, mcr, new LinkedList<string>(usersVendors.Select(x => x.Email).Distinct()));
}
}
private void SendExpiringNotificationsApps()
{
int days = SettingsKeyInfoProvider.GetIntValue(SiteContext.CurrentSiteName + ".ERPExpirationInterval");
if (days <= 0)
{
// notifications disabled
return;
}
// create email message from template
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("ERP.App.AboutExpire", SiteContext.CurrentSiteID);
if (eti == null)
{
// template not found
return;
}
// select applications near expiration
var applicationsNearExpiration = ApplicationTreeNodeProvider.GetApplications()
.WhereTrue("AppShowCommercialProfile")
.WhereGreaterOrEquals("AppCommercialProfileValidTo", DateTime.Today)
.WhereLessOrEquals("AppCommercialProfileValidTo", DateTime.Today.AddDays(days))
.Where(new WhereCondition()
.WhereNull("AppExpirationNotificationSent")
.Or()
.WhereLessOrEquals("AppExpirationNotificationSent", DateTime.Today.AddDays(-days - 4))
)
.ToArray();
if (!applicationsNearExpiration.Any())
{
// no apps to expire
return;
}
foreach (var applicationNearExpiration in applicationsNearExpiration)
{
// find vendors users accounts for the application
int vendorID = applicationNearExpiration.AppVendorID;
if (vendorID == 0)
{
EventLogProvider.LogEvent(EventType.ERROR, "PublishingTask", "NotifApp", null, "Vendor ID " + vendorID + " from Application ID " + applicationNearExpiration.AppID + " can not be retrieved.");
continue;
}
var vendorUsers = UserInfoProvider.GetUsers().WhereEquals("UserVendorID", vendorID).ToArray();
if (!vendorUsers.Any())
{
// no vendor user account found, noone to send the email to
EventLogProvider.LogEvent(EventType.WARNING, "PublishingTask", "NotifApp", null, "Users accounts for vendor ID " + vendorID + " not found (during sending expiration notification email)");
return;
}
MacroResolver mcr = MacroResolver.GetInstance();
mcr.SetNamedSourceData("appname", applicationNearExpiration.AppName);
mcr.SetNamedSourceData("enddate", applicationNearExpiration.AppCommercialProfileValidTo.ToShortDateString());
// mark node as notified, so no more notification emails are sent in the future
applicationNearExpiration.AppExpirationNotificationSent = DateTime.Now;
applicationNearExpiration.Update();
// send email
ERPDataHelper.SendEmailToVendor(eti, mcr, new LinkedList<string>(vendorUsers.Select(x => x.Email).Distinct()));
}
}
#endregion
}
}