View Javadoc

1   /**
2    * Copyright 2004-2013 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.hr.time.detail.web;
17  
18  import java.sql.Date;
19  import java.text.DateFormat;
20  import java.text.SimpleDateFormat;
21  import java.util.ArrayList;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.LinkedHashMap;
26  import java.util.LinkedList;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import org.apache.commons.collections.CollectionUtils;
32  import org.apache.commons.lang.StringUtils;
33  import org.apache.commons.lang.time.DateUtils;
34  import org.joda.time.DateTime;
35  import org.joda.time.format.ISODateTimeFormat;
36  import org.json.simple.JSONValue;
37  import org.kuali.hr.lm.accrual.AccrualCategory;
38  import org.kuali.hr.lm.leaveblock.LeaveBlock;
39  import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
40  import org.kuali.hr.lm.leaveplan.LeavePlan;
41  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
42  import org.kuali.hr.time.calendar.CalendarEntries;
43  import org.kuali.hr.time.earncode.EarnCode;
44  import org.kuali.hr.time.earncodegroup.EarnCodeGroup;
45  import org.kuali.hr.time.principal.PrincipalHRAttributes;
46  import org.kuali.hr.time.roles.TkUserRoles;
47  import org.kuali.hr.time.service.base.TkServiceLocator;
48  import org.kuali.hr.time.timeblock.TimeBlock;
49  import org.kuali.hr.time.timeblock.TimeHourDetail;
50  import org.kuali.hr.time.util.TKUser;
51  import org.kuali.hr.time.util.TKUtils;
52  import org.kuali.hr.time.util.TkConstants;
53  import org.kuali.hr.time.workarea.WorkArea;
54  import org.kuali.rice.krad.util.GlobalVariables;
55  
56  public class ActionFormUtils {
57  
58     // public static void validateHourLimit(TimeDetailActionFormBase tdaf) throws Exception {
59     //     List<String> warningMessages = TkServiceLocator.getTimeOffAccrualService().validateAccrualHoursLimit(tdaf.getTimesheetDocument());
60     //      addUniqueWarningsToForm(tdaf, warningMessages);
61     // }
62  
63      public static void addWarningTextFromEarnGroup(TimeDetailActionFormBase tdaf) throws Exception {
64          List<String> warningMessages = TkServiceLocator.getEarnCodeGroupService().warningTextFromEarnCodeGroupsOfDocument(tdaf.getTimesheetDocument());
65          addUniqueWarningsToForm(tdaf, warningMessages);
66      }
67  
68      public static void addUnapprovedIPWarningFromClockLog(TimeDetailActionFormBase tdaf) {
69      	List<String> warningMessages = new ArrayList<String>();
70      	Set<String> aSet = new HashSet<String>();
71      	if(tdaf.getTimesheetDocument() != null) {
72  	    	List<TimeBlock> tbList = tdaf.getTimesheetDocument().getTimeBlocks();
73  	    	if(CollectionUtils.isNotEmpty(tbList)) {
74  		    	 aSet.addAll(TkServiceLocator.getClockLogService().getUnapprovedIPWarning(tbList));
75  		        
76  	    	}
77      	}
78      	warningMessages.addAll(aSet);
79      	addUniqueWarningsToForm(tdaf, warningMessages);
80      }
81      
82      public static void addUniqueWarningsToForm(TimeDetailActionFormBase tdaf, List<String> warningMessages) {
83          if (!warningMessages.isEmpty()) {
84              Set<String> aSet = new HashSet<String>();
85              aSet.addAll(warningMessages);
86              aSet.addAll(tdaf.getWarningMessages()); //Only warnings. TODO: Do we need actions and info messages here?
87  
88              List<String> aList = new ArrayList<String>();
89              aList.addAll(aSet);
90              tdaf.setWarningMessages(aList);
91          }
92      }
93  
94  //    public static String getTimeBlockJSONMap(List<TimeBlock> blocks) {
95  //        List<Map<String, Object>> jsonList = getTimeBlocksJson(blocks, null);
96  //        Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
97  //        for (Map<String, Object> tbm : jsonList) {
98  //            String id = (String) tbm.get("id");
99  //            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 five color palette
122             aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5));
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             timeBlockMap.put("earnCode", timeBlock.getEarnCode());
207             timeBlockMap.put("earnCodeDesc", TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), TKUtils.getCurrentDate()).getDescription());
208             //TODO: need to cache this or pre-load it when the app boots up
209             // EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), new java.sql.Date(timeBlock.getBeginTimestamp().getTime()));
210             timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType());
211 
212             // TODO: Cleanup the start / end time related properties. We certainly don't need all of them.
213             // The ones which are used by the javascript are startDate, endDate, startTime, endTime, startTimeHourMinute, and endTimeHourMinute
214             timeBlockMap.put("start", start.toString(ISODateTimeFormat.dateTimeNoMillis()));
215             timeBlockMap.put("end", end.toString(ISODateTimeFormat.dateTimeNoMillis()));
216             timeBlockMap.put("startDate", start.toString(TkConstants.DT_BASIC_DATE_FORMAT));
217             timeBlockMap.put("endDate", end.toString(TkConstants.DT_BASIC_DATE_FORMAT));
218             timeBlockMap.put("startNoTz", start.toString(ISODateTimeFormat.dateHourMinuteSecond()));
219             timeBlockMap.put("endNoTz", end.toString(ISODateTimeFormat.dateHourMinuteSecond()));
220             // start / endTimeHourMinute fields are for only for the display purpose
221             timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
222             timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
223             // start / endTime are the actual fields used by the adding / editing timeblocks
224             timeBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
225             timeBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
226             timeBlockMap.put("id", timeBlock.getTkTimeBlockId() == null ? null : timeBlock.getTkTimeBlockId().toString());
227             timeBlockMap.put("hours", timeBlock.getHours());
228             timeBlockMap.put("amount", timeBlock.getAmount());
229             timeBlockMap.put("timezone", timezone);
230             timeBlockMap.put("assignment", new AssignmentDescriptionKey(timeBlock.getJobNumber(), timeBlock.getWorkArea(), timeBlock.getTask()).toAssignmentKeyString());
231             timeBlockMap.put("tkTimeBlockId", timeBlock.getTkTimeBlockId() != null ? timeBlock.getTkTimeBlockId() : "");
232             timeBlockMap.put("lunchDeleted", timeBlock.isLunchDeleted());
233 
234             List<Map<String, Object>> timeHourDetailList = new LinkedList<Map<String, Object>>();
235             for (TimeHourDetail timeHourDetail : timeBlock.getTimeHourDetails()) {
236                 Map<String, Object> timeHourDetailMap = new LinkedHashMap<String, Object>();
237                 timeHourDetailMap.put("earnCode", timeHourDetail.getEarnCode());
238                 timeHourDetailMap.put("hours", timeHourDetail.getHours());
239                 timeHourDetailMap.put("amount", timeHourDetail.getAmount());
240 
241                 // if there is a lunch hour deduction, add a flag to the timeBlockMap
242                 if (StringUtils.equals(timeHourDetail.getEarnCode(), "LUN")) {
243                     timeBlockMap.put("lunchDeduction", true);
244                 }
245 
246                 timeHourDetailList.add(timeHourDetailMap);
247             }
248             timeBlockMap.put("timeHourDetails", JSONValue.toJSONString(timeHourDetailList));
249 
250             timeBlockList.add(timeBlockMap);
251         }
252 
253 //        Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
254 //        for (Map<String, Object> tbm : timeBlockList) {
255 //            String id = (String) tbm.get("id");
256 //            jsonMappedList.put(id, tbm);
257 //        }
258         return JSONValue.toJSONString(timeBlockList);
259     }
260     
261     
262     /**
263      * This method will build the leave blocks JSON data structure needed for calendar
264      * manipulation and processing on the client side.
265      *
266      * @param leaveBlocks
267      * @return
268      */
269     public static String getLeaveBlocksJson(List<LeaveBlock> leaveBlocks) {
270     	if (CollectionUtils.isEmpty(leaveBlocks)) {
271             return "";
272         }
273         List<Map<String, Object>> leaveBlockList = new LinkedList<Map<String, Object>>();
274         for (LeaveBlock leaveBlock : leaveBlocks) {
275         	Map<String, Object> leaveBlockMap = new LinkedHashMap<String, Object>();
276         	leaveBlockMap.put("title", leaveBlock.getAssignmentTitle());
277         	leaveBlockMap.put("assignment", leaveBlock.getAssignmentKey());
278         	leaveBlockMap.put("earnCode", leaveBlock.getEarnCode());
279         	leaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId());
280         	leaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount().toString());
281         	DateTime leaveDate = new DateTime(leaveBlock.getLeaveDate());
282         	leaveBlockMap.put("leaveDate", leaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
283         	leaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId());
284         	leaveBlockMap.put("canTransfer", TkServiceLocator.getPermissionsService().canTransferSSTOUsage(leaveBlock));
285         	leaveBlockMap.put("startDate", leaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
286         	leaveBlockMap.put("endDate", leaveDate.toString(TkConstants.DT_BASIC_DATE_FORMAT));
287         	
288         	if(leaveBlock.getBeginTimestamp() != null && leaveBlock.getEndTimestamp() != null) {
289 	            DateTime start = new DateTime(leaveBlock.getBeginTimestamp().getTime());
290 	        	DateTime end = new DateTime(leaveBlock.getEndTimestamp().getTime());
291 	        	leaveBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
292 	        	leaveBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
293 	        	leaveBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
294 	        	leaveBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
295 	        	leaveBlockMap.put("startDate", start.toString(TkConstants.DT_BASIC_DATE_FORMAT));
296 	        	leaveBlockMap.put("endDate", end.toString(TkConstants.DT_BASIC_DATE_FORMAT));
297             }
298         	
299         	leaveBlockList.add(leaveBlockMap);
300         }
301     	return JSONValue.toJSONString(leaveBlockList);
302     }
303     
304     public static Map<String, String> getPayPeriodsMap(List<CalendarEntries> payPeriods, String viewPrincipal) {
305     	// use linked map to keep the order of the pay periods
306     	Map<String, String> pMap = Collections.synchronizedMap(new LinkedHashMap<String, String>());
307     	if (payPeriods == null || payPeriods.isEmpty()) {
308             return pMap;
309         }
310     	payPeriods.removeAll(Collections.singletonList(null));
311     	Collections.sort(payPeriods);  // sort the pay period list by getBeginPeriodDate
312     	Collections.reverse(payPeriods);  // newest on top
313     	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
314         for (CalendarEntries pce : payPeriods) {
315         	
316         	// Check if service date of user is after the Calendar entry
317             Date asOfDate = new Date(DateUtils.addDays(pce.getEndPeriodDate(),-1).getTime());
318     		PrincipalHRAttributes principalHRAttributes = null;
319     		
320     		if(viewPrincipal != null) {
321     			principalHRAttributes = TkServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(viewPrincipal, asOfDate);
322     		} else {
323     			pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
324     		}
325     		
326     		if(principalHRAttributes != null && pce != null && pce.getHrCalendarEntriesId()!= null && pce.getBeginPeriodDate() != null && pce.getEndPeriodDate() != null ) {
327     			Date startCalDate = principalHRAttributes.getServiceDate();
328     			if(startCalDate != null) {
329     				if(!(pce.getBeginPeriodDate().compareTo(startCalDate) < 0)) {
330     	        		//pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format(pce.getEndPeriodDate()));
331     	                //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
332     	                pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
333             		} 
334             	} else {
335             		pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));    			
336             	}
337     		} 
338     		
339         }
340         
341     	return pMap;
342     }
343     
344     // detect if the passed-in calendar entry is the current one
345     public static boolean getOnCurrentPeriodFlag(CalendarEntries pce) {
346     	Date currentDate = TKUtils.getTimelessDate(null);
347     	String viewPrincipal = TKUser.getCurrentTargetPersonId();
348         CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal,  currentDate);
349 
350         if(pce != null && calendarEntry != null && calendarEntry.equals(pce)) {
351     		return true;
352     	}
353     	return false;
354     }
355      
356     public static String getUnitOfTimeForEarnCode(EarnCode earnCode) {
357 //    	AccrualCategory acObj = null;
358 //    	if(earnCode.getAccrualCategory() != null) {
359 //    		acObj = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), TKUtils.getCurrentDate());
360 //    	}
361 //    	String unitTime = (acObj!= null ? acObj.getUnitOfTime() : earnCode.getRecordMethod()) ;
362     	String unitTime = earnCode.getRecordMethod() ;
363     	return unitTime;
364     }
365     
366     public static int getPlanningMonthsForEmployee(String principalid) {
367 		int plannningMonths = 0;
368 		PrincipalHRAttributes principalHRAttributes = TkServiceLocator
369 				.getPrincipalHRAttributeService().getPrincipalCalendar(
370 						principalid, TKUtils.getCurrentDate());
371 		if (principalHRAttributes != null
372 				&& principalHRAttributes.getLeavePlan() != null) {
373 			LeavePlan lp = TkServiceLocator.getLeavePlanService()
374 					.getLeavePlan(principalHRAttributes.getLeavePlan(),
375 							TKUtils.getCurrentDate());
376 			if (lp != null && lp.getPlanningMonths() != null) {
377 				plannningMonths = Integer.parseInt(lp.getPlanningMonths());
378 			}
379 		}
380 		return plannningMonths;
381     }
382     
383     public static List<CalendarEntries> getAllCalendarEntriesForYear(List<CalendarEntries> entries, String year) {
384     	List<CalendarEntries> results = new ArrayList<CalendarEntries>();
385     	DateFormat df = new SimpleDateFormat("yyyy");
386     	 
387     	for(CalendarEntries ce : entries) {
388     		if(df.format(ce.getBeginPeriodDate()).equals(year)) {
389     			results.add(ce);
390     		}
391     	}
392     	return results;
393     }
394     
395 }
396