| | 1 | | using MUNity.Models.Resolution; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Text; |
| | 5 | | using System.Linq; |
| | 6 | | using MUNity.Extensions.ObservableCollectionExtensions; |
| | 7 | |
|
| | 8 | | namespace MUNity.Extensions.ResolutionExtensions |
| | 9 | | { |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Get some more funcinality for working with amendments on the Resolution. documents |
| | 13 | | /// </summary> |
| | 14 | | public static class AmendmentExtensions |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// Returns all the Amendments for the operative paragraph with the given Id. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="section"></param> |
| | 20 | | /// <param name="id"></param> |
| | 21 | | /// <returns></returns> |
| | 22 | | public static List<AbstractAmendment> AmendmentsForOperativeParagraph(this OperativeSection section, string id) |
| 0 | 23 | | { |
| 0 | 24 | | var result = new List<AbstractAmendment>(); |
| | 25 | |
|
| 0 | 26 | | result.AddRange(section.AddAmendments.Where(n => n.TargetSectionId == id)); |
| 0 | 27 | | result.AddRange(section.ChangeAmendments.Where(n => n.TargetSectionId == id)); |
| 0 | 28 | | result.AddRange(section.DeleteAmendments.Where(n => n.TargetSectionId == id)); |
| 0 | 29 | | result.AddRange(section.MoveAmendments.Where(n => n.TargetSectionId == id)); |
| 0 | 30 | | return result; |
| 0 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Adds a new Amendment into the Amendment list. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="section"></param> |
| | 37 | | /// <param name="amendment"></param> |
| | 38 | | private static void PushAmendment(this OperativeSection section, AbstractAmendment amendment) |
| 0 | 39 | | { |
| | 40 | | // For now every Amendment has a TargetSectionId this could maybe be different one day |
| | 41 | | // Remember to move this function if this day ever comes. |
| 0 | 42 | | if (section.FirstOrDefault(n => n.OperativeParagraphId == amendment.TargetSectionId) == null) |
| 0 | 43 | | throw new MUNity.Exceptions.Resolution.OperativeParagraphNotFoundException(); |
| | 44 | |
|
| 0 | 45 | | if (amendment is AddAmendment addAmendment) |
| 0 | 46 | | { |
| 0 | 47 | | section.AddAmendments.Add(addAmendment); |
| 0 | 48 | | } |
| 0 | 49 | | else if (amendment is ChangeAmendment changeAmendment) |
| 0 | 50 | | { |
| 0 | 51 | | section.ChangeAmendments.Add(changeAmendment); |
| 0 | 52 | | } |
| 0 | 53 | | else if (amendment is DeleteAmendment deleteAmendment) |
| 0 | 54 | | { |
| 0 | 55 | | section.DeleteAmendments.Add(deleteAmendment); |
| 0 | 56 | | } |
| 0 | 57 | | else if (amendment is MoveAmendment moveAmendment) |
| 0 | 58 | | { |
| 0 | 59 | | section.MoveAmendments.Add(moveAmendment); |
| 0 | 60 | | } |
| | 61 | | else |
| 0 | 62 | | { |
| 0 | 63 | | throw new MUNity.Exceptions.Resolution.UnsupportedAmendmentTypeException(); |
| | 64 | | } |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Removes an amentment from its list. Note that this is not the same |
| | 69 | | /// as Deny. This will just remove the given instance of the amendment. |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="section"></param> |
| | 72 | | /// <param name="amendment"></param> |
| | 73 | | public static void RemoveAmendment(this OperativeSection section, AbstractAmendment amendment) |
| 0 | 74 | | { |
| 0 | 75 | | if (amendment is AddAmendment addAmendment) |
| 0 | 76 | | { |
| 0 | 77 | | section.AddAmendments.RemoveAll(n => n.TargetSectionId == amendment.TargetSectionId); |
| 0 | 78 | | section.Paragraphs.RemoveAll(n => n.OperativeParagraphId == amendment.TargetSectionId); |
| 0 | 79 | | } |
| 0 | 80 | | else if (amendment is ChangeAmendment changeAmendment) |
| 0 | 81 | | { |
| 0 | 82 | | section.ChangeAmendments.Remove(changeAmendment); |
| 0 | 83 | | } |
| 0 | 84 | | else if (amendment is DeleteAmendment deleteAmendment) |
| 0 | 85 | | { |
| 0 | 86 | | section.DeleteAmendments.Remove(deleteAmendment); |
| 0 | 87 | | } |
| 0 | 88 | | else if (amendment is MoveAmendment moveAmendment) |
| 0 | 89 | | { |
| 0 | 90 | | section.MoveAmendments.Remove(moveAmendment); |
| 0 | 91 | | section.Paragraphs.RemoveAll(n => moveAmendment.NewTargetSectionId == n.OperativeParagraphId); |
| 0 | 92 | | } |
| | 93 | | else |
| 0 | 94 | | { |
| 0 | 95 | | throw new MUNity.Exceptions.Resolution.UnsupportedAmendmentTypeException(); |
| | 96 | | } |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | /// <summary> |
| | 100 | | /// Creates a new Amendment to delete an operative paragraph. |
| | 101 | | /// </summary> |
| | 102 | | /// <param name="section"></param> |
| | 103 | | /// <param name="paragraphId"></param> |
| | 104 | | /// <returns></returns> |
| | 105 | | public static DeleteAmendment CreateDeleteAmendment(this OperativeSection section, string paragraphId) |
| 0 | 106 | | { |
| 0 | 107 | | if (section.FindOperativeParagraph(paragraphId) == null) |
| 0 | 108 | | throw new MUNity.Exceptions.Resolution.OperativeParagraphNotFoundException(); |
| | 109 | |
|
| 0 | 110 | | DeleteAmendment newAmendment = new DeleteAmendment |
| 0 | 111 | | { |
| 0 | 112 | | TargetSectionId = paragraphId |
| 0 | 113 | | }; |
| 0 | 114 | | section.PushAmendment(newAmendment); |
| 0 | 115 | | return newAmendment; |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Creates a new Amendment for a TextChange of a paragraph with a given Id. |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="section">The operative Section of the resolution that this Admendment should be created in and |
| | 122 | | /// <param name="paragraphId">The OperativeParagraphId of the target paragraph that the text should be changed i |
| | 123 | | /// <param name="newText">The new Text that the paragraph should be set to if the created Amendment is Accepted/ |
| | 124 | | /// <returns></returns> |
| | 125 | | public static ChangeAmendment CreateChangeAmendment(this OperativeSection section, string paragraphId, string ne |
| 0 | 126 | | { |
| 0 | 127 | | if (section.FindOperativeParagraph(paragraphId) == null) |
| 0 | 128 | | throw new MUNity.Exceptions.Resolution.OperativeParagraphNotFoundException(); |
| | 129 | |
|
| 0 | 130 | | var newAmendment = new ChangeAmendment |
| 0 | 131 | | { |
| 0 | 132 | | TargetSectionId = paragraphId, |
| 0 | 133 | | NewText = newText |
| 0 | 134 | | }; |
| 0 | 135 | | section.PushAmendment(newAmendment); |
| 0 | 136 | | return newAmendment; |
| 0 | 137 | | } |
| | 138 | |
|
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Creates a Move Amendment. This will also create a new Operative paragraph at the palce where the amendment s |
| | 142 | | /// will copy the text once when the amendment is created and once when the amendment is accepted. If the text o |
| | 143 | | /// text of the virtual operative paragraph may differ. |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="section"></param> |
| | 146 | | /// <param name="paragraphId"></param> |
| | 147 | | /// <param name="targetIndex"></param> |
| | 148 | | /// <param name="parentParagraph"></param> |
| | 149 | | /// <returns></returns> |
| | 150 | | public static MoveAmendment CreateMoveAmendment(this OperativeSection section, string paragraphId, int targetInd |
| 0 | 151 | | { |
| 0 | 152 | | var sourceParagraph = section.FindOperativeParagraph(paragraphId); |
| 0 | 153 | | if (sourceParagraph == null) |
| 0 | 154 | | throw new MUNity.Exceptions.Resolution.OperativeParagraphNotFoundException(); |
| | 155 | |
|
| 0 | 156 | | var newAmendment = new MoveAmendment |
| 0 | 157 | | { |
| 0 | 158 | | TargetSectionId = paragraphId |
| 0 | 159 | | }; |
| 0 | 160 | | var virtualParagraph = new OperativeParagraph |
| 0 | 161 | | { |
| 0 | 162 | | IsLocked = true, |
| 0 | 163 | | IsVirtual = true, |
| 0 | 164 | | Text = sourceParagraph.Text |
| 0 | 165 | | }; |
| 0 | 166 | | newAmendment.NewTargetSectionId = virtualParagraph.OperativeParagraphId; |
| 0 | 167 | | section.InsertIntoRealPosition(virtualParagraph, targetIndex, parentParagraph); |
| 0 | 168 | | section.PushAmendment(newAmendment); |
| 0 | 169 | | return newAmendment; |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | /// <summary> |
| | 173 | | /// Creates a new Add Amendment to add a new operative paragraph at a certain position. This will also create a |
| | 174 | | /// the new paragraph may be when it is accepted. |
| | 175 | | /// </summary> |
| | 176 | | /// <param name="section">The operative Section of the Resolution where this paragraph should be added.</param> |
| | 177 | | /// <param name="targetIndex">The index where the paragraph should be added.</param> |
| | 178 | | /// <param name="text">The Text of the new paragraph</param> |
| | 179 | | /// <param name="parentParagraph">Use this parent paragraph if the new paragraph should be a sub point</param> |
| | 180 | | /// <returns></returns> |
| | 181 | | public static AddAmendment CreateAddAmendment(this OperativeSection section, int targetIndex, string text = "", |
| 0 | 182 | | { |
| 0 | 183 | | var virtualParagraph = new OperativeParagraph(text) |
| 0 | 184 | | { |
| 0 | 185 | | IsVirtual = true, |
| 0 | 186 | | Visible = false |
| 0 | 187 | | }; |
| 0 | 188 | | section.InsertIntoRealPosition(virtualParagraph, targetIndex, parentParagraph); |
| 0 | 189 | | var amendment = new AddAmendment |
| 0 | 190 | | { |
| 0 | 191 | | TargetSectionId = virtualParagraph.OperativeParagraphId |
| 0 | 192 | | }; |
| 0 | 193 | | section.PushAmendment(amendment); |
| 0 | 194 | | return amendment; |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | /// <summary> |
| | 198 | | /// Creates a new amendment to delete a certain operative paragraph. |
| | 199 | | /// </summary> |
| | 200 | | /// <param name="section"></param> |
| | 201 | | /// <param name="paragraph"></param> |
| | 202 | | /// <returns></returns> |
| 0 | 203 | | public static DeleteAmendment CreateDeleteAmendment(this OperativeSection section, OperativeParagraph paragraph) |
| | 204 | |
|
| | 205 | | /// <summary> |
| | 206 | | /// Creates a new Move Amendment. The given targetIndex is the position the new Virtual Paragraph will be locate |
| | 207 | | /// Type in 0 to move it to the beginning of the list. Note that 1 will also move it to the start! |
| | 208 | | /// When you have two Paragraphs (A and B) and want A to move behind B (B, A) you need to set the targetIndex to |
| | 209 | | /// </summary> |
| | 210 | | /// <param name="section">The Operative Section that you want to crate the Move Amendment at.</param> |
| | 211 | | /// <param name="paragraph"></param> |
| | 212 | | /// <param name="targetIndex"></param> |
| | 213 | | /// <param name="parentParagraph"></param> |
| | 214 | | /// <returns></returns> |
| | 215 | | public static MoveAmendment CreateMoveAmendment(this OperativeSection section, OperativeParagraph paragraph, int |
| 0 | 216 | | section.CreateMoveAmendment(paragraph.OperativeParagraphId, targetIndex, parentParagraph); |
| | 217 | |
|
| | 218 | | /// <summary> |
| | 219 | | /// Creates a new Text change amendment. |
| | 220 | | /// </summary> |
| | 221 | | /// <param name="section"></param> |
| | 222 | | /// <param name="paragraph"></param> |
| | 223 | | /// <param name="newText"></param> |
| | 224 | | /// <returns></returns> |
| 0 | 225 | | public static ChangeAmendment CreateChangeAmendment(this OperativeSection section, OperativeParagraph paragraph, |
| | 226 | | } |
| | 227 | | } |