Thursday, October 30, 2014

Template



Email Template
static void Main(string[] args)
        {
           
            try
            {
                new batchjobs.jobalertsnew().Run();
                if (args[0] == "Odesk")
                {
                    ConfigureLogFile("Odesk");
                    new batchjobs. jobalertsnew ().Run();
                }
             
            }
            catch (Exception ex)
            {

            }
           
        }
Program
public void Run()
        {
            using (var context = new HuntableDevEntities())
            {
                LoggingManager.Debug("Entering into Jobs Remember");
                var emailtemplate = EmailTemplateManager.GetTemplate(EmailTemplates.JobRememberEmail);
                var date = DateTime.Now.AddDays(-14);
                //string date = DateTime.Now.Date.AddDays(-14).ToString("dd/MM/yyyy");

                var newdates = DateTime.Now.AddDays(-14).ToString("yyyy-MM-dd");
                DateTime d = DateTime.ParseExact(newdates, "yyyy-MM-dd", CultureInfo.InvariantCulture);

                string datee = DateTime.Now.Date.AddDays(-14).ToString();
                DateTime dates = Convert.ToDateTime(datee);
               

                var input = DateTime.Now.AddDays(-14).ToString("yyyy-MM-dd");
                var dt = DateTime.Parse(input);
                var output = dt.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture);
                ///DateTime datess = DateTime.ParseExact(output, @"yyyy-MM-dd", CultureInfo.InvariantCulture);
                ///DateTime dtt = Convert.ToDateTime(output);
                ///string output = "2014-10-16 00:00:00.000";
                ///DateTime date =Convert.ToDateTime(DateTime.Now.Date.AddDays(-14).ToString("dd/MM/yyyy"), new CultureInfo("en-Us"));
               /// DateTime date = DateTime.ParseExact(datee, "dd-MM-yyyy", CultureInfo.InvariantCulture);

                ///var jobs = context.Jobs.Where(x =>x.CreatedDateTime == date).ToList();

                ///var jobs = context.Jobs.Where(x => EntityFunctions.TruncateTime(x.CreatedDateTime) == date && (x.IsDeleted == false || x.IsDeleted == null) && (x.IsRssJob == false || x.IsRssJob == null)).ToList();
                ///var jobs = context.Jobs.Take(10);
                ///var jobs = context.Jobs.Where(s => s.UserId == 3253);
                ///var jobss = context.Users.Where(x =>x.CreatedDateTime == date).ToList();


                var jobs = context.JobAlertsBasedOnDate();
                   
                foreach (var job in jobs)
                {
                    try
                    {
                        var userdetails = context.Users.FirstOrDefault(x => x.Id == job.UserId);
                        var jobapplicationcount = context.JobApplications.Count(x => x.JobId == job.Id);
                        var jobUrl = new UrlGenerator().JobsUrlGenerator(job.Id);
                        var newdate = DateTime.Now.AddDays(+16).ToString("dd/MM/yyyy", new CultureInfo("en-US"));
                        //string nnn = newdate.ToString("dd/MM/yyyy hh:mm t\\M");

                        var valuesList = new Hashtable
                                               {
                                                  { "Name" , userdetails.FirstName+""+userdetails.LastName},
                                                  {"JobTitle" ,job.Title},
                                                  {"TotalVIews" , job.TotalViews},
                                                  {"TotalApplications" , jobapplicationcount},
                                                   {"JobDescription" ,job.JobDescription},
                                                    {"Salary" , job.Salary},
                                                    {"NewDate",newdate},
                                                    {"JobUrl","https:/url.com/"+jobUrl}
                                               };
                        string baseUrl = "https://url.com/";
                        valuesList.Add("Server Url", "https://url.com/");

                        string userProfilePicturePath = Path.Combine(baseUrl, userdetails.UserProfilePictureDisplayUrl.Replace("~/", string.Empty));
                        valuesList.Add("ProfilePic", userProfilePicturePath);


                        var body = SnovaUtil.LoadTemplate(emailtemplate.TemplateText, valuesList);
                        BatchJobshuntable.SnovaUtil.SendEmail(emailtemplate.Subject, body, new List<string> { userdetails.EmailAddress }.ToArray(), null, null);
                    }
                    catch (Exception exception)
                    {
                        LoggingManager.Debug("Exception caught in" + exception);
                        throw;
                    }
                }
            }
              
        }
url generator
public string JobsUrlGenerator(int id)
        {
            using (var context = new HuntableDevEntities())
            {
                var job = context.Jobs.FirstOrDefault(x => x.Id == id);
                var companyname = (job.IsRssJob != true) ? job.CompanyName : string.Empty;
                var location = job.JobLocationUrl;
                int salary = (job.Salary != 0) ? job.Salary : 0;
                string jobtitle = job.Title.Replace(" ", "-");
                string companynm = companyname.Replace(" ", "-");


                if (companyname == null && location != null)
                {
                    string joburl = "jobs/" + Regex.Replace(jobtitle, "[^a-zA-Z0-9% ._]", "-") + "-" + Regex.Replace(location, "[^a-zA-Z0-9% ._]", "-") + "-" +
                                    salary + "-" + id;
                    return joburl;
                }
                if (location == null && companyname != null)
                {
                    string joburl = "jobs/" + Regex.Replace(jobtitle, "[^a-zA-Z0-9% ._]", "-") + "-" + Regex.Replace(companynm, "[^a-zA-Z0-9% ._]", "-") + "-" +
                                    salary + "-" + id;
                    return joburl;
                }
                if (location == null && companyname == null)
                {
                    string joburl = "jobs/" + Regex.Replace(jobtitle, "[^a-zA-Z0-9% ._]", "-") + "-" +
                                    salary + "-" + id;
                    return joburl;
                }
                string joburl1 = "jobs/" + Regex.Replace(jobtitle, "[^a-zA-Z0-9% ._]", "-") + "-" + Regex.Replace(companynm, "[^a-zA-Z0-9% ._]", "-") + "-" + Regex.Replace(location, "[^a-zA-Z0-9% ._]", "-") + "-" +
                                    salary + "-" + id;
                return joburl1;
            }


        }
User profile picture
public string UserProfilePictureDisplayUrl
        {
            get
            {
                return new FileStoreService().GetDownloadUrl(PersonalLogoFileStoreId);
            }
        }

Download url
public string GetDownloadUrl(int? imageId)
        {
            if (imageId.HasValue && imageId > 0)
            {
                return "~/LoadFile.ashx?id=" + imageId;
            }

            return "~/images/nomore.jpg";
        }
Load template
public static string
            LoadTemplate(string body, Hashtable data)
        {
            LoggingManager.Debug("Request for loading template: " + body);
            var loadedTemplate = data.Cast<DictionaryEntry>().Aggregate(body, (current, entry) => current.Replace("[[DynamicValue=#" + entry.Key + "#]]", Convert.ToString(entry.Value)));
            return HttpUtility.HtmlDecode(loadedTemplate);
        }
Send email
public static void SendEmail(string subject, string body, string[] toEmails, string attachment = null, string cc = null, string replyto = null)
        {
            try
            {
                var msg = new MailMessage();

                string fromUserName = System.Configuration.ConfigurationManager.AppSettings["FromUserName"];
                string userName = System.Configuration.ConfigurationManager.AppSettings["FromEmail"];
                string password = System.Configuration.ConfigurationManager.AppSettings["FromEmailPassword"];
                string smtpAddress = System.Configuration.ConfigurationManager.AppSettings["SMTPAddress"];
                int smtpPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SMTPPort"]);
                bool enableSsl = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["EnableSsl"]);
                msg.From = new MailAddress(userName, fromUserName);

                msg.Subject = subject;

                msg.Body = body;
                msg.IsBodyHtml = true;
                if (replyto != null)
                {
                    msg.ReplyTo = new MailAddress(replyto, replyto);
                }

                foreach (var toEmail in toEmails) msg.To.Add(toEmail);

                if (cc != null) msg.CC.Add(cc);

                if (attachment != null) msg.Attachments.Add(new Attachment(attachment, "application/pdf"));

                var smtp = new SmtpClient(smtpAddress, smtpPort) { EnableSsl = enableSsl, Credentials = new NetworkCredential(userName, password) };
                smtp.Send(msg);

                LoggingManager.Debug("Email Sending Completed");
            }
            catch (Exception exception)
            {
                LoggingManager.Error(exception);
            }
        }