public CustomDataConfiguration Invoke(int projectId, string configurationName,
                                              ConfigurationType configurationType)
        {
            var project = projectRepository.GetByIdWithCustomDatas(projectId);

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

            if (string.IsNullOrEmpty(configurationName))
            {
                switch (configurationType)
                {
                case ConfigurationType.Boolean:
                    return(new BooleanProjectConfiguration());

                case ConfigurationType.Number:
                    return(new NumberProjectConfiguration());

                case ConfigurationType.Text:
                    return(new TextProjectConfiguration());

                default:
                    throw new Exception(
                              $"Wrong policy ({configurationType}) given to GetPolicyConfigurationOrDefaultFromProject.");
                }
            }

            var projectCustomData =
                project.ProjectCustomData.FirstOrDefault(pp => pp.ConfigurationName == configurationName);

            if (projectCustomData != null)
            {
                try
                {
                    return(jsonConvertService.DeserializeProject(configurationType,
                                                                 projectCustomData.SerializedConfiguration));
                }
                catch (Exception e)
                {
                    throw new Exception(
                              $"There was a problem with deserializing policy configurations of project with id: {projectId}, " +
                              $"policy: {configurationName}, exception message: {e.Message}.");
                }
            }

            throw new Exception($"Something went wrong in GetCustomDataConfigurationOrDefaultForProject");
        }
Пример #2
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");
        }
        public void SetDefaultUserCustomData(Project project, User newUser)
        {
            var projectCustomData = project.ProjectCustomData;

            foreach (var customData in projectCustomData)
            {
                switch (customData.ConfigurationType)
                {
                case ConfigurationType.Boolean:
                {
                    var projectConfiguration =
                        (BooleanProjectConfiguration)
                        customDataJsonConvertService.DeserializeProject(ConfigurationType.Boolean,
                                                                        customData.SerializedConfiguration);
                    newUser.UserCustomData.Add(new UserCustomData
                        {
                            ProjectCustomDataId     = customData.Id,
                            SerializedConfiguration =
                                customDataJsonConvertService.Serialize(new BooleanUserConfiguration
                            {
                                Value = projectConfiguration.Default
                            })
                        });
                    break;
                }

                case ConfigurationType.Number:
                {
                    var projectConfiguration =
                        (NumberProjectConfiguration)
                        customDataJsonConvertService.DeserializeProject(ConfigurationType.Number,
                                                                        customData.SerializedConfiguration);
                    newUser.UserCustomData.Add(new UserCustomData
                        {
                            ProjectCustomDataId     = customData.Id,
                            SerializedConfiguration =
                                customDataJsonConvertService.Serialize(new NumberUserConfiguration
                            {
                                Value = projectConfiguration.Default
                            })
                        });
                    break;
                }

                case ConfigurationType.Text:
                {
                    var projectConfiguration =
                        (TextProjectConfiguration)
                        customDataJsonConvertService.DeserializeProject(ConfigurationType.Text,
                                                                        customData.SerializedConfiguration);

                    newUser.UserCustomData.Add(new UserCustomData
                        {
                            ProjectCustomDataId     = customData.Id,
                            SerializedConfiguration =
                                customDataJsonConvertService.Serialize(new TextUserConfiguration
                            {
                                Value = projectConfiguration.Default
                            })
                        });
                    break;
                }
                }
            }
        }