Beispiel #1
0
        public void Start(PluginStartArguments pluginStartArgs)
        {
            IHostingEnvironment hostingEnvironment = (IHostingEnvironment)pluginStartArgs.ServiceProvider.GetService(typeof(IHostingEnvironment));
            // Sample how to read configuration from erp config file
            // you can use your own config
            var configurationBuilder = new ConfigurationBuilder()
                //.SetBasePath(PlatformServices.Default.Application.ApplicationBasePath)
                .SetBasePath(hostingEnvironment.ContentRootPath)
                .AddJsonFile("config.json");
            var configuration = configurationBuilder.Build();

            Debug.WriteLine("WebVella Sample Plugin start called");
        }
 private void ExecutePluginStart(IServiceProvider serviceProvider)
 {
     //search and execute Start method for each plugin
     //if there are multiple types, marked by PluginStartupAttribute, with Start method, they all will be executed
     foreach (var plugin in plugins)
     {
         foreach (var assembly in plugin.Assemblies)
         {
             if (plugin.Assemblies.Any(x => x.FullName == assembly.FullName))
             {
                 foreach (Type type in assembly.GetTypes())
                 {
                     if (type.GetCustomAttributes(typeof(PluginStartupAttribute), true).Length > 0)
                     {
                         try
                         {
                             var method = type.GetMethod("Start");
                             if (method != null)
                             {
                                 PluginStartArguments args = new PluginStartArguments {
                                     Plugin = plugin, ServiceProvider = serviceProvider
                                 };
                                 method.Invoke(new DynamicObjectCreater(type).CreateInstance(), new object[] { args });
                             }
                         }
                         catch (Exception ex)
                         {
                             throw new Exception("An exception is thrown while execute start for plugin : '" +
                                                 assembly.FullName + ";" + type.Namespace + "." + type.Name + "'", ex);
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #3
0
 private void ExecutePluginStart(IServiceProvider serviceProvider)
 {
     //search and execute Start method for each plugin
     //if there are multiple types, marked by PluginStartupAttribute, with Start method, they all will be executed
     foreach (var plugin in plugins)
     {
         foreach (var assembly in plugin.Assemblies)
         {
             if (plugin.Assemblies.Any(x => x.FullName == assembly.FullName))
             {
                 foreach (Type type in assembly.GetTypes())
                 {
                     if (type.GetCustomAttributes(typeof(PluginStartupAttribute), true).Length > 0)
                     {
                         try
                         {
                             var method = type.GetMethod("Start");
                             if (method != null)
                             {
                                 PluginStartArguments args = new PluginStartArguments { Plugin = plugin, ServiceProvider= serviceProvider };
                                 method.Invoke(new DynamicObjectCreater(type).CreateInstance(), new object[] { args });
                             }
                         }
                         catch (Exception ex)
                         {
                             throw new Exception("An exception is thrown while execute start for plugin : '" +
                              assembly.FullName + ";" + type.Namespace + "." + type.Name + "'", ex);
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #4
0
        private bool createSampleRecords = false; //To be enabled requires the CRM sample records

        #endregion Fields

        #region Methods

        public void Start(PluginStartArguments pluginStartArgs)
        {
            //initialize static context
            StaticContext.Initialize(pluginStartArgs.Plugin, pluginStartArgs.ServiceProvider);

            var entMan = new EntityManager();
            var relMan = new EntityRelationManager();
            var recMan = new RecordManager();
            var storeSystemSettings = DbContext.Current.SettingsRepository.Read();
            var systemSettings = new SystemSettings(storeSystemSettings);

            using (SecurityContext.OpenSystemScope())
            {
                //Create transaction
                using (var connection = DbContext.Current.CreateConnection())
                {
                    try
                    {
                        connection.BeginTransaction();

                        //Here we need to initialize or update the environment based on the plugin requirements.
                        //The default place for the plugin data is the "plugin_data" entity -> the "data" text field, which is used to store stringified JSON
                        //containing the plugin settings or version

                        #region << 1.Get the current ERP database version and checks for other plugin dependencies >>
                        if (systemSettings.Version > 0)
                        {
                            //Do something if database version is not what you expect
                        }

                        //This plugin needs the webvella-crm plugin to be installed, so we will check this here
                        var installedPlugins = new PluginService().Plugins;
                        var crmPluginFound = false;
                        foreach (var plugin in installedPlugins)
                        {
                            switch (plugin.Name)
                            {
                                case "webvella-crm":
                                    crmPluginFound = true;
                                    break;
                                default:
                                    break;
                            }
                        }

                        if (!crmPluginFound)
                            throw new Exception("'webvella-crm' plugin is required for the 'webvella-project' to operate");

                        #endregion

                        #region << 2.Get the current plugin settings from the database >>
                        var currentPluginSettings = new PluginSettings();
                        QueryObject pluginDataQueryObject = EntityQuery.QueryEQ("name", WEBVELLA_PROJECT_PLUGIN_NAME);
                        var pluginDataQuery = new EntityQuery("plugin_data", "*", pluginDataQueryObject);
                        var pluginDataQueryResponse = recMan.Find(pluginDataQuery);
                        if (!pluginDataQueryResponse.Success)
                            throw new Exception("plugin 'webvella-project' failed to get its settings due to: " + pluginDataQueryResponse.Message);

                        if (pluginDataQueryResponse.Object == null || !pluginDataQueryResponse.Object.Data.Any() || pluginDataQueryResponse.Object.Data[0]["data"] == DBNull.Value)
                        {
                            //plugin was not installed
                            currentPluginSettings.Version = 20160429;
                            {
                                string json = JsonConvert.SerializeObject(currentPluginSettings);
                                var settingsEntityRecord = new EntityRecord();
                                settingsEntityRecord["id"] = WEBVELLA_PROJECT_PLUGIN_ID;
                                settingsEntityRecord["name"] = WEBVELLA_PROJECT_PLUGIN_NAME;
                                settingsEntityRecord["data"] = json;
                                var settingsSaveReponse = recMan.CreateRecord("plugin_data", settingsEntityRecord);
                                if (!settingsSaveReponse.Success)
                                    throw new Exception("plugin 'webvella-project' failed to save its settings in the database due to: " + pluginDataQueryResponse.Message);
                            }
                        }
                        else
                        {
                            string json = (string)((List<EntityRecord>)pluginDataQueryResponse.Object.Data)[0]["data"];
                            currentPluginSettings = JsonConvert.DeserializeObject<PluginSettings>(json);
                        }
                        #endregion

                        #region << 3. Run methods based on the current installed version of the plugin >>
                        if (currentPluginSettings.Version < 20160430)
                        {
                            try
                            {
                                currentPluginSettings.Version = 20160430;
                                Patch160430(entMan,relMan,recMan,createSampleRecords);
                            }
                            catch (Exception ex)
                            {
                                var exception = ex;
                                throw ex;
                            }
                        }

                        if (currentPluginSettings.Version < 20160610)
                        {
                            try
                            {
                                currentPluginSettings.Version = 20160610;
                                Patch160610(entMan, relMan, recMan, createSampleRecords);
                            }
                            catch (Exception ex)
                            {
                                var exception = ex;
                                throw ex;
                            }
                        }

                        if (currentPluginSettings.Version < 20160613)
                        {
                            try
                            {
                                currentPluginSettings.Version = 20160613;
                                Patch160613(entMan, relMan, recMan, createSampleRecords);
                            }
                            catch (Exception ex)
                            {
                                var exception = ex;
                                throw ex;
                            }
                        }

                        if (currentPluginSettings.Version < 20160627)
                        {
                            try
                            {
                                currentPluginSettings.Version = 20160627;
                                Patch160627(entMan, relMan, recMan, createSampleRecords);
                            }
                            catch (Exception ex)
                            {
                                var exception = ex;
                                throw ex;
                            }
                        }

                        if (currentPluginSettings.Version < 20160707)
                        {
                            try
                            {
                                currentPluginSettings.Version = 20160707;
                                Patch160707(entMan, relMan, recMan, createSampleRecords);
                            }
                            catch (Exception ex)
                            {
                                var exception = ex;
                                throw ex;
                            }
                        }

                        //if (currentPluginSettings.Version < 20160714)
                        //{
                        //	try
                        //	{
                        //		currentPluginSettings.Version = 20160714;
                        //		Patch160714(entMan, relMan, recMan, createSampleRecords);
                        //	}
                        //	catch (Exception ex)
                        //	{
                        //		var exception = ex;
                        //		throw ex;
                        //	}
                        //}

                        #endregion

                        #region << 4. Save needed changes to the plugin setting data >>
                        {
                            string json = JsonConvert.SerializeObject(currentPluginSettings);
                            var settingsEntityRecord = new EntityRecord();
                            settingsEntityRecord["id"] = WEBVELLA_PROJECT_PLUGIN_ID;
                            settingsEntityRecord["name"] = WEBVELLA_PROJECT_PLUGIN_NAME;
                            settingsEntityRecord["data"] = json;
                            var settingsUpdateReponse = recMan.UpdateRecord("plugin_data", settingsEntityRecord);
                            if (!settingsUpdateReponse.Success)
                                throw new Exception("plugin 'webvella-project' failed to update its settings in the database due to: " + pluginDataQueryResponse.Message);
                        }
                        #endregion

                        connection.CommitTransaction();
                    }
                    catch (Exception ex)
                    {
                        connection.RollbackTransaction();
                        throw ex;
                    }
                }
            }
        }