private async void LoginExecute(object obj) { try { IsBusy = true; LoginStatus = "Login..."; PhaidraClient client = new PhaidraClient(Url, SearchEngineUrl, Username, Password); var searchEngineTestResult = await client.FindCollectionBySystemTag("/"); if (!searchEngineTestResult.Success) { LoginStatus = "Search engine URL invalid"; return; } bool loginSuccessful = await client.SigninTest(); if (!loginSuccessful) { LoginStatus = "Login failed"; return; } LoginStatus = "Login successful"; PhaidraConfig.Instance.Url = Url; PhaidraConfig.Instance.Username = Username; PhaidraConfig.Instance.SearchEngineUrl = SearchEngineUrl; if (Password != PhaidraConfig.Instance.Password) { // if stored password is different than login password -> delete stored password PhaidraConfig.Instance.Password = null; } PhaidraConfig.Save(); DialogResult = true; CloseAction?.Invoke(); } finally { IsBusy = false; } }
private async Task ProcessImport() { try { List <Dictionary <int, string> > rows = null; Dictionary <int, string> headers = null; CsvFI = new System.IO.FileInfo(CsvPath); try { using (var csvReader = new CsvReader(CsvPath, ";", SelectedEncoding)) { ProgressText = "Reading CSV file"; headers = await csvReader.ReadHeaders(); rows = await csvReader.ReadRows(); } } catch (Exception ex) { Logger.LogE($"Error in ProcessImport: {ex.ToString()}"); ProgressText = $"Error: {ex.Message}"; } PhaidraClient client = new PhaidraClient(_loginViewModel.Url, _loginViewModel.SearchEngineUrl, _loginViewModel.Username, _loginViewModel.Password); ProgressValue = 0; ProgressMaximum = rows.Count; bool mappingsValid; var mappings = _mappingsViewModel.GetMappings(out mappingsValid); PhaidraMetadataGenerator metadataGenerator = new PhaidraMetadataGenerator(mappings); for (int i = 0; i < rows.Count; i++) { PhaidraFile file = new PhaidraFile() { BaseDirectory = RootDir }; try { ProgressValue = i + 1; ProgressText = $"Processing file {i + 1}/{ProgressMaximum}"; var row = rows[i]; Logger.LogI($"Processing file {i + 1}/{ProgressMaximum} \r\n({string.Join(";", row)})"); file.CsvRow = row; Logger.LogI($"Creating Phaidra attributes"); file.Metadata = new JObject(); metadataGenerator.SetMetadata(row, file); //foreach (PhaidraMapping mapping in mappings) //{ // var values = new Dictionary<string, string>(); // foreach (var col in mapping.PhaidraAttributeFields2CsvColumns) // { // if (col.SelectedCsvColumn != CsvColumn.Empty) // { // values[col.Field.Key] = row[col.SelectedCsvColumn.Header.Key]; // } // } // mapping.PhaidraAttribute.WriteAttributeContent(file, values); //} Logger.LogI($"Creating Phaidra attributes completed"); if (!string.IsNullOrEmpty(file.PhaidraId)) { Logger.LogI($"file {file.PhaidraId} already uploaded"); // file already uploaded continue; } if (PhaidraConfig.Instance.SimulateUpload == false) { string collectionPath = file.CollectionPath; Logger.LogI($"current collection: {collectionPath}"); if (!string.IsNullOrEmpty(PhaidraConfig.Instance.CollectionRoot)) { Logger.LogI($"using collection root: {PhaidraConfig.Instance.CollectionRoot}"); collectionPath = PhaidraConfig.Instance.CollectionRoot + "/" + collectionPath; } Logger.LogI($"used collection: {collectionPath}"); var createCollectionPathResult = await client.CreateCollectionPath(collectionPath); if (createCollectionPathResult != null) { foreach (var createdCollection in createCollectionPathResult.CreatedCollections) { Logger.LogI($"created collection: {createdCollection.Value} ({createdCollection.Key})"); } } Logger.LogI($"Uploading file"); var uploadResult = await client.UploadFileWithJsonMetadata(file.Fullfilename, file.Metadata.ToString(), file.MimeType, file.PhaidraType); if (uploadResult.Success) { file.PhaidraId = uploadResult.CreatedPid; Logger.LogI($"Upload successful. PID od uploaded file: {file.PhaidraId}"); var addCollectionMemberResult = await client.AddCollectionMembers(createCollectionPathResult.CollectionPathPid, new List <string> { uploadResult.CreatedPid }); if (addCollectionMemberResult.Success) { Logger.LogI($"Add member of file PID {uploadResult.CreatedPid} to collection PID {createCollectionPathResult.CollectionPathPid} successful"); } else { Logger.LogE($"Add member of file PID {uploadResult.CreatedPid} to collection PID {createCollectionPathResult.CollectionPathPid} failed"); } } else { Logger.LogE($"Add member of file PID {uploadResult.CreatedPid} to collection PID {createCollectionPathResult.CollectionPathPid} failed"); } } else { file.PhaidraId = $"gen-id:{i}"; Logger.LogI($"Simulate upload (PID: {file.PhaidraId})"); } metadataGenerator.WriteAttributeContentToCSV(file); //foreach (PhaidraMapping mapping in mappings) //{ // var values = new Dictionary<string, string>(); // mapping.PhaidraAttribute.WriteAttributeContentToCSV(file, values); // foreach (var col in mapping.PhaidraAttributeFields2CsvColumns) // { // if (col.SelectedCsvColumn != CsvColumn.Empty) // { // if (values.TryGetValue(col.Field.Key, out string value)) // { // int colIndex = col.SelectedCsvColumn.Header.Key; // file.CsvRow[colIndex] = value; // } // } // } //} CsvReader.WriteCSVToDisk(CsvPath, headers, rows, ";", SelectedEncoding); if (_cancellationPending) { Logger.LogI("Import cancelled"); return; } } catch (Exception ex) { Logger.LogE($"Error in ProcessImport 2: {ex.ToString()}"); file.Errors.Add($"An unexpected exception has occurred: {ex.ToString()}"); } WriteDebugFile(file, i); } ProgressText = "Processing finished"; } catch (Exception ex) { Logger.LogE($"Unhandled Exception: {ex.ToString()}"); } }