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.tklm.time.detail.web;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.HashMap;
22  import java.util.HashSet;
23  import java.util.LinkedHashMap;
24  import java.util.LinkedList;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.apache.commons.collections.CollectionUtils;
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.commons.lang.time.DateUtils;
32  import org.joda.time.DateTime;
33  import org.joda.time.LocalDate;
34  import org.joda.time.format.ISODateTimeFormat;
35  import org.json.simple.JSONValue;
36  import org.kuali.kpme.core.KPMENamespace;
37  import org.kuali.kpme.core.assignment.AssignmentDescriptionKey;
38  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
39  import org.kuali.kpme.core.earncode.EarnCode;
40  import org.kuali.kpme.core.leaveplan.LeavePlan;
41  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
42  import org.kuali.kpme.core.role.KPMERole;
43  import org.kuali.kpme.core.service.HrServiceLocator;
44  import org.kuali.kpme.core.util.HrConstants;
45  import org.kuali.kpme.core.util.HrContext;
46  import org.kuali.kpme.core.workarea.WorkArea;
47  import org.kuali.kpme.tklm.common.TkConstants;
48  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
49  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
50  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
51  import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
52  import org.kuali.kpme.tklm.time.timehourdetail.TimeHourDetail;
53  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
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 = new ArrayList<String>();
65          
66          TimesheetDocument tdoc = tdaf.getTimesheetDocument();
67          
68          warningMessages = HrServiceLocator.getEarnCodeGroupService().getWarningTextFromEarnCodeGroups(tdoc.getEarnCodeMap());
69          addUniqueWarningsToForm(tdaf, warningMessages);
70      }
71  
72      public static void addUnapprovedIPWarningFromClockLog(TimeDetailActionFormBase tdaf) {
73      	List<String> warningMessages = new ArrayList<String>();
74      	Set<String> aSet = new HashSet<String>();
75      	if(tdaf.getTimesheetDocument() != null) {
76  	    	List<TimeBlock> tbList = tdaf.getTimesheetDocument().getTimeBlocks();
77  	    	if(CollectionUtils.isNotEmpty(tbList)) {
78  		    	 aSet.addAll(TkServiceLocator.getClockLogService().getUnapprovedIPWarning(tbList));
79  		        
80  	    	}
81      	}
82      	warningMessages.addAll(aSet);
83      	addUniqueWarningsToForm(tdaf, warningMessages);
84      }
85      
86      public static void addUniqueWarningsToForm(TimeDetailActionFormBase tdaf, List<String> warningMessages) {
87          if (!warningMessages.isEmpty()) {
88              Set<String> aSet = new HashSet<String>();
89              aSet.addAll(warningMessages);
90              aSet.addAll(tdaf.getWarningMessages()); //Only warnings. TODO: Do we need actions and info messages here?
91  
92              List<String> aList = new ArrayList<String>();
93              aList.addAll(aSet);
94              tdaf.setWarningMessages(aList);
95          }
96      }
97  
98  //    public static String getTimeBlockJSONMap(List<TimeBlock> blocks) {
99  //        List<Map<String, Object>> jsonList = getTimeBlocksJson(blocks, null);
100 //        Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
101 //        for (Map<String, Object> tbm : jsonList) {
102 //            String id = (String) tbm.get("id");
103 //            jsonMappedList.put(id, tbm);
104 //        }
105 //        return JSONValue.toJSONString(jsonMappedList);
106 //    }
107 
108     public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks, List<LeaveBlock> leaveBlocks) {
109     	Map<String, String> aMap = new HashMap<String, String>();
110         List<String> assignmentKeys = new ArrayList<String>();
111 
112         for (TimeBlock tb : timeBlocks) {
113             if (!assignmentKeys.contains(tb.getAssignmentKey())) {
114                 assignmentKeys.add(tb.getAssignmentKey());
115             }
116         }
117         for(LeaveBlock lb : leaveBlocks) {
118         	if (!assignmentKeys.contains(lb.getAssignmentKey())) {
119                 assignmentKeys.add(lb.getAssignmentKey());
120             }
121         }
122         Collections.sort(assignmentKeys);
123 
124         for (int i = 0; i < assignmentKeys.size(); i++) {
125             // pick a color from a 15 color palette
126             aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 15));
127         }
128 
129         return aMap;
130     }
131     
132     public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks) {
133         Map<String, String> aMap = new HashMap<String, String>();
134         List<String> assignmentKeys = new ArrayList<String>();
135 
136         for (TimeBlock tb : timeBlocks) {
137             if (!assignmentKeys.contains(tb.getAssignmentKey())) {
138                 assignmentKeys.add(tb.getAssignmentKey());
139             }
140         }
141 
142         Collections.sort(assignmentKeys);
143 
144         for (int i = 0; i < assignmentKeys.size(); i++) {
145             // pick a color from a five color palette
146             aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5));
147         }
148 
149         return aMap;
150     }
151 
152     /**
153      * This method will build the JSON data structure needed for calendar
154      * manipulation and processing on the client side. Start and End times here
155      * are based on the pre-timezone adjusted times startDisplayTime, and
156      * endDisplayTime.
157      *
158      * @param timeBlocks
159      * @return
160      */
161     public static String getTimeBlocksJson(List<TimeBlock> timeBlocks) {
162         if (timeBlocks == null || timeBlocks.size() == 0) {
163             return "";
164         }
165 
166         List<Map<String, Object>> timeBlockList = new LinkedList<Map<String, Object>>();
167         String timezone = HrServiceLocator.getTimezoneService().getUserTimezone();
168 
169         String principalId = GlobalVariables.getUserSession().getPrincipalId();
170 
171         boolean isAnyApprover = HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay())
172                 || HrServiceLocator.getKPMERoleService().principalHasRole(principalId, KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName(), LocalDate.now().toDateTimeAtStartOfDay());
173 
174         for (TimeBlock timeBlock : timeBlocks) {
175             Map<String, Object> timeBlockMap = new LinkedHashMap<String, Object>();
176 
177             WorkArea workArea = HrServiceLocator.getWorkAreaService().getWorkAreaWithoutRoles(timeBlock.getWorkArea(), timeBlock.getEndDateTime().toLocalDate());
178             String workAreaDesc = workArea.getDescription();
179 
180             timeBlockMap.put("isApprover", isAnyApprover);
181             timeBlockMap.put("isSynchronousUser", timeBlock.getClockLogCreated());
182 
183             timeBlockMap.put("canEditTb", TkServiceLocator.getTKPermissionService().canEditTimeBlock(principalId, timeBlock));
184             timeBlockMap.put("canEditTBOvt", TkServiceLocator.getTKPermissionService().canEditOvertimeEarnCode(principalId, timeBlock));
185 
186             if (TkServiceLocator.getTKPermissionService().canEditTimeBlockAllFields(principalId, timeBlock)) {
187                 timeBlockMap.put("canEditTBAll", true);
188                 timeBlockMap.put("canEditTBAssgOnly", false);
189             } else {
190                 timeBlockMap.put("canEditTBAll", false);
191                 timeBlockMap.put("canEditTBAssgOnly", true);
192             }
193 
194             //    tracking any kind of 'mutating' state with this object, it's just a one off modification under a specific circumstance.
195             DateTime start = timeBlock.getBeginTimeDisplay();
196             DateTime end = timeBlock.getEndTimeDisplay();
197 
198             /**
199              * This is the timeblock backward pushing logic.
200              * the purpose of this is to accommodate the virtual day mode where the start/end period time is not from 12a to 12a.
201              * A timeblock will be pushed back if the timeblock is still within the previous interval
202              */
203             if (timeBlock.getPushBackward()) {
204                 start = start.minusDays(1);
205                 end = end.minusDays(1);
206             }
207 
208             timeBlockMap.put("documentId", timeBlock.getDocumentId());
209             timeBlockMap.put("title", workAreaDesc);
210             EarnCode ec = HrServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), timeBlock.getBeginDateTime().toLocalDate());
211             timeBlockMap.put("earnCode", timeBlock.getEarnCode());
212             timeBlockMap.put("earnCodeDesc", ec != null ? ec.getDescription() : StringUtils.EMPTY);
213             //TODO: need to cache this or pre-load it when the app boots up
214             // EarnCode earnCode = HrServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), timeBlock.getBeginDateTime().toLocalDate());
215             timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType());
216 
217             // TODO: Cleanup the start / end time related properties. We certainly don't need all of them.
218             // The ones which are used by the javascript are startDate, endDate, startTime, endTime, startTimeHourMinute, and endTimeHourMinute
219             timeBlockMap.put("start", start.toString(ISODateTimeFormat.dateTimeNoMillis()));
220             timeBlockMap.put("end", end.toString(ISODateTimeFormat.dateTimeNoMillis()));
221             timeBlockMap.put("startDate", start.toString(HrConstants.DT_BASIC_DATE_FORMAT));
222             timeBlockMap.put("endDate", end.toString(HrConstants.DT_BASIC_DATE_FORMAT));
223             timeBlockMap.put("startNoTz", start.toString(ISODateTimeFormat.dateHourMinuteSecond()));
224             timeBlockMap.put("endNoTz", end.toString(ISODateTimeFormat.dateHourMinuteSecond()));
225             // start / endTimeHourMinute fields are for only for the display purpose
226             timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
227             timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
228             // start / endTime are the actual fields used by the adding / editing timeblocks
229             timeBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
230             timeBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
231             timeBlockMap.put("id", timeBlock.getTkTimeBlockId() == null ? null : timeBlock.getTkTimeBlockId());
232             timeBlockMap.put("hours", timeBlock.getHours());
233             timeBlockMap.put("amount", timeBlock.getAmount());
234             timeBlockMap.put("timezone", timezone);
235             timeBlockMap.put("assignment", new AssignmentDescriptionKey(timeBlock.getJobNumber(), timeBlock.getWorkArea(), timeBlock.getTask()).toAssignmentKeyString());
236             timeBlockMap.put("tkTimeBlockId", timeBlock.getTkTimeBlockId() != null ? timeBlock.getTkTimeBlockId() : "");
237             timeBlockMap.put("lunchDeleted", timeBlock.isLunchDeleted());
238 
239             List<Map<String, Object>> timeHourDetailList = new LinkedList<Map<String, Object>>();
240             for (TimeHourDetail timeHourDetail : timeBlock.getTimeHourDetails()) {
241                 Map<String, Object> timeHourDetailMap = new LinkedHashMap<String, Object>();
242                 timeHourDetailMap.put("earnCode", timeHourDetail.getEarnCode());
243                 timeHourDetailMap.put("hours", timeHourDetail.getHours());
244                 timeHourDetailMap.put("amount", timeHourDetail.getAmount());
245 
246                 // if there is a lunch hour deduction, add a flag to the timeBlockMap
247                 if (StringUtils.equals(timeHourDetail.getEarnCode(), "LUN")) {
248                     timeBlockMap.put("lunchDeduction", true);
249                 }
250 
251                 timeHourDetailList.add(timeHourDetailMap);
252             }
253             timeBlockMap.put("timeHourDetails", JSONValue.toJSONString(timeHourDetailList));
254 
255             timeBlockList.add(timeBlockMap);
256             //System.out.println("\n\n\n\n");
257             //System.out.println(sw.prettyPrint());
258             //System.out.println("\n\n\n\n");
259         }
260 //        Map<String, Map<String, Object>> jsonMappedList = new HashMap<String, Map<String, Object>>();
261 //        for (Map<String, Object> tbm : timeBlockList) {
262 //            String id = (String) tbm.get("id");
263 //            jsonMappedList.put(id, tbm);
264 //        }
265         String value = JSONValue.toJSONString(timeBlockList);
266 
267         return value;
268     }
269     
270     
271     /**
272      * This method will build the leave blocks JSON data structure needed for calendar
273      * manipulation and processing on the client side.
274      *
275      * @param leaveBlocks
276      * @return
277      */
278     public static String getLeaveBlocksJson(List<LeaveBlock> leaveBlocks) {
279     	if (CollectionUtils.isEmpty(leaveBlocks)) {
280             return "";
281         }
282         List<Map<String, Object>> leaveBlockList = new LinkedList<Map<String, Object>>();
283         for (LeaveBlock leaveBlock : leaveBlocks) {
284         	Map<String, Object> leaveBlockMap = new LinkedHashMap<String, Object>();
285         	leaveBlockMap.put("title", leaveBlock.getAssignmentTitle());
286         	leaveBlockMap.put("assignment", leaveBlock.getAssignmentKey());
287         	leaveBlockMap.put("earnCode", leaveBlock.getEarnCode());
288         	leaveBlockMap.put("lmLeaveBlockId", leaveBlock.getLmLeaveBlockId());
289         	leaveBlockMap.put("leaveAmount", leaveBlock.getLeaveAmount().toString());
290         	DateTime leaveDate = leaveBlock.getLeaveLocalDate().toDateTimeAtStartOfDay();
291         	leaveBlockMap.put("leaveDate", leaveDate.toString(HrConstants.DT_BASIC_DATE_FORMAT));
292         	leaveBlockMap.put("id", leaveBlock.getLmLeaveBlockId());
293         	leaveBlockMap.put("canTransfer", LmServiceLocator.getLMPermissionService().canTransferSSTOUsage(leaveBlock));
294         	leaveBlockMap.put("startDate", leaveDate.toString(HrConstants.DT_BASIC_DATE_FORMAT));
295         	leaveBlockMap.put("endDate", leaveDate.toString(HrConstants.DT_BASIC_DATE_FORMAT));
296         	
297         	if(leaveBlock.getBeginTimestamp() != null && leaveBlock.getEndTimestamp() != null) {
298 	            DateTime start = leaveBlock.getBeginDateTime();
299 	        	DateTime end = leaveBlock.getEndDateTime();
300 	        	leaveBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
301 	        	leaveBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
302 	        	leaveBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
303 	        	leaveBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
304 	        	leaveBlockMap.put("startDate", start.toString(HrConstants.DT_BASIC_DATE_FORMAT));
305 	        	leaveBlockMap.put("endDate", end.toString(HrConstants.DT_BASIC_DATE_FORMAT));
306             }
307         	
308         	leaveBlockList.add(leaveBlockMap);
309         }
310     	return JSONValue.toJSONString(leaveBlockList);
311     }
312     
313     public static Map<String, String> getPayPeriodsMap(List<CalendarEntry> payPeriods, String viewPrincipal) {
314     	// use linked map to keep the order of the pay periods
315     	Map<String, String> pMap = Collections.synchronizedMap(new LinkedHashMap<String, String>());
316     	if (payPeriods == null || payPeriods.isEmpty()) {
317             return pMap;
318         }
319     	payPeriods.removeAll(Collections.singletonList(null));
320     	Collections.sort(payPeriods);  // sort the pay period list by getBeginPeriodDate
321     	Collections.reverse(payPeriods);  // newest on top
322     	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
323         for (CalendarEntry pce : payPeriods) {
324         	// Check if service date of user is after the Calendar entry
325             DateTime asOfDate = pce.getEndPeriodFullDateTime().minusDays(1);
326     		PrincipalHRAttributes principalHRAttributes = null;
327     		
328     		if(viewPrincipal != null) {
329     			principalHRAttributes = HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(viewPrincipal, asOfDate.toLocalDate());
330     		} else {
331     			pMap.put(pce.getHrCalendarEntryId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
332     		}
333     		
334     		if(principalHRAttributes != null && pce != null && pce.getHrCalendarEntryId()!= null && pce.getBeginPeriodDate() != null && pce.getEndPeriodDate() != null ) {
335     			LocalDate startCalDate = principalHRAttributes.getServiceLocalDate();
336     			if(startCalDate != null) {
337     				if(!(pce.getBeginPeriodFullDateTime().compareTo(startCalDate.toDateTimeAtStartOfDay()) < 0)) {
338     	        		//pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format(pce.getEndPeriodDate()));
339     	                //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
340     	                pMap.put(pce.getHrCalendarEntryId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
341             		} 
342             	} else {
343             		pMap.put(pce.getHrCalendarEntryId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));    			
344             	}
345     		} 
346     		
347         }
348         
349     	return pMap;
350     }
351     
352     // detect if the passed-in calendar entry is the current one
353     public static boolean isOnCurrentPeriodFlag(CalendarEntry pce) {
354     	String viewPrincipal = HrContext.getTargetPrincipalId();
355         CalendarEntry calendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(viewPrincipal, new LocalDate().toDateTimeAtStartOfDay());
356 
357         if(pce != null && calendarEntry != null && calendarEntry.equals(pce)) {
358     		return true;
359     	}
360     	return false;
361     }
362      
363     public static String getUnitOfTimeForEarnCode(EarnCode earnCode) {
364 //    	AccrualCategory acObj = null;
365 //    	if(earnCode.getAccrualCategory() != null) {
366 //    		acObj = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), TKUtils.getCurrentDate());
367 //    	}
368 //    	String unitTime = (acObj!= null ? acObj.getUnitOfTime() : earnCode.getRecordMethod()) ;
369     	String unitTime = earnCode.getRecordMethod() ;
370     	return unitTime;
371     }
372     
373     public static int getPlanningMonthsForEmployee(String principalid) {
374 		int plannningMonths = 0;
375 		PrincipalHRAttributes principalHRAttributes = HrServiceLocator
376 				.getPrincipalHRAttributeService().getPrincipalCalendar(
377 						principalid, LocalDate.now());
378 		if (principalHRAttributes != null
379 				&& principalHRAttributes.getLeavePlan() != null) {
380 			LeavePlan lp = HrServiceLocator.getLeavePlanService()
381 					.getLeavePlan(principalHRAttributes.getLeavePlan(),
382 							LocalDate.now());
383 			if (lp != null && lp.getPlanningMonths() != null) {
384 				plannningMonths = Integer.parseInt(lp.getPlanningMonths());
385 			}
386 		}
387 		return plannningMonths;
388     }
389     
390     public static List<CalendarEntry> getAllCalendarEntriesForYear(List<CalendarEntry> calendarEntries, String year) {
391     	List<CalendarEntry> allCalendarEntriesForYear = new ArrayList<CalendarEntry>();
392     	
393 	    for (CalendarEntry calendarEntry : calendarEntries) {
394 	    	String calendarEntryYear = calendarEntry.getBeginPeriodFullDateTime().toString("yyyy");
395 	    	if (StringUtils.equals(calendarEntryYear, year)) {
396 	    		allCalendarEntriesForYear.add(calendarEntry);
397 	    	}
398 	    }
399     	
400     	return allCalendarEntriesForYear;
401     }
402     
403 }
404