| | 1 | | using MRA.DTO.Enums.Drawing; |
| | 2 | | using MRA.DTO.Enums.DrawingFilter; |
| | 3 | | using MRA.DTO.Models; |
| | 4 | | using MRA.Infrastructure.Enums; |
| | 5 | | using MRA.Infrastructure.Settings; |
| | 6 | | using static MRA.Infrastructure.Settings.Options.DatabaseSettings.DatabaseDrawingsTagsOptions; |
| | 7 | |
|
| | 8 | | namespace MRA.Services.Models.Drawings; |
| | 9 | |
|
| | 10 | | public class DrawingTagManager |
| | 11 | | { |
| | 12 | | public const string TAG_SEPARATOR = " "; |
| | 13 | |
|
| | 14 | | private readonly AppSettings _appConfig; |
| | 15 | |
|
| | 16 | |
|
| 20 | 17 | | public DrawingTagManager(AppSettings appConfig) |
| | 18 | | { |
| 20 | 19 | | _appConfig = appConfig; |
| 20 | 20 | | } |
| | 21 | |
|
| | 22 | |
|
| | 23 | | public DrawingModel SetAutomaticTags(DrawingModel document) |
| | 24 | | { |
| 1 | 25 | | var list = new List<string>(); |
| 1 | 26 | | list = SetAutomaticTags_Name(document, list).ToList(); |
| 1 | 27 | | list = SetAutomaticTags_ModelName(document, list).ToList(); |
| 1 | 28 | | list = SetAutomaticTags_Title(document, list).ToList(); |
| 1 | 29 | | list = SetAutomaticTags_Software(document, list).ToList(); |
| 1 | 30 | | list = SetAutomaticTags_Paper(document, list).ToList(); |
| 1 | 31 | | list = SetAutomaticTags_Type(document, list).ToList(); |
| 1 | 32 | | list = SetAutomaticTags_ProductType(document, list).ToList(); |
| 1 | 33 | | list = SetAutomaticTags_ProductName(document, list).ToList(); |
| 1 | 34 | | list = SetAutomaticTags_Tags(document, list).ToList(); |
| 1 | 35 | | document.Tags = DeleteAndAdjustTags(list); |
| | 36 | |
|
| 1 | 37 | | return document; |
| | 38 | | } |
| | 39 | |
|
| | 40 | | private static IEnumerable<string> SetAutomaticTags_Name(DrawingModel document, List<string> list) |
| | 41 | | { |
| 3 | 42 | | list.AddRange(document.Name.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| 1 | 43 | | return list; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | private static IEnumerable<string> SetAutomaticTags_ModelName(DrawingModel document, List<string> list) |
| | 47 | | { |
| 3 | 48 | | list.AddRange(document.ModelName.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| 1 | 49 | | return list; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | private static IEnumerable<string> SetAutomaticTags_Title(DrawingModel document, List<string> list) |
| | 53 | | { |
| 5 | 54 | | list.AddRange(document.Title.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| 1 | 55 | | return list; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | private static IEnumerable<string> SetAutomaticTags_Software(DrawingModel document, List<string> list) |
| | 59 | | { |
| 1 | 60 | | if (document.Software != (int) EnumExtensions.GetDefaultValue<DrawingSoftwares>()) |
| | 61 | | { |
| 4 | 62 | | list.AddRange(document.SoftwareName.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| | 63 | | } |
| 1 | 64 | | return list; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | private static IEnumerable<string> SetAutomaticTags_Paper(DrawingModel document, List<string> list) |
| | 68 | | { |
| 1 | 69 | | if (document.Paper != (int) EnumExtensions.GetDefaultValue<DrawingPaperSizes>()) |
| | 70 | | { |
| 2 | 71 | | list.AddRange(document.PaperHuman.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| | 72 | | } |
| 1 | 73 | | return list; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private static IEnumerable<string> SetAutomaticTags_Type(DrawingModel document, List<string> list) |
| | 77 | | { |
| 1 | 78 | | if (document.Type != (int) EnumExtensions.GetDefaultValue<DrawingTypes>()) |
| | 79 | | { |
| 2 | 80 | | list.AddRange(document.TypeName.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| | 81 | | } |
| 1 | 82 | | return list; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | private static IEnumerable<string> SetAutomaticTags_ProductType(DrawingModel document, List<string> list) |
| | 86 | | { |
| 1 | 87 | | if (document.ProductType != (int) EnumExtensions.GetDefaultValue<DrawingProductTypes>()) |
| | 88 | | { |
| 2 | 89 | | list.AddRange(document.ProductTypeName.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| | 90 | | } |
| 1 | 91 | | return list; |
| | 92 | | } |
| | 93 | |
|
| | 94 | | private static IEnumerable<string> SetAutomaticTags_ProductName(DrawingModel document, List<string> list) |
| | 95 | | { |
| 4 | 96 | | list.AddRange(document.ProductName.Split(TAG_SEPARATOR).Select(x => x.ToLower())); |
| 1 | 97 | | return list; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | private static IEnumerable<string> SetAutomaticTags_Tags(DrawingModel document, List<string> list) |
| | 101 | | { |
| 1 | 102 | | list.AddRange(document.Tags); |
| 1 | 103 | | return list; |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public IEnumerable<string> DeleteAndAdjustTags(IEnumerable<string> tags) |
| | 107 | | { |
| 2 | 108 | | var toDelete = _appConfig.Database.Drawings.Tags.Delete; |
| 2 | 109 | | var toReplace = _appConfig.Database.Drawings.Tags.Replace; |
| | 110 | |
|
| 80 | 111 | | var processedTags = tags.Select(tag => ReplaceCharacters(tag, toReplace)); |
| | 112 | |
|
| 2 | 113 | | var filteredTags = processedTags |
| 78 | 114 | | .SelectMany(tag => SplitAndFilter(tag, toDelete)) |
| 2 | 115 | | .Distinct(); |
| | 116 | |
|
| 2 | 117 | | return filteredTags; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | private static string ReplaceCharacters(string tag, IEnumerable<DatabaseDrawingsTagsReplaceOptions> toReplace) |
| | 121 | | { |
| 78 | 122 | | var result = tag.ToLower(); |
| 468 | 123 | | foreach (var change in toReplace) |
| | 124 | | { |
| 156 | 125 | | result = result.Replace(change.Key, change.Value); |
| | 126 | | } |
| 78 | 127 | | return result; |
| | 128 | | } |
| | 129 | |
|
| | 130 | | private static IEnumerable<string> SplitAndFilter(string tag, IEnumerable<string> toDelete) |
| | 131 | | { |
| 78 | 132 | | return tag.Split(TAG_SEPARATOR) |
| 78 | 133 | | .Select(part => part.Trim().ToLower()) |
| 156 | 134 | | .Where(part => !string.IsNullOrEmpty(part) && !toDelete.Contains(part)); |
| | 135 | | } |
| | 136 | | } |