private void saveLogButton_Click(object sender, EventArgs e) { if (DialogResult.OK == saveFileDialog2.ShowDialog(this)) { var safeFile = saveFileDialog2.FileName; var fullFile = saveFileDialog2.FileName; if (!fullFile.EndsWith(".csv")) { fullFile += ".csv"; } if (new FileInfo(fullFile).Exists) { if (DialogResult.OK == MessageBox.Show("Der findes allerede en fil med dette navn. Brug et andet navn.")) { if (DialogResult.OK == saveFileDialog2.ShowDialog(this)) { safeFile = saveFileDialog2.FileName; fullFile = saveFileDialog2.FileName; } } } var csv = ToCsv(AnswerInfo.CsvHeader(), _answerInfoCollection.Values.Select( answerInfo => answerInfo.ToCsvLine()).ToList()); File.WriteAllText(fullFile, csv); Log(string.Format("Eksportlog er gemt i '{0}'.", fullFile)); } }
public static IEnumerable <AnswerInfo> GetJournalInfos(DataTable table, int surveyId) { var infos = new List <AnswerInfo>(); foreach (DataRow row in table.Rows) { var answerInfo = new AnswerInfo(AnswerMetadata.GetJournalInfo(row)); if (surveyId == 2) { answerInfo.Besvarelsesdato_CBCL = answerInfo.Besvarelsesdato; } else { answerInfo.Besvarelsesdato_TRF = answerInfo.Besvarelsesdato; } infos.Add(answerInfo); } return(infos); }
private void importCsvButton_Click(object sender, EventArgs e) { var dir = FindCodebooks(); if (dir.Contains("not found")) { Log("Error: Codebooks could not be found. Check locations, folder 'Codebooks' must be in current directory."); //throw new ApplicationException("Codebooks could not be found"); } var codebookFiles = new List <String> { @"\codebook_cc.xml", @"\codebook_ccy.xml", @"\codebook_ct.xml", @"\codebook_tt.xml", @"\codebook_ycy.xml" }; var fullFilePaths = codebookFiles.Select(f => dir + f); CodeBookXmlLoader codebookLoader; try { codebookLoader = new CodeBookXmlLoader(fullFilePaths); _codebooks = codebookLoader.Read(); } catch (Exception ex) { Log("Fejl indlæsning kodebøger: " + ex.Message); Log(ex.StackTrace); throw; } Log("Indlæste kodebøger: " + codebookFiles.Aggregate((a, b) => a + ", " + b)); // read answers var answersLoader = new AnswersCsvLoader(); Log("Indlæser svar i CSV-format"); Dictionary <int, DataTable> answers; try { answers = answersLoader.ReadCsvAnswers(_files); } catch (Exception ex) { Log("Fejl indlæsning svar: " + ex.Message); Log(ex.StackTrace); throw; } var answerTables = answers.GroupBy(a => a.Key); foreach (var grouping in answerTables) { var surveyId = grouping.Key; var journalInfos = grouping.SelectMany(group => AnswerInfo.GetJournalInfos(group.Value, surveyId)); foreach (var journalInfo in journalInfos) { AddExportInfo(journalInfo, surveyId); } } var journalInfoColumns = AnswerMetadata.JournalInfoColumns(); var mixer = new CodebookAnswerMixer(_codebooks.Values); var answersPerSurvey = new Dictionary <int, List <IAnswer> >(); Log("Importerer svar..."); int codebookId = 0; try { foreach (var pair in _codebooks) { codebookId = pair.Key; //.ToString(); ICodebook codebook = pair.Value; if (answers.ContainsKey(codebookId)) { var answersTable = answers[codebookId]; // get answer table and codebook with same surveyId. Just for trying out var answersForSurvey = mixer.ExtractAnswers(codebook, answersTable, journalInfoColumns); answersPerSurvey.Add(answersForSurvey.First, answersForSurvey.Second); } } } catch (Exception ex) { Log("Fejl i mix: " + ex.Message); Log("KodebogID: " + codebookId); Log(ex.StackTrace); throw; } try { _importedAnswers = mixer.CollectAnswersByJournal(answersPerSurvey); } catch (Exception ex) { Log("Fejl saml svar: " + ex.Message); Log(ex.StackTrace); throw; } Log("Svarene er importeret."); foreach (var answerBook in _importedAnswers) { foreach (var journalInfo in answerBook.AnswerMetadataList) { AddExportInfo(new AnswerInfo(journalInfo), true); } } importCsvButton.Enabled = false; saveButton.Enabled = true; }