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.SimpleDateFormat;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.LinkedHashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import org.apache.commons.collections.CollectionUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.apache.commons.lang.time.DateUtils;
33 import org.joda.time.DateTime;
34 import org.joda.time.format.ISODateTimeFormat;
35 import org.json.simple.JSONValue;
36 import org.kuali.hr.time.accrual.AccrualCategory;
37 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
38 import org.kuali.hr.time.calendar.CalendarEntries;
39 import org.kuali.hr.time.earncode.EarnCode;
40 import org.kuali.hr.time.roles.TkUserRoles;
41 import org.kuali.hr.time.service.base.TkServiceLocator;
42 import org.kuali.hr.time.timeblock.TimeBlock;
43 import org.kuali.hr.time.timeblock.TimeHourDetail;
44 import org.kuali.hr.time.util.TKUser;
45 import org.kuali.hr.time.util.TKUtils;
46 import org.kuali.hr.time.util.TkConstants;
47 import org.kuali.hr.time.workarea.WorkArea;
48 import org.kuali.rice.krad.util.GlobalVariables;
49
50 public class ActionFormUtils {
51
52 public static void validateHourLimit(TimeDetailActionFormBase tdaf) throws Exception {
53 List<String> warningMessages = TkServiceLocator.getTimeOffAccrualService().validateAccrualHoursLimit(tdaf.getTimesheetDocument());
54 addUniqueWarningsToForm(tdaf, warningMessages);
55 }
56
57 public static void addWarningTextFromEarnGroup(TimeDetailActionFormBase tdaf) throws Exception {
58 List<String> warningMessages = TkServiceLocator.getEarnGroupService().warningTextFromEarnGroupsOfDocument(tdaf.getTimesheetDocument());
59 addUniqueWarningsToForm(tdaf, warningMessages);
60 }
61
62 public static void addUnapprovedIPWarningFromClockLog(TimeDetailActionFormBase tdaf) {
63 List<String> warningMessages = new ArrayList<String>();
64 Set<String> aSet = new HashSet<String>();
65 if(tdaf.getTimesheetDocument() != null) {
66 List<TimeBlock> tbList = tdaf.getTimesheetDocument().getTimeBlocks();
67 if(CollectionUtils.isNotEmpty(tbList)) {
68 aSet.addAll(TkServiceLocator.getClockLogService().getUnapprovedIPWarning(tbList));
69
70 }
71 }
72 warningMessages.addAll(aSet);
73 addUniqueWarningsToForm(tdaf, warningMessages);
74 }
75
76 public static void addUniqueWarningsToForm(TimeDetailActionFormBase tdaf, List<String> warningMessages) {
77 if (!warningMessages.isEmpty()) {
78 Set<String> aSet = new HashSet<String>();
79 aSet.addAll(warningMessages);
80 aSet.addAll(tdaf.getWarnings());
81 List<String> aList = new ArrayList<String>();
82 aList.addAll(aSet);
83 tdaf.setWarnings(aList);
84 }
85 }
86
87
88
89
90
91
92
93
94
95
96
97 public static Map<String, String> buildAssignmentStyleClassMap(List<TimeBlock> timeBlocks) {
98 Map<String, String> aMap = new HashMap<String, String>();
99 List<String> assignmentKeys = new ArrayList<String>();
100
101 for (TimeBlock tb : timeBlocks) {
102 if (!assignmentKeys.contains(tb.getAssignmentKey())) {
103 assignmentKeys.add(tb.getAssignmentKey());
104 }
105 }
106
107 Collections.sort(assignmentKeys);
108
109 for (int i = 0; i < assignmentKeys.size(); i++) {
110
111 aMap.put(assignmentKeys.get(i), "assignment" + Integer.toString(i % 5));
112 }
113
114 return aMap;
115 }
116
117
118
119
120
121
122
123
124
125
126 public static String getTimeBlocksJson(List<TimeBlock> timeBlocks) {
127
128 if (timeBlocks == null || timeBlocks.size() == 0) {
129 return "";
130 }
131
132 List<Map<String, Object>> timeBlockList = new LinkedList<Map<String, Object>>();
133 String timezone = TkServiceLocator.getTimezoneService().getUserTimezone();
134
135 for (TimeBlock timeBlock : timeBlocks) {
136 Map<String, Object> timeBlockMap = new LinkedHashMap<String, Object>();
137
138 WorkArea workArea = TkServiceLocator.getWorkAreaService().getWorkArea(timeBlock.getWorkArea(), new java.sql.Date(timeBlock.getEndTimestamp().getTime()));
139 String workAreaDesc = workArea.getDescription();
140
141
142 Boolean isAnyApprover = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isAnyApproverActive();
143 timeBlockMap.put("isApprover", isAnyApprover);
144 timeBlockMap.put("isSynchronousUser", timeBlock.getClockLogCreated());
145
146
147 timeBlockMap.put("canEditTb", TkServiceLocator.getPermissionsService().canEditTimeBlock(timeBlock));
148 timeBlockMap.put("canEditTBOvt", TkServiceLocator.getPermissionsService().canEditOvertimeEarnCode(timeBlock));
149 timeBlockMap.put("canAddTB", TkServiceLocator.getPermissionsService().canAddTimeBlock());
150
151 if (TkServiceLocator.getPermissionsService().canEditTimeBlockAllFields(timeBlock)) {
152 timeBlockMap.put("canEditTBAll", true);
153 timeBlockMap.put("canEditTBAssgOnly", false);
154 } else {
155 timeBlockMap.put("canEditTBAll", false);
156 timeBlockMap.put("canEditTBAssgOnly", true);
157 }
158
159
160 DateTime start = timeBlock.getBeginTimeDisplay();
161 DateTime end = timeBlock.getEndTimeDisplay();
162
163
164
165
166
167
168 if (timeBlock.isPushBackward()) {
169 start = start.minusDays(1);
170 end = end.minusDays(1);
171 }
172
173 timeBlockMap.put("documentId", timeBlock.getDocumentId());
174 timeBlockMap.put("title", workAreaDesc);
175 timeBlockMap.put("earnCode", timeBlock.getEarnCode());
176 timeBlockMap.put("earnCodeDesc", TkServiceLocator.getEarnCodeService().getEarnCode(timeBlock.getEarnCode(), TKUtils.getCurrentDate()).getDescription());
177
178
179 timeBlockMap.put("earnCodeType", timeBlock.getEarnCodeType());
180
181
182
183 timeBlockMap.put("start", start.toString(ISODateTimeFormat.dateTimeNoMillis()));
184 timeBlockMap.put("end", end.toString(ISODateTimeFormat.dateTimeNoMillis()));
185 timeBlockMap.put("startDate", start.toString(TkConstants.DT_BASIC_DATE_FORMAT));
186 timeBlockMap.put("endDate", end.toString(TkConstants.DT_BASIC_DATE_FORMAT));
187 timeBlockMap.put("startNoTz", start.toString(ISODateTimeFormat.dateHourMinuteSecond()));
188 timeBlockMap.put("endNoTz", end.toString(ISODateTimeFormat.dateHourMinuteSecond()));
189
190 timeBlockMap.put("startTimeHourMinute", start.toString(TkConstants.DT_BASIC_TIME_FORMAT));
191 timeBlockMap.put("endTimeHourMinute", end.toString(TkConstants.DT_BASIC_TIME_FORMAT));
192
193 timeBlockMap.put("startTime", start.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
194 timeBlockMap.put("endTime", end.toString(TkConstants.DT_MILITARY_TIME_FORMAT));
195 timeBlockMap.put("id", timeBlock.getTkTimeBlockId() == null ? null : timeBlock.getTkTimeBlockId().toString());
196 timeBlockMap.put("hours", timeBlock.getHours());
197 timeBlockMap.put("amount", timeBlock.getAmount());
198 timeBlockMap.put("timezone", timezone);
199 timeBlockMap.put("assignment", new AssignmentDescriptionKey(timeBlock.getJobNumber(), timeBlock.getWorkArea(), timeBlock.getTask()).toAssignmentKeyString());
200 timeBlockMap.put("tkTimeBlockId", timeBlock.getTkTimeBlockId() != null ? timeBlock.getTkTimeBlockId() : "");
201 timeBlockMap.put("lunchDeleted", timeBlock.isLunchDeleted());
202
203 List<Map<String, Object>> timeHourDetailList = new LinkedList<Map<String, Object>>();
204 for (TimeHourDetail timeHourDetail : timeBlock.getTimeHourDetails()) {
205 Map<String, Object> timeHourDetailMap = new LinkedHashMap<String, Object>();
206 timeHourDetailMap.put("earnCode", timeHourDetail.getEarnCode());
207 timeHourDetailMap.put("hours", timeHourDetail.getHours());
208 timeHourDetailMap.put("amount", timeHourDetail.getAmount());
209
210
211 if (StringUtils.equals(timeHourDetail.getEarnCode(), "LUN")) {
212 timeBlockMap.put("lunchDeduction", true);
213 }
214
215 timeHourDetailList.add(timeHourDetailMap);
216 }
217 timeBlockMap.put("timeHourDetails", JSONValue.toJSONString(timeHourDetailList));
218
219 timeBlockList.add(timeBlockMap);
220 }
221
222
223
224
225
226
227 return JSONValue.toJSONString(timeBlockList);
228 }
229
230 public static Map<String, String> getPayPeriodsMap(List<CalendarEntries> payPeriods) {
231
232 Map<String, String> pMap = Collections.synchronizedMap(new LinkedHashMap<String, String>());
233 if (payPeriods == null || payPeriods.isEmpty()) {
234 return pMap;
235 }
236 payPeriods.removeAll(Collections.singletonList(null));
237 Collections.sort(payPeriods);
238 Collections.reverse(payPeriods);
239 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
240 for (CalendarEntries pce : payPeriods) {
241 if(pce != null && pce.getHrCalendarEntriesId()!= null && pce.getBeginPeriodDate() != null && pce.getEndPeriodDate() != null) {
242
243
244 pMap.put(pce.getHrCalendarEntriesId(), sdf.format(pce.getBeginPeriodDate()) + " - " + sdf.format((DateUtils.addMilliseconds(pce.getEndPeriodDate(),-1))));
245 }
246 }
247
248 return pMap;
249 }
250
251
252 public static boolean getOnCurrentPeriodFlag(CalendarEntries pce) {
253 Date currentDate = TKUtils.getTimelessDate(null);
254 String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
255 CalendarEntries calendarEntry = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate);
256
257 if(pce != null && calendarEntry != null && calendarEntry.equals(pce)) {
258 return true;
259 }
260 return false;
261 }
262
263 public static String getUnitOfTimeForEarnCode(EarnCode earnCode) {
264 AccrualCategory acObj = null;
265 if(earnCode.getAccrualCategory() != null) {
266 acObj = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), TKUtils.getCurrentDate());
267 }
268 String unitTime = (acObj!= null ? acObj.getUnitOfTime() : earnCode.getRecordMethod()) ;
269 return unitTime;
270 }
271
272 }
273