public bool DelUser(ISTATUser user)
 {
     try
     {
         ISTAT.SingleSignON.Service.Model.DBUserEntities cnn = new ISTAT.SingleSignON.Service.Model.DBUserEntities();
         User newut = cnn.Users.FirstOrDefault(u => user.UserCode != null && u.UserCode.Trim() == user.UserCode.Trim());
         if (newut == null)
         {
             throw new Exception("User to delete not found");
         }
         if (newut.UserPreferences != null && newut.UserPreferences.Count > 0)
         {
             (from userpreference in cnn.UserPreferences
              where userpreference.UserCode == newut.UserCode
              select userpreference).ToList().ForEach(userpreference => cnn.UserPreferences.Remove(userpreference));
             newut.UserPreferences.Clear();
         }
         cnn.Users.Remove(newut);
         var errors = cnn.GetValidationErrors();
         if (errors != null && errors.Count(e => !e.IsValid) > 0)
         {
             throw new Exception(string.Format("Delete User Error: {0}", errors.FirstOrDefault(e => !e.IsValid).ValidationErrors.FirstOrDefault().ErrorMessage));
         }
         int ris = cnn.SaveChanges();
         return(ris > 0);
     }
     catch (Exception ex)
     {
         throw new Exception(string.Format("Delete User Error: {0}", ex));
     }
 }
        public static ISTATUser ConvertISTATUser(User user)
        {
            ISTATUser istatUser = new ISTATUser()
            {
                UserCode = user.UserCode == null ? null : user.UserCode.Trim(),
                Email    = user.Email == null ? null : user.Email.Trim(),
                Name     = user.Name == null ? null : user.Name.Trim(),
                Surname  = user.Surname == null ? null : user.Surname.Trim(),
                Sex      = user.Sex == null ? null : user.Sex.Trim(),
                Age      = user.Age == null ? null : user.Age.Trim(),
                Country  = user.Country == null ? null : user.Country.Trim(),
                Study    = user.Study == null ? null : user.Study.Trim(),
                Position = user.Position == null ? null : user.Position.Trim(),
                Agency   = user.Agency == null ? null : user.Agency.Trim(),
                Lang     = user.Lang == null ? null : user.Lang.Trim(),
                IsSA     = (user.Role != null && user.Role.Trim() == SAcode)
            };
            var themes = (from pre in user.UserPreferences select pre.PreferenceCode.Trim());

            if (themes != null)
            {
                istatUser.Themes = themes.ToList();
            }
            return(istatUser);
        }
        private ISTATUser ValidNullField(ISTATUser user)
        {
            if (user.UserCode != null && string.IsNullOrEmpty(user.UserCode.Trim()))
            {
                user.UserCode = null;
            }
            if (user.Name != null && string.IsNullOrEmpty(user.Name.Trim()))
            {
                user.Name = null;
            }
            if (user.Surname != null && string.IsNullOrEmpty(user.Surname.Trim()))
            {
                user.Surname = null;
            }
            if (user.Email != null && string.IsNullOrEmpty(user.Email.Trim()))
            {
                user.Email = null;
            }
            if (user.Password != null && string.IsNullOrEmpty(user.Password.Trim()))
            {
                user.Password = null;
            }

            if (user.Age != null && string.IsNullOrEmpty(user.Age.Trim()))
            {
                user.Age = null;
            }
            if (user.Agency != null && string.IsNullOrEmpty(user.Agency.Trim()))
            {
                user.Agency = null;
            }
            if (user.Country != null && string.IsNullOrEmpty(user.Country.Trim()))
            {
                user.Country = null;
            }
            if (user.Position != null && string.IsNullOrEmpty(user.Position.Trim()))
            {
                user.Position = null;
            }
            if (user.Sex != null && string.IsNullOrEmpty(user.Sex.Trim()))
            {
                user.Sex = null;
            }
            if (user.Study != null && string.IsNullOrEmpty(user.Study.Trim()))
            {
                user.Study = null;
            }
            if (user.Lang != null && string.IsNullOrEmpty(user.Lang.Trim()))
            {
                user.Lang = null;
            }

            return(user);
        }
        public ISTATUser AddUser(ISTATUser user)
        {
            try
            {
                ISTAT.SingleSignON.Service.Model.DBUserEntities cnn = new ISTAT.SingleSignON.Service.Model.DBUserEntities();
                user = ValidNullField(user);
                //Controlla Password
                if (!Utils.ValidatePassword(user.Password))
                {
                    throw new Exception("Error Format Password");
                }
                //Controllo stessa mail
                if (cnn.Users.Count(u => u.Email.Trim() == user.Email.Trim()) > 0)
                {
                    throw new Exception("Already exist user with this E-mail");
                }

                User newut = new User()
                {
                    UserCode = Guid.NewGuid().ToString(),
                    Email    = user.Email == null ? null : user.Email.Trim(),
                    Name     = user.Name == null ? null : user.Name.Trim(),
                    Surname  = user.Surname == null ? null : user.Surname.Trim(),
                    Sex      = user.Sex == null ? null : user.Sex.Trim(),
                    Age      = user.Age == null ? null : user.Age.Trim(),
                    Country  = user.Country == null ? null : user.Country.Trim(),
                    Study    = user.Study == null ? null : user.Study.Trim(),
                    Position = user.Position == null ? null : user.Position.Trim(),
                    Agency   = user.Agency == null ? null : user.Agency.Trim(),
                    Lang     = user.Lang == null ? null : user.Lang.Trim(),
                };
                List <UserPreference> up = new List <UserPreference>();
                if (user.Themes != null)
                {
                    user.Themes.ForEach(p => up.Add(new UserPreference()
                    {
                        UserCode = newut.UserCode, PreferenceCode = p.Trim()
                    }));
                }

                newut.UserPreferences = up;
                newut.Password        = Utils.CriptaPassword(user.Password.Trim());

                cnn.Users.Add(newut);
                var errors = cnn.GetValidationErrors();
                if (errors != null && errors.Count(e => !e.IsValid) > 0)
                {
                    throw new Exception(string.Format("Add User Error: {0}", errors.FirstOrDefault(e => !e.IsValid).ValidationErrors.FirstOrDefault().ErrorMessage));
                }

                int ris = cnn.SaveChanges();
                if (ris > 0)
                {
                    return(Utils.ConvertISTATUser(newut));
                }
                else
                {
                    throw new Exception("Add User Error");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Add User Error: {0}", ex));
            }
        }
        public ISTATUser ModUser(ISTATUser user)
        {
            try
            {
                ISTAT.SingleSignON.Service.Model.DBUserEntities cnn = new ISTAT.SingleSignON.Service.Model.DBUserEntities();
                user = ValidNullField(user);
                User newut = cnn.Users.FirstOrDefault(u => user.UserCode != null && u.UserCode.Trim() == user.UserCode.Trim());
                if (newut == null)
                {
                    throw new Exception("User to chenge not found");
                }

                if (newut.Email.Trim() != user.Email.Trim())
                {
                    //Controllo stessa mail
                    if (cnn.Users.Count(u => u.Email.Trim() == user.Email.Trim()) > 0)
                    {
                        throw new Exception("Already exist user with this E-mail");
                    }
                }
                newut.Email    = user.Email == null ? null : user.Email.Trim();
                newut.Name     = user.Name == null ? null : user.Name.Trim();
                newut.Surname  = user.Surname == null ? null : user.Surname.Trim();
                newut.Sex      = user.Sex == null ? null : user.Sex.Trim();
                newut.Age      = user.Age == null ? null : user.Age.Trim();
                newut.Country  = user.Country == null ? null : user.Country.Trim();
                newut.Study    = user.Study == null ? null : user.Study.Trim();
                newut.Position = user.Position == null ? null : user.Position.Trim();
                newut.Agency   = user.Agency == null ? null : user.Agency.Trim();
                newut.Lang     = user.Lang == null ? null : user.Lang.Trim();
                List <UserPreference> up = new List <UserPreference>();
                if (newut.UserPreferences != null && newut.UserPreferences.Count > 0)
                {
                    (from userpreference in cnn.UserPreferences
                     where userpreference.UserCode == newut.UserCode
                     select userpreference).ToList().ForEach(userpreference => cnn.UserPreferences.Remove(userpreference));
                    newut.UserPreferences.Clear();
                }
                if (user.Themes != null)
                {
                    user.Themes.ForEach(p => up.Add(new UserPreference()
                    {
                        UserCode = newut.UserCode, PreferenceCode = p.Trim()
                    }));
                }

                newut.UserPreferences = up;


                var errors = cnn.GetValidationErrors();
                if (errors != null && errors.Count(e => !e.IsValid) > 0)
                {
                    throw new Exception(string.Format("Change User Error: {0}", errors.FirstOrDefault(e => !e.IsValid).ValidationErrors.FirstOrDefault().ErrorMessage));
                }
                int ris = cnn.SaveChanges();
                return(Utils.ConvertISTATUser(newut));
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Modify User Error: {0}", ex));
            }
        }