private NotificationEmail MapNotificationEmail(NotificationEntity objectToMap)
        {
            var notificationEmail = new NotificationEmail(objectToMap.NotificationId);

            _notificationPopulator.Populate(objectToMap, notificationEmail);

            NotificationEmailEntity notificationEmailEntity = objectToMap.NotificationEmail;

            notificationEmail.Body     = notificationEmailEntity.Body;
            notificationEmail.FromName = notificationEmailEntity.FromName;
            try
            {
                notificationEmail.FromEmail = new Email(notificationEmailEntity.FromEmail);
            }
            catch
            {
                // no from email?
            }
            try
            {
                notificationEmail.ToEmail = new Email(notificationEmailEntity.ToEmail);
            }
            catch
            {
                // no to email?
            }
            notificationEmail.Subject = notificationEmailEntity.Subject;

            return(notificationEmail);
        }
        public Notification Save(Notification domainObject)
        {
            using (var adapter = _persistenceLayer.GetDataAccessAdapter())
            {
                if (domainObject.UserId > 0)
                {
                    try
                    {
                        var customer = _customerRepository.GetCustomerByUserId(domainObject.UserId);
                        if (customer != null && ((customer.DoNotContactTypeId.HasValue && (customer.DoNotContactTypeId.Value == (long)DoNotContactType.DoNotContact || customer.DoNotContactTypeId.Value == (long)DoNotContactType.DoNotMail)) || !customer.EnableEmail))
                        //(customer.DoNotContactReasonId.HasValue && customer.DoNotContactReasonId.Value > 0))
                        {
                            return(null);
                        }
                    }
                    catch (ObjectNotFoundInPersistenceException <Customer> )
                    {
                        //Do Nothing
                    }
                }

                NotificationEntity notificationEntity = _notificationMapper.Map(domainObject);
                if (!adapter.SaveEntity(notificationEntity, true, false))
                {
                    throw new PersistenceFailureException("Could not save Notification.");
                }

                if (domainObject as NotificationEmail != null)
                {
                    NotificationEmailEntity notificationEmailEntity = _notificationEmailMapper.Map(domainObject as NotificationEmail);
                    notificationEmailEntity.IsNew = domainObject.Id == 0;
                    notificationEmailEntity.NotificationEmailId = notificationEntity.NotificationId;
                    if (!adapter.SaveEntity(notificationEmailEntity, true))
                    {
                        throw new PersistenceFailureException("Could not save NotificationEmail.");
                    }

                    notificationEntity.NotificationEmail = notificationEmailEntity;
                }

                if (domainObject as NotificationPhone == null)
                {
                    return(_notificationMapper.Map(notificationEntity));
                }

                var notificationPhone = _notificationPhoneMapper.Map(domainObject as NotificationPhone);
                notificationPhone.IsNew = domainObject.Id == 0;
                notificationPhone.NotificationPhoneId = notificationEntity.NotificationId;
                if (!adapter.SaveEntity(notificationPhone, true))
                {
                    throw new PersistenceFailureException("Could not save NotificationEmail.");
                }

                notificationEntity.NotificationPhone = notificationPhone;

                return(_notificationMapper.Map(notificationEntity));
            }
        }