Ejemplo n.º 1
0
        private void PrepareRequestFillPatterns(NotifyRequest request)
        {
            if (!request.IsNeedRetrivePatterns)
            {
                return;
            }

            request.Patterns = new IPattern[request.SenderNames.Length];
            if (request.Patterns.Length == 0)
            {
                return;
            }

            var apProvider = ProviderResolver.GetEnsure <IActionPatternProvider>(request.NotifySource);

            for (var i = 0; i < request.SenderNames.Length; i++)
            {
                var senderName = request.SenderNames[i];
                var pattern    = (apProvider.GetPatternMethod != null ? apProvider.GetPatternMethod(request.NotifyAction, senderName, request) : null) ??
                                 apProvider.GetPattern(request.NotifyAction, senderName) ??
                                 apProvider.GetPattern(request.NotifyAction);

                if (pattern == null)
                {
                    throw new NotifyException(string.Format("For action \"{0}\" by sender \"{1}\" no one patterns getted.", request.NotifyAction.Name, senderName));
                }
                request.Patterns[i] = pattern;
            }
        }
Ejemplo n.º 2
0
        private void PrepareRequestFillSenders(NotifyRequest request)
        {
            if (!request.IsNeedRetriveSenders)
            {
                return;
            }

            var subscriptionSource = ProviderResolver.GetEnsure <ISubscriptionSource>(request.NotifySource);

            request.SenderNames = subscriptionSource.GetSubscriptionMethod(request.NotifyAction, request.Recipient) ?? new string[0];
        }
Ejemplo n.º 3
0
        private void PrepareRequestFillTags(NotifyRequest request)
        {
            if (!request.IsNeedRetriveTags)
            {
                return;
            }

            var patternProvider = ProviderResolver.GetEnsure <IPatternProvider>(request.NotifySource);

            foreach (var pattern in request.Patterns)
            {
                IPatternFormatter formatter;
                try
                {
                    formatter = patternProvider.GetFormatter(pattern) ?? new NullPatternFormatter();
                }
                catch (Exception exc)
                {
                    throw new NotifyException(string.Format("For pattern \"{0}\" formatter not instanced.", pattern), exc);
                }
                ITag[] tags;
                try
                {
                    tags = formatter.GetTags(pattern) ?? new ITag[0];
                }
                catch (Exception exc)
                {
                    throw new NotifyException(string.Format("Get tags from formatter of pattern \"{0}\" failed.", pattern), exc);
                }

                foreach (var tag in tags.Where(tag => !request.Arguments.Exists(tagValue => Equals(tagValue.Tag, tag)) && !request.RequaredTags.Exists(rtag => Equals(rtag, tag))))
                {
                    request.RequaredTags.Add(tag);
                }
            }
        }
Ejemplo n.º 4
0
        private SendResponse CreateNoticeMessageFromNotifyRequest(NotifyRequest request, string sender, out NoticeMessage noticeMessage)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var recipientProvider = ProviderResolver.GetEnsure <IRecipientProvider>(request.NotifySource);
            var recipient         = request.Recipient as IDirectRecipient;

            var addresses = recipient.Addresses;

            if (addresses == null || !addresses.Any())
            {
                addresses = recipientProvider.GetRecipientAddresses(request.Recipient as IDirectRecipient, sender, request.ObjectID);
                recipient = new DirectRecipient(request.Recipient.ID, request.Recipient.Name, addresses);
            }

            recipient     = recipientProvider.FilterRecipientAddresses(recipient);
            noticeMessage = request.CreateMessage(recipient);

            addresses = recipient.Addresses;
            if (addresses == null || !addresses.Any(a => !string.IsNullOrEmpty(a)))
            {
                //checking addresses
                return(new SendResponse(request.NotifyAction, sender, recipient, new NotifyException(string.Format("For recipient {0} by sender {1} no one addresses getted.", recipient, sender))));
            }

            if (request.IsNeedPatternFormatting)
            {
                var pattern = request.GetSenderPattern(sender);
                if (pattern == null)
                {
                    return(new SendResponse(request.NotifyAction, sender, recipient, new NotifyException(String.Format("For action \"{0}\" by sender \"{1}\" no one patterns getted.", request.NotifyAction, sender))));
                }

                noticeMessage.Pattern     = pattern;
                noticeMessage.ContentType = pattern.ContentType;

                if (request.RequaredTags.Count > 0)
                {
                    var dependencyProvider = ProviderResolver.Get <IDependencyProvider>(request.NotifySource);
                    if (dependencyProvider != null)
                    {
                        try
                        {
                            var values = dependencyProvider.GetDependencies(noticeMessage, request.ObjectID, request.RequaredTags.ToArray());
                            request.Arguments.AddRange(values ?? new ITagValue[0]);
                        }
                        catch (Exception exc)
                        {
                            return(new SendResponse(noticeMessage, sender, exc));
                        }
                    }
                }

                noticeMessage.AddArgument(request.Arguments.ToArray());
                var patternProvider = ProviderResolver.GetEnsure <IPatternProvider>(request.NotifySource);

                var formatter = patternProvider.GetFormatter(pattern) ?? new NullPatternFormatter();
                try
                {
                    formatter.FormatMessage(noticeMessage, noticeMessage.Arguments);

                    sysTagFormatter.FormatMessage(
                        noticeMessage, new[]
                    {
                        new TagValue(Context._SYS_RECIPIENT_ID, request.Recipient.ID),
                        new TagValue(Context._SYS_RECIPIENT_NAME, request.Recipient.Name),
                        new TagValue(Context._SYS_RECIPIENT_ADDRESS, addresses != null && addresses.Length > 0 ? addresses[0] : null)
                    }
                        );
                    //Do styling here
                    if (!string.IsNullOrEmpty(pattern.Styler))
                    {
                        //We need to run through styler before templating
                        StyleMessage(noticeMessage);
                    }
                }
                catch (Exception exc)
                {
                    return(new SendResponse(request.NotifyAction, sender, recipient, exc));
                }
            }
            return(null);
        }
Ejemplo n.º 5
0
        private void SendGroupNotify(NotifyRequest request, List <SendResponse> responces)
        {
            if (request.Recipient is IDirectRecipient)
            {
                var subscriptionSource = ProviderResolver.GetEnsure <ISubscriptionSource>(request.NotifySource);
                if (!request.IsNeedCheckSubscriptions || !subscriptionSource.IsUnsubscribe(request.Recipient as IDirectRecipient, request.NotifyAction, request.ObjectID))
                {
                    var directresponses = new List <SendResponse>(1);
                    try
                    {
                        directresponses = SendDirectNotify(request);
                    }
                    catch (Exception exc)
                    {
                        directresponses.Add(new SendResponse(request.NotifyAction, request.Recipient, exc));
                    }
                    responces.AddRange(directresponses);
                }
            }
            else
            {
                if (request.Recipient is IRecipientsGroup)
                {
                    var checkresp = CheckPreventInterceptors(request, InterceptorPlace.GroupSend, null);
                    if (checkresp != null)
                    {
                        responces.Add(checkresp);
                    }
                    else
                    {
                        var recipientProvider = ProviderResolver.GetEnsure <IRecipientProvider>(request.NotifySource);

                        try
                        {
                            var recipients = recipientProvider.GetGroupEntries(request.Recipient as IRecipientsGroup, request.ObjectID) ?? new IRecipient[0];
                            foreach (var recipient in recipients)
                            {
                                try
                                {
                                    var newRequest = request.Split(recipient);
                                    SendGroupNotify(newRequest, responces);
                                }
                                catch (Exception exc)
                                {
                                    responces.Add(new SendResponse(request.NotifyAction, request.Recipient, exc));
                                }
                            }
                        }
                        catch (Exception exc)
                        {
                            responces.Add(new SendResponse(request.NotifyAction, request.Recipient, exc)
                            {
                                Result = SendResult.IncorrectRecipient
                            });
                        }
                    }
                }
                else
                {
                    responces.Add(new SendResponse(request.NotifyAction, request.Recipient, null)
                    {
                        Result    = SendResult.IncorrectRecipient,
                        Exception = new NotifyException("recipient may be IRecipientsGroup or IDirectRecipient")
                    });
                }
            }
        }