private static void SendReport(DateTime now, TimeZoneInfo timeZone, ReportTemplate t)
        {
            var cron = new CronExpression(t.Cron)
            {
                TimeZone = timeZone
            };
            var date = cron.GetTimeAfter(now.AddTicks(-1));

            LogManager.GetLogger("ASC.Web.Projects.Reports").DebugFormat("Find auto report: {0} - {1}, now: {2}, date: {3}", t.Name, t.Cron, now, date);
            if (date != now)
            {
                return;
            }

            var user = CoreContext.UserManager.GetUsers(t.CreateBy);

            if (user.ID == Constants.LostUser.ID || user.Status != EmployeeStatus.Active)
            {
                return;
            }

            SecurityContext.AuthenticateMe(user.ID);

            Thread.CurrentThread.CurrentCulture   = user.GetCulture();
            Thread.CurrentThread.CurrentUICulture = user.GetCulture();

            var message = new NoticeMessage(user, HttpUtility.HtmlDecode(t.Name), "", "html");

            message.AddArgument(new TagValue(CommonTags.SendFrom, CoreContext.TenantManager.GetCurrentTenant().Name));
            message.AddArgument(new TagValue(CommonTags.Priority, 1));

            ReportState state;
            var         template = t;
            var         result   = Report.TryCreateReportFromTemplate(t, (s, u) =>
            {
                try
                {
                    if (string.IsNullOrEmpty(s.Exception))
                    {
                        template.SaveDocbuilderReport(s, u);
                    }

                    SendWhenGenerated(s, user);
                }
                catch (Exception e)
                {
                    logger.Error(e);
                }
            }, message, out state, true);

            if (!result)
            {
                SendWhenGenerated(state, user);
            }
        }
예제 #2
0
        public static void SendAutoReports(DateTime datetime)
        {
            try
            {
                var now = DateTime.UtcNow.Date.AddHours(DateTime.UtcNow.Hour);
                foreach (var t in new DaoFactory(Global.DbID, -1).GetReportDao().GetAutoTemplates())
                {
                    try
                    {
                        var tenant = CoreContext.TenantManager.GetTenant(t.Tenant);
                        if (tenant != null && tenant.Status == TenantStatus.Active && CoreContext.PaymentManager.GetTariff(tenant.TenantId).State < TariffState.NotPaid)
                        {
                            CoreContext.TenantManager.SetCurrentTenant(tenant);
                            var cron = new CronExpression(t.Cron)
                            {
                                TimeZone = CoreContext.TenantManager.GetCurrentTenant().TimeZone
                            };
                            var date = cron.GetTimeAfter(now.AddTicks(-1));

                            LogManager.GetLogger("ASC.Web.Projects.Reports").DebugFormat("Find auto report: {0} - {1}, now: {2}, date: {3}", t.Name, t.Cron, now, date);
                            if (date == now)
                            {
                                var user = CoreContext.UserManager.GetUsers(t.CreateBy);
                                if (user.ID != Constants.LostUser.ID && user.Status == EmployeeStatus.Active)
                                {
                                    SecurityContext.AuthenticateMe(user.ID);

                                    Thread.CurrentThread.CurrentCulture   = user.GetCulture();
                                    Thread.CurrentThread.CurrentUICulture = user.GetCulture();

                                    var result  = Report.CreateNewReport(t.ReportType, t.Filter).BuildReport(ReportViewType.EMail, t.Id);
                                    var message = new NoticeMessage(user, HttpUtility.HtmlDecode(t.Name), result, "html");
                                    message.AddArgument(new TagValue(CommonTags.SendFrom, CoreContext.TenantManager.GetCurrentTenant().Name));
                                    message.AddArgument(new TagValue(CommonTags.Priority, 1));
                                    LogManager.GetLogger("ASC.Web.Projects.Reports").DebugFormat("Send auto report: {0} to {1}, tenant: {2}", t.Name, user, CoreContext.TenantManager.GetCurrentTenant());
                                    WorkContext.NotifyContext.DispatchEngine.Dispatch(message, "email.sender");
                                }
                            }
                        }
                    }
                    catch (System.Security.SecurityException se)
                    {
                        LogManager.GetLogger("ASC.Web.Projects.Reports").Info("SendAutoReports", se);
                    }
                    catch (Exception ex)
                    {
                        LogManager.GetLogger("ASC.Web.Projects.Reports").ErrorFormat("TemplateId: {0}, Temaplate: {1}\r\n{2}", t.Id, t.Filter.ToXml(), ex);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.GetLogger("ASC.Web.Projects.Reports").Error("SendAutoReports", ex);
            }
        }
예제 #3
0
        private SendResponse CreateNoticeMessageFromNotifyRequest(NotifyRequest request, string sender, out NoticeMessage noticeMessage)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            var recipientProvider = request.NotifySource.GetRecipientsProvider();
            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))));
            }

            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;
            noticeMessage.AddArgument(request.Arguments.ToArray());
            var patternProvider = request.NotifySource.GetPatternProvider();

            var formatter = patternProvider.GetFormatter(pattern);

            try
            {
                if (formatter != null)
                {
                    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);
        }