Example #1
0
        public async Task <ActivityDTO> Activate(ActivityDO submittedActivity)
        {
            using (await _configureLock.Lock(submittedActivity.Id))
            {
                try
                {
                    using (var uow = ObjectFactory.GetInstance <IUnitOfWork>())
                    {
                        var existingAction = uow.PlanRepository.GetById <ActivityDO>(submittedActivity.Id);

                        if (existingAction == null)
                        {
                            throw new Exception("Action was not found");
                        }

                        if (existingAction.ActivationState == ActivationState.Activated)
                        {
                            return(Mapper.Map <ActivityDTO>(submittedActivity));
                        }

                        Logger.GetLogger().Info($"Before calling terminal activation of activity (Id - {submittedActivity.Id})");
                        var activatedActivityDTO = await CallTerminalActivityAsync <ActivityDTO>(uow, "activate", null, submittedActivity, Guid.Empty);

                        Logger.GetLogger().Info($"Call to terminal activation of activity (Id - {submittedActivity.Id}) completed");

                        var activatedActivityDo = Mapper.Map <ActivityDO>(activatedActivityDTO);
                        var storage             = _crateManager.GetStorage(activatedActivityDo);
                        var validationCrate     = storage.CrateContentsOfType <ValidationResultsCM>().FirstOrDefault();
                        if (validationCrate == null || !validationCrate.HasErrors)
                        {
                            existingAction.ActivationState = ActivationState.Activated;
                        }
                        UpdateActivityProperties(existingAction, activatedActivityDo);
                        uow.SaveChanges();
                        EventManager.ActionActivated(activatedActivityDo);
                        return(Mapper.Map <ActivityDTO>(activatedActivityDo));
                    }
                }
                catch (Exception e)
                {
                    ReportActivityInvocationError(submittedActivity, e.Message, null, submittedActivity.Id.ToString(), EventManager.TerminalActionActivationFailed);
                    throw;
                }
            }
        }