|
| 1 | +/* |
| 2 | +MIT License |
| 3 | +
|
| 4 | +Copyright (c) 2016 Kent Randall |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | + |
| 26 | +using System; |
| 27 | +using System.Collections.Generic; |
| 28 | +using System.Linq; |
| 29 | +using System.Text; |
| 30 | +using System.Threading.Tasks; |
| 31 | +using NodaTime; |
| 32 | + |
| 33 | +namespace Point85.ShiftSharp.Schedule |
| 34 | +{ |
| 35 | + /// <summary> |
| 36 | + /// Class Rotation maintains a sequenced list of shift and off-shift time periods. |
| 37 | + /// </summary> |
| 38 | + public class Rotation : Named, IComparable<Rotation> |
| 39 | + { |
| 40 | + // working periods in the rotation |
| 41 | + private List<RotationSegment> rotationSegments = new List<RotationSegment>(); |
| 42 | + |
| 43 | + // list of working and non-working days |
| 44 | + private List<TimePeriod> periods; |
| 45 | + |
| 46 | + // name of the day off time period |
| 47 | + private const string DAY_OFF_NAME = "DAY_OFF"; |
| 48 | + |
| 49 | + // 24-hour day off period |
| 50 | + private static DayOff DAY_OFF;// = InitializeDayOff(); |
| 51 | + |
| 52 | + static Rotation() |
| 53 | + { |
| 54 | + DayOff dayOff = null; |
| 55 | + try |
| 56 | + { |
| 57 | + dayOff = new DayOff(DAY_OFF_NAME, "24 hour off period", LocalTime.Midnight, Duration.FromHours(24)); |
| 58 | + } |
| 59 | + catch (Exception) |
| 60 | + { |
| 61 | + // ignore |
| 62 | + } |
| 63 | + DAY_OFF = dayOff; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Default constructor |
| 68 | + */ |
| 69 | + public Rotation() : base() |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Constructor |
| 75 | + * |
| 76 | + * @param name |
| 77 | + * Rotation name |
| 78 | + * @param description |
| 79 | + * Description |
| 80 | + * @throws Exception |
| 81 | + * Exception |
| 82 | + */ |
| 83 | + public Rotation(string name, string description) : base(name, description) |
| 84 | + { |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Get the shifts and off-shifts in the rotation |
| 90 | + * |
| 91 | + * @return List of periods |
| 92 | + */ |
| 93 | + public List<TimePeriod> GetPeriods() |
| 94 | + { |
| 95 | + if (periods == null) |
| 96 | + { |
| 97 | + periods = new List<TimePeriod>(); |
| 98 | + |
| 99 | + // sort by sequence number |
| 100 | + rotationSegments.Sort(); |
| 101 | + |
| 102 | + foreach (RotationSegment segment in rotationSegments) |
| 103 | + { |
| 104 | + // add the on days |
| 105 | + if (segment.GetStartingShift() != null) |
| 106 | + { |
| 107 | + for (int i = 0; i < segment.GetDaysOn(); i++) |
| 108 | + { |
| 109 | + periods.Add(segment.GetStartingShift()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // add the off days |
| 114 | + for (int i = 0; i < segment.GetDaysOff(); i++) |
| 115 | + { |
| 116 | + periods.Add(Rotation.DAY_OFF); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return periods; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Get the number of days in the rotation |
| 126 | + * |
| 127 | + * @return Day count |
| 128 | + */ |
| 129 | + |
| 130 | + public int GetDayCount() |
| 131 | + { |
| 132 | + return GetPeriods().Count; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get the duration of this rotation |
| 137 | + * |
| 138 | + * @return Duration |
| 139 | + */ |
| 140 | + public Duration GetDuration() |
| 141 | + { |
| 142 | + return Duration.FromDays(GetPeriods().Count); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get the shift rotation's total working time |
| 147 | + * |
| 148 | + * @return Duration of working time |
| 149 | + */ |
| 150 | + public Duration GetWorkingTime() |
| 151 | + { |
| 152 | + Duration sum = Duration.Zero; |
| 153 | + |
| 154 | + foreach (TimePeriod period in GetPeriods()) |
| 155 | + { |
| 156 | + if (period.IsWorkingPeriod()) |
| 157 | + { |
| 158 | + sum = sum.Plus(period.GetDuration()); |
| 159 | + } |
| 160 | + } |
| 161 | + return sum; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get the rotation's working periods |
| 166 | + * |
| 167 | + * @return List of {@link RotationSegment} |
| 168 | + */ |
| 169 | + public List<RotationSegment> GetRotationSegments() |
| 170 | + { |
| 171 | + return rotationSegments; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Add a working period to this rotation. A working period starts with a |
| 176 | + * shift and specifies the number of days on and days off |
| 177 | + * |
| 178 | + * @param startingShift |
| 179 | + * {@link Shift} that starts the period |
| 180 | + * @param daysOn |
| 181 | + * Number of days on shift |
| 182 | + * @param daysOff |
| 183 | + * Number of days off shift |
| 184 | + * @return {@link RotationSegment} |
| 185 | + * @throws Exception |
| 186 | + * Exception |
| 187 | + */ |
| 188 | + public RotationSegment AddSegment(Shift startingShift, int daysOn, int daysOff) |
| 189 | + { |
| 190 | + if (startingShift == null) |
| 191 | + { |
| 192 | + throw new Exception("The starting shift must be specified."); |
| 193 | + } |
| 194 | + RotationSegment segment = new RotationSegment(startingShift, daysOn, daysOff, this); |
| 195 | + rotationSegments.Add(segment); |
| 196 | + segment.SetSequence(rotationSegments.Count); |
| 197 | + return segment; |
| 198 | + } |
| 199 | + |
| 200 | + public int CompareTo(Rotation other) |
| 201 | + { |
| 202 | + return GetName().CompareTo(other.GetName()); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Build a string representation of this rotation |
| 207 | + */ |
| 208 | + public override string ToString() |
| 209 | + { |
| 210 | + string named = base.ToString(); |
| 211 | + string rd = WorkSchedule.GetMessage("rotation.duration"); |
| 212 | + string rda = WorkSchedule.GetMessage("rotation.days"); |
| 213 | + string rw = WorkSchedule.GetMessage("rotation.working"); |
| 214 | + string rper = WorkSchedule.GetMessage("rotation.periods"); |
| 215 | + string on = WorkSchedule.GetMessage("rotation.on"); |
| 216 | + string off = WorkSchedule.GetMessage("rotation.off"); |
| 217 | + |
| 218 | + string periodsString = ""; |
| 219 | + |
| 220 | + foreach (TimePeriod period in GetPeriods()) |
| 221 | + { |
| 222 | + if (periodsString.Length > 0) |
| 223 | + { |
| 224 | + periodsString += ", "; |
| 225 | + } |
| 226 | + |
| 227 | + string onOff = period.IsWorkingPeriod() ? on : off; |
| 228 | + periodsString += period.GetName() + " (" + onOff + ")"; |
| 229 | + } |
| 230 | + |
| 231 | + string text = named + "\n" + rper + ": [" + periodsString + "], " + rd + ": " + GetDuration() + ", " + rda |
| 232 | + + ": " + GetDuration().Days + ", " + rw + ": " + GetWorkingTime(); |
| 233 | + |
| 234 | + return text; |
| 235 | + } |
| 236 | + } |
| 237 | +} |
0 commit comments