/// <summary>
        /// Writes the new import failure line.
        /// </summary>
        /// <param name="failure">The failure.</param>
        /// <param name="importFailuresReportFileName">Name of the import failures report file.</param>
        public void WriteNewImportFailureLine(ImportFailure failure, string importFailuresReportFileName)
        {
            bool reportExists             = File.Exists(importFailuresReportFileName);
            List <ImportFailure> failures = new List <ImportFailure>();

            if (reportExists)
            {
                using (FileStream fs = new FileStream(importFailuresReportFileName, FileMode.OpenOrCreate))
                {
                    XmlDictionaryReaderQuotas XRQ = new XmlDictionaryReaderQuotas();
                    XRQ.MaxStringContentLength = int.MaxValue;
                    XmlDictionaryReader    reader = XmlDictionaryReader.CreateTextReader(fs, XRQ);
                    DataContractSerializer ser    = new DataContractSerializer(typeof(List <ImportFailure>));
                    failures = (List <ImportFailure>)ser.ReadObject(reader, true);
                    reader.Close();
                    fs.Close();
                }
            }

            using (FileStream writer = new FileStream(importFailuresReportFileName, FileMode.OpenOrCreate))
            {
                failures.Add(failure);
                DataContractSerializer ser = new DataContractSerializer(typeof(List <ImportFailure>));
                //Write updated failures report
                ser.WriteObject(writer, failures);
                writer.Close();
            }
        }
        /// <summary>
        /// Imports the specified profile.
        /// </summary>
        /// <param name="profile">The profile.</param>
        /// <param name="transportReportFileName">Name of the transport report file.</param>
        private void Import(TransportationProfile profile, string transportReportFileName)
        {
            int totalTreatedRecords    = 0;
            int totalImportFailures    = 0;
            int totalImportSuccess     = 0;
            int ReconnectionRetryCount = 5;

            try
            {
                TransportReport report = new TransportReport(transportReportFileName);
                //Get Transport Report
                if (File.Exists(transportReportFileName))
                {
                    report = ReadTransportReport(transportReportFileName);
                }

                MSCRMConnection connection = profile.getTargetConneciton();;
                _serviceProxy = cm.connect(connection);
                IOrganizationService service = (IOrganizationService)_serviceProxy;
                LogManager.WriteLog("Start importing data in " + connection.ConnectionName);

                //Mesure import time
                DateTime importStartDT = DateTime.Now;

                //Order import according to profile's import order
                IOrderedEnumerable <SelectedEntity> orderedSelectedEntities = profile.SelectedEntities.OrderBy(e => e.TransportOrder);

                foreach (SelectedEntity ee in orderedSelectedEntities)
                {
                    //Check if there are any records to import
                    if (ee.ExportedRecords == 0)
                    {
                        continue;
                    }

                    //Mesure import time
                    DateTime entityImportStartDT = DateTime.Now;

                    string   entityFolderPath = Folder + "\\" + profile.ProfileName + "\\Data\\" + ee.EntityName;
                    string[] filePaths        = Directory.GetFiles(entityFolderPath, "*.xml");

                    LogManager.WriteLog("Importing " + ee.EntityName + " records.");
                    int treatedRecordsForEntity  = 0;
                    int importedRecordsForEntity = 0;
                    int importFailuresForEntity  = 0;
                    foreach (string filePath in filePaths)
                    {
                        List <Type> knownTypes = new List <Type>();
                        knownTypes.Add(typeof(Entity));

                        XmlDictionaryReaderQuotas XRQ = new XmlDictionaryReaderQuotas();
                        XRQ.MaxStringContentLength = int.MaxValue;

                        using (FileStream fs = new FileStream(filePath, FileMode.Open))
                            using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, XRQ))
                            {
                                DataContractSerializer ser      = new DataContractSerializer(typeof(EntityCollection), knownTypes);
                                EntityCollection       fromDisk = (EntityCollection)ser.ReadObject(reader, true);

                                foreach (Entity e in fromDisk.Entities)
                                {
                                    //Records mapping for the Lookup attributes
                                    Entity entity = MapRecords(profile, e);

                                    string executingOperation = "";
                                    try
                                    {
                                        if (profile.ImportMode == 0)
                                        {
                                            executingOperation = "Create";
                                            service.Create(entity);
                                        }
                                        else if (profile.ImportMode == 1)
                                        {
                                            try
                                            {
                                                executingOperation = "Update";
                                                service.Update(entity);
                                            }
                                            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> )
                                            {
                                                executingOperation = "Create";
                                                service.Create(entity);
                                            }
                                        }
                                        else if (profile.ImportMode == 2)
                                        {
                                            executingOperation = "Update";
                                            service.Update(entity);
                                        }
                                        importedRecordsForEntity++;
                                        totalImportSuccess++;
                                    }
                                    catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
                                    {
                                        totalImportFailures++;
                                        importFailuresForEntity++;
                                        ImportFailure failure = new ImportFailure
                                        {
                                            CreatedOn  = DateTime.Now.ToString(),
                                            EntityName = ee.EntityName,
                                            Operation  = executingOperation,
                                            Reason     = ex.Detail.Message,
                                            Url        = profile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + ee.EntityName + "&id=" + entity.Id.ToString()
                                        };
                                        report.TotalImportFailures += 1;
                                        //Insert the Failure line in the Failures Report
                                        WriteNewImportFailureLine(failure, importFailuresReportFileName);
                                    }
                                    catch (Exception ex)
                                    {
                                        //Check if the authentification session is expired
                                        if (ex.InnerException != null && ex.InnerException.Message.StartsWith("ID3242"))
                                        {
                                            LogManager.WriteLog("Error:The CRM authentication session expired. Reconnection attempt n° " + ReconnectionRetryCount);
                                            ReconnectionRetryCount--;
                                            //On 5 failed reconnections exit
                                            if (ReconnectionRetryCount == 0)
                                            {
                                                throw;
                                            }

                                            _serviceProxy = cm.connect(connection);
                                            service       = (IOrganizationService)_serviceProxy;
                                            LogManager.WriteLog("Error:The CRM authentication session expired.");
                                            totalImportFailures++;
                                            importFailuresForEntity++;
                                            ImportFailure failure = new ImportFailure
                                            {
                                                CreatedOn  = DateTime.Now.ToString(),
                                                EntityName = ee.EntityName,
                                                Operation  = executingOperation,
                                                Reason     = ex.InnerException.Message,
                                                Url        = profile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + ee.EntityName + "&id=" + entity.Id.ToString()
                                            };
                                            report.TotalImportFailures += 1;
                                            //Insert the Failure line in the Failures Report
                                            WriteNewImportFailureLine(failure, importFailuresReportFileName);
                                        }
                                        else
                                        {
                                            throw;
                                        }
                                    }
                                    totalTreatedRecords++;
                                    treatedRecordsForEntity++;
                                    updateTransportReport(report, ee, importedRecordsForEntity, importFailuresForEntity, entityImportStartDT);
                                }
                            }
                    }
                    LogManager.WriteLog("Treated " + treatedRecordsForEntity + " " + ee.EntityName + " records with " + importedRecordsForEntity + " successfully imported records and " + importFailuresForEntity + " failures.");
                }

                TimeSpan importTimeSpan = DateTime.Now - importStartDT;
                LogManager.WriteLog("Import finished for " + connection.ConnectionName + ". Treated " + totalTreatedRecords + " records in " + importTimeSpan.ToString().Substring(0, 10) + ". Successfuly imported " + totalImportSuccess + " records and " + totalImportFailures + " failures.");
                //WriteTransportReport(report, transportReportFileName);
            }
            catch (FaultException <Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message);
                }
                else
                {
                    LogManager.WriteLog("Error:" + ex.Message);
                }
            }
        }
        private int ImportGUI(string transporimportFailurestReportFileName, BackgroundWorker worker, DoWorkEventArgs e)
        {
            totalTreatedRecords = 0;
            totalImportFailures = 0;
            totalImportSuccess = 0;
            int ReconnectionRetryCount = 5;

            try
            {
                TransportReport report = new TransportReport(man.ReportFileName);
                //Get Transport Report
                if (File.Exists(man.ReportFileName))
                {
                    report = man.ReadTransportReport(man.ReportFileName);
                }

                MSCRMConnection connection = currentProfile.getTargetConneciton(); ;
                _serviceProxy = cm.connect(connection);
                IOrganizationService service = (IOrganizationService)_serviceProxy;
                LogManager.WriteLog("Start importing data in " + connection.ConnectionName);

                //Mesure import time
                DateTime importStartDT = DateTime.Now;

                //Order import according to profile's import order
                IOrderedEnumerable<SelectedEntity> orderedSelectedEntities = currentProfile.SelectedEntities.OrderBy(se => se.TransportOrder);

                foreach (SelectedEntity ee in orderedSelectedEntities)
                {
                    //Check if there are any records to import
                    if (ee.ExportedRecords == 0)
                    {
                        continue;
                    }

                    //Mesure import time
                    DateTime entityImportStartDT = DateTime.Now;
                    currentlyTransportedEntity = ee.EntityName;

                    string entityFolderPath = man.Folder + "\\" + currentProfile.ProfileName + "\\Data\\" + ee.EntityName;
                    string[] filePaths = Directory.GetFiles(entityFolderPath, "*.xml");

                    LogManager.WriteLog("Importing " + ee.EntityName + " records.");
                    int treatedRecordsForEntity = 0;
                    int importedRecordsForEntity = 0;
                    int importFailuresForEntity = 0;

                    foreach (string filePath in filePaths)
                    {
                        List<Type> knownTypes = new List<Type>();
                        knownTypes.Add(typeof(Entity));

                        XmlDictionaryReaderQuotas XRQ = new XmlDictionaryReaderQuotas();
                        XRQ.MaxStringContentLength = int.MaxValue;

                        using (FileStream fs = new FileStream(filePath, FileMode.Open))
                        using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, XRQ))
                        {
                            DataContractSerializer ser = new DataContractSerializer(typeof(EntityCollection), knownTypes);
                            EntityCollection fromDisk = (EntityCollection)ser.ReadObject(reader, true);

                            foreach (Entity en in fromDisk.Entities)
                            {
                                if (worker.CancellationPending)
                                {
                                    e.Cancel = true;
                                    return 0;
                                }

                                //Records mapping for the Lookup attributes
                                Entity entity = man.MapRecords(currentProfile, en);

                                string executingOperation = "";
                                try
                                {
                                    if (currentProfile.ImportMode == 0)
                                    {
                                        executingOperation = "Create";
                                        CreateRecordWithStatusIfAny(entity, service);
                                    }
                                    else if (currentProfile.ImportMode == 1)
                                    {
                                        try
                                        {
                                            executingOperation = "Update";
                                            service.Update(entity);
                                        }
                                        catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
                                        {
                                            executingOperation = "Create";
                                            CreateRecordWithStatusIfAny(entity, service);
                                        }
                                        catch (Exception ex)
                                        {
                                           Console.WriteLine(ex.Message);
                                        }
                                    }
                                    else if (currentProfile.ImportMode == 2)
                                    {
                                        executingOperation = "Update";
                                        service.Update(entity);
                                    }
                                    importedRecordsForEntity++;
                                    totalImportSuccess++;
                                }
                                catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
                                {
                                    totalImportFailures++;
                                    importFailuresForEntity++;
                                    ImportFailure failure = new ImportFailure
                                    {
                                        CreatedOn = DateTime.Now.ToString(),
                                        EntityName = ee.EntityName,
                                        Operation = executingOperation,
                                        Reason = ex.Detail.Message,
                                        Url = currentProfile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + ee.EntityName + "&id=" + entity.Id.ToString()
                                    };
                                    report.TotalImportFailures += 1;
                                    //Insert the Failure line in the Failures Report
                                    man.WriteNewImportFailureLine(failure, man.importFailuresReportFileName);
                                }
                                catch (Exception ex)
                                {
                                    //Check if the authentification session is expired
                                    if (ex.InnerException != null && ex.InnerException.Message.StartsWith("ID3242"))
                                    {
                                        LogManager.WriteLog("Error:The CRM authentication session expired. Reconnection attempt n° " + ReconnectionRetryCount);
                                        ReconnectionRetryCount--;
                                        //On 5 failed reconnections exit
                                        if (ReconnectionRetryCount == 0)
                                            throw;

                                        _serviceProxy = cm.connect(connection);
                                        service = (IOrganizationService)_serviceProxy;
                                        LogManager.WriteLog("Error:The CRM authentication session expired.");
                                        totalImportFailures++;
                                        importFailuresForEntity++;
                                        ImportFailure failure = new ImportFailure
                                        {
                                            CreatedOn = DateTime.Now.ToString(),
                                            EntityName = ee.EntityName,
                                            Operation = executingOperation,
                                            Reason = ex.InnerException.Message,
                                            Url = currentProfile.getSourceConneciton().ServerAddress + "main.aspx?pagetype=entityrecord&etn=" + ee.EntityName + "&id=" + entity.Id.ToString()
                                        };
                                        report.TotalImportFailures += 1;
                                        //Insert the Failure line in the Failures Report
                                        man.WriteNewImportFailureLine(failure, man.importFailuresReportFileName);
                                    }
                                    else
                                    {
                                        throw;
                                    }
                                }
                                totalTreatedRecords++;
                                treatedRecordsForEntity++;
                                man.updateTransportReport(report, ee, importedRecordsForEntity, importFailuresForEntity, entityImportStartDT);

                                int percentage = 0;
                                if (currentProfile.TotalExportedRecords != 0)
                                    percentage = totalTreatedRecords * 100 / currentProfile.TotalExportedRecords;
                                worker.ReportProgress(percentage);
                            }
                        }
                    }
                    LogManager.WriteLog("Treated " + treatedRecordsForEntity + " " + ee.EntityName + " records with " + importedRecordsForEntity + " successfully imported records and " + importFailuresForEntity + " failures.");
                }

                TimeSpan importTimeSpan = DateTime.Now - importStartDT;
                LogManager.WriteLog("Import finished for " + connection.ConnectionName + ". Treated " + totalTreatedRecords + " records in " + importTimeSpan.ToString().Substring(0, 10) + ". Successfuly imported " + totalImportSuccess + " records and " + totalImportFailures + " failures.");
                //tpm.WriteTransportReport(report,tpm.transportReportFileName);
                return 0;
            }
            catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
            {
                //Stop Transport threads if running
                bwImport.CancelAsync();
                LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText);
                MessageBox.Show("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText);
            }
            catch (Exception ex)
            {
                //Stop Transport threads if running
                bwImport.CancelAsync();
                if (ex.InnerException != null)
                {
                    LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message);
                    MessageBox.Show("Error:" + ex.Message + "\n" + ex.InnerException.Message);
                }
                else
                {
                    LogManager.WriteLog("Error:" + ex.Message);
                    MessageBox.Show("Error:" + ex.Message);
                }
            }
            return 0;
        }
        /// <summary>
        /// Writes the new import failure line.
        /// </summary>
        /// <param name="failure">The failure.</param>
        /// <param name="importFailuresReportFileName">Name of the import failures report file.</param>
        public void WriteNewImportFailureLine(ImportFailure failure, string importFailuresReportFileName)
        {
            bool reportExists = File.Exists(importFailuresReportFileName);
            List<ImportFailure> failures = new List<ImportFailure>();
            if (reportExists)
            {
                using (FileStream fs = new FileStream(importFailuresReportFileName, FileMode.OpenOrCreate))
                {
                    XmlDictionaryReaderQuotas XRQ = new XmlDictionaryReaderQuotas();
                    XRQ.MaxStringContentLength = int.MaxValue;
                    XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, XRQ);
                    DataContractSerializer ser = new DataContractSerializer(typeof(List<ImportFailure>));
                    failures = (List<ImportFailure>)ser.ReadObject(reader, true);
                    reader.Close();
                    fs.Close();

                }
            }

            using (FileStream writer = new FileStream(importFailuresReportFileName, FileMode.OpenOrCreate))
            {
                failures.Add(failure);
                DataContractSerializer ser = new DataContractSerializer(typeof(List<ImportFailure>));
                //Write updated failures report
                ser.WriteObject(writer, failures);
                writer.Close();
            }
        }