Exemplo n.º 1
0
        /// <summary>
        /// Gets the messages.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        /// filter - Filter cannot be null
        /// or
        /// Direction - Direction filter cannot be empty
        /// </exception>
        /// <exception cref="Eu.EDelivery.AS4.Fe.BusinessException">No messages found</exception>
        public async Task <MessageResult <Message> > GetMessages(MessageFilter filter)
        {
            if (filter == null)
            {
                throw new ArgumentNullException(nameof(filter), @"Filter cannot be null");
            }
            if (filter.Direction == null)
            {
                throw new ArgumentNullException(nameof(filter.Direction), @"Direction filter cannot be empty");
            }

            IQueryable <InMessage>  inMessageQuery  = context.InMessages;
            IQueryable <OutMessage> outMessageQuery = context.OutMessages;

            var inMessages  = filter.Direction.Contains(Direction.Inbound) ? filter.ApplyFilter(inMessageQuery).ProjectTo <Message>(mapperConfig) : null;
            var outMessages = filter.Direction.Contains(Direction.Outbound) ? filter.ApplyFilter(outMessageQuery).ProjectTo <Message>(mapperConfig) : null;

            IQueryable <Message> result = null;

            if (inMessages != null && outMessages != null)
            {
                result = inMessages.Concat(outMessages);
            }
            else if (inMessages != null)
            {
                result = inMessages;
            }
            else if (outMessages != null)
            {
                result = outMessages;
            }
            if (result == null)
            {
                throw new BusinessException("No messages found");
            }

            var returnValue = await filter.ToResult(filter.ApplyStatusFilter(result).OrderByDescending(msg => msg.InsertionTime));

            UpdateHasExceptions(returnValue, await GetExceptionIds(returnValue));

            return(returnValue);
        }