-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
ProjectCalendar.java
2054 lines (1837 loc) · 60 KB
/
ProjectCalendar.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* file: ProjectCalendar.java
* author: Jon Iles
* copyright: (c) Packwood Software 2002-2003
* date: 28/11/2003
*/
/*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License, or (at your
* option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
package net.sf.mpxj;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import net.sf.mpxj.common.DateHelper;
import net.sf.mpxj.common.NumberHelper;
/**
* This class represents the a Calendar Definition record. Both base calendars
* and calendars derived from base calendars are represented by instances
* of this class. The class is used to define the working and non-working days
* of the week. The default calendar defines Monday to Friday as working days.
*/
public final class ProjectCalendar extends ProjectCalendarWeek implements ProjectEntityWithUniqueID
{
/**
* Default constructor.
*
* @param file the parent file to which this record belongs.
*/
public ProjectCalendar(ProjectFile file)
{
m_projectFile = file;
if (file.getProjectConfig().getAutoCalendarUniqueID() == true)
{
setUniqueID(Integer.valueOf(file.getProjectConfig().getNextCalendarUniqueID()));
}
}
/**
* Retrieve the number of minutes per day for this calendar.
*
* @return minutes per day
*/
public int getMinutesPerDay()
{
return m_minutesPerDay == null ? NumberHelper.getInt(getParentFile().getProjectProperties().getMinutesPerDay()) : m_minutesPerDay.intValue();
}
/**
* Retrieve the number of minutes per week for this calendar.
*
* @return minutes per week
*/
public int getMinutesPerWeek()
{
return m_minutesPerWeek == null ? NumberHelper.getInt(getParentFile().getProjectProperties().getMinutesPerWeek()) : m_minutesPerWeek.intValue();
}
/**
* Retrieve the number of minutes per month for this calendar.
*
* @return minutes per month
*/
public int getMinutesPerMonth()
{
return m_minutesPerMonth == null ? NumberHelper.getInt(getParentFile().getProjectProperties().getMinutesPerMonth()) : m_minutesPerMonth.intValue();
}
/**
* Retrieve the number of minutes per year for this calendar.
*
* @return minutes per year
*/
public int getMinutesPerYear()
{
return m_minutesPerYear == null ? NumberHelper.getInt(getParentFile().getProjectProperties().getMinutesPerYear()) : m_minutesPerYear.intValue();
}
/**
* Set the number of minutes per day for this calendar.
*
* @param minutes number of minutes
*/
public void setMinutesPerDay(Integer minutes)
{
m_minutesPerDay = minutes;
}
/**
* Set the number of minutes per week for this calendar.
*
* @param minutes number of minutes
*/
public void setMinutesPerWeek(Integer minutes)
{
m_minutesPerWeek = minutes;
}
/**
* Set the number of minutes per month for this calendar.
*
* @param minutes number of minutes
*/
public void setMinutesPerMonth(Integer minutes)
{
m_minutesPerMonth = minutes;
}
/**
* Set the number of minutes per year for this calendar.
*
* @param minutes number of minutes
*/
public void setMinutesPerYear(Integer minutes)
{
m_minutesPerYear = minutes;
}
/**
* Add an empty work week.
*
* @return new work week
*/
public ProjectCalendarWeek addWorkWeek()
{
ProjectCalendarWeek week = new ProjectCalendarWeek();
week.setParent(this);
m_workWeeks.add(week);
m_weeksSorted = false;
clearWorkingDateCache();
return week;
}
/**
* Clears the list of calendar exceptions.
*/
public void clearWorkWeeks()
{
m_workWeeks.clear();
m_weeksSorted = false;
clearWorkingDateCache();
}
/**
* Retrieve the work weeks associated with this calendar.
*
* @return list of work weeks
*/
public List<ProjectCalendarWeek> getWorkWeeks()
{
return m_workWeeks;
}
/**
* Used to add exceptions to the calendar. The MPX standard defines
* a limit of 250 exceptions per calendar.
*
* @param fromDate exception start date
* @param toDate exception end date
* @return ProjectCalendarException instance
*/
public ProjectCalendarException addCalendarException(Date fromDate, Date toDate)
{
ProjectCalendarException bce = new ProjectCalendarException(fromDate, toDate);
m_exceptions.add(bce);
m_expandedExceptions.clear();
m_exceptionsSorted = false;
clearWorkingDateCache();
return bce;
}
/**
* Clears the list of calendar exceptions.
*/
public void clearCalendarExceptions()
{
m_exceptions.clear();
m_expandedExceptions.clear();
m_exceptionsSorted = false;
clearWorkingDateCache();
}
/**
* This method retrieves a list of exceptions to the current calendar.
*
* @return List of calendar exceptions
*/
public List<ProjectCalendarException> getCalendarExceptions()
{
sortExceptions();
return m_exceptions;
}
/**
* Used to add working hours to the calendar. Note that the MPX file
* definition allows a maximum of 7 calendar hours records to be added to
* a single calendar.
*
* @param day day number
* @return new ProjectCalendarHours instance
*/
@Override public ProjectCalendarHours addCalendarHours(Day day)
{
clearWorkingDateCache();
return super.addCalendarHours(day);
}
/**
* Attaches a pre-existing set of hours to the correct
* day within the calendar.
*
* @param hours calendar hours instance
*/
@Override public void attachHoursToDay(ProjectCalendarHours hours)
{
clearWorkingDateCache();
super.attachHoursToDay(hours);
}
/**
* Removes a set of calendar hours from the day to which they
* are currently attached.
*
* @param hours calendar hours instance
*/
@Override public void removeHoursFromDay(ProjectCalendarHours hours)
{
clearWorkingDateCache();
super.removeHoursFromDay(hours);
}
/**
* Sets the ProjectCalendar instance from which this calendar is derived.
*
* @param calendar base calendar instance
*/
public void setParent(ProjectCalendar calendar)
{
// I've seen a malformed MSPDI file which sets the parent calendar to itself.
// Silently ignore this here.
if (calendar != this)
{
if (getParent() != null)
{
getParent().removeDerivedCalendar(this);
}
super.setParent(calendar);
if (calendar != null)
{
calendar.addDerivedCalendar(this);
}
clearWorkingDateCache();
}
}
@Override public ProjectCalendar getParent()
{
return (ProjectCalendar) super.getParent();
}
/**
* Method indicating whether a day is a working or non-working day.
*
* @param day required day
* @return true if this is a working day
*/
public boolean isWorkingDay(Day day)
{
DayType value = getWorkingDay(day);
boolean result;
if (value == DayType.DEFAULT)
{
ProjectCalendar cal = getParent();
if (cal != null)
{
result = cal.isWorkingDay(day);
}
else
{
result = (day != Day.SATURDAY && day != Day.SUNDAY);
}
}
else
{
result = (value == DayType.WORKING);
}
return (result);
}
/**
* This method is provided to allow an absolute period of time
* represented by start and end dates into a duration in working
* days based on this calendar instance. This method takes account
* of any exceptions defined for this calendar.
*
* @param startDate start of the period
* @param endDate end of the period
* @return new Duration object
*/
public Duration getDuration(Date startDate, Date endDate)
{
Calendar cal = DateHelper.popCalendar(startDate);
int days = getDaysInRange(startDate, endDate);
int duration = 0;
Day day = Day.getInstance(cal.get(Calendar.DAY_OF_WEEK));
while (days > 0)
{
if (isWorkingDate(cal.getTime(), day) == true)
{
++duration;
}
--days;
day = day.getNextDay();
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1);
}
DateHelper.pushCalendar(cal);
return (Duration.getInstance(duration, TimeUnit.DAYS));
}
/**
* Retrieves the time at which work starts on the given date, or returns
* null if this is a non-working day.
*
* @param date Date instance
* @return start time, or null for non-working day
*/
public Date getStartTime(Date date)
{
Date result = m_startTimeCache.get(date);
if (result == null)
{
ProjectCalendarDateRanges ranges = getRanges(date, null, null);
if (ranges == null)
{
result = getParentFile().getProjectProperties().getDefaultStartTime();
}
else
{
result = ranges.getRange(0).getStart();
}
result = DateHelper.getCanonicalTime(result);
m_startTimeCache.put(new Date(date.getTime()), result);
}
return result;
}
/**
* Retrieves the time at which work finishes on the given date, or returns
* null if this is a non-working day.
*
* @param date Date instance
* @return finish time, or null for non-working day
*/
public Date getFinishTime(Date date)
{
Date result = null;
if (date != null)
{
ProjectCalendarDateRanges ranges = getRanges(date, null, null);
if (ranges == null)
{
result = getParentFile().getProjectProperties().getDefaultEndTime();
result = DateHelper.getCanonicalTime(result);
}
else
{
Date rangeStart = result = ranges.getRange(0).getStart();
Date rangeFinish = ranges.getRange(ranges.getRangeCount() - 1).getEnd();
Date startDay = DateHelper.getDayStartDate(rangeStart);
Date finishDay = DateHelper.getDayStartDate(rangeFinish);
result = DateHelper.getCanonicalTime(rangeFinish);
//
// Handle the case where the end of the range is at midnight -
// this will show up as the start and end days not matching
//
if (startDay != null && finishDay != null && startDay.getTime() != finishDay.getTime())
{
result = DateHelper.addDays(result, 1);
}
}
}
return result;
}
/**
* Given a start date and a duration, this method calculates the
* end date. It takes account of working hours in each day, non working
* days and calendar exceptions. If the returnNextWorkStart parameter is
* set to true, the method will return the start date and time of the next
* working period if the end date is at the end of a working period.
*
* @param startDate start date
* @param duration duration
* @param returnNextWorkStart if set to true will return start of next working period
* @return end date
*/
public Date getDate(Date startDate, Duration duration, boolean returnNextWorkStart)
{
ProjectProperties properties = getParentFile().getProjectProperties();
// Note: Using a double allows us to handle date values that are accurate up to seconds.
// However, it also means we need to truncate the value to 2 decimals to make the
// comparisons work as sometimes the double ends up with some extra e.g. .0000000000003
// that wreak havoc on the comparisons.
double remainingMinutes = NumberHelper.round(duration.convertUnits(TimeUnit.MINUTES, properties).getDuration(), 2);
//
// Can we skip come computation by working forward from the
// last call to this method?
//
Date getDateLastStartDate = m_getDateLastStartDate;
double getDateLastRemainingMinutes = m_getDateLastRemainingMinutes;
m_getDateLastStartDate = startDate;
m_getDateLastRemainingMinutes = remainingMinutes;
if (m_getDateLastResult != null && DateHelper.compare(startDate, getDateLastStartDate) == 0 && remainingMinutes >= getDateLastRemainingMinutes)
{
startDate = m_getDateLastResult;
remainingMinutes = remainingMinutes - getDateLastRemainingMinutes;
}
Calendar cal = Calendar.getInstance();
cal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
while (remainingMinutes > 0)
{
//
// Get the current date and time and determine how many
// working hours remain
//
Date currentDate = cal.getTime();
endCal.setTime(currentDate);
endCal.add(Calendar.DAY_OF_YEAR, 1);
Date currentDateEnd = DateHelper.getDayStartDate(endCal.getTime());
double currentDateWorkingMinutes = getWork(currentDate, currentDateEnd, TimeUnit.MINUTES).getDuration();
//
// We have more than enough hours left
//
if (remainingMinutes > currentDateWorkingMinutes)
{
//
// Deduct this day's hours from our total
//
remainingMinutes = NumberHelper.round(remainingMinutes - currentDateWorkingMinutes, 2);
//
// Move the calendar forward to the next working day
//
Day day;
int nonWorkingDayCount = 0;
do
{
cal.add(Calendar.DAY_OF_YEAR, 1);
day = Day.getInstance(cal.get(Calendar.DAY_OF_WEEK));
++nonWorkingDayCount;
if (nonWorkingDayCount > MAX_NONWORKING_DAYS)
{
cal.setTime(startDate);
cal.add(Calendar.DAY_OF_YEAR, 1);
remainingMinutes = 0;
break;
}
}
while (!isWorkingDate(cal.getTime(), day));
//
// Retrieve the start time for this day
//
Date startTime = getStartTime(cal.getTime());
DateHelper.setTime(cal, startTime);
}
else
{
//
// We have less hours to allocate than there are working hours
// in this day. We need to calculate the time of day at which
// our work ends.
//
ProjectCalendarDateRanges ranges = getRanges(cal.getTime(), cal, null);
//
// Now we have the range of working hours for this day,
// step through it to work out the end point
//
Date endTime = null;
Date currentDateStartTime = DateHelper.getCanonicalTime(currentDate);
boolean firstRange = true;
for (DateRange range : ranges)
{
//
// Skip this range if its end is before our start time
//
Date rangeStart = range.getStart();
Date rangeEnd = range.getEnd();
if (rangeStart == null || rangeEnd == null)
{
continue;
}
Date canonicalRangeEnd = DateHelper.getCanonicalTime(rangeEnd);
Date canonicalRangeStart = DateHelper.getCanonicalTime(rangeStart);
Date rangeStartDay = DateHelper.getDayStartDate(rangeStart);
Date rangeEndDay = DateHelper.getDayStartDate(rangeEnd);
if (rangeStartDay.getTime() != rangeEndDay.getTime())
{
canonicalRangeEnd = DateHelper.addDays(canonicalRangeEnd, 1);
}
if (firstRange && canonicalRangeEnd.getTime() < currentDateStartTime.getTime())
{
continue;
}
//
// Move the start of the range if our current start is
// past the range start
//
if (firstRange && canonicalRangeStart.getTime() < currentDateStartTime.getTime())
{
canonicalRangeStart = currentDateStartTime;
}
firstRange = false;
double rangeMinutes;
rangeMinutes = canonicalRangeEnd.getTime() - canonicalRangeStart.getTime();
rangeMinutes /= (1000 * 60);
if (remainingMinutes > rangeMinutes)
{
remainingMinutes = NumberHelper.round(remainingMinutes - rangeMinutes, 2);
}
else
{
if (Duration.durationValueEquals(remainingMinutes, rangeMinutes))
{
endTime = canonicalRangeEnd;
if (rangeStartDay.getTime() != rangeEndDay.getTime())
{
// The range ends the next day, so let's adjust our date accordingly.
cal.add(Calendar.DAY_OF_YEAR, 1);
}
}
else
{
endTime = new Date((long) (canonicalRangeStart.getTime() + (remainingMinutes * (60 * 1000))));
returnNextWorkStart = false;
}
remainingMinutes = 0;
break;
}
}
DateHelper.setTime(cal, endTime);
}
}
m_getDateLastResult = cal.getTime();
if (returnNextWorkStart)
{
updateToNextWorkStart(cal);
}
return cal.getTime();
}
/**
* Given a finish date and a duration, this method calculates backwards to the
* start date. It takes account of working hours in each day, non working
* days and calendar exceptions.
*
* @param finishDate finish date
* @param duration duration
* @return start date
*/
public Date getStartDate(Date finishDate, Duration duration)
{
ProjectProperties properties = getParentFile().getProjectProperties();
// Note: Using a double allows us to handle date values that are accurate up to seconds.
// However, it also means we need to truncate the value to 2 decimals to make the
// comparisons work as sometimes the double ends up with some extra e.g. .0000000000003
// that wreak havoc on the comparisons.
double remainingMinutes = NumberHelper.round(duration.convertUnits(TimeUnit.MINUTES, properties).getDuration(), 2);
Calendar cal = Calendar.getInstance();
cal.setTime(finishDate);
Calendar startCal = Calendar.getInstance();
while (remainingMinutes > 0)
{
//
// Get the current date and time and determine how many
// working hours remain
//
Date currentDate = cal.getTime();
startCal.setTime(currentDate);
startCal.add(Calendar.DAY_OF_YEAR, -1);
Date currentDateEnd = DateHelper.getDayEndDate(startCal.getTime());
double currentDateWorkingMinutes = getWork(currentDateEnd, currentDate, TimeUnit.MINUTES).getDuration();
//
// We have more than enough hours left
//
if (remainingMinutes > currentDateWorkingMinutes)
{
//
// Deduct this day's hours from our total
//
remainingMinutes = NumberHelper.round(remainingMinutes - currentDateWorkingMinutes, 2);
//
// Move the calendar backward to the previous working day
//
int count = 0;
Day day;
do
{
if (count > 7)
{
break; // Protect against a calendar with all days non-working
}
count++;
cal.add(Calendar.DAY_OF_YEAR, -1);
day = Day.getInstance(cal.get(Calendar.DAY_OF_WEEK));
}
while (!isWorkingDate(cal.getTime(), day));
if (count > 7)
{
// We have a calendar with no working days.
return null;
}
//
// Retrieve the finish time for this day
//
Date finishTime = getFinishTime(cal.getTime());
DateHelper.setTime(cal, finishTime);
}
else
{
//
// We have less hours to allocate than there are working hours
// in this day. We need to calculate the time of day at which
// our work starts.
//
ProjectCalendarDateRanges ranges = getRanges(cal.getTime(), cal, null);
//
// Now we have the range of working hours for this day,
// step through it to work out the start point
//
Date startTime = null;
Date currentDateFinishTime = DateHelper.getCanonicalTime(currentDate);
boolean firstRange = true;
// Traverse from end to start
for (int i = ranges.getRangeCount() - 1; i >= 0; i--)
{
DateRange range = ranges.getRange(i);
//
// Skip this range if its start is after our end time
//
Date rangeStart = range.getStart();
Date rangeEnd = range.getEnd();
if (rangeStart == null || rangeEnd == null)
{
continue;
}
Date canonicalRangeEnd = DateHelper.getCanonicalTime(rangeEnd);
Date canonicalRangeStart = DateHelper.getCanonicalTime(rangeStart);
Date rangeStartDay = DateHelper.getDayStartDate(rangeStart);
Date rangeEndDay = DateHelper.getDayStartDate(rangeEnd);
if (rangeStartDay.getTime() != rangeEndDay.getTime())
{
canonicalRangeEnd = DateHelper.addDays(canonicalRangeEnd, 1);
}
if (firstRange && canonicalRangeStart.getTime() > currentDateFinishTime.getTime())
{
continue;
}
//
// Move the end of the range if our current end is
// before the range end
//
if (firstRange && canonicalRangeEnd.getTime() > currentDateFinishTime.getTime())
{
canonicalRangeEnd = currentDateFinishTime;
}
firstRange = false;
double rangeMinutes;
rangeMinutes = canonicalRangeEnd.getTime() - canonicalRangeStart.getTime();
rangeMinutes /= (1000 * 60);
if (remainingMinutes > rangeMinutes)
{
remainingMinutes = NumberHelper.round(remainingMinutes - rangeMinutes, 2);
}
else
{
if (Duration.durationValueEquals(remainingMinutes, rangeMinutes))
{
startTime = canonicalRangeStart;
}
else
{
startTime = new Date((long) (canonicalRangeEnd.getTime() - (remainingMinutes * (60 * 1000))));
}
remainingMinutes = 0;
break;
}
}
DateHelper.setTime(cal, startTime);
}
}
return cal.getTime();
}
/**
* This method finds the start of the next working period.
*
* @param cal current Calendar instance
*/
private void updateToNextWorkStart(Calendar cal)
{
Date originalDate = cal.getTime();
//
// Find the date ranges for the current day
//
ProjectCalendarDateRanges ranges = getRanges(originalDate, cal, null);
if (ranges != null)
{
//
// Do we have a start time today?
//
Date calTime = DateHelper.getCanonicalTime(cal.getTime());
Date startTime = null;
for (DateRange range : ranges)
{
Date rangeStart = DateHelper.getCanonicalTime(range.getStart());
Date rangeEnd = DateHelper.getCanonicalTime(range.getEnd());
Date rangeStartDay = DateHelper.getDayStartDate(range.getStart());
Date rangeEndDay = DateHelper.getDayStartDate(range.getEnd());
if (rangeStartDay.getTime() != rangeEndDay.getTime())
{
rangeEnd = DateHelper.addDays(rangeEnd, 1);
}
if (calTime.getTime() < rangeEnd.getTime())
{
if (calTime.getTime() > rangeStart.getTime())
{
startTime = calTime;
}
else
{
startTime = rangeStart;
}
break;
}
}
//
// If we don't have a start time today - find the next working day
// then retrieve the start time.
//
if (startTime == null)
{
Day day;
int nonWorkingDayCount = 0;
do
{
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) + 1);
day = Day.getInstance(cal.get(Calendar.DAY_OF_WEEK));
++nonWorkingDayCount;
if (nonWorkingDayCount > MAX_NONWORKING_DAYS)
{
cal.setTime(originalDate);
break;
}
}
while (!isWorkingDate(cal.getTime(), day));
startTime = getStartTime(cal.getTime());
}
DateHelper.setTime(cal, startTime);
}
}
/**
* This method finds the finish of the previous working period.
*
* @param cal current Calendar instance
*/
private void updateToPreviousWorkFinish(Calendar cal)
{
Date originalDate = cal.getTime();
//
// Find the date ranges for the current day
//
ProjectCalendarDateRanges ranges = getRanges(originalDate, cal, null);
if (ranges != null)
{
//
// Do we have a start time today?
//
Date calTime = DateHelper.getCanonicalTime(cal.getTime());
Date finishTime = null;
for (DateRange range : ranges)
{
Date rangeEnd = DateHelper.getCanonicalTime(range.getEnd());
Date rangeStartDay = DateHelper.getDayStartDate(range.getStart());
Date rangeEndDay = DateHelper.getDayStartDate(range.getEnd());
if (rangeStartDay.getTime() != rangeEndDay.getTime())
{
rangeEnd = DateHelper.addDays(rangeEnd, 1);
}
if (calTime.getTime() >= rangeEnd.getTime())
{
finishTime = rangeEnd;
break;
}
}
//
// If we don't have a finish time today - find the previous working day
// then retrieve the finish time.
//
if (finishTime == null)
{
Day day;
int nonWorkingDayCount = 0;
do
{
cal.set(Calendar.DAY_OF_YEAR, cal.get(Calendar.DAY_OF_YEAR) - 1);
day = Day.getInstance(cal.get(Calendar.DAY_OF_WEEK));
++nonWorkingDayCount;
if (nonWorkingDayCount > MAX_NONWORKING_DAYS)
{
cal.setTime(originalDate);
break;
}
}
while (!isWorkingDate(cal.getTime(), day));
finishTime = getFinishTime(cal.getTime());
}
DateHelper.setTime(cal, finishTime);
}
}
/**
* Utility method to retrieve the next working date start time, given
* a date and time as a starting point.
*
* @param date date and time start point
* @return date and time of next work start
*/
public Date getNextWorkStart(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
updateToNextWorkStart(cal);
return cal.getTime();
}
/**
* Utility method to retrieve the previous working date finish time, given
* a date and time as a starting point.
*
* @param date date and time start point
* @return date and time of previous work finish
*/
public Date getPreviousWorkFinish(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
updateToPreviousWorkFinish(cal);
return cal.getTime();
}
/**
* This method allows the caller to determine if a given date is a
* working day. This method takes account of calendar exceptions.
*
* @param date Date to be tested
* @return boolean value
*/
public boolean isWorkingDate(Date date)
{
Calendar cal = DateHelper.popCalendar(date);
Day day = Day.getInstance(cal.get(Calendar.DAY_OF_WEEK));
DateHelper.pushCalendar(cal);
return isWorkingDate(date, day);
}
/**
* This private method allows the caller to determine if a given date is a
* working day. This method takes account of calendar exceptions. It assumes
* that the caller has already calculated the day of the week on which
* the given day falls.
*
* @param date Date to be tested
* @param day Day of the week for the date under test
* @return boolean flag
*/
private boolean isWorkingDate(Date date, Day day)
{
ProjectCalendarDateRanges ranges = getRanges(date, null, day);
return ranges.getRangeCount() != 0;
}
/**
* This method calculates the absolute number of days between two dates.
* Note that where two date objects are provided that fall on the same
* day, this method will return one not zero. Note also that this method
* assumes that the dates are passed in the correct order, i.e.
* startDate < endDate.
*
* @param startDate Start date
* @param endDate End date
* @return number of days in the date range
*/
private int getDaysInRange(Date startDate, Date endDate)
{
int result;
Calendar cal = DateHelper.popCalendar(endDate);
int endDateYear = cal.get(Calendar.YEAR);
int endDateDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
cal.setTime(startDate);
if (endDateYear == cal.get(Calendar.YEAR))
{
result = (endDateDayOfYear - cal.get(Calendar.DAY_OF_YEAR)) + 1;
}
else
{
result = 0;
do
{
result += (cal.getActualMaximum(Calendar.DAY_OF_YEAR) - cal.get(Calendar.DAY_OF_YEAR)) + 1;
cal.roll(Calendar.YEAR, 1);
cal.set(Calendar.DAY_OF_YEAR, 1);
}
while (cal.get(Calendar.YEAR) < endDateYear);