Ejemplo n.º 1
0
        public void ProcessMessage(IHandlerContext <EMailSentEvent> context)
        {
            if (!context.TransportMessage.IsHandledHere())
            {
                return;
            }

            OrderProcess orderProcess;

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                orderProcess = _repository.Get(new Guid(context.TransportMessage.CorrelationId));

                if (orderProcess == null)
                {
                    throw new ApplicationException(
                              string.Format("Could not find an order process with correlation id '{0}'.",
                                            context.TransportMessage.CorrelationId));
                }

                var orderProcessStatus = new OrderProcessStatus("EMail Sent");

                orderProcess.AddStatus(orderProcessStatus);

                _repository.AddStatus(orderProcessStatus, orderProcess.Id);
            }

            context.Send(new CompleteOrderProcessCommand
            {
                OrderProcessId = orderProcess.Id
            }, c => c.Local());
        }
Ejemplo n.º 2
0
        public void ProcessMessage(IHandlerContext <AcceptOrderProcessCommand> context)
        {
            OrderProcess orderProcess;

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                var stream = _eventStore.Get(context.Message.OrderProcessId);

                if (stream.IsEmpty)
                {
                    return;
                }

                orderProcess = new OrderProcess(context.Message.OrderProcessId);
                stream.Apply(orderProcess);

                stream.AddEvent(orderProcess.ChangeStatus("Order Accepted"));

                _eventStore.SaveEventStream(stream);
            }

            context.Send(orderProcess.CreateOrderCommand(), c => c.WithCorrelationId(orderProcess.Id.ToString()));

            context.Publish(new OrderProcessAcceptedEvent
            {
                OrderProcessId = orderProcess.Id
            });
        }
Ejemplo n.º 3
0
        public void ProcessMessage(IHandlerContext <InvoiceCreatedEvent> context)
        {
            if (!context.TransportMessage.IsHandledHere())
            {
                return;
            }

            var orderProcessId = new Guid(context.TransportMessage.CorrelationId);

            var orderProcess = new OrderProcess(orderProcessId);

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                var stream = _eventStore.Get(orderProcessId);

                if (stream.IsEmpty)
                {
                    throw new ApplicationException(
                              string.Format("Could not find an order process with correlation id '{0}'.",
                                            context.TransportMessage.CorrelationId));
                }

                stream.Apply(orderProcess);

                stream.AddEvent(orderProcess.ChangeStatus("Invoice Created"));
                stream.AddEvent(orderProcess.AssignInvoiceId(context.Message.InvoiceId));

                _eventStore.Save(stream);
            }

            context.Send(orderProcess.SendEMailCommand());
        }
        public void ProcessMessage(IHandlerContext <RegisterEndpointCommand> context)
        {
            var message = context.Message;

            if (string.IsNullOrEmpty(message.EndpointName)
                ||
                string.IsNullOrEmpty(message.MachineName)
                ||
                string.IsNullOrEmpty(message.BaseDirectory))
            {
                return;
            }

            using (_databaseContextFactory.Create())
            {
                _endpointQuery.Register(
                    message.EndpointName,
                    message.MachineName,
                    message.BaseDirectory,
                    message.EntryAssemblyQualifiedName,
                    message.InboxWorkQueueUri,
                    message.ControlInboxWorkQueueUri);
            }

            if (!string.IsNullOrEmpty(message.InboxWorkQueueUri))
            {
                context.Send(new AddQueueCommand
                {
                    QueueUri = message.InboxWorkQueueUri
                }, c => c.Local());
            }
        }
        public void ProcessMessage(IHandlerContext <EMailSentEvent> context)
        {
            if (!context.TransportMessage.IsHandledHere())
            {
                return;
            }

            var orderProcessId = new Guid(context.TransportMessage.CorrelationId);

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                var stream = _eventStore.Get(orderProcessId);

                if (stream.IsEmpty)
                {
                    throw new ApplicationException(
                              string.Format("Could not find an order process with correlation id '{0}'.",
                                            context.TransportMessage.CorrelationId));
                }

                var orderProcess = new OrderProcess(orderProcessId);
                stream.Apply(orderProcess);

                stream.AddEvent(orderProcess.ChangeStatus("Dispatched-EMail Sent"));

                _eventStore.SaveEventStream(stream);
            }

            context.Send(new CompleteOrderProcessCommand
            {
                OrderProcessId = orderProcessId
            }, c => c.Local());
        }
Ejemplo n.º 6
0
        public void ProcessMessage(IHandlerContext <RegisterMemberCommand> context)
        {
            Console.WriteLine($"[MEMBER REGISTRATION REQUESTED]: username = '******'");

            context.Send(new MemberRegisteredEvent
            {
                UserName = context.Message.UserName
            }, c => c.Reply());
        }
Ejemplo n.º 7
0
        public void ProcessMessage(IHandlerContext <UserRegisteredEvent> context)
        {
            if (!context.Message.RegisteredBy.Equals("system", StringComparison.InvariantCultureIgnoreCase))
            {
                return;
            }

            context.Send(new ActivateUserCommand {
                Id = context.Message.Id
            }, c => c.Local());
        }
Ejemplo n.º 8
0
        public void ProcessMessage(IHandlerContext <Message> context)
        {
            if (!Reply)
            {
                return;
            }

            context.Send(new Message {
                Body = BodyGenerator.GetNext()
            }, c => c.Reply());
        }
Ejemplo n.º 9
0
        public void ProcessMessage(IHandlerContext <RegisterOrderProcessCommand> context)
        {
            var message = context.Message;

            var orderProcess = new OrderProcess
            {
                OrderId         = null,
                InvoiceId       = null,
                CustomerName    = message.CustomerName,
                CustomerEMail   = message.CustomerEMail,
                TargetSystem    = message.TargetSystem,
                TargetSystemUri = message.TargetSystemUri
            };

            orderProcess.GenerateOrderNumber();

            foreach (var quotedProduct in message.QuotedProducts)
            {
                orderProcess.AddItem(new OrderProcessItem(quotedProduct.ProductId, quotedProduct.Description,
                                                          quotedProduct.Price));
            }

            var status = new OrderProcessStatus("Cooling Off");

            orderProcess.AddStatus(status);

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                _repository.Add(orderProcess);
            }

            context.Publish(new OrderProcessRegisteredEvent
            {
                OrderProcessId  = orderProcess.Id,
                QuotedProducts  = message.QuotedProducts,
                CustomerName    = message.CustomerName,
                CustomerEMail   = message.CustomerEMail,
                OrderNumber     = orderProcess.OrderNumber,
                OrderDate       = orderProcess.DateRegistered,
                OrderTotal      = orderProcess.Total(),
                Status          = status.Status,
                StatusDate      = status.StatusDate,
                TargetSystem    = message.TargetSystem,
                TargetSystemUri = message.TargetSystemUri
            });

            context.Send(new AcceptOrderProcessCommand
            {
                OrderProcessId = orderProcess.Id
            }, c => c.Defer(DateTime.Now.AddSeconds(10)).Local());
        }
Ejemplo n.º 10
0
        public void ProcessMessage(IHandlerContext <RegisterOrderProcessCommand> context)
        {
            var message = context.Message;

            var orderProcess = new OrderProcess();

            var stream = _eventStore.CreateEventStream(orderProcess.Id);

            var initialized = orderProcess.Initialize();

            stream.AddEvent(initialized);
            stream.AddEvent(orderProcess.AssignCustomer(message.CustomerName, message.CustomerEMail));
            stream.AddEvent(orderProcess.AssignTargetSystem(message.TargetSystem, message.TargetSystemUri));

            var status = orderProcess.ChangeStatus("Cooling Off");

            stream.AddEvent(status);

            foreach (var quotedProduct in message.QuotedProducts)
            {
                stream.AddEvent(orderProcess.AddItem(quotedProduct.ProductId, quotedProduct.Description,
                                                     quotedProduct.Price));
            }

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                _eventStore.Save(stream);
            }

            context.Publish(new OrderProcessRegisteredEvent
            {
                OrderProcessId  = orderProcess.Id,
                QuotedProducts  = message.QuotedProducts,
                CustomerName    = message.CustomerName,
                CustomerEMail   = message.CustomerEMail,
                OrderNumber     = initialized.OrderNumber,
                OrderDate       = initialized.DateRegistered,
                OrderTotal      = orderProcess.Total,
                Status          = status.Status,
                StatusDate      = status.StatusDate,
                TargetSystem    = message.TargetSystem,
                TargetSystemUri = message.TargetSystemUri
            });

            context.Send(new AcceptOrderProcessCommand
            {
                OrderProcessId = orderProcess.Id
            }, c => c.Defer(DateTime.Now.AddSeconds(10)).Local());
        }
Ejemplo n.º 11
0
        private void SaveQueue(IHandlerContext <RegisterEndpointCommand> context, string uri, string processor,
                               string type)
        {
            if (string.IsNullOrEmpty(uri))
            {
                return;
            }

            context.Send(new SaveQueueCommand
            {
                QueueUri  = uri,
                Processor = processor,
                Type      = type
            }, c => c.Local());
        }
        public void ProcessMessage(
            IHandlerContext <RegisterMemberCommand> context)
        {
            Console.WriteLine();
            Console.WriteLine(
                "[MEMBER REGISTERED] : user name = '{0}'",
                context.Message.UserName);
            Console.WriteLine();

            context.Send(
                message: new MemberRegisteredEvent
            {
                UserName = context.Message.UserName
            },
                configure: c => c.Reply());
        }
Ejemplo n.º 13
0
        public void ProcessMessage(IHandlerContext <OrderCreatedEvent> context)
        {
            if (!context.TransportMessage.IsHandledHere())
            {
                return;
            }

            OrderProcess orderProcess;

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                orderProcess = _repository.Get(new Guid(context.TransportMessage.CorrelationId));

                if (orderProcess == null)
                {
                    throw new ApplicationException(
                              string.Format("Could not find an order process with correlation id '{0}'.",
                                            context.TransportMessage.CorrelationId));
                }

                var orderProcessStatus = new OrderProcessStatus("Order Created");

                orderProcess.AddStatus(orderProcessStatus);

                _repository.SaveOrderId(context.Message.OrderId, orderProcess.Id);
                _repository.AddStatus(orderProcessStatus, orderProcess.Id);
            }

            var createInvoiceCommand = new CreateInvoiceCommand
            {
                OrderId             = context.Message.OrderId,
                AccountContactName  = orderProcess.CustomerName,
                AccountContactEMail = orderProcess.CustomerEMail
            };

            foreach (var messageOrderItem in context.Message.Items)
            {
                createInvoiceCommand.Items.Add(new MessageInvoiceItem
                {
                    Description = messageOrderItem.Description,
                    Price       = messageOrderItem.Price
                });
            }

            context.Send(createInvoiceCommand, c => c.WithCorrelationId(orderProcess.Id.ToString()));
        }
        public void ProcessMessage(IHandlerContext <AcceptOrderProcessCommand> context)
        {
            OrderProcess orderProcess;

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                orderProcess = _repository.Get(context.Message.OrderProcessId);

                if (orderProcess == null)
                {
                    // Probably cancelled in the meantime.
                    // In a production system you would probably check this against some audit mechanism.
                    return;
                }

                var orderProcessStatus = new OrderProcessStatus("Order Accepted");

                orderProcess.AddStatus(orderProcessStatus);

                _repository.AddStatus(orderProcessStatus, orderProcess.Id);
            }

            var createOrderCommand = new CreateOrderCommand
            {
                OrderNumber   = orderProcess.OrderNumber,
                OrderDate     = orderProcess.DateRegistered,
                CustomerName  = orderProcess.CustomerName,
                CustomerEMail = orderProcess.CustomerEMail
            };

            foreach (var orderProcessItem in orderProcess.OrderProcessItems)
            {
                createOrderCommand.Items.Add(new MessageOrderItem
                {
                    Description = orderProcessItem.Description,
                    Price       = orderProcessItem.Price
                });
            }

            context.Send(createOrderCommand, c => c.WithCorrelationId(orderProcess.Id.ToString()));

            context.Publish(new OrderProcessAcceptedEvent
            {
                OrderProcessId = orderProcess.Id
            });
        }
        public void ProcessMessage(IHandlerContext <RegisterUserCommand> context)
        {
            var message = context.Message;
            var id      = Guid.NewGuid();

            Registered registered;

            using (_databaseContextFactory.Create())
            {
                var key = User.Key(message.EMail);

                if (_keyStore.Contains(key))
                {
                    if (!message.EMail.Equals("shuttle", StringComparison.InvariantCultureIgnoreCase))
                    {
                        context.Send(new SendEMailCommand
                        {
                            Body       = string.Format("<p>Hello,</p><br/><p>Unfortunately e-mail '{0}' has been assigned before your user could be registered.  Please register again.</p><br/><p>Regards</p>", message.EMail),
                            Subject    = "User registration failure",
                            IsBodyHtml = true,
                            To         = message.EMail
                        });
                    }

                    return;
                }

                _keyStore.Add(id, key);

                var user   = new User(id);
                var stream = new EventStream(id);
                registered = user.Register(message.EMail, message.PasswordHash, message.RegisteredBy);

                stream.AddEvent(registered);

                _eventStore.SaveEventStream(stream);
            }

            context.Publish(new UserRegisteredEvent
            {
                Id             = id,
                EMail          = message.EMail,
                RegisteredBy   = message.RegisteredBy,
                DateRegistered = registered.DateRegistered
            });
        }
Ejemplo n.º 16
0
        public void ProcessMessage(IHandlerContext <ExecuteTestCommand> context)
        {
            if (string.IsNullOrEmpty(context.TransportMessage.SenderInboxWorkQueueUri))
            {
                return;
            }

            var message = context.Message;

            if (!Enum.TryParse(message.LogLevel, true, out ContextLogLevel logLevel))
            {
                logLevel = ContextLogLevel.None;
            }

            using (_databaseContextFactory.Create())
            {
                var test = _testRepository.Get(message.Id);

                var executionContext = _executionService
                                       .Execute(message.Id, test.ArgumentValues(), new ContextLogger(logLevel));

                var response = new TestExecutedEvent
                {
                    Id        = test.Id,
                    FormulaId = test.FormulaId,
                    Log       = executionContext.Logger.ToString()
                };

                if (!executionContext.HasException)
                {
                    response.Exception      = executionContext.Exception.Message;
                    response.FormulaContext = new Messages.v1.TransferObjects.FormulaContext();

                    response.FormulaContext = GetFormulaContext(executionContext.RootFormulaContext);
                }
                else
                {
                    response.Result = executionContext.GetResult();
                }

                context.Send(response, c => c.Reply());
            }
        }
        public void ProcessMessage(IHandlerContext <InvoiceCreatedEvent> context)
        {
            if (!context.TransportMessage.IsHandledHere())
            {
                return;
            }

            OrderProcess orderProcess;

            using (_databaseContextFactory.Create(ProcessManagementData.ConnectionStringName))
            {
                orderProcess = _repository.Get(new Guid(context.TransportMessage.CorrelationId));

                if (orderProcess == null)
                {
                    throw new ApplicationException(
                              string.Format("Could not find an order process with correlation id '{0}'.",
                                            context.TransportMessage.CorrelationId));
                }

                var orderProcessStatus = new OrderProcessStatus("Invoice Created");

                orderProcess.AddStatus(orderProcessStatus);

                _repository.SaveInvoiceId(context.Message.InvoiceId, orderProcess.Id);
                _repository.AddStatus(orderProcessStatus, orderProcess.Id);
            }

            context.Send(new SendEMailCommand
            {
                RecipientEMail = orderProcess.CustomerEMail,
                HtmlBody       =
                    string.Format(
                        "Hello {0},<br/><br/>Your order number {1} has been dispatched.<br/><br/>Regards,<br/>The Shuttle Books Team",
                        orderProcess.CustomerName, orderProcess.OrderNumber)
            });
        }
Ejemplo n.º 18
0
        public void ProcessMessage(IHandlerContext <RegisterDocumentCommand> context)
        {
            Guard.AgainstNull(context, nameof(context));

            using (_databaseContextFactory.Create())
            {
                var document = _documentRepository.Get(context.Message.Id);

                var status = !SuspiciousExpression.IsMatch(document.FileName)
                    ? _malwareService.Register(document)
                    : ServiceStatus.Suspicious;

                switch (status)
                {
                case ServiceStatus.Cleared:
                {
                    document.Cleared();

                    _documentRepository.Save(document);

                    context.Publish(new DocumentProcessedEvent
                        {
                            Id          = document.Id,
                            ReferenceId = document.ReferenceId,
                            SystemName  = document.SystemName,
                            Suspicious  = false
                        });

                    break;
                }

                case ServiceStatus.Suspicious:
                {
                    document.Suspicious();

                    _documentRepository.Save(document);

                    context.Publish(new DocumentProcessedEvent
                        {
                            Id          = document.Id,
                            ReferenceId = document.ReferenceId,
                            SystemName  = document.SystemName,
                            Suspicious  = true
                        });

                    break;
                }

                case ServiceStatus.Registered:
                case ServiceStatus.Processing:
                {
                    _documentRepository.Save(document);

                    context.Send(new PollDocumentCommand
                        {
                            Id = document.Id
                        }, c => c.Defer(DateTime.Now.AddSeconds(5)).Local());

                    break;
                }
                }
            }
        }
Ejemplo n.º 19
0
        public void ProcessMessage(IHandlerContext <PollDocumentCommand> context)
        {
            Guard.AgainstNull(context, nameof(context));

            object commandMessage       = null;
            object eventMessage         = null;
            var    pollIntervalTimeSpan = TimeSpan.Zero;

            using (_databaseContextFactory.Create())
            {
                var document = _documentRepository.Get(context.Message.Id);

                var status = _malwareService.Poll(document);

                switch (status)
                {
                case ServiceStatus.Cleared:
                {
                    document.Cleared();

                    _documentRepository.Save(document);

                    eventMessage = new DocumentProcessedEvent
                    {
                        Id          = document.Id,
                        ReferenceId = document.ReferenceId,
                        SystemName  = document.SystemName,
                        Suspicious  = false
                    };

                    break;
                }

                case ServiceStatus.Suspicious:
                {
                    document.Suspicious();

                    _documentRepository.Save(document);

                    eventMessage = new DocumentProcessedEvent
                    {
                        Id          = document.Id,
                        ReferenceId = document.ReferenceId,
                        SystemName  = document.SystemName,
                        Suspicious  = true
                    };

                    break;
                }
                }

                switch (status)
                {
                case ServiceStatus.Processing:
                case ServiceStatus.Registered:
                {
                    _documentRepository.Save(document);

                    commandMessage = new PollDocumentCommand
                    {
                        Id = document.Id
                    };

                    pollIntervalTimeSpan = TimeSpan.FromSeconds(5);

                    if (document.ContainsProperty("PollIntervalTimeSpan"))
                    {
                        TimeSpan.TryParse(document.GetPropertyValue("PollIntervalTimeSpan"),
                                          out pollIntervalTimeSpan);
                    }

                    break;
                }
                }
            }

            if (commandMessage != null)
            {
                context.Send(commandMessage, c =>
                {
                    if (pollIntervalTimeSpan == TimeSpan.Zero)
                    {
                        return;
                    }

                    c.Defer(DateTime.Now.Add(pollIntervalTimeSpan)).Local();
                });
            }

            if (eventMessage != null)
            {
                context.Publish(eventMessage);
            }
        }
Ejemplo n.º 20
0
        public void ProcessMessage(IHandlerContext <SendEMailCommand> context)
        {
            var message = context.Message;

            var body = message.Body;

            if (!message.IsBodyHtml && body.Contains("\n"))
            {
                body = body.Replace("\r", "").Replace("\n", "\r\n");
            }

            var mail = new MailMessage(message.From, message.To.Replace(';', ','), message.Subject, body)
            {
                IsBodyHtml = message.IsBodyHtml,
                Priority   = message.GetMailPriority()
            };

            if (message.HasBodyEncoding())
            {
                mail.BodyEncoding = message.GetBodyEncoding();
            }

            if (message.HasSubjectEncoding())
            {
                mail.SubjectEncoding = message.GetSubjectEncoding();
            }

            if (!string.IsNullOrEmpty(message.CC))
            {
                mail.CC.Add(message.CC.Replace(';', ','));
            }

            if (!string.IsNullOrEmpty(message.Bcc))
            {
                mail.Bcc.Add(message.Bcc.Replace(';', ','));
            }

            var attachments = "(none)";

            if (message.Attachments.Count > 0)
            {
                var attachmentsBuilder = new StringBuilder();

                foreach (var file in message.Attachments)
                {
                    var path = Path.Combine(_configuration.AttachmentFolder, file);

                    if (!File.Exists(path))
                    {
                        throw new UnrecoverableHandlerException(string.Format("Cannot find attachment '{0}'.  The e-mail cannot be sent.",
                                                                              path));
                    }

                    mail.Attachments.Add(new Attachment(new FileStream(path, FileMode.Open), file));

                    attachmentsBuilder.AppendFormat("{0}{1}", attachmentsBuilder.Length > 0 ? ";" : string.Empty, file);
                }

                attachments = attachmentsBuilder.ToString();
            }

            _log.Trace(string.Format("[sending mail] : from = {0} / to = {1} / subject = {2} / attachments = {3}", message.From,
                                     message.To, message.Subject, attachments));

            using (var client = new SmtpClient())
            {
                client.Send(mail);
            }

            foreach (var file in message.Attachments)
            {
                context.Send(new RemoveAttachmentCommand
                {
                    File = file
                }, c => c.Local());
            }
        }
Ejemplo n.º 21
0
 public void Send(ISocketMessage message, Guid?id = null)
 {
     workSocket.Send(message, SocketId, id);
 }