Exemple #1
0
        internal ApplyConfigurationResult ApplyModuleConfiguration <TModule, TConfiguration>(TConfiguration configuration, ModuleConfigurationManipulator <TModule> moduleConfigurationManipulator, TModule module)
            where TModule : ModuleCore <TModule>
            where TConfiguration : ModuleConfiguration <TModule>, new()
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            var context = AppCore.GetUserContextManager().GetCurrentUserContext();

            var permissionCheck = module.CheckPermission(context, ModulesConstants.PermissionSaveConfiguration);

            if (permissionCheck == CheckPermissionVariant.Denied)
            {
                return(ApplyConfigurationResult.PermissionDenied);
            }

            var moduleType          = typeof(TModule);
            var moduleCoreAttribute = moduleType.GetCustomAttribute <ModuleCoreAttribute>();

            var urlNameEncoded = System.Web.HttpUtility.UrlEncode(configuration.UrlName);

            configuration.UrlName = urlNameEncoded;

            using (var db = new CoreContext())
                using (var scope = db.CreateScope())
                {
                    var moduleConfig = db.Module.FirstOrDefault(x => x.IdModule == module.ID);
                    if (moduleConfig == null)
                    {
                        var fullName = Utils.TypeNameHelper.GetFullNameCleared(typeof(TModule));
                        moduleConfig = db.Module.FirstOrDefault(x => x.UniqueKey == fullName);
                        if (moduleConfig == null)
                        {
                            moduleConfig = new ModuleConfig()
                            {
                                UniqueKey = fullName, DateChange = DateTime.Now, IdUserChange = 0
                            };
                            db.Module.Add(moduleConfig);
                        }
                    }

                    moduleConfig.Configuration = configuration._valuesProvider.Save();
                    moduleConfig.DateChange    = DateTime.Now;
                    moduleConfig.IdUserChange  = context.IdUser;

                    db.SaveChanges();
                    scope.Complete();

                    module.ID             = moduleConfig.IdModule;
                    module._moduleUrlName = string.IsNullOrEmpty(configuration.UrlName) ? moduleCoreAttribute.DefaultUrlName : configuration.UrlName;
                    moduleConfigurationManipulator._valuesProviderUsable.Load(moduleConfig.Configuration);
                }

            return(ApplyConfigurationResult.Success);
        }
Exemple #2
0
        private void LoadModuleCustom <TModuleType>(TModuleType module)
            where TModuleType : ModuleCore <TModuleType>
        {
            var fullName = typeof(TModuleType).FullName;

            try
            {
                if (AppCore.AppDebugLevel >= DebugLevel.Common)
                {
                    Debug.WriteLine($"{nameof(ModulesManager)}.{nameof(LoadModuleCustom)}: загрузка модуля '{typeof(TModuleType)}'.");
                }

                var moduleType          = typeof(TModuleType);
                var moduleCoreAttribute = moduleType.GetCustomAttribute <ModuleCoreAttribute>();

                var moduleRegisterHandlerTypes = AppCore.GetQueryTypes().Where(x => typeof(IModuleRegisteredHandler).IsAssignableFrom(x)).ToList();
                var moduleRegisterHandlers     = moduleRegisterHandlerTypes.Select(x => AppCore.Get <IModuleRegisteredHandler>(x)).ToList();

                using (var db = new CoreContext())
                {
                    fullName = Utils.TypeNameHelper.GetFullNameCleared(moduleType);

                    if (AppCore.AppDebugLevel >= DebugLevel.Detailed)
                    {
                        Debug.WriteLine($"{nameof(ModulesManager)}.{nameof(LoadModuleCustom)}: поиск настроек.");
                    }

                    var config = db.Module.Where(x => x.UniqueKey == fullName).FirstOrDefault();
                    if (config == null)
                    {
                        config = new ModuleConfig()
                        {
                            UniqueKey = fullName, DateChange = DateTime.Now
                        };
                        db.Module.Add(config);
                        db.SaveChanges();
                    }

                    if (AppCore.AppDebugLevel >= DebugLevel.Detailed)
                    {
                        Debug.WriteLine($"{nameof(ModulesManager)}.{nameof(LoadModuleCustom)}: применение настроек.");
                    }

                    module.ID             = config.IdModule;
                    module._moduleCaption = moduleCoreAttribute.Caption;
                    module._moduleUrlName = moduleCoreAttribute.DefaultUrlName;

                    var configurationManipulator = new ModuleConfigurationManipulator <TModuleType>(module, CreateValuesProviderForModule(module));
                    ((IComponentStartable)configurationManipulator).Start(AppCore);
                    module._configurationManipulator = configurationManipulator;

                    var cfg = configurationManipulator.GetUsable <ModuleConfiguration <TModuleType> >();

                    if (AppCore.AppDebugLevel >= DebugLevel.Detailed)
                    {
                        Debug.WriteLine($"{nameof(ModulesManager)}.{nameof(LoadModuleCustom)}: инициализация.");
                    }

                    if (!string.IsNullOrEmpty(cfg.UrlName))
                    {
                        module._moduleUrlName = cfg.UrlName;
                    }
                    module.InitModule();
                    moduleRegisterHandlers.ForEach(x => x.OnModuleInitialized <TModuleType>(module));

                    _modules.RemoveAll(x => x.Item1 == typeof(TModuleType));
                    _modules.Add(new Tuple <Type, ModuleCore>(typeof(TModuleType), module));

                    AppCore.Get <JournalingManager>().RegisterJournalTyped <TModuleType>($"Журнал событий модуля '{module.Caption}'");

                    this.RegisterEvent(
                        EventType.Info,
                        $"Загрузка модуля '{fullName}'",
                        $"Модуль загружен на основе типа '{module.GetType().FullName}' с Id={config.IdModule}."
                        );

                    if (AppCore.AppDebugLevel >= DebugLevel.Detailed)
                    {
                        Debug.WriteLine($"{nameof(ModulesManager)}.{nameof(LoadModuleCustom)}: успешно.");
                    }
                }
            }
            catch (Exception ex)
            {
                if (AppCore.AppDebugLevel >= DebugLevel.Common)
                {
                    Debug.WriteLine($"{nameof(ModulesManager)}.{nameof(LoadModuleCustom)}: ошибка.");
                }
                if (AppCore.AppDebugLevel >= DebugLevel.Detailed)
                {
                    Debug.WriteLine($"{nameof(ModulesManager)}.{nameof(LoadModuleCustom)}: {ex}");
                }

                this.RegisterEvent(
                    EventType.Error,
                    $"Загрузка модуля '{fullName}'",
                    $"Ошибка загрузки.",
                    ex
                    );
                throw;
            }
        }