Esempio n. 1
0
        private void UpdateScriptCommand(object node)
        {
            if (!(node is TreeNodeViewModel treeNode))
            {
                return;
            }
            if (!(treeNode.NodePayload is MetaScript script))
            {
                return;
            }

            MessageBoxResult result = MessageBox.Show("Обновить скрипт \"" + script.Name + "\" ?",
                                                      "DaJet", MessageBoxButton.OKCancel, MessageBoxImage.Question);

            if (result != MessageBoxResult.OK)
            {
                return;
            }

            WebServer      webServer = treeNode.GetAncestorPayload <WebServer>();
            DatabaseInfo   database  = treeNode.GetAncestorPayload <DatabaseInfo>();
            DatabaseServer server    = treeNode.GetAncestorPayload <DatabaseServer>();

            string url = GetActionScriptUrl(server, database, script);

            ScriptingController controller = Services.GetService <ScriptingController>();

            byte[] bytes = controller.ReadScriptAsBytes(server, database, MetaScriptType.Script, script.Name);
            script.SourceCode = Convert.ToBase64String(bytes);

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                WriteIndented = true
            };
            string        requestJson = JsonSerializer.Serialize(script, options);
            StringContent body        = new StringContent(requestJson, Encoding.UTF8, "application/json");

            script.SourceCode = string.Empty; // we do not need source code any more

            IHttpClientFactory http   = Services.GetService <IHttpClientFactory>();
            HttpClient         client = http.CreateClient(webServer.Name);

            if (client.BaseAddress == null)
            {
                client.BaseAddress = new Uri(webServer.Address);
            }

            try
            {
                var response = client.PutAsync(url, body).Result;

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    _ = MessageBox.Show("Script has been updated successfully.", script.Name, MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    ShowHttpError(script.Name + " (error)", response);
                }
            }
            catch (Exception ex)
            {
                _ = MessageBox.Show(ex.Message, script.Name);
            }
        }
Esempio n. 2
0
        public void CreateScriptNode(WebServer webServer, DatabaseServer server, DatabaseInfo database, MetaScript script)
        {
            if (RootNode == null)
            {
                return;
            }
            foreach (TreeNodeViewModel webServerNode in RootNode.TreeNodes)
            {
                if (webServerNode.NodePayload != webServer)
                {
                    continue;
                }

                DatabaseServer dataServer = webServer.DatabaseServers.Where(s => s.Identity == server.Identity).FirstOrDefault();
                if (dataServer == null)
                {
                    dataServer = new DatabaseServer()
                    {
                        Name     = server.Name,
                        Identity = server.Identity
                    };
                    DatabaseInfo newDatabase = new DatabaseInfo()
                    {
                        Name     = database.Name,
                        Identity = database.Identity,
                        Scripts  = new List <MetaScript>()
                        {
                            script
                        }
                    };
                    dataServer.Databases.Add(newDatabase);
                    webServer.DatabaseServers.Add(dataServer);

                    TreeNodeViewModel serverNode = CreateDatabaseServerNode(webServerNode, dataServer);
                    webServerNode.TreeNodes.Add(serverNode);
                    TreeNodeViewModel databaseNode = CreateDatabaseNode(serverNode, newDatabase);
                    serverNode.TreeNodes.Add(databaseNode);
                    TreeNodeViewModel scriptNode = CreateScriptNode(databaseNode, script);
                    databaseNode.TreeNodes.Add(scriptNode);
                }
                else
                {
                    DatabaseInfo dbref = dataServer.Databases.Where(db => db.Identity == database.Identity).FirstOrDefault();
                    if (dbref == null)
                    {
                        DatabaseInfo newDatabase = new DatabaseInfo()
                        {
                            Name     = database.Name,
                            Identity = database.Identity,
                            Scripts  = new List <MetaScript>()
                            {
                                script
                            }
                        };
                        dataServer.Databases.Add(newDatabase);

                        TreeNodeViewModel serverNode = webServerNode.TreeNodes.Where(n => n.NodePayload == dataServer).FirstOrDefault();
                        if (serverNode == null)
                        {
                            serverNode = CreateDatabaseServerNode(webServerNode, dataServer);
                            webServerNode.TreeNodes.Add(serverNode);
                        }
                        TreeNodeViewModel databaseNode = CreateDatabaseNode(serverNode, newDatabase);
                        serverNode.TreeNodes.Add(databaseNode);
                        TreeNodeViewModel scriptNode = CreateScriptNode(databaseNode, script);
                        databaseNode.TreeNodes.Add(scriptNode);
                    }
                    else
                    {
                        if (dbref.Scripts.Where(s => s == script).FirstOrDefault() == null)
                        {
                            dbref.Scripts.Add(script);
                        }

                        TreeNodeViewModel serverNode = webServerNode.TreeNodes.Where(n => n.NodePayload == dataServer).FirstOrDefault();
                        if (serverNode == null)
                        {
                            serverNode = CreateDatabaseServerNode(webServerNode, dataServer);
                            webServerNode.TreeNodes.Add(serverNode);
                        }
                        TreeNodeViewModel databaseNode = serverNode.TreeNodes.Where(n => n.NodePayload == dbref).FirstOrDefault();
                        if (databaseNode == null)
                        {
                            databaseNode = CreateDatabaseNode(serverNode, dbref);
                            serverNode.TreeNodes.Add(databaseNode);
                        }
                        TreeNodeViewModel scriptNode = databaseNode.TreeNodes.Where(n => n.NodePayload == script).FirstOrDefault();
                        if (scriptNode == null)
                        {
                            scriptNode = CreateScriptNode(databaseNode, script);
                            databaseNode.TreeNodes.Add(scriptNode);
                        }
                    }
                }
                SaveWebSettings();
                break;
            }
        }
Esempio n. 3
0
        private void ExecuteScriptCommand(object node)
        {
            if (!(node is TreeNodeViewModel treeNode))
            {
                return;
            }
            if (!(treeNode.NodePayload is MetaScript script))
            {
                return;
            }

            WebServer      webServer = treeNode.GetAncestorPayload <WebServer>();
            DatabaseInfo   database  = treeNode.GetAncestorPayload <DatabaseInfo>();
            DatabaseServer server    = treeNode.GetAncestorPayload <DatabaseServer>();

            string url = GetExecuteScriptUrl(null, server, database, script);

            //ScriptingController controller = Services.GetService<ScriptingController>();
            //string sourceCode = controller.ReadScriptSourceCode(server, database, MetaScriptType.Script, script.Name);

            //IMetadataService metadata = Services.GetService<IMetadataService>();
            //metadata.AttachDatabase(string.IsNullOrWhiteSpace(server.Address) ? server.Name : server.Address, database);

            //IScriptingService scripting = Services.GetService<IScriptingService>();
            //TSqlFragment syntaxTree = scripting.ParseScript(sourceCode, out IList<ParseError> errors);
            //if (errors.Count > 0) { ShowParseErrors(errors); return; }

            //DeclareVariableStatementVisitor visitor = new DeclareVariableStatementVisitor();
            //syntaxTree.Accept(visitor);

            string        requestJson = string.Empty;
            StringContent body        = new StringContent(requestJson, Encoding.UTF8, "application/json");

            IHttpClientFactory http   = Services.GetService <IHttpClientFactory>();
            HttpClient         client = http.CreateClient(webServer.Name);

            if (client.BaseAddress == null)
            {
                client.BaseAddress = new Uri(webServer.Address);
            }

            try
            {
                var response = client.PostAsync(url, body).Result;

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    MainWindowViewModel   mainWindow = Services.GetService <MainWindowViewModel>();
                    ScriptEditorViewModel editor     = Services.GetService <ScriptEditorViewModel>();
                    editor.Name = $"{script.Name} (WEB response)";
                    string responseJson = response.Content.ReadAsStringAsync().Result;
                    editor.ScriptCode = url + Environment.NewLine + responseJson;
                    ScriptEditorView scriptView = new ScriptEditorView()
                    {
                        DataContext = editor
                    };
                    mainWindow.AddNewTab(editor.Name, scriptView);
                }
                else
                {
                    ShowHttpError(script.Name + " (error)", response);
                }
            }
            catch (Exception ex)
            {
                _ = MessageBox.Show(ex.Message, script.Name);
            }
        }
Esempio n. 4
0
 public WebServerFormViewModel(WebServer model) : this()
 {
     Model = model;
 }