Exemplo n.º 1
0
        public bool Send(
            EnumHelper.EntityTypeWrapper entityTypeWrapper,
            string dbConnectionString,
            int entityTypeId,
            int entityId,
            string entityExternalId,
            int databaseId,
            DateTime messageArrivedTime
            )
        {
            var result = false;

            var logged = false;

            messageArrivedTime = TimeZoneInfo.ConvertTimeToUtc(messageArrivedTime);

            try
            {
                var isSsoEnabled = _databaseManager.IsSsoEnabled(dbConnectionString);

                if (_databaseManager.IsPublishEnabled(dbConnectionString, entityTypeId))
                {
                    using (IMessageBus bus = _messagingFactory.CreateMessageBus())
                    {
                        // Create an event payload
                        switch (entityTypeWrapper)
                        {
                        case EnumHelper.EntityTypeWrapper.Recipe:
                        {
                            _logger.Debug("enter createEventUpdate");
                            var payload = _eventFactory.CreateUpdateEvent <RecipeUpdated, RecipeUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            _logger.Debug("exit createEventUpdate");

                            var isSetOrchestrationSentDate = _databaseManager.IsSetOrchestrationSentDate(dbConnectionString, entityId);

                            if (isSetOrchestrationSentDate || payload.ChangeType == Events.ChangeType.UPDATE)
                            {
                                result = Publish(bus, payload);

                                _logger.Debug("exit publish recipe");

                                if (result)
                                {
                                    _databaseManager.UpdateOrchestrationSentDate(dbConnectionString, entityId);
                                }
                            }
                            else
                            {
                                result = true;

                                _logger.Warn("RecipeUpdated message was not published, because OrchestrationSentDate is not set or ChangeType is Archive");
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.MealPeriod:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <MealPeriodUpdated, MealPeriodUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.Group:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <GroupUpdated, GroupUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.User:
                        {
                            if (isSsoEnabled)
                            {
                                IMessage messagePayload;

                                if (string.IsNullOrEmpty(entityExternalId))
                                {
                                    messagePayload = CommandFactory.CreateAccountCommand(dbConnectionString, entityId, databaseId);
                                }
                                else
                                {
                                    messagePayload = CommandFactory.UpdateAccountCommand(dbConnectionString, entityId, databaseId);
                                }

                                result = Send(bus, messagePayload);
                            }
                            else
                            {
                                _logger.Warn("SSO not enabled");
                                result = true;
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.UserActivated:
                        {
                            if (isSsoEnabled)
                            {
                                var userCommandAccountActivated = CommandFactory.ActivateAccountCommand(entityId, databaseId);
                                result = Send(bus, userCommandAccountActivated);
                            }
                            else
                            {
                                _logger.Warn("SSO not enabled");
                                result = true;
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.SendUserUpdatedEvent:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <UserUpdated, UserUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.SendUserUpdatedEventAndCommand:
                        {
                            var userCreatedEventPayload = _eventFactory.CreateUpdateEvent <UserUpdated, UserUpdatedBuilder>(dbConnectionString, entityId, databaseId);

                            result = Publish(bus, userCreatedEventPayload);
                            if (isSsoEnabled)
                            {
                                IMessage messagePayload;

                                if (string.IsNullOrEmpty(entityExternalId))
                                {
                                    messagePayload = CommandFactory.CreateAccountCommand(dbConnectionString, entityId, databaseId);
                                }
                                else
                                {
                                    messagePayload = CommandFactory.UpdateAccountCommand(dbConnectionString, entityId, databaseId);
                                }

                                result = result && Send(bus, messagePayload);
                            }
                            break;
                        }

                        case EnumHelper.EntityTypeWrapper.UserGroup:
                        {
                            var userIds = _databaseManager.GetUsersInGroup(dbConnectionString, entityId);
                            if (userIds != null && userIds.Any())
                            {
                                foreach (var userId in userIds)
                                {
                                    var payload = _eventFactory.CreateUpdateEvent <UserUpdated, UserUpdatedBuilder>(dbConnectionString, userId, databaseId);
                                    result = Publish(bus, payload);
                                    LogDatabase(dbConnectionString, entityTypeId, entityId, messageArrivedTime, result);
                                    logged = true;
                                }
                            }
                            else
                            {
                                result = true;
                                _logger.Warn($"No users for usergroupId {entityId}");
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.Menu:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <MenuUpdated, MenuUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.Ingredient:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <IngredientUpdated, IngredientUpdatedBuilder>(dbConnectionString, entityId, databaseId);

                            var isSetOrchestrationSentDate = _databaseManager.IsSetOrchestrationSentDate(dbConnectionString, entityId);

                            if (isSetOrchestrationSentDate || payload.ChangeType == Events.ChangeType.UPDATE)
                            {
                                result = Publish(bus, payload);

                                if (result)
                                {
                                    _databaseManager.UpdateOrchestrationSentDate(dbConnectionString, entityId);
                                }
                            }
                            else
                            {
                                result = true;

                                _logger.Warn("IngredientUpdated message was not published, because OrchestrationSentDate is not set or ChangeType is Archive");
                            }
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.SendSupplierUpdatedEvent:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <SupplierUpdated, SupplierUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.ProductSet:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <SetUpdated, SetUpdatedBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        case EnumHelper.EntityTypeWrapper.ProductNutrition:
                        {
                            var payload = _eventFactory.CreateUpdateEvent <RecepiNutritionUpdated, RecepiNutritionUpdateBuilder>(dbConnectionString, entityId, databaseId);
                            result = Publish(bus, payload);
                        }
                        break;

                        default:
                            throw new NotSupportedException();
                        }
                    }
                }

                if (!logged)
                {
                    _logger.Debug("enter LogDatabase");
                    LogDatabase(dbConnectionString,
                                entityTypeId,
                                entityId,
                                messageArrivedTime,
                                result);
                    _logger.Debug("exit LogDatabase");
                }
            }
            catch (Exception ex)
            {
                _logger.Fatal("StarChef MSMQService Orchestrate failed to send to Orchestration in Send.", ex);
            }
            _logger.Debug("Finish Send");
            return(result);
        }