protected Member CheckMemberExists(ISession session, MemberDetailModel model)
        {
            var member = Mapper.Map(model, new Member());
            var existingMember = session.CreateCriteria(typeof(Member))
                .List<Member>().FirstOrDefault(x => x.MemberKey.Equals(member.MemberKey));

            // you cannot change your name
            if (existingMember != null)
            {
                if (existingMember.LegacyId == model.LegacyId) return existingMember;

                //get last cher of firstname check if numerical if so increment it if not add 1
                var duplicateCounterCheck = model.Firstname.Substring(model.Firstname.Length - 1);
                var duplicateCounter = 0;
                int.TryParse(duplicateCounterCheck, out duplicateCounter);
                duplicateCounter = duplicateCounter + 1;
                model.Firstname = string.Format("{0}{1}",
                    model.Firstname.Substring(0, model.Firstname.Length - (duplicateCounter == 1 ? 0 : 1)),
                    duplicateCounter);
                member = CheckMemberExists(session, model);
            }

            return member;
            //return existingMember != null ? Mapper.Map(model, existingMember) : member;
        }
        public int Register(MemberDetailModel model)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        // check if member already exists
                        var member = CheckMemberExists(session, model);

                        var address = CheckAddressExists(session, model);
                        var contact = CheckContactExists(session, model);

                        if (contact != null)
                        {
                            SetAudit(contact);
                            session.SaveOrUpdate(contact);
                        }

                        if (address != null)
                        {
                            SetAudit(address);
                            session.SaveOrUpdate(address);
                        }

                        member.Contact = contact;
                        member.Address = address;

                        SetAudit(member);
                        session.SaveOrUpdate(member);

                        transaction.Commit();
                        return member.Id;
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(LogLevel.Error, ex, string.Empty, null);
                        transaction.Rollback();
                        return 0;
                    }


                    // var registrationSubject = ConfigurationManager.AppSettings.Get("RegistrationSubject");

                    // _emailServices.SendActivationEmail(model.Email, model.Password, registrationSubject);

                }
            }
        }
        protected Contact CheckContactExists(ISession session, MemberDetailModel model)
        {
            var contact = Mapper.Map(model, new Contact());

            if (string.IsNullOrEmpty(contact.ContactKey)) return null;

            var existingContact = session.CreateCriteria(typeof(Contact))
                .List<Contact>().FirstOrDefault(x => x.ContactKey.Equals(contact.ContactKey));

            return existingContact != null ? Mapper.Map(model, existingContact) : contact;
        }
        protected Address CheckAddressExists(ISession session, MemberDetailModel model)
        {
            var address = Mapper.Map(model, new Address());

            if (string.IsNullOrEmpty(address.AddressKey)) return null;

            var existingAddress = session.CreateCriteria(typeof(Address))
                .List<Address>().FirstOrDefault(x => x.AddressKey.Equals(address.AddressKey));

            return existingAddress != null ? Mapper.Map(model, existingAddress) : address;
        }
        public void SetMemberType(MemberDetailModel model, string memberTypeName)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    try
                    {
                        var memberType = session.CreateCriteria(typeof (MemberType))
                            .List<MemberType>().FirstOrDefault(x => x.Name.Equals(memberTypeName));

                        var member = CheckMemberExists(session, model);

                        member.AddMemberType(memberType);

                        SetAudit(member);
                        session.SaveOrUpdate(member);

                        transaction.Commit();
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(LogLevel.Error, ex, string.Empty, null);
                        transaction.Rollback();
                    }
                }
            }
        }
        private MemberDetailModel ApplyNullRules(MemberDetailModel model)
        {
            if (model.Postcode == "unknown" || string.IsNullOrEmpty(model.Postcode))
            {
                model.Address = null;
                model.Postcode = null;
            }

            if (model.Email == "unknown" || string.IsNullOrEmpty(model.Email))
            {
                model.Email = null;
            }

            return model;
        }
        private MemberDetailModel ApplyJuniorRules(MemberDetailModel model)
        {
            model.Address = null;
            model.Email = null;
            model.Phone = null;
            model.Postcode = null;

            return model;
        }