Пример #1
0
        public Response MakeRequest(WorkflowType selection)
        {
            string section = "Add Order(s)";

            Console.Clear();
            Console.Title = $"{Utilities.ProgramTitle} - {section}";
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Green);
            Console.SetCursorPosition((Console.WindowWidth - section.Length) / 2, Console.CursorTop);
            Console.WriteLine(section.Color("White"));
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Green);

            var request = new Request
            {
                Type     = WorkflowType.Add,
                Date     = Utilities.GetDate("Order Date", true),
                Customer = Utilities.GetString("Company Name", new Regex(@"^[\w ,.]{2,20}$"), false).Trim().ToTitle(),
                Product  = Utilities.GetString("Product Name", new Regex(@"^[\w ,.]{2,20}$"), false).Trim().ToTitle(),
                State    = Utilities.GetString("State", new Regex(@"^[A-Za-z]{2}$"), false).ToUpper(),
                Area     = Utilities.GetNumber <decimal>("Area of Material in Sq. Ft. (minimum: 100)", new Regex(@"^\d{3,7}(\.\d+)?$"))
            };

            IWorkflow flow     = WorkflowFactory.Create(selection);
            Response  response = flow.Execute(request);

            return(response);
        }
Пример #2
0
        public static void ExecuteLoop(
            RequestMock request,
            TimeSpan timeout,
            IErrorHandler <SubRequestMock, RequestStepMock> defaultErrorHandler = null,
            IInterceptor <SubRequestMock, RequestStepMock> defaultInterceptor   = null,
            IErrorHandler <SubRequestMock, RequestStepMock> customErrorHandler  = null,
            IInterceptor <SubRequestMock, RequestStepMock> customInterceptor    = null)
        {
            var wf          = new RequestWorkflowMock();
            var requestPipe = WorkflowFactory.Create <RequestMock, RequestStepMock>(request, new TimeSpan(0, 0, 3), TimeoutLifetime.Step);
            var wf2         = new SubRequestWorkflowMock();

            requestPipe
            .InvokeStep(wf.ReadString, RequestStepMock.Step1)
            .InvokeStep(wf.Parse, RequestStepMock.Step2)
            .InvokeLoop <SubRequestMock, RequestStepMock>(wf.GetStrings, RequestStepMock.Step7, r =>
            {
                var pipe = WorkflowFactory.Create(
                    r, timeout, TimeoutLifetime.Workflow, defaultErrorHandler, defaultInterceptor);

                pipe.InvokeStep(wf2.ReadString, RequestStepMock.Step1, customErrorHandler, customInterceptor)
                .InvokeStep(wf2.Parse, RequestStepMock.Step2, customErrorHandler, customInterceptor)
                .InvokeStep(wf2.Mul, RequestStepMock.Step3, customErrorHandler, customInterceptor)
                .InvokeStep(wf2.Sleep, RequestStepMock.Step4, customErrorHandler, customInterceptor)
                .InvokeStep(wf2.AddValue, RequestStepMock.Step5, customErrorHandler, customInterceptor);
            })
            .InvokeStep(wf.Done, RequestStepMock.Done);
        }
Пример #3
0
        [TestCase("2/2/2020", 9)] // unknown order number
        public void OrderNotFoundReturnsNull(string date, int orderNumber)
        {
            IWorkflow workflow = WorkflowFactory.Create(WorkflowType.Find);

            var request = new Request
            {
                Type        = WorkflowType.Find,
                Date        = DateTime.Parse(date),
                OrderNumber = orderNumber
            };

            Response response = workflow.Execute(request);

            Assert.IsNull(response.Order);
            Assert.IsFalse(response.Success);
        }
Пример #4
0
        public void CanFindExistingOrder(string date, int orderNumber)
        {
            IWorkflow workflow = WorkflowFactory.Create(WorkflowType.Find);

            var request = new Request
            {
                Type        = WorkflowType.Find,
                Date        = DateTime.Parse(date),
                OrderNumber = orderNumber
            };

            Response response = workflow.Execute(request);

            Assert.IsNotNull(response.Order);
            Assert.IsTrue(response.Success);
            Assert.AreEqual(orderNumber, response.Order.OrderNumber);
        }
Пример #5
0
        public Response MakeRequest(WorkflowType selection)
        {
            string section = "Remove Order(s)";

            Console.Clear();
            Console.Title = $"{Utilities.ProgramTitle} - {section}";
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Red);
            Console.SetCursorPosition((Console.WindowWidth - section.Length) / 2, Console.CursorTop);
            Console.WriteLine(section.Color("White"));
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Red);

            var request = new Request
            {
                Type        = WorkflowType.Remove,
                Date        = Utilities.GetDate("Order Date", false),
                OrderNumber = Utilities.GetNumber <int>("Order Number", new Regex(@"^\d{1,5}$"))
            };

            IWorkflow flow     = WorkflowFactory.Create(WorkflowType.Find);
            Response  response = flow.Execute(request);

            if (response.Success)
            {
                Order removalTarget = response.Order;

                Utilities.PrintOrderDetails(removalTarget, WorkflowType.Remove);
                Console.WriteLine("Please confirm that you want to remove this order.");

                string polarOption = Utilities.GetString("(Y)es or (N)o",
                                                         new Regex(@"^Y(es)?$|^N(o)?$", RegexOptions.IgnoreCase), false).ToUpper();

                if (polarOption == "Y" || polarOption == "YES")
                {
                    flow     = WorkflowFactory.Create(selection);
                    response = flow.Execute(request);
                }
                else
                {
                    response.Success = false;
                    response.Message = $"Order #{removalTarget.OrderNumber} removal cancelled. Ending workflow...";
                    return(response);
                }
            }
            return(response);
        }
Пример #6
0
        public static void ExecuteChain(
            RequestMock request,
            TimeSpan timeout,
            TimeoutLifetime timeoutLifetime,
            IErrorHandler <RequestMock, RequestStepMock> defaultErrorHandler = null,
            IInterceptor <RequestMock, RequestStepMock> defaultInterceptor   = null,
            IErrorHandler <RequestMock, RequestStepMock> customErrorHandler  = null,
            IInterceptor <RequestMock, RequestStepMock> customInterceptor    = null)
        {
            var wf   = new RequestWorkflowMock();
            var pipe = WorkflowFactory.Create(request, timeout, timeoutLifetime, defaultErrorHandler, defaultInterceptor);

            pipe.InvokeStep(wf.ReadString, RequestStepMock.Step1, customErrorHandler, customInterceptor)
            .InvokeStep(wf.Parse, RequestStepMock.Step2, customErrorHandler, customInterceptor)
            .InvokeStep(wf.Mul, RequestStepMock.Step3, customErrorHandler, customInterceptor)
            .InvokeStep(wf.Sleep, RequestStepMock.Step4, customErrorHandler, customInterceptor)
            .InvokeStep(wf.Done, RequestStepMock.Done, customErrorHandler, customInterceptor);
        }
Пример #7
0
        [TestCase("9/9/2020", false, 0)] // unknown date
        public void CanDisplayOrderIfRequestDataCorrect(string date, bool expectedResult, int expectedOrderCount)
        {
            IWorkflow workflow = WorkflowFactory.Create(WorkflowType.Display);

            var request = new Request
            {
                Type = WorkflowType.Display,
                Date = DateTime.Parse(date)
            };

            Response response = workflow.Execute(request);

            Assert.AreEqual(expectedResult, response.Success);

            if (response.Success)
            {
                Assert.AreEqual(expectedOrderCount, response.Orders.Count);
            }
        }
Пример #8
0
        [TestCase("2/2/2020", "Facebook", "Dirt", "PA", 200, false)] // product not found
        public void CanAddOrderIfRequestDataCorrect(string date, string customer, string product, string state,
            decimal area, bool expectedResult)
        {
            IWorkflow workflow = WorkflowFactory.Create(WorkflowType.Add);

            var request = new Request
            {
                Type     = WorkflowType.Add,
                Date     = DateTime.Parse(date),
                Customer = customer,
                Product  = product,
                State    = state,
                Area     = area
            };

            Response response = workflow.Execute(request);

            Assert.AreEqual(expectedResult, response.Success);
        }
Пример #9
0
        [TestCase("1/1/2020", 99, false)] // order number not found
        public void CanRemoveOrderIfRequestDataCorrect(string date, int orderNumber, bool expectedResult)
        {
            IWorkflow workflow = WorkflowFactory.Create(WorkflowType.Find);

            var request = new Request
            {
                Type        = WorkflowType.Find,
                Date        = DateTime.Parse(date),
                OrderNumber = orderNumber
            };

            Response response = workflow.Execute(request);

            if (response.Success)
            {
                request.Type = WorkflowType.Remove;
                response     = workflow.Execute(request);

                Assert.AreEqual(expectedResult, response.Success);
            }
        }
Пример #10
0
        public Response MakeRequest(WorkflowType selection)
        {
            string section = "Display Order(s)";

            Console.Clear();
            Console.Title = $"{Utilities.ProgramTitle} - {section}";
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Yellow);
            Console.SetCursorPosition((Console.WindowWidth - section.Length) / 2, Console.CursorTop);
            Console.WriteLine(section.Color("White"));
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Yellow);

            var request = new Request
            {
                Type = WorkflowType.Display,
                Date = Utilities.GetDate("Order Date", false)
            };

            IWorkflow flow     = WorkflowFactory.Create(selection);
            Response  response = flow.Execute(request);

            return(response);
        }
Пример #11
0
        public Response MakeRequest(WorkflowType selection)
        {
            string section = "Edit Order(s)";

            Console.Clear();
            Console.Title = $"{Utilities.ProgramTitle} - {section}";
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Blue);
            Console.SetCursorPosition((Console.WindowWidth - section.Length) / 2, Console.CursorTop);
            Console.WriteLine(section.Color("White"));
            Utilities.GenerateSeparator('=', Console.WindowWidth, ConsoleColor.Blue);

            var request = new Request
            {
                Type        = WorkflowType.Find,
                Date        = Utilities.GetDate("Order Date", false),
                OrderNumber = Utilities.GetNumber <int>("Order Number", new Regex(@"^\d{1,5}$"))
            };

            IWorkflow flow     = WorkflowFactory.Create(request.Type);
            Response  response = flow.Execute(request);

            if (response.Success)
            {
                Order editTarget = response.Order;
                Utilities.PrintOrderDetails(editTarget, WorkflowType.Edit);

                string newCustomer = Utilities.GetString("New Company Name", new Regex(@"^$|^[\w ,.]{2,20}$"), false).Trim().ToTitle(); // ?? editTarget.Customer;
                string newProduct  = Utilities.GetString("New Product Name", new Regex(@"^$|^[\w ,.]{2,20}$"), false).Trim().ToTitle(); // ?? editTarget.Product.ToString();
                string newState    = Utilities.GetString("New State", new Regex(@"^$|^[A-Za-z]{2}$"), false).ToUpper();                 // ?? editTarget.StateTax.StateAbbreviation).ToUpper();
                string newArea     = Utilities.GetString("New Area of Material (sq. ft.)", new Regex(@"^$|^\d{3,7}(\.\d+)?$"), false);

                request = new Request
                {
                    Type        = WorkflowType.Edit,
                    Date        = request.Date,
                    OrderNumber = request.OrderNumber,
                    Customer    = string.IsNullOrEmpty(newCustomer) ? editTarget.Customer : newCustomer,
                    Product     = string.IsNullOrEmpty(newProduct) ? editTarget.Product.ProductType : newProduct,
                    State       = string.IsNullOrEmpty(newState) ? editTarget.StateTax.StateAbbreviation : newState,
                    Area        = string.IsNullOrEmpty(newArea) ? editTarget.Area : decimal.Parse(newArea)
                };

                flow     = WorkflowFactory.Create(WorkflowType.Find);
                response = flow.Execute(request);

                Order updatedSample = new Order()
                {
                    OrderNumber = request.OrderNumber,
                    Customer    = request.Customer,
                    Area        = request.Area,
                    Product     = new Product()
                    {
                        ProductType = request.Product
                    },
                    StateTax = new StateTax()
                    {
                        StateAbbreviation = request.State
                    },
                    Total = (response.Order.Product.CostPerSquareFoot
                             + response.Order.Product.LaborCostPerSquareFoot)
                            * request.Area
                            * (1 + (response.Order.StateTax.TaxRate / 100))
                };

                Utilities.PrintOrderDetails(updatedSample, WorkflowType.Edit);

                Console.WriteLine("\nPlease confirm to keep these changes.");

                string polarOption = Utilities.GetString("(Y)es or (N)o",
                                                         new Regex(@"^Y(es)?$|^N(o)?$", RegexOptions.IgnoreCase), false).ToUpper();

                if (polarOption == "Y" || polarOption == "YES")
                {
                    flow     = WorkflowFactory.Create(selection);
                    response = flow.Execute(request);
                }
                else
                {
                    response.Success = false;
                    response.Message = $"Order #{editTarget.OrderNumber} editing cancelled. Ending workflow...";
                    return(response);
                }
            }
            return(response);
        }
Пример #12
0
        public virtual object Execute(Type sourceType, dynamic data, dynamic lastResult)
        {
            var returnType      = sourceType;
            var fieldExpression = default(IFieldExpression);

            if (Request.Project != null)
            {
                var parameter = Expression.Parameter(sourceType);

                if (string.IsNullOrEmpty(Request.Project.Name))
                {
                    fieldExpression = WQL.TypeCreator
                                      .CreateProjection(parameter, (JArray)Request.Project.Args)
                                      .Compose();
                }
                else
                {
                    //WQL.TypeCreator.BuildExpression(sourceType, Request.Project);
                }
            }

            if (fieldExpression == default(IFieldExpression))
            {
                var projectionInterface = Request.Project.Type
                                          .GetInterfaces()
                                          .FirstOrDefault(o =>
                                                          o.IsGenericType &&
                                                          o.GetGenericTypeDefinition() == typeof(IProjection <,>)
                                                          );

                if (projectionInterface != null)
                {
                    returnType = projectionInterface.GenericTypeArguments
                                 .LastOrDefault();
                }

                Projection = Reflection.CreateProjection(sourceType, Request.Project.Name, new Type[] { sourceType, returnType }, Request.Project.Args);
            }
            else
            {
                var projection = (IWorQLessProjection)Reflection.CreateProjection(sourceType, Request.Project.Name, new Type[] { sourceType, returnType }, Request.Project.Args);
                projection.FieldExpression = fieldExpression;
                Projection = projection;
                returnType = fieldExpression.ReturnType;
            }

            var workflow = WorkflowFactory.Create(sourceType, returnType, this);

            var execute = workflow
                          .GetType()
                          .GetMethod(nameof(IWorkflow <object, object> .Execute));

            try
            {
                var result = execute
                             .Invoke(workflow, new object[] { data });

                return(result);
            }
            catch (Exception ex)
            {
                throw ex?.InnerException ?? ex;
            }
        }