private Entity GetExcelPopulated(Entity entity, int column, CreateExcelDoc excell_app, ref int row, IEnumerable <Entity> elements, int countActivities, bool isParent, ref object[,] data)
        {
            int countOfCompletedFields = 0;

            foreach (string attributeName in ApplicationSetting.AttributesSchemaList)
            {
                if (entity.Attributes.Contains(attributeName) && entity.Attributes[attributeName] != null)
                {
                    countOfCompletedFields++;
                    if (ApplicationSetting.AttributesToDisplay.Contains(attributeName))
                    {
                        var    attributeObj   = entity.Attributes[attributeName];
                        string type           = attributeObj.GetType().Name;
                        string attributeValue = GetAttributeValue(entity, attributeName, attributeObj, type);
                        // excell_app.addData(row, column, attributeValue, "A" + row, "B" + row);
                        data[row, column] = attributeValue;
                    }
                }
                if (ApplicationSetting.AttributesToDisplay.Contains(attributeName))
                {
                    column++;
                }
            }
            #region # Activities Count#

            IEnumerable <Entity> activityGroupBy = null;

            if (elements != null && elements.Count() > 0)
            {
                activityGroupBy = elements.Where(cnt => cnt.Attributes.Contains("Activities.activitytypecode") && cnt.Attributes["Activities.activitytypecode"] != null);
            }

            int countOfActivities = activityGroupBy != null?activityGroupBy.Count() : countActivities;

            if (!isParent)
            {
                entity.Attributes["ActivitiesCount"] = countOfActivities;
            }

            entity.Attributes["CountOfCompletedFields"] = countOfCompletedFields;
            data[row, column]     = Convert.ToString(countOfActivities);
            data[row, column + 1] = Convert.ToString(countOfCompletedFields);
            // excell_app.addData(row, column, Convert.ToString(countOfActivities), "A" + row, "B" + row);
            // excell_app.addData(row, column + 1, Convert.ToString(countOfCompletedFields), "A" + row, "B" + row);
            row++;
            return(entity);

            #endregion # Activities Count#
        }
        private void GenerateHeader(CreateExcelDoc excell_app, ref object [,] data)
        {
            int counter = 0;

            for (counter = 0; counter < ApplicationSetting.AttributesToDisplayName.Count(); counter++)
            {
                data[0, counter] = ApplicationSetting.AttributesToDisplayName[counter];
                // excell_app.createHeaders(1, counter + 1, ApplicationSetting.AttributesToDisplayName[counter], "A1", "A1", 0, "", true, 10, "n");
            }

            data[0, counter]     = "Activities Count";
            data[0, counter + 1] = "Count of Fields entered";
            //excell_app.createHeaders(1, counter + 1, "Activities Count", "A1", "A1", 0, "", true, 10, "n");
            // excell_app.createHeaders(1, counter + 2, "Count of Fields entered", "A1", "A1", 0, "", true, 10, "n");
        }
        private void tsbGenerate_Click(object sender, EventArgs e)
        {
            #region # Varibale declaration #
            EntityCollection entityColl = null;
            CreateExcelDoc   excell_app = null;
            int  progressCounter        = 0;
            bool createJob = false;
            #endregion # Varibale declaration #

            if (txtFilePath.Text.Length == 0)
            {
                MessageBox.Show(this, "Please select file location.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (this.lvAttributes.CheckedItems == null || this.lvAttributes.CheckedItems.Count <= 0)
            {
                MessageBox.Show(this, "Please select Attributes.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.cmbDuplicateDetectionJobs.SelectedItem == null && ApplicationSetting.ExistingJobIds != null && ApplicationSetting.ExistingJobIds.Count > 0)
            {
                // MessageBox.Show(this, "Please select existing Job to run.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                DialogResult result = MessageBox.Show("Please select existing Job to run. If you want to run Job for all records click Yes, else click No button.", "Confirmation", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    createJob = true;
                }
                else if (result == DialogResult.No)
                {
                    return;
                }
            }
            else if (ApplicationSetting.ExistingJobIds.Count <= 0)
            {
                DialogResult result = MessageBox.Show("If you want to run Job for all records click Yes, else click No button.", "Confirmation", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    createJob = true;
                }
                else if (result == DialogResult.No)
                {
                    return;
                }
            }

            GetAttributesList();

            WorkAsync(new WorkAsyncInfo
            {
                Message       = string.Concat("Duplicate ", ApplicationSetting.SelectedEntity.DisplayName, " detection started......"),
                AsyncArgument = null,
                Work          = (bw, evt) =>
                {
                    #region # Bulk Detection #
                    Guid jobId          = Guid.Empty;
                    bool isJobCompleted = false;
                    if (createJob == true)
                    {
                        BulkDetectDuplicatesRequest request = new BulkDetectDuplicatesRequest()
                        {
                            JobName = string.Format("{0} {1} {2}", "Detect duplicate", ApplicationSetting.SelectedEntity.DisplayName, DateTime.Now),
                            Query   = new QueryExpression()
                            {
                                EntityName = ApplicationSetting.SelectedEntity.LogicalName,
                                ColumnSet  = new ColumnSet(true)
                            },

                            RecurrencePattern   = String.Empty,
                            RecurrenceStartTime = DateTime.Now,
                            ToRecipients        = new Guid[0],
                            CCRecipients        = new Guid[0]
                        };

                        BulkDetectDuplicatesResponse _response = (BulkDetectDuplicatesResponse)Service.Execute(request);
                        jobId = _response.JobId;
                        UpdateJobStatus(jobId);
                        isJobCompleted = IsJobCompleted(jobId);
                    }
                    else
                    {
                        this.Invoke(new Action(() => { jobId = new Guid(cmbDuplicateDetectionJobs.SelectedValue.ToString()); }));

                        isJobCompleted = true;
                    }

                    #endregion # Bulk Detection #

                    if (jobId != Guid.Empty && isJobCompleted)
                    {
                        ApplicationSetting.JobId.Add(jobId);

                        entityColl = GetDuplicateRecords(jobId);
                        ApplicationSetting.DuplicateCollection = new EntityCollection();
                        ApplicationSetting.DuplicateRecords    = new List <DuplicateRecords>();

                        if (entityColl != null && entityColl.Entities.Count > 0)
                        {
                            this.Invoke(new Action(() =>
                            {
                                btnMergeDuplicates.Enabled     = true;
                                rbCreatedOn.Enabled            = true;
                                rbModifiedOn.Enabled           = true;
                                rbNoOfActivities.Enabled       = true;
                                rbnNoOfCompletedFields.Enabled = true;
                            }));

                            excell_app = new CreateExcelDoc(txtFilePath.Text.Trim());

                            //GenerateHeader(excell_app);

                            bw.ReportProgress(100, string.Concat("Duplicate ", ApplicationSetting.SelectedEntity.DisplayName, " found (", entityColl.Entities.Count, ")"));

                            int row = 1;

                            bw.ReportProgress(0, "Retrieving duplicate records........");
                            int rowCount = 0;
                            IEnumerable <System.Linq.IGrouping <object, Entity> > entityCollGroupBy = entityColl.Entities.GroupBy(ent => ent.Attributes[ApplicationSetting.SelectedEntity.PrimaryIdAttribute]);
                            List <Guid> chidList = new List <Guid>();
                            foreach (IEnumerable <Entity> elements in entityCollGroupBy)
                            {
                                foreach (Entity entity in elements)
                                {
                                    bw.ReportProgress(progressCounter * 100 / entityColl.Entities.Count(), string.Concat("Generating duplicate report........"));
                                    if (entity != null)
                                    {
                                        if (!DoesItContain(ApplicationSetting.DuplicateRecords, entity.Id))
                                        {
                                            rowCount++;
                                            DuplicateRecords rec = new DuplicateRecords();
                                            rec.MasterId         = entity.Id;
                                            if (elements != null && elements.Count() > 0)
                                            {
                                                var activityGroupBy = elements.Where(cnt => cnt.Attributes.Contains("Activities.activitytypecode") && cnt.Attributes["Activities.activitytypecode"] != null);
                                                entity.Attributes["ActivitiesCount"] = activityGroupBy != null ? activityGroupBy.Count() : 0;
                                            }
                                            else
                                            {
                                                entity.Attributes["ActivitiesCount"] = 0;
                                            }
                                            rec.MasterEntity = entity;

                                            Entity duplicateTemp = new Entity(ApplicationSetting.SelectedEntity.LogicalName);
                                            duplicateTemp.Id     = entity.Id;
                                            var re = new RetrieveDuplicatesRequest
                                            {
                                                BusinessEntity     = duplicateTemp,
                                                MatchingEntityName = duplicateTemp.LogicalName,
                                                PagingInfo         = new PagingInfo()
                                                {
                                                    PageNumber = 1, Count = 500
                                                }
                                            };

                                            var response = (RetrieveDuplicatesResponse)Service.Execute(re);
                                            if (response != null && response.DuplicateCollection != null)
                                            {
                                                List <Entity> childRecords = new List <Entity>();
                                                rec.ChildIds = new List <Guid>();
                                                foreach (Entity duplicate in response.DuplicateCollection.Entities)
                                                {
                                                    if (!DoesItContain(ApplicationSetting.DuplicateRecords, duplicate.Id))
                                                    {
                                                        rowCount++;
                                                        rec.ChildIds.Add(duplicate.Id);
                                                        chidList.Add(duplicate.Id);
                                                        childRecords.Add(duplicate);
                                                    }
                                                }
                                                rec.ChildEntity = childRecords;
                                            }
                                            ApplicationSetting.DuplicateRecords.Add(rec);
                                        }
                                        progressCounter++;

                                        break;// Break every time...Row is repeating for every activity
                                    }
                                }
                            }

                            bw.ReportProgress(0, "Generating Duplicate report........");
                            progressCounter = 0;
                            List <DuplicateRecords> DuplicateRecords = ApplicationSetting.DuplicateRecords;
                            EntityCollection childs = new EntityCollection();
                            if (chidList != null && chidList.Count() > 0)
                            {
                                childs = GetRelatedDuplicateRecords(chidList.ToArray());
                            }

                            int columnCount = ApplicationSetting.AttributesToDisplayName.Count() + 2;
                            var data        = new object[rowCount + 1, columnCount];

                            GenerateHeader(excell_app, ref data);

                            for (int indexofRecord = 0; indexofRecord < DuplicateRecords.Count(); indexofRecord++)
                            {
                                bw.ReportProgress(progressCounter * 100 / DuplicateRecords.Count(), string.Concat("Generating Duplicate report........"));
                                ApplicationSetting.DuplicateRecords[indexofRecord].MasterEntity = GetExcelPopulated(DuplicateRecords[indexofRecord].MasterEntity, 0, excell_app, ref row, null, Convert.ToInt32(DuplicateRecords[indexofRecord].MasterEntity.Attributes["ActivitiesCount"]), true, ref data);

                                for (int indexOfChild = 0; indexOfChild < DuplicateRecords[indexofRecord].ChildEntity.Count(); indexOfChild++)
                                {
                                    if (DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild) != null)
                                    {
                                        var activities = childs != null && childs.Entities.Count > 0 ? (childs.Entities.Where(i => ((EntityReference)i.Attributes["regardingobjectid"]).Id == DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild).Id).Count()) : 0;
                                        ApplicationSetting.DuplicateRecords[indexofRecord].ChildEntity[indexOfChild] = GetExcelPopulated(DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild), 0, excell_app, ref row, null, activities, false, ref data);
                                    }
                                }
                                progressCounter++;
                            }

                            excell_app.WriteArray(rowCount + 1, columnCount, data);
                            bw.ReportProgress(100, "Duplicate detection completed........");

                            excell_app.worksheet.SaveAs(txtFilePath.Text);

                            this.Invoke(new Action(() => { MessageBox.Show(this, "Duplicate Detection Completed."); }));

                            this.Invoke(new Action(() =>
                            {
                                excell_app.app.DisplayAlerts  = true;
                                excell_app.app.Calculation    = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationAutomatic;
                                excell_app.app.ScreenUpdating = true;
                                excell_app.app.Visible        = true;
                            }));

                            excell_app.ReleaseObject();
                            excell_app = null;

                            this.Invoke(new Action(() => { txtFilePath.Text = ""; }));
                        }
                        else
                        {
                            this.Invoke(new Action(() => { MessageBox.Show(this, "No Duplicates found."); }));
                        }

                        cmbDuplicateDetectionJobs.DataSource = null;
                        // Bind the Jobs
                        GetExistingJobs();
                    }
                },
                PostWorkCallBack = evt =>
                {
                    if (evt.Error != null)
                    {
                        this.Invoke(new Action(() => { MessageBox.Show(this, "An error occured " + evt.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }));
                    }
                },
                ProgressChanged = evt => { SetWorkingMessage(string.Format("{0}%\r\n{1}", evt.ProgressPercentage, evt.UserState)); }
            });
        }