/// <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);
                }
            }
        }
        /// <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);
                }
            }
        }
        /// <summary>
        /// Exports the specified profile.
        /// </summary>
        /// <param name="profile">The profile.</param>
        /// <param name="transportReportFileName">Name of the transport report file.</param>
        private void Export(TransportationProfile profile, string transportReportFileName)
        {
            try
            {
                TransportReport report = new TransportReport(transportReportFileName);
                //Get Transport Report
                if (File.Exists(transportReportFileName))
                {
                    report = ReadTransportReport(transportReportFileName);
                }

                //Clean Data folder
                string dataExportFolder = Folder + "\\" + profile.ProfileName + "\\Data";
                if (Directory.Exists(dataExportFolder))
                {
                    Directory.Delete(dataExportFolder, true);
                }
                Directory.CreateDirectory(dataExportFolder);

                MSCRMConnection connection = profile.getSourceConneciton();
                EnvStructure    es         = ReadEnvStructure(profile.SourceConnectionName);
                _serviceProxy = cm.connect(connection);
                IOrganizationService       service         = (IOrganizationService)_serviceProxy;
                List <TransportReportLine> TransportReport = new List <TransportReportLine>();
                profile.TotalExportedRecords = 0;
                //Mesure export time
                DateTime exportStartDT = DateTime.Now;

                LogManager.WriteLog("Start exporting data from " + connection.ConnectionName);

                int recordCount = 0;
                if (es != null)
                {
                    //Order export according to profile's transport order
                    IOrderedEnumerable <SelectedEntity> orderedSelectedEntities = profile.SelectedEntities.OrderBy(se => se.TransportOrder);

                    foreach (SelectedEntity ee in orderedSelectedEntities)
                    {
                        LogManager.WriteLog("Exporting data for entity " + ee.EntityName);
                        DateTime entityExportStartDT = DateTime.Now;
                        string   fetchXml            = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                        fetchXml += "<entity name='" + ee.EntityName + "'>";
                        //Get Entity structure
                        EnvEntity strE = new EnvEntity();
                        foreach (EnvEntity envE in es.Entities)
                        {
                            if (envE.EntityName == ee.EntityName)
                            {
                                strE = envE;
                                break;
                            }
                        }

                        //Create fetchXML Query
                        foreach (string ea in strE.Attributes)
                        {
                            if (ee.IgnoredAttributes == null)
                            {
                                fetchXml += "<attribute name='" + ea + "' />";
                            }
                            else if (!ee.IgnoredAttributes.Contains(ea))
                            {
                                fetchXml += "<attribute name='" + ea + "' />";
                            }
                        }

                        //Add Query filter
                        fetchXml += ee.Filter;

                        fetchXml += "</entity></fetch>";
                        int recordCountPerEntity = ExportEntity(profile, fetchXml);
                        recordCount += recordCountPerEntity;

                        DateTime            entityExportEndDT = DateTime.Now;
                        TimeSpan            ts = entityExportEndDT - entityExportStartDT;
                        TransportReportLine transportReportLine = new TransportReportLine();
                        transportReportLine.Entity           = ee.EntityName;
                        transportReportLine.ExportedRecords  = recordCountPerEntity;
                        report.TotalExportedRecords         += recordCountPerEntity;
                        transportReportLine.ExportedIn       = ts.ToString().Substring(0, 10);
                        transportReportLine.ExportStartedAt  = entityExportStartDT.ToString();
                        transportReportLine.ExportFinishedAt = entityExportEndDT.ToString();
                        report.ReportLines.Add(transportReportLine);
                        WriteTransportReport(report, transportReportFileName);
                    }
                }

                TimeSpan exportTimeSpan = DateTime.Now - exportStartDT;

                LogManager.WriteLog("Export finished for " + profile.SourceConnectionName + ". Exported " + recordCount + " records in " + exportTimeSpan.ToString().Substring(0, 10));
            }
            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);
                }
            }
        }
        /// <summary>
        /// Exports the specified profile.
        /// </summary>
        /// <param name="profile">The profile.</param>
        /// <param name="transportReportFileName">Name of the transport report file.</param>
        private void Export(TransportationProfile profile, string transportReportFileName)
        {
            try
            {
                TransportReport report = new TransportReport(transportReportFileName);
                //Get Transport Report
                if (File.Exists(transportReportFileName))
                {
                    report = ReadTransportReport(transportReportFileName);
                }

                //Clean Data folder
                string dataExportFolder = Folder + "\\" + profile.ProfileName + "\\Data";
                if (Directory.Exists(dataExportFolder))
                {
                    Directory.Delete(dataExportFolder, true);
                }
                Directory.CreateDirectory(dataExportFolder);

                MSCRMConnection connection = profile.getSourceConneciton();
                EnvStructure es = ReadEnvStructure(profile.SourceConnectionName);
                _serviceProxy = cm.connect(connection);
                IOrganizationService service = (IOrganizationService)_serviceProxy;
                List<TransportReportLine> TransportReport = new List<TransportReportLine>();
                profile.TotalExportedRecords = 0;
                //Mesure export time
                DateTime exportStartDT = DateTime.Now;

                LogManager.WriteLog("Start exporting data from " + connection.ConnectionName);

                int recordCount = 0;
                if (es != null)
                {
                    //Order export according to profile's transport order
                    IOrderedEnumerable<SelectedEntity> orderedSelectedEntities = profile.SelectedEntities.OrderBy(se => se.TransportOrder);

                    foreach (SelectedEntity ee in orderedSelectedEntities)
                    {
                        LogManager.WriteLog("Exporting data for entity " + ee.EntityName);
                        DateTime entityExportStartDT = DateTime.Now;
                        string fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
                        fetchXml += "<entity name='" + ee.EntityName + "'>";
                        //Get Entity structure
                        EnvEntity strE = new EnvEntity();
                        foreach (EnvEntity envE in es.Entities)
                        {
                            if (envE.EntityName == ee.EntityName)
                            {
                                strE = envE;
                                break;
                            }
                        }

                        //Create fetchXML Query
                        foreach (string ea in strE.Attributes)
                        {
                            if (ee.IgnoredAttributes == null)
                            {
                                fetchXml += "<attribute name='" + ea + "' />";
                            }
                            else if (!ee.IgnoredAttributes.Contains(ea))
                            {
                                fetchXml += "<attribute name='" + ea + "' />";
                            }
                        }

                        //Add Query filter
                        fetchXml += ee.Filter;

                        fetchXml += "</entity></fetch>";
                        int recordCountPerEntity = ExportEntity(profile, fetchXml);
                        recordCount += recordCountPerEntity;

                        DateTime entityExportEndDT = DateTime.Now;
                        TimeSpan ts = entityExportEndDT - entityExportStartDT;
                        TransportReportLine transportReportLine = new TransportReportLine();
                        transportReportLine.Entity = ee.EntityName;
                        transportReportLine.ExportedRecords = recordCountPerEntity;
                        report.TotalExportedRecords += recordCountPerEntity;
                        transportReportLine.ExportedIn = ts.ToString().Substring(0, 10);
                        transportReportLine.ExportStartedAt = entityExportStartDT.ToString();
                        transportReportLine.ExportFinishedAt = entityExportEndDT.ToString();
                        report.ReportLines.Add(transportReportLine);
                        WriteTransportReport(report, transportReportFileName);
                    }
                }

                TimeSpan exportTimeSpan = DateTime.Now - exportStartDT;

                LogManager.WriteLog("Export finished for " + profile.SourceConnectionName + ". Exported " + recordCount + " records in " + exportTimeSpan.ToString().Substring(0, 10));
            }
            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);
                }
            }
        }