View Javadoc

1   /**
2    * Copyright 2012 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   *
15   */
16  package org.kuali.student.enrollment.class2.acal.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.time.DateFormatUtils;
20  import org.apache.log4j.Logger;
21  import org.joda.time.MutableDateTime;
22  import org.kuali.student.enrollment.class2.acal.dto.TimeSetWrapper;
23  import org.kuali.student.r2.common.dto.RichTextInfo;
24  import org.kuali.student.r2.common.util.date.DateFormatters;
25  
26  import java.util.Date;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  /**
31   * This class //TODO ...
32   *
33   * @author Kuali Student Team
34   */
35  public class CommonUtils {
36      public static void assembleTimeSet(TimeSetWrapper timeSetWrapper, Date startDate, Date endDate) throws Exception{
37  
38          if (startDate !=null) {
39              String startDateFullString = DateFormatters.MONTH_DAY_YEAR_TIME_DATE_FORMATTER.format(startDate);
40              String[] timeStr = startDateFullString.split(" ");
41              timeSetWrapper.setStartDate(DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.parse(timeStr[0]));
42              if (!"12:00".equals(timeStr[1])){
43                  timeSetWrapper.setStartTime(timeStr[1]);
44              }
45              timeSetWrapper.setStartTimeAmPm(timeStr[2].toLowerCase());
46          }
47  
48          if (endDate !=null) {
49              String endDateFullString = DateFormatters.MONTH_DAY_YEAR_TIME_DATE_FORMATTER.format(endDate);
50              String[] timeStr = endDateFullString.split(" ");
51              timeSetWrapper.setEndDate(DateFormatters.MONTH_DAY_YEAR_DATE_FORMATTER.parse(timeStr[0]));
52              if (!"12:00".equals(timeStr[1])){
53                  timeSetWrapper.setEndTime(timeStr[1]);
54              }
55              timeSetWrapper.setEndTimeAmPm(timeStr[2].toLowerCase());
56  
57          }
58      }
59  
60      public static boolean isValidDateRange(Date startDate,Date endDate){
61          if(startDate != null && endDate != null) {
62              if (startDate.after(endDate) || endDate.before(startDate)){
63                  return false;
64              }
65          }
66          return true;
67      }
68  
69      public static String formatDate(Date date){
70          return DateFormatUtils.format(date, CalendarConstants.DEFAULT_DATE_FORMAT);
71      }
72  
73      public static boolean isDateWithinRange(Date startDate,Date endDate,Date checkDate){
74          if(startDate != null && endDate != null && checkDate != null) {
75              if ((!checkDate.equals(startDate) &&  checkDate.before(startDate)) || (!checkDate.equals(endDate) && checkDate.after(endDate))){
76                  return false;
77              }
78          }
79          return true;
80      }
81  
82      /**
83       * Allows you to see if a date range overlaps another date range.
84       *
85       * This can be used to determine if a Holiday Calendar overlaps an Academic Calendar.
86       *
87       * @param periodStartDate
88       * @param periodEndDate
89       * @param subStart
90       * @param subEnd
91       * @return
92       */
93      public static boolean doDatesOverlap(Date periodStartDate, Date periodEndDate, Date subStart, Date subEnd){
94          boolean bRet = false;
95  
96          int compStart = subStart.compareTo(periodEndDate);
97          int compEnd = subEnd.compareTo(periodStartDate);
98          if (compStart <= 0 && compEnd >= 0) {
99              bRet = true;
100         }
101 
102         return bRet;
103     }
104 
105     public static RichTextInfo buildDesc(String descr){
106         RichTextInfo rti = new RichTextInfo();
107         rti.setPlain(descr);
108         return rti;
109     }
110 
111     public static Date getDateWithTime(Date date, String hourMinute, String amPm) {
112         if (null == date) {
113             return null;
114         }
115 
116         MutableDateTime dateTime = new MutableDateTime(date);
117 
118         if (StringUtils.isNotBlank(hourMinute)) {
119             int hour = Integer.parseInt(StringUtils.substringBefore(hourMinute,":"));
120             if (StringUtils.equalsIgnoreCase(amPm,"PM")) {
121                 // 24-hour clock; e.g. 12 PM = hour 12; 8 PM = hour 20
122                 if (hour < 12) {
123                     hour += 12;
124                 }
125             }
126             else // if amPm is blank/null, assume AM
127             if (hour == 12) {
128                 hour = 0;  // 12 AM is stored in Calendar as hour 0
129             }
130             dateTime.setTime(hour, Integer.parseInt(StringUtils.substringAfter(hourMinute,":")), 0, 0);
131         }
132 
133         return dateTime.toDate();
134     }
135 
136     public static String getAdminOrgNameById(String id){
137         //TODO: hard-coded for now, going to call OrgService
138         String adminOrgName = null;
139         Map<String, String> allHcOrgs = new HashMap<String, String>();
140         allHcOrgs.put("102", "Registrar's Office");
141 
142         if(allHcOrgs.containsKey(id)){
143             adminOrgName = allHcOrgs.get(id);
144         }
145 
146         return adminOrgName;
147     }
148 
149     public static void logDebugMsg(Logger logger, String message) {
150         if (logger.isDebugEnabled()){
151             logger.debug(message);
152         }
153     }
154 }