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.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Map;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.commons.lang.StringUtils;
29 import org.apache.log4j.Logger;
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33 import org.json.simple.JSONArray;
34 import org.json.simple.JSONValue;
35 import org.kuali.hr.job.Job;
36 import org.kuali.hr.lm.leaveSummary.LeaveSummary;
37 import org.kuali.hr.lm.leaveblock.LeaveBlock;
38 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
39 import org.kuali.hr.time.assignment.Assignment;
40 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
41 import org.kuali.hr.time.calendar.CalendarEntries;
42 import org.kuali.hr.time.detail.validation.TimeDetailValidationUtil;
43 import org.kuali.hr.time.earncode.EarnCode;
44 import org.kuali.hr.time.paytype.PayType;
45 import org.kuali.hr.time.service.base.TkServiceLocator;
46 import org.kuali.hr.time.timesheet.TimesheetDocument;
47 import org.kuali.hr.time.timesheet.web.TimesheetAction;
48 import org.kuali.hr.time.util.TKContext;
49 import org.kuali.hr.time.util.TKUtils;
50 import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
51 import org.kuali.rice.krad.util.ObjectUtils;
52
53 public class TimeDetailWSAction extends TimesheetAction {
54
55 private static final Logger LOG = Logger.getLogger(TimeDetailWSAction.class);
56
57 @Override
58 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
59 return super.execute(mapping, form, request, response);
60 }
61
62
63
64
65
66
67
68
69
70
71
72
73 @SuppressWarnings("unchecked")
74 public ActionForward validateTimeEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
75 TimeDetailActionFormBase tdaf = (TimeDetailActionFormBase) form;
76 JSONArray errorMsgList = new JSONArray();
77 List<String> errors;
78
79 EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(tdaf.getSelectedEarnCode(), tdaf.getTimesheetDocument().getAsOfDate());
80 if(ec != null
81 && (ec.getLeavePlan() != null || ec.getEligibleForAccrual().equals("N"))) {
82 errors = this.validateLeaveEntry(tdaf);
83 } else {
84 errors = TimeDetailValidationUtil.validateTimeEntryDetails(tdaf);
85 }
86
87
88 errorMsgList.addAll(errors);
89
90 tdaf.setOutputString(JSONValue.toJSONString(errorMsgList));
91 return mapping.findForward("ws");
92 }
93
94 public List<String> validateLeaveEntry(TimeDetailActionFormBase tdaf) throws Exception {
95 List<String> errorMsgList = new ArrayList<String>();
96 CalendarEntries payCalendarEntry = tdaf.getPayCalendarDates();
97 if(ObjectUtils.isNotNull(payCalendarEntry)) {
98 LeaveSummary ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(TKContext.getTargetPrincipalId(), tdaf.getPayCalendarDates());
99 LeaveBlock lb = null;
100 if(StringUtils.isNotEmpty(tdaf.getLmLeaveBlockId())) {
101 lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock(tdaf.getLmLeaveBlockId());
102 }
103
104 BigDecimal leaveAmount = tdaf.getLeaveAmount();
105 if(leaveAmount == null) {
106 Long startTime = TKUtils.convertDateStringToTimestampWithoutZone(tdaf.getStartDate(), tdaf.getStartTime()).getTime();
107 Long endTime = TKUtils.convertDateStringToTimestampWithoutZone(tdaf.getEndDate(), tdaf.getEndTime()).getTime();
108 leaveAmount = TKUtils.getHoursBetween(startTime, endTime);
109 }
110
111
112 errorMsgList.addAll(LeaveCalendarValidationUtil.validateParametersForLeaveEntry(tdaf.getSelectedEarnCode(), tdaf.getPayCalendarDates(),
113 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getStartTime(), tdaf.getEndTime(), tdaf.getSelectedAssignment(), null, tdaf.getLmLeaveBlockId()));
114
115 errorMsgList.addAll(LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage(tdaf.getSelectedEarnCode(),
116 tdaf.getStartDate(), tdaf.getEndDate(), leaveAmount, lb));
117
118 errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, tdaf.getSelectedEarnCode(),
119 tdaf.getStartDate(), tdaf.getEndDate(), leaveAmount, lb));
120 }
121 return errorMsgList;
122 }
123
124
125
126 public ActionForward getDepartmentForJobNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
127 KualiMaintenanceForm kualiForm = (KualiMaintenanceForm) form;
128
129 String principalId = (String) request.getAttribute("principalId");
130 Long jobNumber = (Long) request.getAttribute("jobNumber");
131
132 Job job = TkServiceLocator.getJobService().getJob(principalId, jobNumber, TKUtils.getCurrentDate());
133 kualiForm.setAnnotation(job.getDept());
134
135 return mapping.findForward("ws");
136 }
137
138 public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
139 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
140 List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>();
141
142 if (StringUtils.isNotBlank(tdaf.getSelectedAssignment())) {
143 List<Assignment> assignments = tdaf.getTimesheetDocument().getAssignments();
144 AssignmentDescriptionKey key = new AssignmentDescriptionKey(tdaf.getSelectedAssignment());
145 Map<String, EarnCode> regEarnCodes = getRegularEarnCodes(tdaf.getTimesheetDocument());
146 for (Assignment assignment : assignments) {
147 if (assignment.getJobNumber().equals(key.getJobNumber()) &&
148 assignment.getWorkArea().equals(key.getWorkArea()) &&
149 assignment.getTask().equals(key.getTask())) {
150 List<EarnCode> earnCodes = new ArrayList<EarnCode>();
151 if (tdaf.isTimeBlockReadOnly()) {
152 if (regEarnCodes.containsKey(assignment.getAssignmentKey())) {
153 earnCodes.add(regEarnCodes.get(assignment.getAssignmentKey()));
154 }
155 } else {
156 earnCodes.addAll(TkServiceLocator.getEarnCodeService()
157 .getEarnCodesForTime(assignment, tdaf.getTimesheetDocument().getAsOfDate(), tdaf.isTimeBlockReadOnly()));
158 }
159 for (EarnCode earnCode : earnCodes) {
160 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
161 earnCodeMap.put("assignment", assignment.getAssignmentKey());
162 earnCodeMap.put("earnCode", earnCode.getEarnCode());
163 earnCodeMap.put("desc", earnCode.getDescription());
164 earnCodeMap.put("type", earnCode.getEarnCodeType());
165
166 earnCodeMap.put("leavePlan", earnCode.getLeavePlan());
167 if(StringUtils.isNotEmpty(earnCode.getLeavePlan())) {
168 earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed());
169 earnCodeMap.put("unitOfTime", ActionFormUtils.getUnitOfTimeForEarnCode(earnCode));
170 }
171 earnCodeMap.put("eligibleForAccrual", earnCode.getEligibleForAccrual());
172 earnCodeList.add(earnCodeMap);
173 }
174 }
175 }
176 }
177 LOG.info(tdaf.toString());
178 tdaf.setOutputString(JSONValue.toJSONString(earnCodeList));
179 return mapping.findForward("ws");
180 }
181
182 private Map<String, EarnCode> getRegularEarnCodes(TimesheetDocument td) {
183 Map<String, EarnCode> regEarnCodes = new HashMap<String, EarnCode>();
184 if (td != null) {
185 for (Assignment a : td.getAssignments()) {
186 if (a.getJob() != null
187 && a.getJob().getPayTypeObj() != null) {
188 PayType payType = a.getJob().getPayTypeObj();
189 EarnCode ec = payType.getRegEarnCodeObj();
190 if (ec == null
191 && StringUtils.isNotEmpty(payType.getRegEarnCode())) {
192 ec = TkServiceLocator.getEarnCodeService().getEarnCode(payType.getRegEarnCode(), payType.getEffectiveDate());
193 }
194 regEarnCodes.put(a.getAssignmentKey(), ec);
195 }
196 }
197 }
198 return regEarnCodes;
199 }
200
201 private List<Map<String, Object>> getAssignmentsForRegEarnCode(TimesheetDocument td, String earnCode) {
202 List<Map<String, Object>> assignments = new ArrayList<Map<String, Object>>();
203 if (td != null) {
204 for (Assignment a : td.getAssignments()) {
205 Map<String, Object> assignment = new HashMap<String, Object>();
206 if (earnCode.equals(a.getJob().getPayTypeObj().getRegEarnCode())) {
207 assignment.put("assignment", a.getAssignmentKey());
208 assignments.add(assignment);
209 }
210 }
211 }
212 return assignments;
213 }
214
215 public ActionForward getValidAssignments(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
216 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
217
218 String earnCode = tdaf.getSelectedEarnCode();
219
220 List<Map<String, Object>> assignments = new ArrayList<Map<String, Object>>();
221 if (tdaf.getTimesheetDocument() != null && StringUtils.isNotEmpty(earnCode)) {
222 assignments = getAssignmentsForRegEarnCode(tdaf.getTimesheetDocument(), earnCode);
223 }
224 tdaf.setOutputString(JSONValue.toJSONString(assignments));
225 return mapping.findForward("ws");
226 }
227
228 public ActionForward getOvertimeEarnCodes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
229 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
230 List<EarnCode> overtimeEarnCodes = TkServiceLocator.getEarnCodeService().getOvertimeEarnCodes(TKUtils.getCurrentDate());
231 List<Map<String, Object>> overtimeEarnCodeList = new LinkedList<Map<String, Object>>();
232
233 for (EarnCode earnCode : overtimeEarnCodes) {
234 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
235 earnCodeMap.put("earnCode", earnCode.getEarnCode());
236 earnCodeMap.put("desc", earnCode.getDescription());
237
238 overtimeEarnCodeList.add(earnCodeMap);
239 }
240
241 LOG.info(tdaf.toString());
242 tdaf.setOutputString(JSONValue.toJSONString(overtimeEarnCodeList));
243 return mapping.findForward("ws");
244 }
245
246 }