//private void EditBooleanConfigurationToUsers(Project project , string configurationName , string oldName)
        //{
        //    if (configurationName == oldName)
        //        return;

        //    foreach (var projectProjectUser in project.ProjectUsers)
        //    {
        //        foreach (var projectCustomData in project.ProjectCustomData.Where(m=> m.ConfigurationName == oldName))
        //        {
        //            try
        //            {
        //                var userCustomData =
        //                    projectCustomData.UserCustomData.FirstOrDefault(m => m.UserId == projectProjectUser.UserId);

        //                projectCustomData.UserCustomData.Remove(userCustomData);
        //                userCustomData.ModificationDate = DateTime.UtcNow;
        //                projectCustomData.UserCustomData.Add(userCustomData);
        //            }
        //            catch (Exception)
        //            {
        //                //ignore
        //            }
        //        }
        //    }
        //}

        private void EditNumberConfigurationToUsers(NumberProjectConfiguration configuration, Project project,
                                                    string configurationName)
        {
            var projectCustomData =
                project.ProjectCustomData.FirstOrDefault(m => m.ConfigurationName == configurationName);

            foreach (var userCustomData in projectCustomData.UserCustomData)
            {
                try
                {
                    var userConfiguration =
                        (NumberUserConfiguration)
                        _jsonConvertService.DeserializeUser(ConfigurationType.Number,
                                                            userCustomData.SerializedConfiguration);

                    var userValueUpdateIsNeeded = (userConfiguration.Value < configuration.LowerBound) ||
                                                  (userConfiguration.Value > configuration.UpperBound);

                    if (!userValueUpdateIsNeeded)
                    {
                        continue;
                    }

                    userConfiguration.Value = configuration.Default;
                    userCustomData.SerializedConfiguration = _jsonConvertService.Serialize(userConfiguration);
                    userCustomData.ModificationDate        = DateTime.UtcNow;
                }
                catch (Exception)
                {
                    //ignore
                }
            }
        }
        public List <Tuple <string, string> > Invoke(Guid projectGuid, string userLogin)
        {
            var user    = _userRepository.GetAll().FirstOrDefault(u => u.Login == userLogin);
            var project = _projectRepository.GetAll().FirstOrDefault(p => p.ProjectGuid == projectGuid);

            if ((user == null) || (project == null))
            {
                throw new ArgumentNullException();
            }

            var projectId = project.Id;

            project = _projectRepository.GetByIdWithCustomDatas(projectId);

            if (project == null)
            {
                throw new ArgumentNullException();
            }

            var userCustomData = user.UserCustomData.Where(ucd => ucd.ProjectCustomData.ProjectId == projectId);

            var list = new List <Tuple <string, string> >();

            foreach (var customData in userCustomData)
            {
                var    key = customData.ProjectCustomData.ConfigurationName;
                string value;

                switch (customData.ProjectCustomData.ConfigurationType)
                {
                case ConfigurationType.Number:
                    value = ((NumberUserConfiguration)_convertService.DeserializeUser(ConfigurationType.Number,
                                                                                      customData.SerializedConfiguration)).Value.ToString();
                    break;

                case ConfigurationType.Boolean:
                    value = ((BooleanUserConfiguration)_convertService.DeserializeUser(ConfigurationType.Boolean,
                                                                                       customData.SerializedConfiguration)).Value.ToString();
                    break;

                case ConfigurationType.Text:
                    value = ((TextUserConfiguration)_convertService.DeserializeUser(ConfigurationType.Text,
                                                                                    customData.SerializedConfiguration)).Value;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                list.Add(new Tuple <string, string>(key, value));
            }

            return(list);
        }
Пример #3
0
        public UserConfigurationView Invoke(int projectId, int userId, ConfigurationType configurationType,
                                            string configurationName)
        {
            var project = projectRepository.GetByIdWithCustomDatas(projectId);

            if (project == null)
            {
                throw new ArgumentNullException($"Could not find project with id: {projectId}.");
            }

            var userCustomConfiguration = userCustomDataRepository.GetUserCustomData(projectId, userId,
                                                                                     configurationName);

            if (userCustomConfiguration == null)
            {
                throw new ArgumentNullException(
                          $"Could not find user configuration data connected with projectId: {projectId} and userId{userId}.");
            }

            switch (configurationType)
            {
            case ConfigurationType.Boolean:
            {
                var userConfiguration = (BooleanUserConfiguration)
                                        jsonConvertService.DeserializeUser(ConfigurationType.Boolean,
                                                                           userCustomConfiguration.SerializedConfiguration);

                return(new BooleanUserConfigurationView
                    {
                        ProjectId = projectId,
                        UserId = userId,
                        ConfigurationName = configurationName,
                        CurrentValue = userConfiguration.Value
                    });
            }

            case ConfigurationType.Number:
            {
                var projectConfiguration =
                    (NumberProjectConfiguration)
                    jsonConvertService.DeserializeProject(ConfigurationType.Number,
                                                          project.ProjectCustomData.FirstOrDefault(m => m.ConfigurationName == configurationName)
                                                          .SerializedConfiguration);
                var userConfiguration =
                    (NumberUserConfiguration)
                    jsonConvertService.DeserializeUser(ConfigurationType.Number,
                                                       userCustomConfiguration.SerializedConfiguration);

                return(new NumberUserConfigurationView
                    {
                        ProjectId = projectId,
                        UserId = userId,
                        ConfigurationName = configurationName,
                        CurrentValue = userConfiguration.Value,
                        LowerBound = projectConfiguration.LowerBound,
                        UpperBound = projectConfiguration.UpperBound
                    });
            }

            case ConfigurationType.Text:
            {
                var projectConfiguration =
                    (TextProjectConfiguration)jsonConvertService.DeserializeProject(ConfigurationType.Text,
                                                                                    project.ProjectCustomData.FirstOrDefault(
                                                                                        m => (m.ConfigurationName == configurationName) && (m.ProjectId == projectId))
                                                                                    .SerializedConfiguration);

                var userConfiguration =
                    (TextUserConfiguration)
                    jsonConvertService.DeserializeUser(ConfigurationType.Text,
                                                       userCustomConfiguration.SerializedConfiguration);

                var possibleValues = new List <string>();

                if (!projectConfiguration.PossibleValues.Contains(projectConfiguration.Default))
                {
                    possibleValues.Add(projectConfiguration.Default);
                }

                possibleValues.AddRange(projectConfiguration.PossibleValues);

                return(new TextUserConfigurationView
                    {
                        ProjectId = projectId,
                        UserId = userId,
                        ConfigurationName = configurationName,
                        CurrentValue = userConfiguration.Value,
                        PossibleValuesList = possibleValues
                    });
            }
            }

            throw new Exception($"Something went wrong in GetUserConfigurationViewForUser");
        }