View Javadoc
1   /**
2    * Copyright 2004-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.kpme.core.calendar;
17  
18  import com.google.common.collect.ImmutableList;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.commons.lang.builder.HashCodeBuilder;
21  import org.joda.time.DateTimeConstants;
22  import org.joda.time.LocalTime;
23  import org.kuali.kpme.core.api.calendar.Calendar;
24  import org.kuali.kpme.core.api.calendar.CalendarContract;
25  import org.kuali.kpme.core.util.HrConstants;
26  import org.kuali.rice.core.api.mo.ModelObjectUtils;
27  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
28  
29  import java.sql.Time;
30  
31  public class CalendarBo extends PersistableBusinessObjectBase implements CalendarContract {
32      public static final String CACHE_NAME = HrConstants.CacheNamespace.NAMESPACE_PREFIX + "Calendar";
33      public static final ModelObjectUtils.Transformer<CalendarBo, Calendar> toCalendar =
34              new ModelObjectUtils.Transformer<CalendarBo, Calendar>() {
35                  public Calendar transform(CalendarBo input) {
36                      return CalendarBo.to(input);
37                  };
38              };
39      public static final ModelObjectUtils.Transformer<Calendar, CalendarBo> toCalendarBo =
40              new ModelObjectUtils.Transformer<Calendar, CalendarBo>() {
41                  public CalendarBo transform(Calendar input) {
42                      return CalendarBo.from(input);
43                  };
44              };
45      public static final ImmutableList<String> BUSINESS_KEYS = new ImmutableList.Builder<String>()
46              .add("calendarName")
47              .build();
48      /**
49       *
50       */
51  	private static final long serialVersionUID = 1L;
52  
53  	private String hrCalendarId;
54  	private String calendarName;
55  	private String calendarDescriptions;
56  
57  	private String flsaBeginDay;
58  	private Time flsaBeginTime;
59  	private String calendarTypes;
60  	private int flsaBeginDayConstant = -1;
61  
62  	public String getHrCalendarId() {
63  		return hrCalendarId;
64  	}
65  
66  	public void setHrCalendarId(String hrCalendarId) {
67  		this.hrCalendarId = hrCalendarId;
68  	}
69  
70  	public String getCalendarName() {
71  		return calendarName;
72  	}
73  
74  	public void setCalendarName(String calendarName) {
75  		this.calendarName = calendarName;
76  	}
77  
78  	public String getCalendarTypes() {
79  		return calendarTypes;
80  	}
81  
82  	public void setCalendarTypes(String calendarTypes) {
83  		this.calendarTypes = calendarTypes;
84  	}
85  
86  	public void setFlsaBeginDayConstant(int flsaBeginDayConstant) {
87  		this.flsaBeginDayConstant = flsaBeginDayConstant;
88  	}
89  
90  	public String getCalendarDescriptions() {
91  		return calendarDescriptions;
92  	}
93  
94  	public void setCalendarDescriptions(String calendarDescriptions) {
95  		this.calendarDescriptions = calendarDescriptions;
96  	}
97  
98  	public String getFlsaBeginDay() {
99  		return flsaBeginDay;
100 	}
101 
102 	public void setFlsaBeginDay(String flsaBeginDay) {
103 		this.flsaBeginDay = flsaBeginDay;
104 		this.setFlsaBeinDayConstant(flsaBeginDay);
105 	}
106 
107 	public Time getFlsaBeginTime() {
108 		return flsaBeginTime;
109 	}
110 
111     public LocalTime getFlsaBeginLocalTime() {
112         return getFlsaBeginTime() == null ? null : LocalTime.fromDateFields(getFlsaBeginTime());
113     }
114 
115 	public void setFlsaBeginTime(Time flsaBeginTime) {
116 		this.flsaBeginTime = flsaBeginTime;
117 	}
118 
119 	/**
120 	 * This method sets a constant matching those listed in
121 	 * org.joda.time.DateTimeConstants for day comparisons.
122 	 *
123 	 * Currently this is 'hard-coded' to be English specific, it would
124 	 * be trivial to change and support more than one language/day naming
125 	 * convention.
126 	 *
127 	 * @param day
128 	 */
129 	private void setFlsaBeinDayConstant(String day) {
130 		if (!StringUtils.isEmpty(day)) {
131 			day = day.toLowerCase().trim();
132 
133 			if (day.startsWith("m")) {
134 				this.flsaBeginDayConstant = DateTimeConstants.MONDAY;
135 			} else if (day.startsWith("tu")) {
136 				this.flsaBeginDayConstant = DateTimeConstants.TUESDAY;
137 			} else if (day.startsWith("w")) {
138 				this.flsaBeginDayConstant = DateTimeConstants.WEDNESDAY;
139 			} else if (day.startsWith("th")) {
140 				this.flsaBeginDayConstant = DateTimeConstants.THURSDAY;
141 			} else if (day.startsWith("f")) {
142 				this.flsaBeginDayConstant = DateTimeConstants.FRIDAY;
143 			} else if (day.startsWith("sa")) {
144 				this.flsaBeginDayConstant = DateTimeConstants.SATURDAY;
145 			} else if (day.startsWith("su")) {
146 				this.flsaBeginDayConstant = DateTimeConstants.SUNDAY;
147 			}
148 		}
149 	}
150 
151     public int getFlsaEndDayConstant() {
152         if (this.getFlsaBeginDayConstant() == DateTimeConstants.MONDAY) {
153             return DateTimeConstants.SUNDAY;
154         } else if (this.getFlsaBeginDayConstant() == DateTimeConstants.TUESDAY) {
155             return DateTimeConstants.MONDAY;
156         } else if (this.getFlsaBeginDayConstant() == DateTimeConstants.WEDNESDAY) {
157             return DateTimeConstants.TUESDAY;
158         } else if (this.getFlsaBeginDayConstant() == DateTimeConstants.THURSDAY) {
159             return DateTimeConstants.WEDNESDAY;
160         } else if (this.getFlsaBeginDayConstant() == DateTimeConstants.FRIDAY) {
161             return DateTimeConstants.THURSDAY;
162         } else if (this.getFlsaBeginDayConstant() == DateTimeConstants.SATURDAY) {
163             return DateTimeConstants.FRIDAY;
164         } else if (this.getFlsaBeginDayConstant() == DateTimeConstants.SUNDAY) {
165             return DateTimeConstants.SATURDAY;
166         }
167         return 0;
168     }
169 
170 	/**
171 	 * org.joda.time.DateTimeConstants.MONDAY
172 	 * ...
173 	 * org.joda.time.DateTimeConstants.SUNDAY
174 	 *
175 	 * @return an int representing the FLSA start day, sourced from
176 	 * org.joda.time.DateTimeConstants in the interval [1,7].
177 	 */
178 	public int getFlsaBeginDayConstant() {
179 		if (flsaBeginDayConstant < 0) {
180 			this.setFlsaBeinDayConstant(this.getFlsaBeginDay());
181 		}
182 		return flsaBeginDayConstant;
183 	}
184 
185     @Override
186     public int hashCode() {
187         return HashCodeBuilder.reflectionHashCode(this);
188     }
189 
190     @Override
191     public boolean equals(Object o) {
192         if (o instanceof CalendarBo) {
193             CalendarBo pc = (CalendarBo)o;
194             return this.getHrCalendarId().compareTo(pc.getHrCalendarId()) == 0;
195         } else {
196             return false;
197         }
198     }
199 
200     public static CalendarBo from(Calendar im) {
201         if (im == null) {
202             return null;
203         }
204         CalendarBo cal = new CalendarBo();
205         cal.setHrCalendarId(im.getHrCalendarId());
206         cal.setCalendarName(im.getCalendarName());
207         cal.setCalendarDescriptions(im.getCalendarDescriptions());
208         cal.setFlsaBeginDay(im.getFlsaBeginDay());
209         cal.setFlsaBeginTime(im.getFlsaBeginLocalTime() == null ? null : new Time(im.getFlsaBeginLocalTime().toDateTimeToday().getMillis()));
210         cal.setCalendarTypes(im.getCalendarTypes());
211         cal.setFlsaBeginDayConstant(im.getFlsaBeginDayConstant());
212 
213         cal.setVersionNumber(im.getVersionNumber());
214         cal.setObjectId(im.getObjectId());
215 
216         return cal;
217     }
218 
219     public static Calendar to(CalendarBo bo) {
220         if (bo == null) {
221             return null;
222         }
223 
224         return Calendar.Builder.create(bo).build();
225     }
226 }