async Task StartWorkflow(RequestCommand cmd, ExpandoObject dataToStart, TextWriter writer)
        {
            if (_workflowEngine == null)
            {
                throw new InvalidOperationException($"Service 'IWorkflowEngine' not registered");
            }
            var swi = new StartWorkflowInfo
            {
                DataSource = cmd.CurrentSource,
                Schema     = cmd.CurrentSchema,
                Model      = cmd.CurrentModel,
                ModelId    = dataToStart.Get <Int64>("Id"),
                ActionBase = cmd.ActionBase
            };

            if (swi.ModelId == 0)
            {
                throw new RequestModelException("Id must be specified to 'startProcess' command");
            }
            if (!String.IsNullOrEmpty(cmd.file))
            {
                swi.Source = $"file:{cmd.file}";
            }
            swi.Comment = dataToStart.Get <String>("Comment");
            swi.UserId  = dataToStart.Get <Int64>("UserId");
            if (swi.Source == null)
            {
                throw new RequestModelException($"File or clrType must be specified to 'startProcess' command");
            }
            WorkflowResult wr = await _workflowEngine.StartWorkflow(swi);

            WriteJsonResult(writer, wr);
        }
        async Task ExecuteCommand(RequestCommand cmd, ExpandoObject dataToExec, HttpResponseBase response)
        {
            switch (cmd.type)
            {
            case CommandType.sql:
                await ExecuteSqlCommand(cmd, dataToExec, response.Output);

                break;

            case CommandType.startProcess:
                await StartWorkflow(cmd, dataToExec, response.Output);

                break;

            case CommandType.resumeProcess:
                await ResumeWorkflow(cmd, dataToExec, response.Output);

                break;

            case CommandType.clr:
                await ExecuteClrCommand(cmd, dataToExec, response.Output);

                break;

            case CommandType.xml:
                await ExecuteXmlCommand(cmd, dataToExec, response);

                break;

            default:
                throw new RequestModelException($"Invalid command type '{cmd.type}'");
            }
        }
Beispiel #3
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            var swi = new StartWorkflowInfo
            {
                DataSource = cmd.CurrentSource,
                Schema     = cmd.CurrentSchema,
                Model      = cmd.CurrentModel,
                ModelId    = dataToExec.Get <Int64>("Id"),
                ActionBase = cmd.ActionBase
            };

            if (swi.ModelId == 0)
            {
                throw new RequestModelException("Id must be specified to 'startProcess' command");
            }
            if (!String.IsNullOrEmpty(cmd.file))
            {
                swi.Source = $"file:{cmd.file}";
            }
            swi.Comment = dataToExec.Get <String>("Comment");
            swi.UserId  = dataToExec.Get <Int64>("UserId");
            if (swi.Source == null)
            {
                throw new RequestModelException($"File or clrType must be specified to 'startProcess' command");
            }
            WorkflowResult wr = await _workflowEngine.StartWorkflow(swi);

            return(new ServerCommandResult(JsonConvert.SerializeObject(wr, JsonHelpers.StandardSerializerSettings)));
        }
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (String.IsNullOrEmpty(cmd.procedure))
            {
                throw new RequestModelException("A procedure must be specified for sql-type command");
            }
            IDataModel model = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.CommandProcedure, dataToExec, cmd.commandTimeout);

            //_host.CheckTypes(cmd.Path, cmd.checkTypes, model);

            String invokeTarget = cmd.GetInvokeTarget();

            if (invokeTarget != null)
            {
                var clr = new ClrInvoker();
                clr.EnableThrow();
                clr.Invoke(invokeTarget, dataToExec);                 // after execute
            }
            var r = new ServerCommandResult();

            if (model == null)
            {
                r.Data = "{}";
            }
            else
            {
                r.Data = JsonConvert.SerializeObject(model.Root, JsonHelpers.StandardSerializerSettings);
            }
            return(r);
        }
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            var r    = new ServerCommandResult();
            var qm   = _messaging.CreateQueuedMessage();
            var prms = cmd.parameters;

            qm.Key      = prms.Get <String>("key");
            qm.Template = prms.Get <String>("template");
            qm.TargetId = Int64.Parse(cmd.Id);
            qm.Source   = "ServerCommand";
            qm.Parameters.Append(dataToExec);
            bool immediately = true;

            if (prms.HasProperty(nameof(immediately)))
            {
                immediately = prms.Get <Boolean>(nameof(immediately));
            }
            long msgId = await _messaging.QueueMessageAsync(qm, immediately);

            if (immediately)
            {
                await _messaging.SendMessageAsync(msgId);
            }
            r.Data = $"{{\"status\":\"success\", \"id\":{msgId}}}";
            return(r);
        }
 void ExecuteScriptCommand(RequestCommand cmd, ExpandoObject dataToExec, TextWriter writer)
 {
     if (String.IsNullOrEmpty(cmd.template))
     {
         throw new RequestModelException($"template must be specified for command '{cmd.command}'");
     }
     // SYNC!
 }
        async Task ExecuteSqlCommand(RequestCommand cmd, ExpandoObject dataToExec, TextWriter writer)
        {
            if (String.IsNullOrEmpty(cmd.procedure))
            {
                throw new RequestModelException("A procedure must be specified for sql-type command");
            }
            IDataModel model = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.CommandProcedure, dataToExec);

            WriteDataModel(model, writer);
        }
Beispiel #8
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (!String.IsNullOrEmpty(cmd.model))
            {
                var dmParams = dataToExec.Clone(new String[] { "url", "method", "headers", "body" });
                _dataModel = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.LoadProcedure, dmParams);
            }

            String url     = Resolve(dataToExec.Get <String>("url"), dataToExec)?.ToString();
            String method  = dataToExec.Get <String>("method")?.ToString()?.ToLowerInvariant();
            var    headers = dataToExec.Get <ExpandoObject>("headers");
            var    body    = dataToExec.Get <Object>("body");
            String bodyStr = null;

            HttpMethod mtd = HttpMethod.Get;

            if (method == "post")
            {
                mtd     = HttpMethod.Post;
                bodyStr = Resolve(body, dataToExec);
            }

            using (var msg = new HttpRequestMessage(mtd, url))
            {
                SetHeaders(msg, headers, dataToExec);
                if (bodyStr != null && mtd == HttpMethod.Post)
                {
                    msg.Content = new StringContent(bodyStr, Encoding.UTF8, "application/json");
                }
                using (var result = await _httpService.HttpClient.SendAsync(msg))
                {
                    if (result.IsSuccessStatusCode)
                    {
                        return(new ServerCommandResult(await result.Content?.ReadAsStringAsync())
                        {
                            ContentType = result.Content?.Headers?.ContentType?.MediaType
                        });
                    }
                    else
                    {
                        throw new RequestModelException($"CallApi Failed. statusCode:{result.StatusCode}, content:{await result.Content.ReadAsStringAsync()}");
                    }
                }
            }
        }
        async Task ExecuteSqlCommand(RequestCommand cmd, ExpandoObject dataToExec, TextWriter writer)
        {
            if (String.IsNullOrEmpty(cmd.procedure))
            {
                throw new RequestModelException("A procedure must be specified for sql-type command");
            }
            IDataModel model = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.CommandProcedure, dataToExec, cmd.commandTimeout);

            String invokeTarget = cmd.GetInvokeTarget();

            if (invokeTarget != null)
            {
                var clr = new ClrInvoker();
                clr.EnableThrow();
                clr.Invoke(invokeTarget, dataToExec);                 // after execute
            }
            WriteDataModel(model, writer);
        }
        async Task ExecuteClrCommand(RequestCommand cmd, ExpandoObject dataToExec, TextWriter writer)
        {
            if (String.IsNullOrEmpty(cmd.clrType))
            {
                throw new RequestModelException($"clrType must be specified for command '{cmd.command}'");
            }
            var    invoker = new ClrInvoker();
            Object result;

            if (cmd.async)
            {
                result = await invoker.InvokeAsync(cmd.clrType, dataToExec);
            }
            else
            {
                result = invoker.Invoke(cmd.clrType, dataToExec);
            }
            writer.Write(JsonConvert.SerializeObject(result, StandardSerializerSettings));
        }
Beispiel #11
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (String.IsNullOrEmpty(cmd.clrType))
            {
                throw new RequestModelException($"clrType must be specified for command '{cmd.command}'");
            }
            var    invoker = new ClrInvoker();
            Object result;

            if (cmd.async)
            {
                result = await invoker.InvokeAsync(cmd.clrType, dataToExec);
            }
            else
            {
                result = invoker.Invoke(cmd.clrType, dataToExec);
            }
            return(new ServerCommandResult(JsonConvert.SerializeObject(result, JsonHelpers.StandardSerializerSettings)));
        }
Beispiel #12
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            var rwi = new ResumeWorkflowInfo
            {
                Id = dataToExec.Get <Int64>("Id")
            };

            if (rwi.Id == 0)
            {
                throw new RequestModelException("InboxId must be specified");
            }
            rwi.UserId  = dataToExec.Get <Int64>("UserId");
            rwi.Answer  = dataToExec.Get <String>("Answer");
            rwi.Comment = dataToExec.Get <String>("Comment");
            rwi.Params  = dataToExec.Get <ExpandoObject>("Params");
            WorkflowResult wr = await _workflowEngine.ResumeWorkflow(rwi);

            return(new ServerCommandResult(JsonConvert.SerializeObject(wr, JsonHelpers.StandardSerializerSettings)));
        }
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            if (String.IsNullOrEmpty(cmd.file))
            {
                throw new RequestModelException($"'file' must be specified for command '{cmd.command}'");
            }
            String code = await _reader.ReadTextFileAsync(cmd.Path, cmd.file.AddExtension("js"));

            if (code == null)
            {
                throw new RequestModelException($"File not found '{cmd.file}'");
            }
            _engine.SetCurrentDirectory(cmd.Path);
            var retval = _engine.Execute(code, dataToExec, cmd.args);

            return(new ServerCommandResult()
            {
                Data = JsonConvert.SerializeObject(retval, JsonHelpers.StandardSerializerSettings)
            });
        }
Beispiel #14
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            List <String> xmlSchemaPathes = null;

            if (cmd.xmlSchemas != null)
            {
                xmlSchemaPathes = new List <String>();
                foreach (var schema in cmd.xmlSchemas)
                {
                    xmlSchemaPathes.Add(_host.ApplicationReader.MakeFullPath(cmd.Path, schema + ".xsd"));
                }
            }

            if (xmlSchemaPathes == null)
            {
                throw new RequestModelException("The xml-schemes are not specified");
            }

            foreach (var path in xmlSchemaPathes)
            {
                if (!_host.ApplicationReader.FileExists(path))
                {
                    throw new RequestModelException($"File not found '{path}'");
                }
            }

            IDataModel dm = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.XmlProcedure, dataToExec);

            var xmlCreator = new XmlCreator(xmlSchemaPathes, dm, "UTF-8")
            {
                Validate = cmd.validate
            };
            var bytes = xmlCreator.CreateXml();

            return(new ServerCommandResult(Encoding.UTF8.GetString(bytes))
            {
                ConentType = "text/xml"
            });
        }
        async Task ExecuteXmlCommand(RequestCommand cmd, ExpandoObject dataToExec, HttpResponseBase response)
        {
            List <String> xmlSchemaPathes = null;

            if (cmd.xmlSchemas != null)
            {
                xmlSchemaPathes = new List <String>();
                foreach (var schema in cmd.xmlSchemas)
                {
                    xmlSchemaPathes.Add(Host.MakeFullPath(false, cmd.Path, schema + ".xsd"));
                }
            }

            if (xmlSchemaPathes == null)
            {
                throw new RequestModelException("The xml-schemes are not specified");
            }

            foreach (var path in xmlSchemaPathes)
            {
                if (!System.IO.File.Exists(path))
                {
                    throw new RequestModelException($"File not found '{path}'");
                }
            }

            IDataModel dm = await DbContext.LoadModelAsync(cmd.CurrentSource, cmd.XmlProcedure, dataToExec);

            var xmlCreator = new XmlCreator(xmlSchemaPathes, dm, "UTF-8")
            {
                Validate = cmd.validate
            };
            var bytes = xmlCreator.CreateXml();

            response.ContentType = "text/xml";
            var chars = Encoding.UTF8.GetString(bytes).ToCharArray();

            response.Write(chars, 0, chars.Length);
        }
        public Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            // SYNC!
            if (String.IsNullOrEmpty(cmd.template))
            {
                throw new RequestModelException($"template must be specified for command '{cmd.command}'");
            }
            IDataModel dataModel = null;

            if (!String.IsNullOrEmpty(cmd.CurrentModel))
            {
                dataModel = _dbContext.LoadModel(cmd.CurrentSource, cmd.LoadProcedure, dataToExec, cmd.commandTimeout);
            }
            var ssi = new ServerScriptInfo
            {
                DataModel = dataModel,
                Path      = cmd.Path,
                Template  = cmd.template,
                Parameter = cmd.commandProp
            };
            var resultJson = _scriptProcessor.RunScript(ssi, "invoke");
            var result     = JsonConvert.DeserializeObject <ExpandoObject>(resultJson?.ToString(), new ExpandoObjectConverter());
            var status     = result.Get <String>("status");

            switch (status)
            {
            case "save":
            {
                var dataToSave = result.Get <ExpandoObject>("data");
                var savedDm    = _dbContext.SaveModel(cmd.CurrentSource, cmd.UpdateProcedure, dataToSave, dataToExec);
                return(Task.FromResult(new ServerCommandResult("{}")));
            }

            case "error":
                throw new RequestModelException(result.Get <String>("message"));
            }
            return(Task.FromResult(new ServerCommandResult("{}")));
        }
Beispiel #17
0
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            String url     = Resolve(dataToExec.Get <String>("url"), dataToExec)?.ToString();
            String method  = dataToExec.Get <String>("method")?.ToString()?.ToLowerInvariant();
            var    headers = dataToExec.Get <ExpandoObject>("headers");
            var    body    = dataToExec.Get <Object>("body");
            String bodyStr = null;

            HttpMethod mtd = HttpMethod.Get;

            if (method == "post")
            {
                mtd     = HttpMethod.Post;
                bodyStr = ResolveBody(body, dataToExec);
            }

            using (var msg = new HttpRequestMessage(mtd, url))
            {
                SetHeaders(msg, headers, dataToExec);
                if (bodyStr != null && mtd == HttpMethod.Post)
                {
                    msg.Content = new StringContent(bodyStr, Encoding.UTF8, "application/json");
                }
                var result = await _httpClient.SendAsync(msg);

                if (result.IsSuccessStatusCode)
                {
                    return(new ServerCommandResult(await result.Content.ReadAsStringAsync())
                    {
                        ConentType = result.Content.Headers.ContentType.MediaType
                    });
                }
                else
                {
                    throw new RequestModelException($"CallApi Failed. statusCode:{result.StatusCode}, content:{await result.Content.ReadAsStringAsync()}");
                }
            }
        }
        async Task ResumeWorkflow(RequestCommand cmd, ExpandoObject dataToStart, TextWriter writer)
        {
            if (_workflowEngine == null)
            {
                throw new InvalidOperationException($"Service 'IWorkflowEngine' not registered");
            }
            var rwi = new ResumeWorkflowInfo
            {
                Id = dataToStart.Get <Int64>("Id")
            };

            if (rwi.Id == 0)
            {
                throw new RequestModelException("InboxId must be specified");
            }
            rwi.UserId  = dataToStart.Get <Int64>("UserId");
            rwi.Answer  = dataToStart.Get <String>("Answer");
            rwi.Comment = dataToStart.Get <String>("Comment");
            rwi.Params  = dataToStart.Get <ExpandoObject>("Params");
            WorkflowResult wr = await _workflowEngine.ResumeWorkflow(rwi);

            WriteJsonResult(writer, wr);
        }
        public async Task <ServerCommandResult> Execute(RequestCommand cmd, ExpandoObject dataToExec)
        {
            await ProcessDbEvents(_dbContext, cmd.CurrentSource, _host.IsAdminMode);

            return(new ServerCommandResult());
        }
        async Task ExecuteSqlCommand(RequestCommand cmd, ExpandoObject dataToExec, TextWriter writer)
        {
            IDataModel model = await _dbContext.LoadModelAsync(cmd.CurrentSource, cmd.CommandProcedure, dataToExec);

            WriteDataModel(model, writer);
        }