Exemplo n.º 1
0
        public override void WriteAttributeContent(PhaidraFile file, Dictionary <string, string> values)
        {
            var jsonLd = GetJsonLd(file.Metadata);

            string keywordStr = GetStringValue(values, CONST_Keyword);

            if (!string.IsNullOrEmpty(keywordStr))
            {
                var keywords = keywordStr.Split(CONST_SplitChars, StringSplitOptions.RemoveEmptyEntries).Select(i => i.Trim()).ToList();

                JArray subjectArray = GetOrCreateProperty <JArray>(jsonLd, "dce:subject");
                foreach (var keyword in keywords)
                {
                    if (!string.IsNullOrEmpty(keyword))
                    {
                        string language = GetStringValue(values, CONST_KeywordLang);

                        var prefLabelObj = new JObject(
                            new JProperty("@value", keyword)
                            );

                        if (!string.IsNullOrEmpty(language))
                        {
                            prefLabelObj.Add("@language", language);
                        }

                        subjectArray.Add(new JObject(
                                             new JProperty("skos:prefLabel", new JArray(prefLabelObj)),
                                             new JProperty("@type", "skos:Concept")));
                    }
                }
            }
        }
Exemplo n.º 2
0
        private static void ShowCreatedMetadata(PhaidraAttribute attribute, Dictionary <string, string> values)
        {
            PhaidraFile phaidraFile = new PhaidraFile();

            attribute.WriteAttributeContent(phaidraFile, values);
            Console.WriteLine($"Attribute {attribute.Name}:");
            Console.WriteLine(phaidraFile.Metadata.ToString());
        }
Exemplo n.º 3
0
        private async void DebugExecute(object obj)
        {
            var vm = new DebugViewModel();

            bool          valid;
            List <string> messages;
            var           mappings = GetMappingsWithMessages(out valid, out messages);

            if (valid)
            {
                var metadataGenerator = new PhaidraMetadataGenerator(mappings);

                PhaidraFile file = new PhaidraFile();
                file.BaseDirectory = _rootDir;
                if (_currentRowIndex < _rows.Count)
                {
                    StringBuilder debugSb = new StringBuilder();
                    metadataGenerator.SetMetadata(_rows[_currentRowIndex], file);
                    debugSb.AppendLine("Metadata: ");
                    debugSb.AppendLine(file.Metadata.ToString());

                    debugSb.AppendLine("Full file name: ");
                    debugSb.AppendLine(file.Fullfilename);
                    debugSb.AppendLine("Collection path: ");
                    debugSb.AppendLine(file.CollectionPath);
                    debugSb.AppendLine("Phaidra type: ");
                    debugSb.AppendLine(file.PhaidraType);
                    debugSb.AppendLine("Mime type: ");
                    debugSb.AppendLine(file.MimeType);
                    if (file.Errors.Count > 0)
                    {
                        debugSb.AppendLine("File Errors: ");
                        foreach (var fileError in file.Errors)
                        {
                            debugSb.AppendLine($"- {fileError}");
                        }
                    }
                    vm.DebugText = debugSb.ToString();
                }
            }
            else
            {
                StringBuilder errorSb = new StringBuilder();
                errorSb.AppendLine("Errors: ");

                foreach (var message in messages)
                {
                    errorSb.AppendLine($"- {message}");
                }
                vm.DebugText = errorSb.ToString();
            }

            DebugWindow window = new DebugWindow(vm);
            await window.ShowDialog(_parentWindow);
        }
 public void SetMetadata(Dictionary <int, string> row, PhaidraFile 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);
     }
 }
Exemplo n.º 5
0
        private void WriteDebugFile(PhaidraFile file, int index)
        {
            if (file.Errors.Count > 0)
            {
                StringBuilder sbErr = new StringBuilder();
                sbErr.AppendLine("File has Errors: ");

                foreach (var error in file.Errors)
                {
                    sbErr.AppendLine($" - {error}");
                }

                Logger.LogE(sbErr.ToString());
            }

            if (!PhaidraConfig.Instance.WriteDebugFiles)
            {
                return;
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"Method: {file.PhaidraType}");
            sb.AppendLine($"Full file name: {file.Fullfilename}");
            sb.AppendLine($"Mime type: {file.MimeType}");
            sb.AppendLine($"Collection path: {file.CollectionPath}");
            sb.AppendLine($"Errors: \r\n{string.Join("\r\n", file.Errors)}\r\n");
            sb.AppendLine($"Meta data: \r\n\r\n{file.Metadata.ToString()}");

            string filename = "";

            if (!string.IsNullOrEmpty(file.Fullfilename))
            {
                filename = "_" + Path.GetFileName(file.Fullfilename);
            }

            var fi = new FileInfo(Path.Combine(CsvFI.Directory.FullName, "DebugFiles", (index.ToString().PadLeft(4, '0')) + filename + ".txt"));

            if (!fi.Directory.Exists)
            {
                fi.Directory.Create();
            }
            File.WriteAllText(fi.FullName, sb.ToString());
        }
 public void WriteAttributeContentToCSV(PhaidraFile 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;
                 }
             }
         }
     }
 }
Exemplo n.º 7
0
        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()}");
            }
        }