private void TransportReportViewer_Load(object sender, EventArgs e) { this.DragEnter += Form1_DragEnter; this.DragDrop += Form1_DragDrop; if (reportFileName == "") { return; } report = tpm.ReadTransportReport(reportFileName); labelTransportationProfileName.Text = report.TransportProfileName; labelTransportCompleted.Text = (report.TransportCompleted) ? "Yes" : "No"; labelTransportStartedAt.Text = report.TransportStartedAt; labelTransportFinishedAt.Text = report.TransportFinishedAt; labelTransportedIn.Text = report.TransportedIn; labelTotalExportedRecords.Text = report.TotalExportedRecords.ToString(); labelTotalImportedRecords.Text = report.TotalImportedRecords.ToString(); labelTotalImportFailures.Text = report.TotalImportFailures.ToString(); SortableBindingList <TransportReportLine> sortedReportLines = new SortableBindingList <TransportReportLine>(report.ReportLines); dataGridView1.DataSource = sortedReportLines; if (report.TotalImportFailures > 0 && File.Exists(reportFailuresFileName)) { buttonFailuresReport.Visible = true; } else { buttonFailuresReport.Visible = false; } }
/// <summary> /// Writes the transport report. /// </summary> /// <param name="tr">The Transport Report.</param> /// <param name="transportReportFileName">Name of the transport report file.</param> public void WriteTransportReport(TransportReport tr, string transportReportFileName) { FileStream writer = new FileStream(transportReportFileName, FileMode.Create); DataContractSerializer ser = new DataContractSerializer(typeof(TransportReport)); ser.WriteObject(writer, tr); writer.Close(); }
/// <summary> /// Runs the profile. /// </summary> /// <param name="profile">The profile.</param> /// <returns></returns> public string RunProfile(TransportationProfile profile) { LogManager.WriteLog("Running Transportation Profile: " + profile.ProfileName); //Check if there are selected entities to transport if (profile.SelectedEntities == null || profile.SelectedEntities.Count == 0) { LogManager.WriteLog("No entities selected for transport. Select the entities and then run the profile"); return(""); } DateTime now = DateTime.Now; ReportFileName = Folder + "\\" + profile.ProfileName + "\\ExecutionReports\\TransportReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; importFailuresReportFileName = Folder + "\\" + profile.ProfileName + "\\ExecutionReports\\ImportFailuresReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; //Initialize Execution Reports folder string executionReportsFolder = Folder + "\\" + profile.ProfileName + "\\ExecutionReports"; if (!Directory.Exists(executionReportsFolder)) { Directory.CreateDirectory(executionReportsFolder); } //Create Transport Report TransportReport tr = new TransportReport(profile.ProfileName); WriteTransportReport(tr, ReportFileName); //Check for the Operation to execute if (profile.Operation == 0) { //Export data Export(profile, ReportFileName); } else if (profile.Operation == 1) { //Import Data Import(profile, ReportFileName); } else if (profile.Operation == 2) { //Transport data => Export + Import Export(profile, ReportFileName); Import(profile, ReportFileName); } TransportReport report = ReadTransportReport(ReportFileName); report.TransportFinishedAt = DateTime.Now.ToString(); report.TransportCompleted = true; TimeSpan TransportTimeSpan = DateTime.Now - Convert.ToDateTime(report.TransportStartedAt); report.TransportedIn = TransportTimeSpan.ToString().Substring(0, 10); WriteTransportReport(report, ReportFileName); return(ReportFileName); }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string[] filePath = openFileDialog1.FileNames; foreach (string fileLoc in filePath) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; this.reportFailuresFileName = reportFileName.Replace("TransportReport", "ImportFailuresReport"); try { report = tpm.ReadTransportReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelTransportationProfileName.Text = report.TransportProfileName; labelTransportCompleted.Text = (report.TransportCompleted) ? "Yes" : "No"; labelTransportStartedAt.Text = report.TransportStartedAt; labelTransportFinishedAt.Text = report.TransportFinishedAt; labelTransportedIn.Text = report.TransportedIn; labelTotalExportedRecords.Text = report.TotalExportedRecords.ToString(); labelTotalImportedRecords.Text = report.TotalImportedRecords.ToString(); labelTotalImportFailures.Text = report.TotalImportFailures.ToString(); SortableBindingList <TransportReportLine> sortedReportLines = new SortableBindingList <TransportReportLine>(report.ReportLines); dataGridView1.DataSource = sortedReportLines; //Retrieve Failures Report FileName if (report.TotalImportFailures > 0 && File.Exists(reportFailuresFileName)) { buttonFailuresReport.Visible = true; } else { buttonFailuresReport.Visible = false; } } } } }
private void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop)); foreach (string fileLoc in filePaths) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; this.reportFailuresFileName = reportFileName.Replace("TransportReport", "ImportFailuresReport"); try { report = tpm.ReadTransportReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelTransportationProfileName.Text = report.TransportProfileName; labelTransportCompleted.Text = (report.TransportCompleted) ? "Yes" : "No"; labelTransportStartedAt.Text = report.TransportStartedAt; labelTransportFinishedAt.Text = report.TransportFinishedAt; labelTransportedIn.Text = report.TransportedIn; labelTotalExportedRecords.Text = report.TotalExportedRecords.ToString(); labelTotalImportedRecords.Text = report.TotalImportedRecords.ToString(); labelTotalImportFailures.Text = report.TotalImportFailures.ToString(); SortableBindingList <TransportReportLine> sortedReportLines = new SortableBindingList <TransportReportLine>(report.ReportLines); dataGridView1.DataSource = sortedReportLines; //Retrieve Failures Report FileName if (report.TotalImportFailures > 0 && File.Exists(reportFailuresFileName)) { buttonFailuresReport.Visible = true; } else { buttonFailuresReport.Visible = false; } } } } }
/// <summary> /// Updates the transport report. /// </summary> /// <param name="report">The report.</param> /// <param name="ee">The ee.</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(TransportReport report, SelectedEntity ee, int importedRecordsForEntity, int importFailuresForEntity, DateTime entityImportStartDT) { bool addNewLine = true; foreach (TransportReportLine reportLine in report.ReportLines) { if (reportLine.Entity == ee.EntityName) { 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) { TransportReportLine currentLine = new TransportReportLine(); currentLine.Entity = ee.EntityName; 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> /// Reads the transport report. /// </summary> /// <param name="transportReportFileName">Name of the transport report file.</param> /// <returns>The Transport Report</returns> public TransportReport ReadTransportReport(string transportReportFileName) { if (File.Exists(transportReportFileName)) { try { XmlDictionaryReaderQuotas XRQ = new XmlDictionaryReaderQuotas(); XRQ.MaxStringContentLength = int.MaxValue; using (FileStream fs = new FileStream(transportReportFileName, FileMode.Open)) using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, XRQ)) { DataContractSerializer ser = new DataContractSerializer(typeof(TransportReport)); TransportReport tr = (TransportReport)ser.ReadObject(reader, true); return(tr); } } catch (SerializationException) { throw; } } return(null); }
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> /// Updates the transport report. /// </summary> /// <param name="report">The report.</param> /// <param name="ee">The ee.</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(TransportReport report, SelectedEntity ee, int importedRecordsForEntity, int importFailuresForEntity, DateTime entityImportStartDT) { bool addNewLine = true; foreach (TransportReportLine reportLine in report.ReportLines) { if (reportLine.Entity == ee.EntityName) { 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) { TransportReportLine currentLine = new TransportReportLine(); currentLine.Entity = ee.EntityName; 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> /// Runs the profile. /// </summary> /// <param name="profile">The profile.</param> /// <returns></returns> public string RunProfile(TransportationProfile profile) { LogManager.WriteLog("Running Transportation Profile: " + profile.ProfileName); //Check if there are selected entities to transport if (profile.SelectedEntities == null || profile.SelectedEntities.Count == 0) { LogManager.WriteLog("No entities selected for transport. Select the entities and then run the profile"); return ""; } DateTime now = DateTime.Now; ReportFileName = Folder + "\\" + profile.ProfileName + "\\ExecutionReports\\TransportReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; importFailuresReportFileName = Folder + "\\" + profile.ProfileName + "\\ExecutionReports\\ImportFailuresReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; //Initialize Execution Reports folder string executionReportsFolder = Folder + "\\" + profile.ProfileName + "\\ExecutionReports"; if (!Directory.Exists(executionReportsFolder)) { Directory.CreateDirectory(executionReportsFolder); } //Create Transport Report TransportReport tr = new TransportReport(profile.ProfileName); WriteTransportReport(tr, ReportFileName); //Check for the Operation to execute if (profile.Operation == 0) { //Export data Export(profile, ReportFileName); } else if (profile.Operation == 1) { //Import Data Import(profile, ReportFileName); } else if (profile.Operation == 2) { //Transport data => Export + Import Export(profile, ReportFileName); Import(profile, ReportFileName); } TransportReport report = ReadTransportReport(ReportFileName); report.TransportFinishedAt = DateTime.Now.ToString(); report.TransportCompleted = true; TimeSpan TransportTimeSpan = DateTime.Now - Convert.ToDateTime(report.TransportStartedAt); report.TransportedIn = TransportTimeSpan.ToString().Substring(0, 10); WriteTransportReport(report, ReportFileName); return ReportFileName; }
private void TransportReportViewer_Load(object sender, EventArgs e) { this.DragEnter += Form1_DragEnter; this.DragDrop += Form1_DragDrop; if (reportFileName == "") return; report = tpm.ReadTransportReport(reportFileName); labelTransportationProfileName.Text = report.TransportProfileName; labelTransportCompleted.Text = (report.TransportCompleted) ? "Yes" : "No"; labelTransportStartedAt.Text = report.TransportStartedAt; labelTransportFinishedAt.Text = report.TransportFinishedAt; labelTransportedIn.Text = report.TransportedIn; labelTotalExportedRecords.Text = report.TotalExportedRecords.ToString(); labelTotalImportedRecords.Text = report.TotalImportedRecords.ToString(); labelTotalImportFailures.Text = report.TotalImportFailures.ToString(); SortableBindingList<TransportReportLine> sortedReportLines = new SortableBindingList<TransportReportLine>(report.ReportLines); dataGridView1.DataSource = sortedReportLines; if (report.TotalImportFailures > 0 && File.Exists(reportFailuresFileName)) { buttonFailuresReport.Visible = true; } else { buttonFailuresReport.Visible = false; } }
/// <summary> /// Writes the transport report. /// </summary> /// <param name="tr">The Transport Report.</param> /// <param name="transportReportFileName">Name of the transport report file.</param> public void WriteTransportReport(TransportReport tr, string transportReportFileName) { FileStream writer = new FileStream(transportReportFileName, FileMode.Create); DataContractSerializer ser = new DataContractSerializer(typeof(TransportReport)); ser.WriteObject(writer, tr); writer.Close(); }
private void openToolStripMenuItem_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string[] filePath = openFileDialog1.FileNames; foreach (string fileLoc in filePath) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; this.reportFailuresFileName = reportFileName.Replace("TransportReport", "ImportFailuresReport"); try { report = tpm.ReadTransportReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelTransportationProfileName.Text = report.TransportProfileName; labelTransportCompleted.Text = (report.TransportCompleted) ? "Yes" : "No"; labelTransportStartedAt.Text = report.TransportStartedAt; labelTransportFinishedAt.Text = report.TransportFinishedAt; labelTransportedIn.Text = report.TransportedIn; labelTotalExportedRecords.Text = report.TotalExportedRecords.ToString(); labelTotalImportedRecords.Text = report.TotalImportedRecords.ToString(); labelTotalImportFailures.Text = report.TotalImportFailures.ToString(); SortableBindingList<TransportReportLine> sortedReportLines = new SortableBindingList<TransportReportLine>(report.ReportLines); dataGridView1.DataSource = sortedReportLines; //Retrieve Failures Report FileName if (report.TotalImportFailures > 0 && File.Exists(reportFailuresFileName)) { buttonFailuresReport.Visible = true; } else { buttonFailuresReport.Visible = false; } } } } }
private void Form1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop)); foreach (string fileLoc in filePaths) { // Code to read the contents of the text file if (File.Exists(fileLoc)) { this.reportFileName = fileLoc; this.reportFailuresFileName = reportFileName.Replace("TransportReport", "ImportFailuresReport"); try { report = tpm.ReadTransportReport(reportFileName); } catch (Exception ex) { MessageBox.Show("Invalid Report File: \n" + ex.Message); } labelTransportationProfileName.Text = report.TransportProfileName; labelTransportCompleted.Text = (report.TransportCompleted) ? "Yes" : "No"; labelTransportStartedAt.Text = report.TransportStartedAt; labelTransportFinishedAt.Text = report.TransportFinishedAt; labelTransportedIn.Text = report.TransportedIn; labelTotalExportedRecords.Text = report.TotalExportedRecords.ToString(); labelTotalImportedRecords.Text = report.TotalImportedRecords.ToString(); labelTotalImportFailures.Text = report.TotalImportFailures.ToString(); SortableBindingList<TransportReportLine> sortedReportLines = new SortableBindingList<TransportReportLine>(report.ReportLines); dataGridView1.DataSource = sortedReportLines; //Retrieve Failures Report FileName if (report.TotalImportFailures > 0 && File.Exists(reportFailuresFileName)) { buttonFailuresReport.Visible = true; } else { buttonFailuresReport.Visible = false; } } } } }
/// <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); } } }
private int ExportGUI(string transportReportFileName, BackgroundWorker worker, DoWorkEventArgs e) { TransportReport report = null; try { report = new TransportReport(man.ReportFileName); //Get Transport Report if (File.Exists(man.ReportFileName)) { report = man.ReadTransportReport(man.ReportFileName); } //Clean Data folder string dataExportFolder = man.Folder + "\\" + currentProfile.ProfileName + "\\Data"; if (Directory.Exists(dataExportFolder)) { Directory.Delete(dataExportFolder, true); } Directory.CreateDirectory(dataExportFolder); MSCRMConnection connection = currentProfile.getSourceConneciton(); EnvStructure es = man.ReadEnvStructure(currentProfile.SourceConnectionName); man._serviceProxy = cm.connect(connection); IOrganizationService service = (IOrganizationService)_serviceProxy; List<TransportReportLine> TransportReport = new List<TransportReportLine>(); currentProfile.TotalExportedRecords = 0; //Mesure export time DateTime exportStartDT = DateTime.Now; LogManager.WriteLog("Start exporting data from " + connection.ConnectionName); int recordCount = 0; if (es != null) { int treatedEntities = 1; //Order export according to profile's transport order IOrderedEnumerable<SelectedEntity> orderedSelectedEntities = currentProfile.SelectedEntities.OrderBy(se => se.TransportOrder); foreach (SelectedEntity ee in orderedSelectedEntities) { if (worker.CancellationPending) { e.Cancel = true; return 0; } int percentage = 0; if (currentProfile.SelectedEntities.Count != 0) percentage = (int)(100 * treatedEntities / currentProfile.SelectedEntities.Count); worker.ReportProgress(percentage); treatedEntities++; currentlyTransportedEntity = ee.EntityName; 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 = man.ExportEntity(currentProfile, 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); man.WriteTransportReport(report, transportReportFileName); } } TimeSpan exportTimeSpan = DateTime.Now - exportStartDT; LogManager.WriteLog("Export finished for " + currentProfile.SourceConnectionName + ". Exported " + recordCount + " records in " + exportTimeSpan.ToString().Substring(0, 10)); return recordCount; } catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex) { //Stop Transport threads if running transportStopped = true; e.Cancel = true; MessageBox.Show("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); LogManager.WriteLog("Error:" + ex.Detail.Message + "\n" + ex.Detail.TraceText); } catch (Exception ex) { //Stop Transport threads if running transportStopped = true; e.Cancel = true; if (ex.InnerException != null) { MessageBox.Show("Error:" + ex.Message + "\n" + ex.InnerException.Message); LogManager.WriteLog("Error:" + ex.Message + "\n" + ex.InnerException.Message); } else { MessageBox.Show("Error:" + ex.Message); LogManager.WriteLog("Error:" + ex.Message); } } return 0; }
/// <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 void runProfileToolStripMenuItem_Click(object sender, EventArgs e) { if (!SaveProfile()) return; LogManager.WriteLog("Running Transportation Profile: " + currentProfile.ProfileName); //Check if there are selected entities to transport if (currentProfile.SelectedEntities == null || currentProfile.SelectedEntities.Count == 0) { MessageBox.Show("No entities selected for transport. Select the entities and then run the profile"); LogManager.WriteLog("No entities selected for transport. Select the entities and then run the profile"); return; } DateTime now = DateTime.Now; man.ReportFileName = man.Folder + "\\" + currentProfile.ProfileName + "\\ExecutionReports\\TransportReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; man.importFailuresReportFileName = man.Folder + "\\" + currentProfile.ProfileName + "\\ExecutionReports\\ImportFailuresReport" + now.Year + "-" + now.Month + "-" + now.Day + "-" + now.Hour + "-" + now.Minute + "-" + now.Second + ".xml"; //Initialize Execution Reports folder string executionReportsFolder = man.Folder + "\\" + currentProfile.ProfileName + "\\ExecutionReports"; if (!Directory.Exists(executionReportsFolder)) { Directory.CreateDirectory(executionReportsFolder); } //Create Transport Report TransportReport tr = new TransportReport(currentProfile.ProfileName); man.WriteTransportReport(tr, man.ReportFileName); transportReportToolStripMenuItem.Visible = false; labelImportSuccess.Text = ""; labelImportFailures.Text = ""; label2.Visible = false; label3.Visible = false; if (currentProfile.Operation == 0 || currentProfile.Operation == 2) { if (!transportRunning) { buttonStopTransport.Visible = true; runProfileToolStripMenuItem.Enabled = false; pgbState.Visible = true; SetFields(false); toolStripStatusLabel.Text = "Initializing Data Export"; if (bwExport.IsBusy != true) bwExport.RunWorkerAsync(); } else { MessageBox.Show("Transport already running!"); } } else if (currentProfile.Operation == 1) { if (!transportRunning) { buttonStopTransport.Visible = true; runProfileToolStripMenuItem.Enabled = false; pgbState.Visible = true; SetFields(false); toolStripStatusLabel.Text = "Initializing Data Import"; label2.Visible = true; label3.Visible = true; if (bwImport.IsBusy != true) bwImport.RunWorkerAsync(); } else { MessageBox.Show("Transport already running!"); } } }