public NotificationMessage GetInitialMediaRequestMessage(MSI_MediaRequestResponse submittedRequest)
        {
            string thisMethod = string.Format("{0}.{1}", thisClass, System.Reflection.MethodBase.GetCurrentMethod().Name);

            NotificationMessage message = null;
            StringBuilder bodyBuilder;
            try
            {
                message = new NotificationMessage();
                message.Subject = "New media request-" + DateTime.Now.ToShortDateString();

                bodyBuilder = new StringBuilder();
                bodyBuilder.Append("<table>");
                bodyBuilder.Append("<tr><td>Portfolio Number</td><td>:</td><td>" + submittedRequest.Portfolio + "</td></tr>");
                bodyBuilder.Append("<tr><td>Account Holder's Name</td><td>:</td><td>" + submittedRequest.NAME + "</td></tr>");
                bodyBuilder.Append("<tr><td>Requested Date</td><td>:</td><td>" + submittedRequest.RequestedDate + "</td></tr>");

                string mediaTypesRequested = "";
                IEnumerable<MSI_MediaTypes> availableMediaTypes = Query.GetMediaTypes();

                foreach (MSI_MediaRequestTypes mediaReqType in submittedRequest.MSI_MediaRequestTypes)
                {
                    mediaTypesRequested = mediaTypesRequested + availableMediaTypes.Where(record => record.ID == mediaReqType.TypeId).SingleOrDefault().Name + ", ";
                }
                bodyBuilder.Append("<tr><td>Requested Media Types</td><td>:</td><td>" + mediaTypesRequested.Substring(0,mediaTypesRequested.Length-2) + "</td></tr>");

                message.BodyText = bodyBuilder.ToString();
            }
            catch (Exception ex)
            {
                ErrorLogHelper.Error("Error while constructing InitialMediaRequestMessage", ex);
            }
            return message;
        }
        public MSI_MediaRequestResponse AddMediaRequestResponse(MSI_MediaRequestResponse submittedRequest)
        {
            string thisMethod = string.Format("{0}.{1}", thisClass, System.Reflection.MethodBase.GetCurrentMethod().Name);
            string logMessage = string.Format("{0}|Method incoming parameters AccountNumber={1}", thisMethod, submittedRequest.ACCOUNT);
            LogHelper.Info(logMessage);

            MSI_MediaRequestResponseRepository repository = new MSI_MediaRequestResponseRepository();
            try
            {
                repository.Add(submittedRequest);
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException validationException)
            {
                foreach (System.Data.Entity.Validation.DbEntityValidationResult errorResult in validationException.EntityValidationErrors)
                {
                    foreach (System.Data.Entity.Validation.DbValidationError error in errorResult.ValidationErrors)
                    {
                        ErrorLogHelper.Error(error.ErrorMessage, validationException);
                    }
                }
            }

            catch (Exception ex)
            {
                ErrorLogHelper.Error(logMessage, ex);
            }
            return submittedRequest;
        }
        public MSI_MediaRequestResponse Post(MSI_MediaRequestResponse submittedRequest)
        {
            string thisMethod = string.Format("{0}.{1}", thisClass, System.Reflection.MethodBase.GetCurrentMethod().Name);
            string logMessage = string.Format("{0}|Method incoming parameters RequestId={1}", thisMethod, submittedRequest.Id);
            LogHelper.Info(logMessage);

            DataQueries query = new DataQueries();
            bool isMediaRequestTypeUpdateMode = false;
            CascadeBusiness.MediaRequest business = new CascadeBusiness.MediaRequest();
            string userRole="";
            List<string> emailRecipients = new List<string>();
            List<string> userIds = new List<string>();
            try
            {
                if (string.IsNullOrEmpty(submittedRequest.Id))
                {
                    submittedRequest.Id = Guid.NewGuid().ToString();

                    foreach (MSI_MediaRequestTypes mediaReqType in submittedRequest.MSI_MediaRequestTypes)
                    {
                        if (mediaReqType.RequestStatusId.HasValue)
                        {
                            if (mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.Fulfilled)
                            {
                                isMediaRequestTypeUpdateMode = true;
                                business.PerfomPostFulfillmentProcess(mediaReqType.Id, mediaReqType.RespondedUserID.Value);
                            }
                            else if (mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.RequestComplete)
                            {
                                isMediaRequestTypeUpdateMode = true;
                                business.PerfomCompleteRequest(mediaReqType.Id, mediaReqType.RespondedUserID.Value);
                            }
                            else if (mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.OriginatorUpdateRequested ||
                                mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.OriginatorInProcess ||
                                mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.NoOriginatorMediaAvailable ||
                                mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.OriginatorFulfilled ||
                                mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.OriginatorResearching ||
                                mediaReqType.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.SentToOriginator
                                )
                            {
                                isMediaRequestTypeUpdateMode = true;
                                business.PerfomUpdateRequest(mediaReqType.Id, mediaReqType.RespondedUserID.Value, mediaReqType.RequestStatusId.Value);
                            }
                        }
                        else
                        {
                            // When Initial Media request through Agency to Media Admin or MediaAdmin to originator
                            userRole = Roles.GetRolesForUser(Membership.GetUser(submittedRequest.RequestedByUserId).UserName).Single();
                            mediaReqType.Id = Guid.NewGuid().ToString();
                            mediaReqType.RequestedId = submittedRequest.Id;
                            mediaReqType.RequestStatusId = business.EvaluateMediaRequestStatus(CascadeBusiness.MediaRequestLevel.Initiate, (CascadeBusiness.UserRoles)Enum.Parse(typeof(CascadeBusiness.UserRoles), userRole, true));
                            mediaReqType.LastUpdatedDate = DateTime.Now;
                            mediaReqType.LastUpdatedBy = submittedRequest.RequestedByUserId;
                        }
                    }
                    submittedRequest.RequestedDate = DateHelper.GetDateWithTimings(submittedRequest.RequestedDate);
                    if (!isMediaRequestTypeUpdateMode)
                    {
                        business.SaveMediaRequestResponse(submittedRequest);

                        business.PerfomPreFulfillmentProcess(submittedRequest.ACCOUNT, null);

                        #region SentToOwner OR SentToOriginator Notification

                        if (userRole == CascadeBusiness.UserRoles.mediaAdmin.ToString())
                        {
                            emailRecipients.Add(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"]);
                            userIds.Add(Membership.GetUser(Membership.GetUserNameByEmail(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"])).ProviderUserKey.ToString());
                        }
                        else if (userRole == CascadeBusiness.UserRoles.agency.ToString())
                        {
                            emailRecipients = MemberShipHelper.GetEmailAddressUserInRole(CascadeBusiness.UserRoles.mediaAdmin.ToString());
                            userIds = MemberShipHelper.GetUserIdsUserInRole(CascadeBusiness.UserRoles.mediaAdmin.ToString());
                        }
                        if (emailRecipients.Count > 0)
                        {
                            CascadeBusiness.NotificationMessage message = business.GetInitialMediaRequestMessage(submittedRequest);
                            Emailer.SendMessage(Emailer.CreateMessage(message.Subject, emailRecipients, message.BodyText, ""));
                            business.SaveMessageNotification(message, userIds);
                        }

                        #endregion
                    }
                    else
                    {
                        #region OriginatorUpdateRequested Notifications
                        MSI_MediaRequestTypes requestType = submittedRequest.MSI_MediaRequestTypes.Where(record => record.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.OriginatorUpdateRequested).FirstOrDefault();
                        if (requestType != null)
                        {
                            userRole = Roles.GetRolesForUser(Membership.GetUser(requestType.RespondedUserID.Value).UserName).Single();

                            if (userRole == CascadeBusiness.UserRoles.mediaAdmin.ToString())
                            {
                                emailRecipients.Add(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"]);
                                userIds.Add(Membership.GetUser(Membership.GetUserNameByEmail(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"])).ProviderUserKey.ToString());
                            }
                            else if (userRole == CascadeBusiness.UserRoles.agency.ToString())
                            {
                                emailRecipients = MemberShipHelper.GetEmailAddressUserInRole(CascadeBusiness.UserRoles.mediaAdmin.ToString());
                                userIds = MemberShipHelper.GetUserIdsUserInRole(CascadeBusiness.UserRoles.mediaAdmin.ToString());
                            }
                            if (emailRecipients.Count > 0)
                            {
                                CascadeBusiness.NotificationMessage message = business.GetUpdateRequestedToOriginatorMessage(submittedRequest.MSI_MediaRequestTypes.Where(record => record.RequestStatusId== (int)CascadeBusiness.MediaRequestStatus.OriginatorUpdateRequested));
                                Emailer.SendMessage(Emailer.CreateMessage(message.Subject, emailRecipients, message.BodyText, ""));
                                business.SaveMessageNotification(message, userIds);
                            }

                        }
                        #endregion

                        #region Fulfilled Notifications
                        requestType = null;
                        requestType = submittedRequest.MSI_MediaRequestTypes.Where(record => record.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.Fulfilled).FirstOrDefault();
                        if (requestType != null)
                        {
                            string userEmail = MemberShipHelper.GetEmailAddress(requestType.RequestedUserID.Value);
                            if (!string.IsNullOrEmpty(userEmail))
                            {
                                emailRecipients.Add(userEmail);
                                userIds.Add(requestType.RequestedUserID.ToString());
                            }
                            if (emailRecipients.Count > 0)
                            {
                                CascadeBusiness.NotificationMessage message = business.GetRequestFulfillmentMessage(submittedRequest.MSI_MediaRequestTypes.Where(record => record.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.Fulfilled));
                                Emailer.SendMessage(Emailer.CreateMessage(message.Subject, emailRecipients, message.BodyText, ""));
                                business.SaveMessageNotification(message, userIds);
                            }

                        }

                        #endregion

                        #region Sent To Originator Notification
                        requestType = null;
                        requestType = submittedRequest.MSI_MediaRequestTypes.Where(record => record.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.SentToOriginator).FirstOrDefault();
                        if (requestType != null)
                        {
                            emailRecipients.Add(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"]);
                            userIds.Add(Membership.GetUser(Membership.GetUserNameByEmail(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"])).ProviderUserKey.ToString());

                            if (emailRecipients.Count > 0)
                            {
                                CascadeBusiness.NotificationMessage message = business.GetInitialMediaRequestMessage(submittedRequest.MSI_MediaRequestTypes.Where(record => record.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.SentToOriginator));
                                Emailer.SendMessage(Emailer.CreateMessage(message.Subject, emailRecipients, message.BodyText, ""));
                                business.SaveMessageNotification(message, userIds);
                            }

                        }

                        #endregion
                    }

                }
                else
                {
                    MSI_MediaRequestTypes mediaType = null;
                    foreach (MSI_MediaRequestTypes mediaReqType in submittedRequest.MSI_MediaRequestTypes)
                    {
                        mediaType = query.GetMediaRequestdType(submittedRequest.Id, mediaReqType.TypeId);
                        if (mediaType == null)
                        {
                            userRole = Roles.GetRolesForUser(Membership.GetUser(submittedRequest.RequestedByUserId).UserName).Single();
                            mediaReqType.Id = Guid.NewGuid().ToString();
                            mediaReqType.RequestedId = submittedRequest.Id;
                            mediaReqType.RequestStatusId = business.EvaluateMediaRequestStatus(CascadeBusiness.MediaRequestLevel.Initiate, (CascadeBusiness.UserRoles)Enum.Parse(typeof(CascadeBusiness.UserRoles), userRole, true));
                            mediaReqType.LastUpdatedDate = DateTime.Now;
                            mediaReqType.LastUpdatedBy = submittedRequest.RequestedByUserId;

                            business.SaveMediaRequestdType(mediaReqType);

                            business.PerfomPreFulfillmentProcess(submittedRequest.ACCOUNT, null);
                        }
                        else
                        {
                        }
                        mediaType = null;

                    }

                    mediaType = submittedRequest.MSI_MediaRequestTypes.Where(record => record.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.SentToOriginator || record.RequestStatusId == (int)CascadeBusiness.MediaRequestStatus.SentToOwner).FirstOrDefault();
                    if (mediaType != null)
                    {
                        #region SentToOwner OR SentToOriginator Notification

                        if (userRole == CascadeBusiness.UserRoles.mediaAdmin.ToString())
                        {
                            emailRecipients.Add(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"]);
                            userIds.Add(Membership.GetUser(Membership.GetUserNameByEmail(System.Configuration.ConfigurationManager.AppSettings["emailOriginatorAccount"])).ProviderUserKey.ToString());
                        }
                        else if (userRole == CascadeBusiness.UserRoles.agency.ToString())
                        {
                            emailRecipients = MemberShipHelper.GetEmailAddressUserInRole(CascadeBusiness.UserRoles.mediaAdmin.ToString());
                            userIds = MemberShipHelper.GetUserIdsUserInRole(CascadeBusiness.UserRoles.mediaAdmin.ToString());
                        }
                        if (emailRecipients.Count > 0)
                        {
                            CascadeBusiness.NotificationMessage message = business.GetInitialMediaRequestMessage(submittedRequest);
                            Emailer.SendMessage(Emailer.CreateMessage(message.Subject, emailRecipients, message.BodyText, ""));
                            business.SaveMessageNotification(message, userIds);
                        }

                        #endregion
                    }

                }
            }

            catch (Exception ex)
            {
                ErrorLogHelper.Error(logMessage, ex);
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(string.Format("Error occur in POST MediaRequest : {0}", ex.Message))
                });

            }

            return submittedRequest;
        }
        public void SaveMediaRequestResponse(MSI_MediaRequestResponse submittedRequest)
        {
            string thisMethod = string.Format("{0}.{1}", thisClass, System.Reflection.MethodBase.GetCurrentMethod().Name);
            string logMessage = string.Format("{0}|Method incoming parameters RequestId={1}", thisMethod, submittedRequest.Id);
            LogHelper.Info(logMessage);

            try
            {
                Query.AddMediaRequestResponse(submittedRequest);
            }
            catch (Exception ex)
            {
                ErrorLogHelper.Error(logMessage, ex);
            }
        }
 public MediaRequestTypes(MSI_MediaRequestTypes mediaRequestTypes, MSI_MediaRequestResponse requestResponse)
 {
     MSIMediaRequestTypes = mediaRequestTypes;
     MSIMediaRequestResponse = requestResponse;
 }