| | 1 | | using MUNity.Extensions.ResolutionExtensions; |
| | 2 | | using MUNity.Models.Resolution; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Collections.ObjectModel; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Text; |
| | 8 | |
|
| | 9 | | namespace MUNity.Troubleshooting.Resa |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// This component searches for errors inside a given Resolution and returns a bug report and will fix some bugs ins |
| | 13 | | /// document. |
| | 14 | | /// </summary> |
| | 15 | | public class ResolutionTroubleshooting |
| | 16 | | { |
| | 17 | | private static IEnumerable<IResolutionBug> Bugfinder |
| | 18 | | { |
| | 19 | | get |
| 0 | 20 | | { |
| 0 | 21 | | yield return new InvalidResolutionHeader(); |
| 0 | 22 | | yield return new InvalidPreamble(); |
| 0 | 23 | | yield return new InvalidOperativeSection(); |
| 0 | 24 | | yield return new InvalidAmendments(); |
| 0 | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Something in the Header is invalid. For example a string is null, but strings should always be string.Empty |
| | 30 | | /// </summary> |
| | 31 | | public class InvalidResolutionHeader : IResolutionBug |
| | 32 | | { |
| 0 | 33 | | private string detectedError = ""; |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Gives back a string containing information about the error. |
| | 37 | | /// </summary> |
| 0 | 38 | | public string Description => detectedError; |
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Detects bugs inside the Header of a resolution and returns true if bugs are found. |
| | 42 | | /// </summary> |
| | 43 | | /// <param name="resolution"></param> |
| | 44 | | /// <returns></returns> |
| | 45 | | public bool Detect(Resolution resolution) |
| 0 | 46 | | { |
| 0 | 47 | | Dictionary<string, object> notNullProps = new Dictionary<string, object>() |
| 0 | 48 | | { |
| 0 | 49 | | { "Header", resolution.Header }, |
| 0 | 50 | | { "AgendaItem", resolution.Header?.AgendaItem }, |
| 0 | 51 | | { "CommitteeName", resolution.Header?.CommitteeName }, |
| 0 | 52 | | { "FullName", resolution.Header?.FullName }, |
| 0 | 53 | | { "Name", resolution.Header?.Name }, |
| 0 | 54 | | { "Session", resolution.Header?.Session }, |
| 0 | 55 | | { "SubmitterName", resolution.Header?.SubmitterName }, |
| 0 | 56 | | { "Topic", resolution.Header?.Topic } |
| 0 | 57 | | }; |
| | 58 | |
|
| 0 | 59 | | foreach (var element in notNullProps) |
| 0 | 60 | | { |
| 0 | 61 | | if (element.Value == null) detectedError += $"{element.Key} is not allowed to be null.\n"; |
| 0 | 62 | | } |
| 0 | 63 | | return notNullProps.ContainsValue(null); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Fixes the bugs that where found before. |
| | 68 | | /// </summary> |
| | 69 | | /// <param name="resolution"></param> |
| | 70 | | /// <returns></returns> |
| | 71 | | public bool Fix(Resolution resolution) |
| 0 | 72 | | { |
| 0 | 73 | | if (resolution.Header == null) resolution.Header = new ResolutionHeader(); |
| 0 | 74 | | if (resolution.Header.AgendaItem == null) resolution.Header.AgendaItem = string.Empty; |
| 0 | 75 | | if (resolution.Header.CommitteeName == null) resolution.Header.CommitteeName = string.Empty; |
| 0 | 76 | | if (resolution.Header.FullName == null) resolution.Header.FullName = string.Empty; |
| 0 | 77 | | if (resolution.Header.Name == null) resolution.Header.Name = string.Empty; |
| 0 | 78 | | if (resolution.Header.Session == null) resolution.Header.Session = string.Empty; |
| 0 | 79 | | if (resolution.Header.SubmitterName == null) resolution.Header.SubmitterName = string.Empty; |
| 0 | 80 | | if (resolution.Header.Topic == null) resolution.Header.Topic = string.Empty; |
| 0 | 81 | | return true; |
| 0 | 82 | | } |
| | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// A component to check the preamble for errors. |
| | 87 | | /// </summary> |
| | 88 | | public class InvalidPreamble : IResolutionBug |
| | 89 | | { |
| 0 | 90 | | private string output = ""; |
| | 91 | |
|
| | 92 | | /// <summary> |
| | 93 | | /// Description of bugs that are found inside the Preamble |
| | 94 | | /// </summary> |
| 0 | 95 | | public string Description => output; |
| | 96 | |
|
| | 97 | | /// <summary> |
| | 98 | | /// Detect bugs inside the preamble |
| | 99 | | /// </summary> |
| | 100 | | /// <param name="resolution"></param> |
| | 101 | | /// <returns></returns> |
| | 102 | | public bool Detect(Resolution resolution) |
| 0 | 103 | | { |
| 0 | 104 | | if (resolution.Preamble == null) |
| 0 | 105 | | { |
| 0 | 106 | | output = "Preamble cannot be null!"; |
| 0 | 107 | | return true; |
| | 108 | | } |
| 0 | 109 | | if (resolution.Preamble.Paragraphs == null) |
| 0 | 110 | | { |
| 0 | 111 | | output = "Preamble Paragraphs cannot be null"; |
| 0 | 112 | | return true; |
| | 113 | | } |
| 0 | 114 | | if (string.IsNullOrEmpty(resolution.Preamble.PreambleId)) |
| 0 | 115 | | { |
| 0 | 116 | | output = "Preamble needs to have an Id"; |
| 0 | 117 | | return true; |
| | 118 | | } |
| 0 | 119 | | return false; |
| 0 | 120 | | } |
| | 121 | |
|
| | 122 | | /// <summary> |
| | 123 | | /// Fixes known bugs inside the preamble. |
| | 124 | | /// </summary> |
| | 125 | | /// <param name="resolution"></param> |
| | 126 | | /// <returns></returns> |
| | 127 | | public bool Fix(Resolution resolution) |
| 0 | 128 | | { |
| 0 | 129 | | if (resolution.Preamble == null) resolution.Preamble = new ResolutionPreamble(); |
| 0 | 130 | | if (resolution.Preamble.Paragraphs == null) resolution.Preamble.Paragraphs = new List<PreambleParagraph> |
| 0 | 131 | | if (string.IsNullOrEmpty(resolution.Preamble.PreambleId)) resolution.Preamble.PreambleId = Guid.NewGuid( |
| 0 | 132 | | return true; |
| 0 | 133 | | } |
| | 134 | | } |
| | 135 | |
|
| | 136 | | /// <summary> |
| | 137 | | /// Resolution Bug Handler for Invalid Operative Sections |
| | 138 | | /// </summary> |
| | 139 | | public class InvalidOperativeSection : IResolutionBug |
| | 140 | | { |
| | 141 | |
|
| 0 | 142 | | private string output = ""; |
| | 143 | | /// <summary> |
| | 144 | | /// A Description of bugs that may be found inside the reoslution |
| | 145 | | /// </summary> |
| 0 | 146 | | public string Description => output; |
| | 147 | |
|
| | 148 | | /// <summary> |
| | 149 | | /// Try to find know bugs inside the resolution. |
| | 150 | | /// </summary> |
| | 151 | | /// <param name="resolution"></param> |
| | 152 | | /// <returns>Returns true if bugs are found or false if no known bugs are found.</returns> |
| | 153 | | public bool Detect(Resolution resolution) |
| 0 | 154 | | { |
| 0 | 155 | | if (resolution.OperativeSection == null) return true; |
| 0 | 156 | | if (resolution.OperativeSection.AddAmendments == null) return true; |
| 0 | 157 | | if (resolution.OperativeSection.ChangeAmendments == null) return true; |
| 0 | 158 | | if (resolution.OperativeSection.DeleteAmendments == null) return true; |
| 0 | 159 | | if (resolution.OperativeSection.MoveAmendments == null) return true; |
| 0 | 160 | | if (string.IsNullOrEmpty(resolution.OperativeSection.OperativeSectionId)) return true; |
| 0 | 161 | | if (resolution.OperativeSection.Paragraphs == null) return true; |
| 0 | 162 | | return false; |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// Attemps to fix the known bugs. Will rerun the Detection at the end and return true if the |
| | 167 | | /// detection cant find any more bugs. Will return false if there are still bugs. This may cant be fixed. |
| | 168 | | /// </summary> |
| | 169 | | /// <param name="resolution"></param> |
| | 170 | | /// <returns>Returns true when the bugs are fixed.</returns> |
| | 171 | | public bool Fix(Resolution resolution) |
| 0 | 172 | | { |
| 0 | 173 | | if (resolution.OperativeSection == null) resolution.OperativeSection = new OperativeSection(); |
| 0 | 174 | | if (resolution.OperativeSection.AddAmendments == null) resolution.OperativeSection.AddAmendments = new L |
| 0 | 175 | | if (resolution.OperativeSection.ChangeAmendments == null) resolution.OperativeSection.ChangeAmendments = |
| 0 | 176 | | if (resolution.OperativeSection.DeleteAmendments == null) resolution.OperativeSection.DeleteAmendments = |
| 0 | 177 | | if (resolution.OperativeSection.MoveAmendments == null) resolution.OperativeSection.MoveAmendments = new |
| 0 | 178 | | if (string.IsNullOrEmpty(resolution.OperativeSection.OperativeSectionId)) resolution.OperativeSection.Op |
| 0 | 179 | | if (resolution.OperativeSection.Paragraphs == null) resolution.OperativeSection.Paragraphs = new List<Op |
| 0 | 180 | | return Detect(resolution) == false; |
| 0 | 181 | | } |
| | 182 | | } |
| | 183 | |
|
| | 184 | | /// <summary> |
| | 185 | | /// Worker to find invalid amendments in the resolution |
| | 186 | | /// </summary> |
| | 187 | | public class InvalidAmendments : IResolutionBug |
| | 188 | | { |
| 0 | 189 | | private string bugs = ""; |
| | 190 | |
|
| | 191 | | /// <summary> |
| | 192 | | /// Description of bugs that may be found inside the resolution. |
| | 193 | | /// </summary> |
| 0 | 194 | | public string Description => bugs; |
| | 195 | |
|
| | 196 | | /// <summary> |
| | 197 | | /// Detect errors inside the resolutions amendments |
| | 198 | | /// </summary> |
| | 199 | | /// <param name="resolution"></param> |
| | 200 | | /// <returns></returns> |
| | 201 | | public bool Detect(Resolution resolution) |
| 0 | 202 | | { |
| 0 | 203 | | var ghosts = resolution.OperativeSection.WhereParagraph(n => n.IsVirtual && ( |
| 0 | 204 | | resolution.OperativeSection.MoveAmendments.All(a => a.NewTargetSectionId != n.OperativeParagraphId) |
| 0 | 205 | | resolution.OperativeSection.AddAmendments.All(a => a.TargetSectionId != n.OperativeParagraphId)) |
| 0 | 206 | | ); |
| | 207 | |
|
| 0 | 208 | | if (ghosts.Any()) |
| 0 | 209 | | { |
| 0 | 210 | | foreach (var ghost in ghosts) |
| 0 | 211 | | { |
| 0 | 212 | | bugs += $"{ghost.OperativeParagraphId} (Virutal: {ghost.IsVirtual}) is a ghost. There is no amen |
| 0 | 213 | | } |
| 0 | 214 | | } |
| | 215 | |
|
| 0 | 216 | | return bugs != ""; |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | /// <summary> |
| | 220 | | /// Fixes the known amendment bugs |
| | 221 | | /// </summary> |
| | 222 | | /// <param name="resolution"></param> |
| | 223 | | /// <returns>Will always return true.</returns> |
| | 224 | | public bool Fix(Resolution resolution) |
| 0 | 225 | | { |
| 0 | 226 | | var ghosts = resolution.OperativeSection.WhereParagraph(n => n.IsVirtual && (resolution.OperativeSection |
| 0 | 227 | | resolution.OperativeSection.AddAmendments.All(a => a.TargetSectionId != n.OperativeParagraphId))); |
| | 228 | |
|
| 0 | 229 | | if (ghosts.Any()) |
| 0 | 230 | | { |
| 0 | 231 | | foreach (var ghost in ghosts) |
| 0 | 232 | | { |
| 0 | 233 | | resolution.OperativeSection.RemoveOperativeParagraph(ghost); |
| 0 | 234 | | } |
| 0 | 235 | | } |
| | 236 | |
|
| 0 | 237 | | return true; |
| 0 | 238 | | } |
| | 239 | | } |
| | 240 | |
|
| | 241 | | /// <summary> |
| | 242 | | /// Check if a given Resolution is corrupted, has any kind of errors. |
| | 243 | | /// </summary> |
| | 244 | | /// <param name="resolution"></param> |
| | 245 | | /// <returns></returns> |
| | 246 | | public static (bool isCorrupted, string log) IsResolutionCorrupted(Resolution resolution) |
| 0 | 247 | | { |
| 0 | 248 | | var result = false; |
| 0 | 249 | | string output = ""; |
| 0 | 250 | | foreach (var finder in Bugfinder) |
| 0 | 251 | | { |
| 0 | 252 | | if (finder.Detect(resolution)) |
| 0 | 253 | | { |
| 0 | 254 | | result = true; |
| 0 | 255 | | output += finder.Description; |
| 0 | 256 | | } |
| 0 | 257 | | } |
| 0 | 258 | | return (result, output); |
| 0 | 259 | | } |
| | 260 | |
|
| | 261 | | /// <summary> |
| | 262 | | /// Fix the Resolution with the registered Bugfinders. |
| | 263 | | /// </summary> |
| | 264 | | /// <param name="resolution"></param> |
| | 265 | | /// <returns></returns> |
| | 266 | | public static bool FixResolution(Resolution resolution) |
| 0 | 267 | | { |
| 0 | 268 | | var result = true; |
| 0 | 269 | | foreach (var finder in Bugfinder) |
| 0 | 270 | | { |
| 0 | 271 | | if (!finder.Fix(resolution)) result = false; |
| 0 | 272 | | } |
| 0 | 273 | | return result; |
| 0 | 274 | | } |
| | 275 | | } |
| | 276 | | } |