public Contact GetContact(decimal ID , bool Fetch)
 {
     Contact contact=new Contact(ID , this._owner);
     if(Fetch)
         contact.Fetch();
     return contact;
 }
        protected void SendNowButton_Click(object sender, System.EventArgs e)
        {
            int keyCount=_gr.SelectedPrimaryKeys.Count;

            if (keyCount==0)
                return;

            Report.ExportFormat exportFormat=(Report.ExportFormat )Enum.Parse(typeof(Report.ExportFormat ), this.ddlFormat.SelectedValue, true);
            Contact[] contacts=new Contact[_gr.SelectedPrimaryKeys.Count];

            for(int i=0;i<keyCount;i++)
            {
                string[] keys=(string[])_gr.SelectedPrimaryKeys[i];
                decimal contactId=decimal.Parse(keys[0]);
                contacts[i]=_user.ContactSystem.GetContact(contactId , true);
            }

            try
            {
                _reportProxy.Open();
                _reportProxy.Execute();
                Guid olapTaskGuid = Guid.NewGuid();
                bool isFromCache=false;

                DistributionManager.Instance.SendReport(_reportProxy, olapTaskGuid, contacts, exportFormat, DateTime.MaxValue, DateTime.Now, out isFromCache);
            }
            catch(Exception exc)
            {
                ShowException(exc);
            }
        }
        public void DeleteContact(Contact contact)
        {
            contact.Validate(true);

            if (BeforeDeleteContact != null)
                BeforeDeleteContact(contact, EventArgs.Empty);

            FI.Common.DataAccess.IContactsDA dacObj=DataAccessFactory.Instance.GetContactsDA();
            dacObj.DeleteContact(contact.Owner.ID , contact.ID);

            contact=null;
        }
        internal Distribution(User Owner , Report report , Contact contact, Report.ExportFormat format)
        {
            _owner=Owner;
            this.Report=report;
            this.Contact=contact;
            this.Format=format;

            FI.Common.DataAccess.IDistributionsDA dacObj=DataAccessFactory.Instance.GetDistributionsDA();
            _id=dacObj.InsertDistribution(_owner.ID, report.ID , contact.ID , report.GetTypeCode() , this.FrequencyType.ToString() , this.FrequencyValue.ToString(), (int)this.Format);

            _isProxy=false;
            _isDirty=false;
        }
        public void SendReport(Report report , Contact[] contacts , Report.ExportFormat Format, DateTime getCachedFrom, out bool isFromCache)
        {
            isFromCache=false;

            if(report.IsProxy)
                throw new Exception("Report cannot be Proxy");

            if(contacts.Length==0)
                return;

            string fileNamePattern=report.GetType().Name + "_" +  report.ID.ToString() + "_";
            string fileName=fileNamePattern + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + Format.ToString();
            string cacheLookupFileName=fileNamePattern + getCachedFrom.ToString("yyyyMMddHHmmss") + "." + Format.ToString();
            string filePath=null;
            string reportString=null;

            // lookup cached report
            string[] lookupPaths=Directory.GetFiles(FI.Common.AppConfig.TempDir, fileNamePattern + "*." + Format.ToString());
            if(lookupPaths!=null)
            {
                foreach(string path in lookupPaths)
                {
                    string file=Path.GetFileName(path);
                    if(file.Length==cacheLookupFileName.Length && file.CompareTo(cacheLookupFileName)>0)
                    {
                        filePath=FI.Common.AppConfig.TempDir+ @"\" + file;
                        isFromCache=true;
                        break;
                    }
                }
            }
            if(filePath==null)
            {
                filePath=FI.Common.AppConfig.TempDir+ @"\" + fileName;
                report.Export(filePath, Format);
            }

            foreach(Contact cnt in contacts)
            {
                if(Format==Report.ExportFormat.HTML && reportString==null)
                {
                    if(cnt.DistributionFormat==Contact.DistributionFormatEnum.MessageBody || cnt.DistributionFormat==Contact.DistributionFormatEnum.Body_And_Attachment)
                    {
                        StreamReader sr=new StreamReader(filePath, System.Text.Encoding.Unicode, true);
                        if(sr!=null)
                        {
                            reportString=sr.ReadToEnd();
                            sr.Close();
                        }
                    }
                }

                //send via email
                try
                {
                    if(cnt.IsProxy)
                        cnt.Fetch();

                    // message object
                    OpenSmtp.Mail.MailMessage msg=new OpenSmtp.Mail.MailMessage();
                    msg.From=new OpenSmtp.Mail.EmailAddress(FI.Common.AppConfig.SmtpSender);
                    msg.To.Add(new OpenSmtp.Mail.EmailAddress(cnt.EMail));
                    msg.Subject=report.Name + " (" + report.Description + ")";

                    // attachment if ordered or report is not html
                    if(cnt.DistributionFormat==Contact.DistributionFormatEnum.Attachment ||
                        cnt.DistributionFormat==Contact.DistributionFormatEnum.Body_And_Attachment ||
                        Format!=Report.ExportFormat.HTML)
                    {
                        OpenSmtp.Mail.Attachment att=new OpenSmtp.Mail.Attachment(filePath);
                        //att.Encoding=System.Web.Mail.MailEncoding.UUEncode;
                        msg.Attachments.Add(att);
                    }

                    // message body (if retport is html)
                    if(Format==Report.ExportFormat.HTML &&
                        (cnt.DistributionFormat==Contact.DistributionFormatEnum.MessageBody ||
                        cnt.DistributionFormat==Contact.DistributionFormatEnum.Body_And_Attachment))
                    {
                        msg.HtmlBody=reportString;
                    }

            //					msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "0"); //This is crucial. put 0 there
            //					msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout", 90);

                    OpenSmtp.Mail.SmtpConfig.LogToText=false;

                    OpenSmtp.Mail.Smtp smtp=new OpenSmtp.Mail.Smtp();
                    smtp.SendTimeout=600;
                    smtp.Host=FI.Common.AppConfig.SmtpServer;
                    if(FI.Common.AppConfig.SmtpUserName!=null && FI.Common.AppConfig.SmtpUserName!="")
                    {
                        smtp.Username=FI.Common.AppConfig.SmtpUserName;
                        smtp.Password=FI.Common.AppConfig.SmtpPassword;
                    }
                    smtp.SendMail(msg);
            //					System.Web.Mail.SmtpMail.SmtpServer=FI.Common.AppConfig.SmtpServer;
            //					System.Web.Mail.SmtpMail.Send(msg);
                }
                catch(Exception exc)
                {
                    // because real exception is inside:
                    while(exc.InnerException!=null)
                    {
                        exc=exc.InnerException;
                    }

                    Common.LogWriter.Instance.WriteEventLogEntry(exc);
                    throw exc;
                }

            }
        }
        public void Fetch()
        {
            this.Validate(true);

            FI.Common.DataAccess.IDistributionsDA dacObj=DataAccessFactory.Instance.GetDistributionsDA();
            System.Data.DataRow row=dacObj.ReadDistribution(this._owner.ID , this.ID).Rows[0];
            this._contact=_owner.ContactSystem.GetContact(int.Parse(row["ContactId"].ToString()),false);
            this._report=_owner.ReportSystem.GetReport(int.Parse(row["ReportId"].ToString()) , _owner.ReportSystem.GetReportType(int.Parse(row["ReportType"].ToString())) , false);
            this.FrequencyType=(Distribution.FrequencyTypeEnum)System.Enum.Parse(typeof(Distribution.FrequencyTypeEnum) , row["FrequencyType"].ToString());
            this.FrequencyValue=(Distribution.FrequencyValueEnum)System.Enum.Parse(typeof(Distribution.FrequencyValueEnum) , row["FrequencyValue"].ToString());
            this.Format=(Report.ExportFormat)((int)(byte)row["Format"]);

            this._isProxy=false;
            this._isDirty=false;
        }
 public void DeleteDistributions(Contact contact)
 {
     FI.Common.DataAccess.IDistributionsDA dacObj=DataAccessFactory.Instance.GetDistributionsDA();
     dacObj.DeleteDistributionsByContact(_owner.ID, contact.ID);
 }
 public Distribution NewDistribution(Report report, Contact contact, Report.ExportFormat format)
 {
     return new Distribution(this.Owner , report , contact, format);
 }