コード例 #1
0
        public void DynamicFetchReturnsData()
        {
            List <dynamic> dynamicEntities = DynamicFetch.ReadFromFetchXml(_dynamicsCrmConnection, "contact", new List <string>()
            {
                "contactid"
            }, new Dictionary <string, string>(), 1, new PagingInformation());

            Assert.AreNotEqual(Guid.Empty, (Guid)dynamicEntities.Single().contactid);
        }
コード例 #2
0
ファイル: CopyNN.cs プロジェクト: kirkenskorshaer/crm
        private List <dynamic> GetOriginIntermediateEntities(string originEntityIdName, string targetIdName, Guid insertedEntityOriginId)
        {
            string originIntermediateEntityName = _databaseCopyNN.originIntermediateEntityName;

            List <string> originIntermediateEntityFields = new List <string>()
            {
                targetIdName,
            };

            Dictionary <string, string> originIntermediateKeyFields = new Dictionary <string, string>()
            {
                { originEntityIdName, insertedEntityOriginId.ToString() }
            };

            List <dynamic> originIntermediateEntities = DynamicFetch.ReadFromFetchXml(_dynamicsCrmConnection, originIntermediateEntityName, originIntermediateEntityFields, originIntermediateKeyFields, null, new PagingInformation());

            return(originIntermediateEntities);
        }
コード例 #3
0
ファイル: CopyNN.cs プロジェクト: kirkenskorshaer/crm
        private List <dynamic> GetInsertedEntities(string insertedEntityName, string insertedEntityIdName, string insertedEntityOriginIdName)
        {
            Guid?insertedEntityId = _databaseCopyNN.insertedEntityId;

            List <string> insertedFields = new List <string>()
            {
                insertedEntityIdName,
                insertedEntityOriginIdName,
            };

            Dictionary <string, string> insertedKeyFields = new Dictionary <string, string>();

            if (insertedEntityId.HasValue)
            {
                insertedKeyFields.Add(insertedEntityIdName, insertedEntityId.Value.ToString());
            }

            List <dynamic> insertedEntities = DynamicFetch.ReadFromFetchXml(_dynamicsCrmConnection, insertedEntityName, insertedFields, insertedKeyFields, null, new PagingInformation());

            return(insertedEntities);
        }
コード例 #4
0
        public override void ExecuteOption(OptionReport report)
        {
            string urlLoginName = _databaseExposeData.urlLoginName;
            string fetchXmlPath = _databaseExposeData.fetchXmlPath;
            string exposePath   = _databaseExposeData.exposePath;
            string exposeName   = _databaseExposeData.exposeName;

            DatabaseUrlLogin      login = DatabaseUrlLogin.GetUrlLogin(Connection, urlLoginName);
            DynamicsCrmConnection dynamicsCrmConnection = DynamicsCrmConnection.GetConnection(login.Url, login.Username, login.Password);

            string fullFetchXmlPath = Config.GetResourcePath(fetchXmlPath);
            string fullExposePath   = Config.GetResourcePath(exposePath);

            List <dynamic> dataList = DynamicFetch.ReadFromFetchXml(dynamicsCrmConnection, fullFetchXmlPath, new PagingInformation());

            JsonSerializerSettings settings = new JsonSerializerSettings()
            {
                NullValueHandling    = NullValueHandling.Ignore,
                StringEscapeHandling = StringEscapeHandling.EscapeNonAscii,
            };

            string json = JsonConvert.SerializeObject(dataList, Formatting.None, settings);

            json = json.Replace("\\r\\n", "\\n");

            if (Directory.Exists(fullExposePath) == false)
            {
                Directory.CreateDirectory(fullExposePath);
            }

            File.WriteAllText(fullExposePath + "/" + exposeName, json, Encoding.UTF8);

            report.Success = true;

            return;
        }
コード例 #5
0
        public override void ExecuteOption(OptionReport report)
        {
            string     urlLoginName      = _databaseSendTableFromMailrelay.urlLoginName;
            Guid?      contactid         = _databaseSendTableFromMailrelay.contactid;
            string     contactIdName     = _databaseSendTableFromMailrelay.contactidName;
            string     queryFindContacts = _databaseSendTableFromMailrelay.queryFindContacts;
            string     queryCreateTable  = _databaseSendTableFromMailrelay.queryCreateTable;
            string     subject           = _databaseSendTableFromMailrelay.subject;
            string     html                       = _databaseSendTableFromMailrelay.html;
            string     tablerow                   = _databaseSendTableFromMailrelay.tablerow;
            string     matchidname                = _databaseSendTableFromMailrelay.matchidname;
            string     limitOnDateName            = _databaseSendTableFromMailrelay.limitOnDateName;
            List <int> requireDataOnDaysFromToday = _databaseSendTableFromMailrelay.requireDataOnDaysFromToday;
            TimeSpan   sleepTimeOnFailiure        = _databaseSendTableFromMailrelay.sleepTimeOnFailiure;
            string     orderbyDescending          = _databaseSendTableFromMailrelay.orderbyDescending;
            string     orderby                    = _databaseSendTableFromMailrelay.orderby;
            string     headerDateFormat           = _databaseSendTableFromMailrelay.headerDateFormat;
            string     tableDateFormat            = _databaseSendTableFromMailrelay.tableDateFormat;

            int mailboxfromid   = _databaseSendTableFromMailrelay.mailboxfromid;
            int mailboxreplyid  = _databaseSendTableFromMailrelay.mailboxreplyid;
            int mailboxreportid = _databaseSendTableFromMailrelay.mailboxreportid;
            int packageid       = _databaseSendTableFromMailrelay.packageid;

            string smtpHost     = _databaseSendTableFromMailrelay.smtpHost;
            string fromEmail    = _databaseSendTableFromMailrelay.fromEmail;
            string toEmail      = _databaseSendTableFromMailrelay.toEmail;
            int    port         = _databaseSendTableFromMailrelay.port;
            string smtpUsername = _databaseSendTableFromMailrelay.smtpUsername;
            string smtpPassword = _databaseSendTableFromMailrelay.smtpPassword;

            DatabaseSendTableFromMailrelay.SendTypeEnum sendType = _databaseSendTableFromMailrelay.sendType;

            SetDynamicsCrmConnectionIfEmpty(urlLoginName);

            PagingInformation pagingInformation = new PagingInformation();

            XDocument xDocument = XDocument.Parse(queryFindContacts);

            XmlHelper.SetCount(xDocument, 1);

            /*
             * if (contactid.HasValue)
             * {
             *      XmlHelper.AddCondition(xDocument, contactIdName, "eq", contactid.ToString());
             * }
             */
            if (string.IsNullOrWhiteSpace(headerDateFormat))
            {
                headerDateFormat = "yyyy-MM-dd";
            }

            if (string.IsNullOrWhiteSpace(tableDateFormat))
            {
                tableDateFormat = "yyyy-MM-dd";
            }

            while (pagingInformation.FirstRun || pagingInformation.MoreRecords)
            {
                dynamic receiver = DynamicFetch.ReadFromFetchXml(_dynamicsCrmConnection, xDocument, pagingInformation).SingleOrDefault();

                if (receiver == null)
                {
                    report.Success = true;
                    return;
                }

                IDictionary <string, object> receiverDictionary = (IDictionary <string, object>)receiver;

                if (contactid.HasValue)
                {
                    if ((Guid)receiverDictionary[contactIdName] != contactid)
                    {
                        continue;
                    }
                }

                if (receiverDictionary.ContainsKey("emailaddress1") == false)
                {
                    continue;
                }

                report.Workload++;

                string fullname      = receiver.fullname;
                string emailaddress1 = receiver.emailaddress1;
                Guid   matchid       = receiver.matchid;

                if (string.IsNullOrWhiteSpace(toEmail) == false)
                {
                    emailaddress1 = toEmail;
                }

                XDocument tableDocument = XDocument.Parse(queryCreateTable);

                XmlHelper.AddCondition(tableDocument, matchidname, "eq", matchid.ToString());

                List <dynamic> tableRowList = DynamicFetch.ReadFromFetchXml(_dynamicsCrmConnection, tableDocument, new PagingInformation());

                StringBuilder rowBuilder = new StringBuilder();

                if (string.IsNullOrWhiteSpace(orderby) == false)
                {
                    tableRowList = tableRowList.OrderBy(row =>
                    {
                        IDictionary <string, object> rowDictionary = (IDictionary <string, object>)row;
                        object sortObject = null;
                        if (rowDictionary.ContainsKey(orderby))
                        {
                            sortObject = rowDictionary[orderby];
                        }
                        return(sortObject);
                    }).ToList();
                }
                if (string.IsNullOrWhiteSpace(orderbyDescending) == false)
                {
                    tableRowList = tableRowList.OrderByDescending(row =>
                    {
                        IDictionary <string, object> rowDictionary = (IDictionary <string, object>)row;
                        object sortObject = null;
                        if (rowDictionary.ContainsKey(orderbyDescending))
                        {
                            sortObject = rowDictionary[orderbyDescending];
                        }
                        return(sortObject);
                    }).ToList();
                }

                foreach (dynamic rowDynamic in tableRowList)
                {
                    IDictionary <string, object> rowDictionary = (IDictionary <string, object>)rowDynamic;

                    string row = InsertFilledFields(tablerow, rowDictionary, tableDateFormat);

                    rowBuilder.Append(row);
                }

                string htmlWithRows = html.Replace("{rows}", rowBuilder.ToString());

                receiver.RowCount = tableRowList.Count();
                DateTime today = DateTime.Now.Date;
                receiver.Today     = today;
                receiver.Yesterday = today.AddDays(-1);

                if (string.IsNullOrWhiteSpace(limitOnDateName) == false)
                {
                    if (requireDataOnDaysFromToday == null || requireDataOnDaysFromToday.Any() == false)
                    {
                        continue;
                    }

                    bool isMailAllowed = false;
                    foreach (int daysToAdd in requireDataOnDaysFromToday)
                    {
                        int rowCount = FindRowCountInPeriod(tableRowList, limitOnDateName, today.AddDays(daysToAdd), today.AddDays(daysToAdd + 1));

                        receiverDictionary["RowCountDay" + daysToAdd.ToString()] = rowCount;

                        if (rowCount != 0)
                        {
                            isMailAllowed = true;
                        }
                    }

                    if (isMailAllowed == false)
                    {
                        continue;
                    }
                }

                report.SubWorkload += receiver.RowCount;

                string htmlWithRowsAndFields = InsertFilledFields(htmlWithRows, receiverDictionary, headerDateFormat);

                bool mailSent = false;

                if (sendType == DatabaseSendTableFromMailrelay.SendTypeEnum.Api)
                {
                    mailSent = TrySendMail(htmlWithRowsAndFields, fullname, emailaddress1, subject, packageid, mailboxfromid, mailboxreplyid, mailboxreportid);
                }

                if (sendType == DatabaseSendTableFromMailrelay.SendTypeEnum.Smtp)
                {
                    mailSent = TrySendSmtpMail(htmlWithRowsAndFields, fullname, fromEmail, emailaddress1, subject, smtpHost, port, smtpUsername, smtpPassword);
                }

                if (mailSent == false)
                {
                    Log.Write(Connection, $"failed sending email to {emailaddress1} sleeping {sleepTimeOnFailiure}", typeof(SendTableFromMailrelay), DataLayer.MongoData.Config.LogLevelEnum.OptionError);
                    Thread.Sleep(sleepTimeOnFailiure);
                }

                report.TextBuilder.AppendLine($"Sent to {emailaddress1} - Rows {receiver.RowCount}");
            }

            report.Success = true;
        }