1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.time.util;
17
18 import java.math.BigDecimal;
19 import java.net.UnknownHostException;
20 import java.sql.Date;
21 import java.sql.Timestamp;
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.*;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.apache.commons.lang.ObjectUtils;
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.log4j.Logger;
31 import org.joda.time.DateTime;
32 import org.joda.time.DateTimeZone;
33 import org.joda.time.Days;
34 import org.joda.time.Interval;
35 import org.joda.time.Period;
36 import org.kuali.hr.time.assignment.Assignment;
37 import org.kuali.hr.time.calendar.CalendarEntries;
38 import org.kuali.hr.time.service.base.TkServiceLocator;
39 import org.kuali.hr.time.task.Task;
40 import org.kuali.rice.core.api.config.property.ConfigContext;
41 import org.springframework.util.NumberUtils;
42
43 public class TKUtils {
44
45 private static final Logger LOG = Logger.getLogger(TKUtils.class);
46
47 public static java.sql.Date getCurrentDate() {
48 return getTimelessDate(null);
49 }
50
51
52
53
54
55 public static String getDayOfMonthFromDateString(String dateString) {
56 String[] date = dateString.split("/");
57 return date[1];
58 }
59
60 public static String getSystemTimeZone() {
61 String configTimezone = TimeZone.getDefault().getID();
62 if (ConfigContext.getCurrentContextConfig() != null
63 && StringUtils.isNotBlank(ConfigContext.getCurrentContextConfig().getProperty(TkConstants.ConfigSettings.KPME_SYSTEM_TIMEZONE).trim())) {
64 String tempTimeZoneId = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.ConfigSettings.KPME_SYSTEM_TIMEZONE);
65
66 if (TimeZone.getTimeZone(tempTimeZoneId) != null) {
67 configTimezone = ConfigContext.getCurrentContextConfig().getProperty(TkConstants.ConfigSettings.KPME_SYSTEM_TIMEZONE);
68 } else {
69 LOG.error("Timezone set by configuration parameter " + TkConstants.ConfigSettings.KPME_SYSTEM_TIMEZONE + " is not a valid time zone id. Using the systems default time zone instead.");
70 }
71 }
72
73
74 return configTimezone;
75 }
76
77 public static DateTimeZone getSystemDateTimeZone() {
78 return DateTimeZone.forID(TKUtils.getSystemTimeZone());
79 }
80
81 public static final Date END_OF_TIME;
82
83 static
84 {
85 TimeZone tz = TimeZone.getTimeZone("UTC");
86 final Calendar c = new GregorianCalendar(tz);
87 c.setTime(new Date(Long.MAX_VALUE));
88 END_OF_TIME = new java.sql.Date(c.getTime().getTime());
89 }
90
91
92
93
94
95
96
97
98
99 public static java.sql.Date getTimelessDate(java.util.Date date) {
100 java.sql.Date jsd = null;
101 if (date == null) {
102 jsd = new java.sql.Date(System.currentTimeMillis());
103 } else {
104 jsd = new java.sql.Date(date.getTime());
105 }
106 return jsd;
107 }
108
109 public static long getDaysBetween(Calendar startDate, Calendar endDate) {
110 Calendar date = (Calendar) startDate.clone();
111 long daysBetween = 0;
112 while (date.before(endDate)) {
113 date.add(Calendar.DAY_OF_MONTH, 1);
114 daysBetween++;
115 }
116 return daysBetween;
117 }
118
119 public static long getDaysBetween(java.util.Date startDate, java.util.Date endDate) {
120 Calendar beginCal = GregorianCalendar.getInstance();
121 Calendar endCal = GregorianCalendar.getInstance();
122 beginCal.setTime(startDate);
123 endCal.setTime(endDate);
124
125 return getDaysBetween(beginCal, endCal);
126 }
127
128 public static BigDecimal getHoursBetween(long start, long end) {
129 long diff = end - start;
130 BigDecimal hrsReminder = new BigDecimal((diff / 3600000.0) % 24);
131
132 if (hrsReminder.compareTo(BigDecimal.ZERO) == 0 && diff > 0) {
133 return new BigDecimal(diff / 3600000.0).setScale(TkConstants.BIG_DECIMAL_SCALE, TkConstants.BIG_DECIMAL_SCALE_ROUNDING).abs();
134 }
135 return hrsReminder.setScale(TkConstants.BIG_DECIMAL_SCALE, TkConstants.BIG_DECIMAL_SCALE_ROUNDING).abs();
136 }
137
138
139
140 public static int getNumberOfWeeks(java.util.Date beginDate, java.util.Date endDate) {
141
142 DateTime beginTime = new DateTime(beginDate);
143 DateTime endTime = new DateTime(endDate);
144
145 int numOfDays = Days.daysBetween(beginTime, endTime).getDays();
146 int numOfWeeks = numOfDays / 7;
147 if (numOfDays % 7 != 0) {
148 numOfWeeks++;
149 }
150 return numOfWeeks;
151 }
152
153 public static String formatAssignmentKey(Long jobNumber, Long workArea, Long task) {
154 String jobNumberString = ObjectUtils.toString(jobNumber, "0");
155 String workAreaString = ObjectUtils.toString(workArea, "0");
156 String taskString = ObjectUtils.toString(task, "0");
157 return jobNumberString + TkConstants.ASSIGNMENT_KEY_DELIMITER + workAreaString + TkConstants.ASSIGNMENT_KEY_DELIMITER + taskString;
158 }
159
160 public static Map<String, String> formatAssignmentDescription(Assignment assignment) {
161 Map<String, String> assignmentDescriptions = new LinkedHashMap<String, String>();
162 String assignmentDescKey = formatAssignmentKey(assignment.getJobNumber(), assignment.getWorkArea(), assignment.getTask());
163 String assignmentDescValue = getAssignmentString(assignment);
164 assignmentDescriptions.put(assignmentDescKey, assignmentDescValue);
165
166 return assignmentDescriptions;
167 }
168
169 public static String getAssignmentString(Assignment assignment) {
170
171
172 if (assignment.getWorkAreaObj() == null
173 || assignment.getJob() == null
174 || assignment.getJobNumber() == null) {
175 return "";
176 }
177
178 String stringTemp = assignment.getWorkAreaObj().getDescription() + " : $"
179 + assignment.getJob().getCompRate().setScale(TkConstants.BIG_DECIMAL_SCALE)
180 + " Rcd " + assignment.getJobNumber() + " " + assignment.getJob().getDept();
181 if(assignment.getTask()!= null) {
182 Task aTask = TkServiceLocator.getTaskService().getTask(assignment.getTask(), assignment.getEffectiveDate());
183 if(aTask != null) {
184
185
186 if(!aTask.getDescription().equals(TkConstants.TASK_DEFAULT_DESP)) {
187 stringTemp += " " + aTask.getDescription();
188 }
189 }
190 }
191 return stringTemp;
192 }
193
194
195
196
197
198
199
200
201
202 public static List<Interval> getDaySpanForCalendarEntry(CalendarEntries calendarEntry, DateTimeZone timeZone) {
203 DateTime beginDateTime = calendarEntry.getBeginLocalDateTime().toDateTime(timeZone);
204 DateTime endDateTime = calendarEntry.getEndLocalDateTime().toDateTime(timeZone);
205
206 List<Interval> dayIntervals = new ArrayList<Interval>();
207
208 DateTime currDateTime = beginDateTime;
209 while (currDateTime.isBefore(endDateTime)) {
210 DateTime prevDateTime = currDateTime;
211 currDateTime = currDateTime.plusDays(1);
212 Interval daySpan = new Interval(prevDateTime, currDateTime);
213 dayIntervals.add(daySpan);
214 }
215
216 return dayIntervals;
217 }
218
219
220
221
222
223
224
225
226
227
228
229 public static List<Interval> getWeekIntervals(java.util.Date beginDate, java.util.Date endDate) {
230 List<Interval> intervals = new ArrayList<Interval>();
231 DateTime beginTime = new DateTime(beginDate);
232 DateTime endTime = new DateTime(endDate);
233
234 int dayIncrement = 7;
235 DateTime previous = beginTime;
236 DateTime nextTime = previous.plusDays(dayIncrement);
237 while (nextTime.isBefore(endTime)) {
238 Interval interval = new Interval(previous, nextTime);
239 intervals.add(interval);
240 previous = nextTime;
241 nextTime = previous.plusDays(dayIncrement);
242 }
243
244 if (previous.isBefore(endTime)) {
245
246 Interval interval = new Interval(previous, endTime);
247 intervals.add(interval);
248 }
249
250 return intervals;
251 }
252
253 public static long convertHoursToMillis(BigDecimal hours) {
254 return hours.multiply(TkConstants.BIG_DECIMAL_MS_IN_H, TkConstants.MATH_CONTEXT).longValue();
255 }
256
257 public static BigDecimal convertMillisToHours(long millis) {
258 return (new BigDecimal(millis)).divide(TkConstants.BIG_DECIMAL_MS_IN_H, TkConstants.MATH_CONTEXT);
259 }
260
261 public static BigDecimal convertMillisToMinutes(long millis) {
262 return (new BigDecimal(millis)).divide(TkConstants.BIG_DECIMAL_MS_IN_M, TkConstants.MATH_CONTEXT);
263 }
264
265 public static BigDecimal convertMillisToDays(long millis) {
266 BigDecimal hrs = convertMillisToHours(millis);
267 return hrs.divide(TkConstants.BIG_DECIMAL_HRS_IN_DAY, TkConstants.MATH_CONTEXT);
268 }
269
270 public static BigDecimal convertMinutesToHours(BigDecimal minutes) {
271 return minutes.divide(TkConstants.BIG_DECIMAL_60, TkConstants.MATH_CONTEXT);
272 }
273
274 public static int convertMillisToWholeDays(long millis) {
275 BigDecimal days = convertMillisToDays(millis);
276 return Integer.parseInt(days.setScale(0, BigDecimal.ROUND_UP).toString());
277 }
278
279
280
281
282
283 public static boolean isVirtualWorkDay(Calendar payCalendarStartTime) {
284 return (payCalendarStartTime.get(Calendar.HOUR_OF_DAY) != 0 || payCalendarStartTime.get(Calendar.MINUTE) != 0
285 && payCalendarStartTime.get(Calendar.AM_PM) != Calendar.AM);
286 }
287
288
289
290
291
292
293
294
295
296
297 public static Timestamp convertDateStringToTimestamp(String dateStr, String timeStr) {
298
299 String[] date = dateStr.split("/");
300 String[] time = timeStr.split(":");
301
302 DateTimeZone dtz = DateTimeZone.forID(TkServiceLocator.getTimezoneService().getUserTimezone());
303
304
305
306
307
308 DateTime dateTime = new DateTime(
309 Integer.parseInt(date[2]),
310 Integer.parseInt(date[0]),
311 Integer.parseInt(date[1]),
312 Integer.parseInt(time[0]),
313 Integer.parseInt(time[1]),
314 0, 0, dtz);
315
316 return new Timestamp(dateTime.getMillis());
317 }
318
319 public static Timestamp convertDateStringToTimestampWithoutZone(String dateStr, String timeStr) {
320
321 String[] date = dateStr.split("/");
322 String[] time = timeStr.split(":");
323
324
325
326
327
328 DateTime dateTime = new DateTime(
329 Integer.parseInt(date[2]),
330 Integer.parseInt(date[0]),
331 Integer.parseInt(date[1]),
332 Integer.parseInt(time[0]),
333 Integer.parseInt(time[1]),
334 0, 0);
335
336 return new Timestamp(dateTime.getMillis());
337 }
338
339 public static String getIPAddressFromRequest(HttpServletRequest request) {
340
341
342 String ip = request.getRemoteAddr();
343 if (ip.indexOf(':') > -1) {
344 LOG.warn("ignoring IPv6 address for clock-in: " + ip);
345 ip = "";
346 }
347
348 return ip;
349 }
350
351 public static Date createDate(int month, int day, int year, int hours, int minutes, int seconds) {
352 DateTime dt = new DateTime(year, month, day, hours, minutes, seconds, 0);
353 return new Date(dt.getMillis());
354 }
355
356 public static String getIPNumber() {
357 try {
358 return java.net.InetAddress.getLocalHost().getHostAddress();
359 } catch (UnknownHostException e) {
360 return "unknown";
361 }
362 }
363
364 public static Timestamp subtractOneSecondFromTimestamp(Timestamp originalTimestamp) {
365 DateTime dt = new DateTime(originalTimestamp);
366 dt = dt.minusSeconds(1);
367 return new Timestamp(dt.getMillis());
368 }
369
370 public static Date subtractOneMillisecondFromDate(java.util.Date date) {
371 DateTime dt = new DateTime(date);
372 dt = dt.minusMillis(1);
373 return new Date(dt.getMillis());
374 }
375
376 public static String formatDate(Date dt) {
377 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
378 return sdf.format(dt);
379 }
380
381
382 public static String formatDateTime(Timestamp timestamp){
383 Date dt = new Date(timestamp.getTime());
384 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
385 return sdf.format(dt);
386 }
387
388 public static Date formatDateString(String date){
389 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
390 try {
391 return new Date(sdf.parse(date).getTime());
392 } catch (ParseException e) {
393 return null;
394 }
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408 public static String getTimezoneOffset(DateTime date) {
409 StringBuilder o = new StringBuilder();
410
411 int offsetms = date.getZone().getOffset(date);
412 boolean negative = offsetms < 0;
413 if (negative) offsetms = offsetms * -1;
414
415 Period period = new Period(offsetms);
416 if (negative) o.append('-');
417 o.append(StringUtils.leftPad(period.getHours() + "", 2, '0'));
418 o.append(StringUtils.leftPad(period.getMinutes() + "", 2, '0'));
419
420 return o.toString();
421 }
422
423 public static String arrayToString(String[] stringArray) {
424 StringBuilder str = new StringBuilder();
425 for (String string : stringArray) {
426 str.append(string);
427 }
428 return str.toString();
429 }
430
431
432
433
434 public static int getSessionTimeoutTime() {
435 return StringUtils.isBlank(ConfigContext.getCurrentContextConfig().getProperty(TkConstants.ConfigSettings.SESSION_TIMEOUT))
436 ? 2700 :
437 Integer.parseInt(ConfigContext.getCurrentContextConfig().getProperty(TkConstants.ConfigSettings.SESSION_TIMEOUT));
438 }
439
440
441
442
443
444
445
446
447
448 public static Timestamp convertDateStringToTimestamp(String dateStr) {
449
450 String[] date = dateStr.split("/");
451
452 DateTimeZone dtz = DateTimeZone.forID(TkServiceLocator.getTimezoneService().getUserTimezone());
453
454
455
456
457
458 DateTime dateTime = new DateTime(
459 Integer.parseInt(date[2]),
460 Integer.parseInt(date[0]),
461 Integer.parseInt(date[1]),
462 0, 0, 0, 0, dtz);
463
464 return new Timestamp(dateTime.getMillis());
465 }
466
467
468
469
470
471
472
473
474
475 public static Timestamp convertDateStringToTimestampNoTimezone(String dateStr) {
476
477 String[] date = dateStr.split("/");
478
479
480
481
482
483 DateTime dateTime = new DateTime(
484 Integer.parseInt(date[2]),
485 Integer.parseInt(date[0]),
486 Integer.parseInt(date[1]),
487 0, 0, 0, 0);
488
489 return new Timestamp(dateTime.getMillis());
490 }
491
492
493 public static Timestamp getCurrentTimestamp() {
494 return new Timestamp(System.currentTimeMillis());
495 }
496
497 public static List<Interval> createDaySpan(DateTime beginDateTime, DateTime endDateTime, DateTimeZone zone) {
498 beginDateTime = beginDateTime.toDateTime(zone);
499 endDateTime = endDateTime.toDateTime(zone);
500 List<Interval> dayIntervals = new ArrayList<Interval>();
501
502 DateTime currDateTime = beginDateTime;
503 while (currDateTime.isBefore(endDateTime)) {
504 DateTime prevDateTime = currDateTime;
505 currDateTime = currDateTime.plusDays(1);
506 Interval daySpan = new Interval(prevDateTime, currDateTime);
507 dayIntervals.add(daySpan);
508 }
509
510 return dayIntervals;
511 }
512
513 public static List<Interval> getDaySpanForCalendarEntry(CalendarEntries calendarEntry) {
514 return getDaySpanForCalendarEntry(calendarEntry, TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
515 }
516
517 public static List<Interval> getFullWeekDaySpanForCalendarEntry(CalendarEntries calendarEntry) {
518 return getFullWeekDaySpanForCalendarEntry(calendarEntry, TkServiceLocator.getTimezoneService().getUserTimezoneWithFallback());
519 }
520
521 public static List<Interval> getFullWeekDaySpanForCalendarEntry(CalendarEntries calendarEntry, DateTimeZone timeZone) {
522 DateTime beginDateTime = calendarEntry.getBeginLocalDateTime().toDateTime(timeZone);
523 DateTime endDateTime = calendarEntry.getEndLocalDateTime().toDateTime(timeZone);
524
525 List<Interval> dayIntervals = new ArrayList<Interval>();
526
527 DateTime currDateTime = beginDateTime;
528 if (beginDateTime.getDayOfWeek() != 7) {
529 currDateTime = beginDateTime.plusDays(0 - beginDateTime.getDayOfWeek());
530 }
531
532 int afterEndDate = 6 - endDateTime.getDayOfWeek();
533 if (endDateTime.getDayOfWeek() == 7 && endDateTime.getHourOfDay() != 0) {
534 afterEndDate = 6;
535 }
536 if (endDateTime.getHourOfDay() == 0) {
537 afterEndDate += 1;
538 }
539 DateTime aDate = endDateTime.plusDays(afterEndDate);
540 while (currDateTime.isBefore(aDate)) {
541 DateTime prevDateTime = currDateTime;
542 currDateTime = currDateTime.plusDays(1);
543 Interval daySpan = new Interval(prevDateTime, currDateTime);
544 dayIntervals.add(daySpan);
545 }
546
547 return dayIntervals;
548 }
549
550 public static java.util.Date removeTime(java.util.Date date) {
551 Calendar cal = Calendar.getInstance();
552 cal.setTime(date);
553 cal.set(Calendar.HOUR_OF_DAY, 0);
554 cal.set(Calendar.MINUTE, 0);
555 cal.set(Calendar.SECOND, 0);
556 cal.set(Calendar.MILLISECOND, 0);
557 return cal.getTime();
558 }
559
560 public static int getWorkDays(java.util.Date startDate, java.util.Date endDate) {
561 int dayCounts = 0;
562 if(startDate.after(endDate)) {
563 return 0;
564 }
565 Calendar cal1 = Calendar.getInstance();
566 cal1.setTime(startDate);
567 Calendar cal2 = Calendar.getInstance();
568 cal2.setTime(endDate);
569
570 while(!cal1.after(cal2)) {
571 if(cal1.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY
572 && cal1.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
573 dayCounts ++;
574 }
575 cal1.add(Calendar.DATE, 1);
576 }
577 return dayCounts;
578 }
579
580 public static boolean isWeekend(java.util.Date aDate) {
581 Calendar cal = Calendar.getInstance();
582 cal.setTime(aDate);
583 if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY
584 || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) {
585 return true;
586 }
587 return false;
588 }
589
590 public static java.util.Date addDates(java.util.Date aDate, int aNumber) {
591 Calendar gc = new GregorianCalendar();
592 gc.setTime(aDate);
593 gc.add(Calendar.DAY_OF_YEAR, aNumber);
594 return gc.getTime();
595 }
596
597 public static java.util.Date addMonths(java.util.Date aDate, int aNumber) {
598 Calendar gc = new GregorianCalendar();
599 gc.setTime(aDate);
600 gc.add(Calendar.MONTH, aNumber);
601 if(gc.getActualMaximum(Calendar.DAY_OF_MONTH) < gc.get(Calendar.DATE)) {
602 gc.set(Calendar.DATE, gc.getActualMaximum(Calendar.DAY_OF_MONTH));
603 }
604
605 return gc.getTime();
606 }
607
608
609
610
611
612
613
614
615
616 public static String getFromDateString(String dateString) {
617 String toDateString = StringUtils.EMPTY;
618
619 if (StringUtils.startsWith(dateString, ">=")) {
620 toDateString = StringUtils.substringAfter(dateString, ">=");
621 } else if (StringUtils.contains(dateString, "..")) {
622 toDateString = StringUtils.substringBefore(dateString, "..");
623 }
624
625 return toDateString;
626 }
627
628
629
630
631
632
633
634
635
636 public static String getToDateString(String dateString) {
637 String toDateString = StringUtils.EMPTY;
638
639 if (StringUtils.startsWith(dateString, "<=")) {
640 toDateString = StringUtils.substringAfter(dateString, "<=");
641 } else if (StringUtils.contains(dateString, "..")) {
642 toDateString = StringUtils.substringAfter(dateString, "..");
643 }
644
645 return toDateString;
646 }
647
648 }