1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
59
60
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());
91
92 List<String> aList = new ArrayList<String>();
93 aList.addAll(aSet);
94 tdaf.setWarningMessages(aList);
95 }
96 }
97
98
99
100
101
102
103
104
105
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
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
146 aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5));
147 }
148
149 return aMap;
150 }
151
152
153
154
155
156
157
158
159
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
195 DateTime start = timeBlock.getBeginTimeDisplay();
196 DateTime end = timeBlock.getEndTimeDisplay();
197
198
199
200
201
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
214
215 timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType());
216
217
218
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
226 timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
227 timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
228
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
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
257
258
259 }
260
261
262
263
264
265 String value = JSONValue.toJSONString(timeBlockList);
266
267 return value;
268 }
269
270
271
272
273
274
275
276
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
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);
321 Collections.reverse(payPeriods);
322 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
323 for (CalendarEntry pce : payPeriods) {
324
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
339
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
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
365
366
367
368
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