1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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     
59     
60     
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()); 
87  
88              List<String> aList = new ArrayList<String>();
89              aList.addAll(aSet);
90              tdaf.setWarningMessages(aList);
91          }
92      }
93  
94  
95  
96  
97  
98  
99  
100 
101 
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             
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             
142             aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5));
143         }
144 
145         return aMap;
146     }
147 
148     
149 
150 
151 
152 
153 
154 
155 
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             
173             Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
174             timeBlockMap.put("isApprover", isAnyApprover);
175             timeBlockMap.put("isSynchronousUser", timeBlock.getClockLogCreated());
176 
177             
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             
191             DateTime start = timeBlock.getBeginTimeDisplay();
192             DateTime end = timeBlock.getEndTimeDisplay();
193 
194             
195 
196 
197 
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             
209             
210             timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType());
211 
212             
213             
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             
221             timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
222             timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
223             
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                 
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 
254 
255 
256 
257 
258         return JSONValue.toJSONString(timeBlockList);
259     }
260     
261     
262     
263 
264 
265 
266 
267 
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     	
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);  
312     	Collections.reverse(payPeriods);  
313     	SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
314         for (CalendarEntries pce : payPeriods) {
315         	
316         	
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     	        		
331     	                
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     
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 
358 
359 
360 
361 
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