/// <summary>
        /// Updates the transport report.
        /// </summary>
        /// <param name="report">The report.</param>
        /// <param name="ee">The Selected N to N Relationship.</param>
        /// <param name="importedRecordsForEntity">The imported records for entity.</param>
        /// <param name="importFailuresForEntity">The import failures for entity.</param>
        /// <param name="entityImportStartDT">The entity import start dt.</param>
        public void updateTransportReport(NtoNTransportReport report, SelectedNtoNRelationship ee, int importedRecordsForEntity, int importFailuresForEntity, DateTime entityImportStartDT)
        {
            bool addNewLine = true;

            foreach (NtoNTransportReportLine reportLine in report.ReportLines)
            {
                if (reportLine.RelationshipSchemaName == ee.RelationshipSchemaName)
                {
                    reportLine.ImportedRecords = importedRecordsForEntity;
                    report.TotalImportedRecords += importedRecordsForEntity;
                    DateTime entityImportEndDT = DateTime.Now;
                    TimeSpan ts = entityImportEndDT - entityImportStartDT;
                    reportLine.ImportStartedAt = entityImportStartDT.ToString();
                    reportLine.ImportFinishedAt = entityImportEndDT.ToString();
                    reportLine.ImportedIn = ts.ToString().Substring(0, 10);
                    reportLine.ImportFailures = importFailuresForEntity;
                    addNewLine = false;
                    break;
                }
            }

            if (addNewLine)
            {
                NtoNTransportReportLine currentLine = new NtoNTransportReportLine();
                currentLine.RelationshipSchemaName = ee.RelationshipSchemaName;
                currentLine.ImportedRecords = importedRecordsForEntity;
                report.TotalImportedRecords += importedRecordsForEntity;
                DateTime entityImportEndDT = DateTime.Now;
                TimeSpan ts = entityImportEndDT - entityImportStartDT;
                currentLine.ImportStartedAt = entityImportStartDT.ToString();
                currentLine.ImportFinishedAt = entityImportEndDT.ToString();
                currentLine.ImportedIn = ts.ToString().Substring(0, 10);
                currentLine.ImportFailures = importFailuresForEntity;
                report.ReportLines.Add(currentLine);
            }
            WriteTransportReport(report, ReportFileName);
        }
        /// <summary>
        /// Alreadies the associated.
        /// </summary>
        /// <param name="osp">The Organization Service Proxy.</param>
        /// <param name="ee">The Selected N to N Relationship.</param>
        /// <param name="Record1">The record 1 Id.</param>
        /// <param name="Record2">The record 2 Id.</param>
        /// <returns>True or False if the two records are already associated</returns>
        public bool AlreadyAssociated(OrganizationServiceProxy osp, SelectedNtoNRelationship ee, Guid Record1, Guid Record2)
        {
            string fetchXML = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' count='1'>
                                      <entity name='{0}'>
                                        <attribute name='{1}' />
                                        <attribute name='{2}' />
                                        <order attribute='{1}' descending='false' />
                                        <filter type='and'>
                                           <condition attribute='{1}' operator='eq' uitype='account' value='{3}' />
                                           <condition attribute='{2}' operator='eq' uitype='contact' value='{4}' />
                                         </filter>
                                      </entity>
                                    </fetch> ", ee.IntersectEntityName, ee.Entity1IntersectAttribute, ee.Entity2IntersectAttribute, Record1, Record2);

            EntityCollection result = osp.RetrieveMultiple(new FetchExpression(fetchXML));
            if (result != null && result.Entities.Count > 0 && result.Entities[0] != null)
                return true;
            else
                return false;
        }
        private bool SaveProfile()
        {
            bool result = true;
            //Check that all fields are provided
            if (string.IsNullOrEmpty(textBoxTransportationProfileName.Text))
            {
                MessageBox.Show("Profile Name is mandatory!");
                return false;
            }

            //Check that the name of the profile is valid
            if (textBoxTransportationProfileName.Text.Contains(" ") ||
                    textBoxTransportationProfileName.Text.Contains("\\") ||
                    textBoxTransportationProfileName.Text.Contains("/") ||
                    textBoxTransportationProfileName.Text.Contains(">") ||
                    textBoxTransportationProfileName.Text.Contains("<") ||
                    textBoxTransportationProfileName.Text.Contains("?") ||
                    textBoxTransportationProfileName.Text.Contains("*") ||
                    textBoxTransportationProfileName.Text.Contains(":") ||
                    textBoxTransportationProfileName.Text.Contains("|") ||
                    textBoxTransportationProfileName.Text.Contains("\"") ||
                    textBoxTransportationProfileName.Text.Contains("'")
                    )
            {
                MessageBox.Show("You shouldn't use spaces nor the following characters (\\/<>?*:|\"') in the Profile Name as it will be used to create folders and files.");
                return false;
            }

            if (comboBoxConnectionSource.SelectedItem == null)
            {
                MessageBox.Show("You must select a Source for the Profile");
                return false;
            }

            if (comboBoxConnectionTarget.SelectedItem == null && comboBoxOperation.SelectedItem.ToString() != "Export Data")
            {
                MessageBox.Show("You must select a Target for the Profile");
                return false;
            }

            //Check if this is a creation
            if (currentProfile == null)
            {
                //Check if a Connection having the same name exist already
                foreach (NtoNAssociationsTransportProfile tp in man.Profiles)
                {
                    if (tp.ProfileName.ToLower() == textBoxTransportationProfileName.Text.ToLower())
                    {
                        MessageBox.Show("Profile with the name " + textBoxTransportationProfileName.Text + " exist already. Please select another name");
                        return false;
                    }
                }

                NtoNAssociationsTransportProfile newProfile = new NtoNAssociationsTransportProfile();
                newProfile.ProfileName = textBoxTransportationProfileName.Text;
                //newProfile.ImportMode = comboBoxImportMode.SelectedIndex;
                newProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString();
                newProfile.setSourceConneciton();
                if (comboBoxOperation.SelectedItem.ToString() != "Export Data")
                {
                    newProfile.TargetConnectionName = comboBoxConnectionTarget.SelectedItem.ToString();
                    newProfile.setTargetConneciton();
                }
                newProfile.SelectedNtoNRelationships = new List<SelectedNtoNRelationship>();

                dataGridView1.EndEdit();
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells[0];

                    if (check.Value != null && (bool)check.Value)
                    {
                        NtoNRelationship nnr = es.NtoNRelationships.Find(r => r.RelationshipSchemaName == (string)row.Cells[1].Value);
                        SelectedNtoNRelationship ee = new SelectedNtoNRelationship();
                        ee.RelationshipSchemaName = nnr.RelationshipSchemaName;
                        ee.IntersectEntityName = nnr.IntersectEntityName;
                        ee.Entity1IntersectAttribute = nnr.Entity1IntersectAttribute;
                        ee.Entity1LogicalName = nnr.Entity1LogicalName;
                        ee.Entity2IntersectAttribute = nnr.Entity2IntersectAttribute;
                        ee.Entity2LogicalName = nnr.Entity2LogicalName;
                        ee.ExportedRecords = 0;
                        newProfile.SelectedNtoNRelationships.Add(ee);
                    }
                }

                newProfile.Operation = comboBoxOperation.SelectedIndex;
                man.CreateProfile(newProfile);
                comboBoxTransportationProfiles.Items.AddRange(new object[] { newProfile.ProfileName });
                comboBoxTransportationProfiles.SelectedItem = newProfile.ProfileName;
                currentProfile = newProfile;
            }
            else
            {
                currentProfile.ProfileName = textBoxTransportationProfileName.Text;
                //currentProfile.ImportMode = comboBoxImportMode.SelectedIndex;
                currentProfile.SourceConnectionName = comboBoxConnectionSource.SelectedItem.ToString();
                currentProfile.setSourceConneciton();
                if (comboBoxOperation.SelectedItem.ToString() != "Export Data")
                {
                    currentProfile.TargetConnectionName = comboBoxConnectionTarget.SelectedItem.ToString();
                    currentProfile.setTargetConneciton();
                }

                //Backup Export Records numbers if existing
                List<SelectedNtoNRelationship> backupSelectedEntites = currentProfile.SelectedNtoNRelationships;

                currentProfile.SelectedNtoNRelationships = new List<SelectedNtoNRelationship>();

                NtoNAssociationsTransportProfile oldProfile = man.GetProfile(currentProfile.ProfileName);

                dataGridView1.EndEdit();
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)row.Cells[0];
                    if (check.Value != null && (bool)check.Value)
                    {
                        NtoNRelationship nnr = es.NtoNRelationships.Find(r => r.RelationshipSchemaName == (string)row.Cells[1].Value);
                        SelectedNtoNRelationship ee = new SelectedNtoNRelationship();
                        ee.RelationshipSchemaName = nnr.RelationshipSchemaName;
                        ee.IntersectEntityName = nnr.IntersectEntityName;
                        ee.Entity1IntersectAttribute = nnr.Entity1IntersectAttribute;
                        ee.Entity1LogicalName = nnr.Entity1LogicalName;
                        ee.Entity2IntersectAttribute = nnr.Entity2IntersectAttribute;
                        ee.Entity2LogicalName = nnr.Entity2LogicalName;
                        ee.ExportedRecords = 0;
                        currentProfile.SelectedNtoNRelationships.Add(ee);
                    }
                }

                currentProfile.Operation = comboBoxOperation.SelectedIndex;
                man.UpdateProfile(currentProfile);
            }

            runProfileToolStripMenuItem.Enabled = true;
            toolStripStatusLabel.Text = "Profile " + currentProfile.ProfileName + " saved.";
            LogManager.WriteLog("Profile " + currentProfile.ProfileName + " saved.");
            return result;
        }