001 /**
002 * Copyright 2004-2013 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.hr.time.detail.web;
017
018 import java.sql.Date;
019 import java.text.DateFormat;
020 import java.text.SimpleDateFormat;
021 import java.util.ArrayList;
022 import java.util.Collections;
023 import java.util.HashMap;
024 import java.util.HashSet;
025 import java.util.LinkedHashMap;
026 import java.util.LinkedList;
027 import java.util.List;
028 import java.util.Map;
029 import java.util.Set;
030
031 import org.apache.commons.collections.CollectionUtils;
032 import org.apache.commons.lang.StringUtils;
033 import org.apache.commons.lang.time.DateUtils;
034 import org.joda.time.DateTime;
035 import org.joda.time.format.ISODateTimeFormat;
036 import org.json.simple.JSONValue;
037 import org.kuali.hr.lm.accrual.AccrualCategory;
038 import org.kuali.hr.lm.leaveblock.LeaveBlock;
039 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
040 import org.kuali.hr.lm.leaveplan.LeavePlan;
041 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
042 import org.kuali.hr.time.calendar.CalendarEntries;
043 import org.kuali.hr.time.earncode.EarnCode;
044 import org.kuali.hr.time.earncodegroup.EarnCodeGroup;
045 import org.kuali.hr.time.principal.PrincipalHRAttributes;
046 import org.kuali.hr.time.roles.TkUserRoles;
047 import org.kuali.hr.time.service.base.TkServiceLocator;
048 import org.kuali.hr.time.timeblock.TimeBlock;
049 import org.kuali.hr.time.timeblock.TimeHourDetail;
050 import org.kuali.hr.time.util.TKUser;
051 import org.kuali.hr.time.util.TKUtils;
052 import org.kuali.hr.time.util.TkConstants;
053 import org.kuali.hr.time.workarea.WorkArea;
054 import org.kuali.rice.krad.util.GlobalVariables;
055
056 public class ActionFormUtils {
057
058 // public static void validateHourLimit(TimeDetailActionFormBase tdaf) throws Exception {
059 // List<String> warningMessages = TkServiceLocator.getTimeOffAccrualService().validateAccrualHoursLimit(tdaf.getTimesheetDocument());
060 // addUniqueWarningsToForm(tdaf, warningMessages);
061 // }
062
063 public static void addWarningTextFromEarnGroup(TimeDetailActionFormBase tdaf) throws Exception {
064 List<String> warningMessages = TkServiceLocator.getEarnCodeGroupService().warningTextFromEarnCodeGroupsOfDocument(tdaf.getTimesheetDocument());
065 addUniqueWarningsToForm(tdaf, warningMessages);
066 }
067
068 public static void addUnapprovedIPWarningFromClockLog(TimeDetailActionFormBase tdaf) {
069 List<String> warningMessages = new ArrayList<String>();
070 Set<String> aSet = new HashSet<String>();
071 if(tdaf.getTimesheetDocument() != null) {
072 List<TimeBlock> tbList = tdaf.getTimesheetDocument().getTimeBlocks();
073 if(CollectionUtils.isNotEmpty(tbList)) {
074 aSet.addAll(TkServiceLocator.getClockLogService().getUnapprovedIPWarning(tbList));
075
076 }
077 }
078 warningMessages.addAll(aSet);
079 addUniqueWarningsToForm(tdaf, warningMessages);
080 }
081
082 public static void addUniqueWarningsToForm(TimeDetailActionFormBase tdaf, List<String> warningMessages) {
083 if (!warningMessages.isEmpty()) {
084 Set<String> aSet = new HashSet<String>();
085 aSet.addAll(warningMessages);
086 aSet.addAll(tdaf.getWarningMessages()); //Only warnings. TODO: Do we need actions and info messages here?
087
088 List<String> aList = new ArrayList<String>();
089 aList.addAll(aSet);
090 tdaf.setWarningMessages(aList);
091 }
092 }
093
094 // public static String getTimeBlockJSONMap(List<TimeBlock> blocks) {
095 // List<Map<String, Object>> jsonList = getTimeBlocksJson(blocks, null);
096 // Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
097 // for (Map<String, Object> tbm : jsonList) {
098 // String id = (String) tbm.get("id");
099 // jsonMappedList.put(id, tbm);
100 // }
101 // return JSONValue.toJSONString(jsonMappedList);
102 // }
103
104 public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks, List<LeaveBlock> leaveBlocks) {
105 Map<String, String> aMap = new HashMap<String, String>();
106 List<String> assignmentKeys = new ArrayList<String>();
107
108 for (TimeBlock tb : timeBlocks) {
109 if (!assignmentKeys.contains(tb.getAssignmentKey())) {
110 assignmentKeys.add(tb.getAssignmentKey());
111 }
112 }
113 for(LeaveBlock lb : leaveBlocks) {
114 if (!assignmentKeys.contains(lb.getAssignmentKey())) {
115 assignmentKeys.add(lb.getAssignmentKey());
116 }
117 }
118 Collections.sort(assignmentKeys);
119
120 for (int i = 0; i < assignmentKeys.size(); i++) {
121 // pick a color from a 15 color palette
122 aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 15));
123 }
124
125 return aMap;
126 }
127
128 public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks) {
129 Map<String, String> aMap = new HashMap<String, String>();
130 List<String> assignmentKeys = new ArrayList<String>();
131
132 for (TimeBlock tb : timeBlocks) {
133 if (!assignmentKeys.contains(tb.getAssignmentKey())) {
134 assignmentKeys.add(tb.getAssignmentKey());
135 }
136 }
137
138 Collections.sort(assignmentKeys);
139
140 for (int i = 0; i < assignmentKeys.size(); i++) {
141 // pick a color from a five color palette
142 aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5));
143 }
144
145 return aMap;
146 }
147
148 /**
149 * This method will build the JSON data structure needed for calendar
150 * manipulation and processing on the client side. Start and End times here
151 * are based on the pre-timezone adjusted times startDisplayTime, and
152 * endDisplayTime.
153 *
154 * @param timeBlocks
155 * @return
156 */
157 public static String getTimeBlocksJson(List<TimeBlock> timeBlocks) {
158
159 if (timeBlocks == null || timeBlocks.size() == 0) {
160 return "";
161 }
162
163 List<Map<String, Object>> timeBlockList = new LinkedList<Map<String, Object>>();
164 String timezone = TkServiceLocator.getTimezoneService().getUserTimezone();
165
166 for (TimeBlock timeBlock : timeBlocks) {
167 Map<String, Object> timeBlockMap = new LinkedHashMap<String, Object>();
168
169 WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(timeBlock.getWorkArea(), new java.sql.Date(timeBlock.getEndTimestamp().getTime()));
170 String workAreaDesc = workArea.getDescription();
171
172 // Roles
173 Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
174 timeBlockMap.put("isApprover", isAnyApprover);
175 timeBlockMap.put("isSynchronousUser", timeBlock.getClockLogCreated());
176
177 // Permissions
178 timeBlockMap.put("canEditTb", TkServiceLocator.getPermissionsService().canEditTimeBlock(timeBlock));
179 timeBlockMap.put("canEditTBOvt", TkServiceLocator.getPermissionsService().canEditOvertimeEarnCode(timeBlock));
180 timeBlockMap.put("canAddTB", TkServiceLocator.getPermissionsService().canAddTimeBlock());
181
182 if (TkServiceLocator.getPermissionsService().canEditTimeBlockAllFields(timeBlock)) {
183 timeBlockMap.put("canEditTBAll", true);
184 timeBlockMap.put("canEditTBAssgOnly", false);
185 } else {
186 timeBlockMap.put("canEditTBAll", false);
187 timeBlockMap.put("canEditTBAssgOnly", true);
188 }
189
190 // tracking any kind of 'mutating' state with this object, it's just a one off modification under a specific circumstance.
191 DateTime start = timeBlock.getBeginTimeDisplay();
192 DateTime end = timeBlock.getEndTimeDisplay();
193
194 /**
195 * This is the timeblock backward pushing logic.
196 * the purpose of this is to accommodate the virtual day mode where the start/end period time is not from 12a to 12a.
197 * A timeblock will be pushed back if the timeblock is still within the previous interval
198 */
199 if (timeBlock.isPushBackward()) {
200 start = start.minusDays(1);
201 end = end.minusDays(1);
202 }
203
204 timeBlockMap.put("documentId", timeBlock.getDocumentId());
205 timeBlockMap.put("title", workAreaDesc);
206 EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), timeBlock.getBeginDate());
207 timeBlockMap.put("earnCode", timeBlock.getEarnCode());
208 timeBlockMap.put("earnCodeDesc", ec != null ? ec.getDescription() : StringUtils.EMPTY);
209 //TODO: need to cache this or pre-load it when the app boots up
210 // EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime()));
211 timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType());
212
213 // TODO: Cleanup the start / end time related properties. We certainly don't need all of them.
214 // The ones which are used by the javascript are startDate, endDate, startTime, endTime, startTimeHourMinute, and endTimeHourMinute
215 timeBlockMap.put("start", start.toString(ISODateTimeFormat.dateTimeNoMillis()));
216 timeBlockMap.put("end", end.toString(ISODateTimeFormat.dateTimeNoMillis()));
217 timeBlockMap.put("startDate", start.toString(TkConstants.DT_BASIC_DATE_FORMAT));
218 timeBlockMap.put("endDate", end.toString(TkConstants.DT_BASIC_DATE_FORMAT));
219 timeBlockMap.put("startNoTz", start.toString(ISODateTimeFormat.dateHourMinuteSecond()));
220 timeBlockMap.put("endNoTz", end.toString(ISODateTimeFormat.dateHourMinuteSecond()));
221 // start / endTimeHourMinute fields are for only for the display purpose
222 timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
223 timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
224 // start / endTime are the actual fields used by the adding / editing timeblocks
225 timeBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
226 timeBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
227 timeBlockMap.put("id", timeBlock.getTkTimeBlockId() == null ? null : timeBlock.getTkTimeBlockId().toString());
228 timeBlockMap.put("hours", timeBlock.getHours());
229 timeBlockMap.put("amount", timeBlock.getAmount());
230 timeBlockMap.put("timezone", timezone);
231 timeBlockMap.put("assignment", new AssignmentDescriptionKey(timeBlock.getJobNumber(), timeBlock.getWorkArea(), timeBlock.getTask()).toAssignmentKeyString());
232 timeBlockMap.put("tkTimeBlockId", timeBlock.getTkTimeBlockId() != null ? timeBlock.getTkTimeBlockId() : "");
233 timeBlockMap.put("lunchDeleted", timeBlock.isLunchDeleted());
234
235 List<Map<String, Object>> timeHourDetailList = new LinkedList<Map<String, Object>>();
236 for (TimeHourDetail timeHourDetail : timeBlock.getTimeHourDetails()) {
237 Map<String, Object> timeHourDetailMap = new LinkedHashMap<String, Object>();
238 timeHourDetailMap.put("earnCode", timeHourDetail.getEarnCode());
239 timeHourDetailMap.put("hours", timeHourDetail.getHours());
240 timeHourDetailMap.put("amount", timeHourDetail.getAmount());
241
242 // if there is a lunch hour deduction, add a flag to the timeBlockMap
243 if (StringUtils.equals(timeHourDetail.getEarnCode(), "LUN")) {
244 timeBlockMap.put("lunchDeduction", true);
245 }
246
247 timeHourDetailList.add(timeHourDetailMap);
248 }
249 timeBlockMap.put("timeHourDetails", JSONValue.toJSONString(timeHourDetailList));
250
251 timeBlockList.add(timeBlockMap);
252 }
253
254 // Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
255 // for (Map<String, Object> tbm : timeBlockList) {
256 // String id = (String) tbm.get("id");
257 // jsonMappedList.put(id, tbm);
258 // }
259 return JSONValue.toJSONString(timeBlockList);
260 }
261
262
263 /**
264 * This method will build the leave blocks JSON data structure needed for calendar
265 * manipulation and processing on the client side.
266 *
267 * @param leaveBlocks
268 * @return
269 */
270 public static String getLeaveBlocksJson(List<LeaveBlock> leaveBlocks) {
271 if (CollectionUtils.isEmpty(leaveBlocks)) {
272 return "";
273 }
274 List<Map<String, Object>> leaveBlockList = new LinkedList<Map<String, Object>>();
275 for (LeaveBlock leaveBlock : leaveBlocks) {
276 Map<String, Object> leaveBlockMap = new LinkedHashMap<String, Object>();
277 leaveBlockMap.put("title", leaveBlock.getAssignmentTitle());
278 leaveBlockMap.put("assignment", leaveBlock.getAssignmentKey());
279 leaveBlockMap.put("earnCode", leaveBlock.getEarnCode());
280 leaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId());
281 leaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount().toString());
282 DateTime leaveDate = new DateTime(leaveBlock.getLeaveDate());
283 leaveBlockMap.put("leaveDate", leaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
284 leaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId());
285 leaveBlockMap.put("canTransfer", TkServiceLocator.getPermissionsService().canTransferSSTOUsage(leaveBlock));
286 leaveBlockMap.put("startDate", leaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
287 leaveBlockMap.put("endDate", leaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
288
289 if(leaveBlock.getBeginTimestamp() != null && leaveBlock.getEndTimestamp() != null) {
290 DateTime start = new DateTime(leaveBlock.getBeginTimestamp().getTime());
291 DateTime end = new DateTime(leaveBlock.getEndTimestamp().getTime());
292 leaveBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
293 leaveBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
294 leaveBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
295 leaveBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
296 leaveBlockMap.put("startDate", start.toString(TkConstants.DT_BASIC_DATE_FORMAT));
297 leaveBlockMap.put("endDate", end.toString(TkConstants.DT_BASIC_DATE_FORMAT));
298 }
299
300 leaveBlockList.add(leaveBlockMap);
301 }
302 return JSONValue.toJSONString(leaveBlockList);
303 }
304
305 public static Map<String, String> getPayPeriodsMap(List<CalendarEntries> payPeriods, String viewPrincipal) {
306 // use linked map to keep the order of the pay periods
307 Map<String, String> pMap = Collections.synchronizedMap(new LinkedHashMap<String, String>());
308 if (payPeriods == null || payPeriods.isEmpty()) {
309 return pMap;
310 }
311 payPeriods.removeAll(Collections.singletonList(null));
312 Collections.sort(payPeriods); // sort the pay period list by getBeginPeriodDate
313 Collections.reverse(payPeriods); // newest on top
314 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
315 for (CalendarEntries pce : payPeriods) {
316
317 // Check if service date of user is after the Calendar entry
318 Date asOfDate = new Date(DateUtils.addDays(pce.getEndPeriodDate(),-1).getTime());
319 PrincipalHRAttributes principalHRAttributes = null;
320
321 if(viewPrincipal != null) {
322 principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(viewPrincipal, asOfDate);
323 } else {
324 pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
325 }
326
327 if(principalHRAttributes != null && pce != null && pce.getHrCalendarEntriesId()!= null && pce.getBeginPeriodDate() != null && pce.getEndPeriodDate() != null ) {
328 Date startCalDate = principalHRAttributes.getServiceDate();
329 if(startCalDate != null) {
330 if(!(pce.getBeginPeriodDate().compareTo(startCalDate) < 0)) {
331 //pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format(pce.getEndPeriodDate()));
332 //getting one millisecond of the endperioddate to match the actual pay period. i.e. pay period end at the 11:59:59:59...PM of that day
333 pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
334 }
335 } else {
336 pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
337 }
338 }
339
340 }
341
342 return pMap;
343 }
344
345 // detect if the passed-in calendar entry is the current one
346 public static boolean getOnCurrentPeriodFlag(CalendarEntries pce) {
347 Date currentDate = TKUtils.getTimelessDate(null);
348 String viewPrincipal = TKUser.getCurrentTargetPersonId();
349 CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate);
350
351 if(pce != null && calendarEntry != null && calendarEntry.equals(pce)) {
352 return true;
353 }
354 return false;
355 }
356
357 public static String getUnitOfTimeForEarnCode(EarnCode earnCode) {
358 // AccrualCategory acObj = null;
359 // if(earnCode.getAccrualCategory() != null) {
360 // acObj = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), TKUtils.getCurrentDate());
361 // }
362 // String unitTime = (acObj!= null ? acObj.getUnitOfTime() : earnCode.getRecordMethod()) ;
363 String unitTime = earnCode.getRecordMethod() ;
364 return unitTime;
365 }
366
367 public static int getPlanningMonthsForEmployee(String principalid) {
368 int plannningMonths = 0;
369 PrincipalHRAttributes principalHRAttributes = TkServiceLocator
370 .getPrincipalHRAttributeService().getPrincipalCalendar(
371 principalid, TKUtils.getCurrentDate());
372 if (principalHRAttributes != null
373 && principalHRAttributes.getLeavePlan() != null) {
374 LeavePlan lp = TkServiceLocator.getLeavePlanService()
375 .getLeavePlan(principalHRAttributes.getLeavePlan(),
376 TKUtils.getCurrentDate());
377 if (lp != null && lp.getPlanningMonths() != null) {
378 plannningMonths = Integer.parseInt(lp.getPlanningMonths());
379 }
380 }
381 return plannningMonths;
382 }
383
384 public static List<CalendarEntries> getAllCalendarEntriesForYear(List<CalendarEntries> entries, String year) {
385 List<CalendarEntries> results = new ArrayList<CalendarEntries>();
386 DateFormat df = new SimpleDateFormat("yyyy");
387
388 for(CalendarEntries ce : entries) {
389 if(df.format(ce.getBeginPeriodDate()).equals(year)) {
390 results.add(ce);
391 }
392 }
393 return results;
394 }
395
396 }
397