Email After Inserting or Updating

wirble edited this page Nov 5, 2016 · 2 revisions
Clone this wiki locally

This is just a direct copy of Ramveer Singh awesome Audit Log with thanks to Volkan for the great template.

            using Serenity.ComponentModel;
            using Serenity.Data;
            using Serenity.Data.Mapping;
            using Serenity.Services;
            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Linq;
            using System.Reflection;
            using System.Web;
            using Serenity;


            namespace yourproject.yourdb
            {
                /// <summary>
                /// This interface is used to log the changes for Insert / Update and Delete.
                /// This identify the Identity Column as Row Id (Unique Id) and save in Audit Table. If Identity column is not found then it use Id (Hard Coded) column.
                /// </summary>
                public interface ICustomEmail
                {
                }

                /// <summary>
                /// This is used if want to store specific IdFields instead of default Identity field (or identity field is not avail able  ).
                /// </summary>
                public interface IExCustomEmail
                {
                    /// <summary>
                    /// Assign the field which need to save as reference id in Audit Log Table
                    /// </summary>
                    Int32Field IdField { get; }
                }

                public class EmailRowBehavior : IImplicitBehavior, ISaveBehavior
                {
                    const string FieldSeperator = "; ";
                    string IdFieldName = null;
                    public bool ActivateFor(Row row)
                    {
                        var emailproc = row as ICustomEmail;
                        if (emailproc == null)
                        {
                            var exemailproc = row as IExCustomEmail;
                            if (exemailproc == null)
                                return false;
                            else
                                IdFieldName = exemailproc.IdField.PropertyName;
                        }
                        return true;
                    }
                    public void OnAfterSave(ISaveRequestHandler handler) {  
                    }
                    public void OnAudit(ISaveRequestHandler handler){}
                    public void OnBeforeSave(ISaveRequestHandler handler) { }
                    public void OnPrepareQuery(ISaveRequestHandler handler, SqlQuery query) { }
                    public void OnReturn(ISaveRequestHandler handler) {

                        if (handler.IsCreate)
                        {

                            IUnitOfWork uow = handler.UnitOfWork;
                                                            uow.OnCommit += () => {
                                                               // send e-mail for this task now, this method will only
                                                               // be called if transaction commits successfully
                                                               NewRowEmailProcess(handler.Row);
                                                            };                              
                        }

                        if (handler.IsUpdate)
                        {
                            IUnitOfWork uow = handler.UnitOfWork;
                                                            uow.OnCommit += () => {
                                                               // send e-mail for this task now, this method will only
                                                               // be called if transaction commits successfully
                                                               UpdatedRowEmailProcess(handler.Row, handler.Old);
                                                            };

                        }

                    }
                    public void OnSetInternalFields(ISaveRequestHandler handler) { }
                    public void OnValidateRequest(ISaveRequestHandler handler) { }

                    private void NewRowEmailProcess(Row CurrentRow)
                    {
                        EmailMessage.SendMsg(); //implement your own

                    }
                    private void UpdatedRowEmailProcess(Row CurrentRow,Row OldRow)
                    {
                        EmailMessage.SendMsg(); //implement your own


                    }
                    string GetPageUrl()
                    {
                        string pageUrl = "";
                        if (HttpContext.Current != null && HttpContext.Current.Request != null)
                        {
                            var httpRequest = HttpContext.Current.Request;
                            if (httpRequest.UrlReferrer != null)
                                pageUrl = httpRequest.UrlReferrer.PathAndQuery;
                            else if (httpRequest.Url != null)
                                pageUrl = httpRequest.Url.PathAndQuery;
                        }
                        return pageUrl;
                    }
                }



            }
  1. create a cs file by copying the above code in your common folder or just your specific module
  2. Rename the namespace
  3. Inherit your RowClass with interface ICustomEmail. This will automatically pic the identity column to have insert in custom email.
  4. Implement your own logic on how you want to email that particular data in the row in functions, NewRowEmailProcess or UpdatedRowEmailProcess