public IActionResult GetAction(Guid actionGuid)
        {
            if (actionGuid == Guid.Empty)
            {
                return(Problem("Empty GUID is invalid."));
            }

            _logger.LogInformation("Enter GetActions.");

            PreingestAction action = null;

            try
            {
                using (var context = new PreIngestStatusContext())
                {
                    action = context.PreingestActionCollection.Find(actionGuid);
                    if (action != null && action.StatisticsSummary != null)
                    {
                        action.Status = context.ActionStateCollection.Where(item => item.ProcessId == action.ProcessId).ToList();
                        action.Status.ToList().ForEach(item => item.Messages = context.ActionStateMessageCollection.Where(m => m.StatusId == item.StatusId).ToList());
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An exception was thrown : {0}, '{1}'.", e.Message, e.StackTrace);
                return(ValidationProblem(String.Format("An exception was thrown : {0}, '{1}'.", e.Message, e.StackTrace)));
            }
            finally { }

            _logger.LogInformation("Exit GetActions.");

            if (action == null)
            {
                return(NotFound(String.Format("Action not found with ID '{0}'", actionGuid)));
            }

            return(new JsonResult(new
            {
                action.Creation,
                action.Description,
                SessionId = action.FolderSessionId,
                action.Name,
                ActionId = action.ProcessId,
                ResultFiles = String.IsNullOrEmpty(action.ResultFiles) ? new string[] { } : action.ResultFiles.Split(";").ToArray(),
                action.ActionStatus,
                Summary = String.IsNullOrEmpty(action.StatisticsSummary) ? new object { } : JsonConvert.DeserializeObject <PreingestStatisticsSummary>(action.StatisticsSummary),
                States = action.Status.Count == 0 ? new object[] { } : action.Status.Select(item => new
                {
                    item.Name,
                    item.StatusId,
                    item.Creation,
                    Messages = (item.Messages.Count == 0) ? new object[] { } : item.Messages.Select(m => new { m.MessageId, m.Creation, m.Description }).ToArray()
                }).ToArray()
            }));
        }
        public IActionResult AddProcessAction(Guid folderSessionGuid, [FromBody] BodyNewAction data)
        {
            if (folderSessionGuid == Guid.Empty)
            {
                return(Problem("Empty GUID is invalid."));
            }
            if (data == null)
            {
                return(Problem("Input data is required"));
            }
            if (String.IsNullOrEmpty(data.Name))
            {
                return(Problem("Name is required"));
            }
            if (String.IsNullOrEmpty(data.Description))
            {
                return(Problem("Description is required"));
            }

            //if(String.IsNullOrEmpty(data.Result))
            //return Problem("Result filename is required.");

            _logger.LogInformation("Enter AddProcessAction.");

            var processId = Guid.NewGuid();
            var session   = new PreingestAction
            {
                ProcessId       = processId,
                FolderSessionId = folderSessionGuid,
                Description     = data.Description,
                Name            = data.Name,
                Creation        = DateTimeOffset.Now,
                ResultFiles     = data.Result
            };

            using (var context = new PreIngestStatusContext())
            {
                try
                {
                    context.Add <PreingestAction>(session);
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "An exception was thrown : {0}, '{1}'.", e.Message, e.StackTrace);
                    return(ValidationProblem(String.Format("An exception was thrown : {0}, '{1}'.", e.Message, e.StackTrace)));
                }
                finally
                {
                    _logger.LogInformation("Exit AddProcessAction.");
                }
            }

            return(new JsonResult(session));
        }
        public IActionResult UpdateProcessAction(Guid actionGuid, [FromBody] BodyUpdate data)
        {
            if (actionGuid == Guid.Empty)
            {
                return(Problem("Empty GUID is invalid."));
            }
            if (data == null)
            {
                return(Problem("Input data is required"));
            }
            if (String.IsNullOrEmpty(data.Result))
            {
                return(Problem("Result of the action (success/error/failed) is required"));
            }
            if (String.IsNullOrEmpty(data.Summary))
            {
                return(Problem("Summary (accepted/rejected/processed) is required"));
            }

            _logger.LogInformation("Enter UpdateProcessAction.");

            PreingestAction currentAction = null;

            using (var context = new PreIngestStatusContext())
            {
                try
                {
                    currentAction = context.Find <PreingestAction>(actionGuid);
                    if (currentAction != null)
                    {
                        currentAction.ActionStatus      = data.Result;
                        currentAction.StatisticsSummary = data.Summary;
                    }
                    context.SaveChanges();
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "An exception was thrown : {0}, '{1}'.", e.Message, e.StackTrace);
                    return(ValidationProblem(String.Format("An exception was thrown : {0}, '{1}'.", e.Message, e.StackTrace)));
                }
                finally
                {
                    _logger.LogInformation("Exit UpdateProcessAction.");
                }
            }

            if (currentAction == null)
            {
                return(NotFound());
            }

            return(new JsonResult(currentAction));
        }