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.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.log4j.Logger;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.json.simple.JSONArray;
33 import org.json.simple.JSONValue;
34 import org.kuali.hr.job.Job;
35 import org.kuali.hr.lm.leaveSummary.LeaveSummary;
36 import org.kuali.hr.lm.leaveblock.LeaveBlock;
37 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
38 import org.kuali.hr.time.assignment.Assignment;
39 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
40 import org.kuali.hr.time.calendar.CalendarEntries;
41 import org.kuali.hr.time.detail.validation.TimeDetailValidationUtil;
42 import org.kuali.hr.time.earncode.EarnCode;
43 import org.kuali.hr.time.paytype.PayType;
44 import org.kuali.hr.time.service.base.TkServiceLocator;
45 import org.kuali.hr.time.timesheet.TimesheetDocument;
46 import org.kuali.hr.time.timesheet.web.TimesheetAction;
47 import org.kuali.hr.time.util.TKContext;
48 import org.kuali.hr.time.util.TKUtils;
49 import org.kuali.hr.time.util.TkConstants;
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
105 errorMsgList.addAll(LeaveCalendarValidationUtil.validateParametersForLeaveEntry(tdaf.getSelectedEarnCode(), tdaf.getPayCalendarDates(),
106 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getStartTime(), tdaf.getEndTime(), tdaf.getSelectedAssignment(), null, tdaf.getLmLeaveBlockId()));
107
108 errorMsgList.addAll(LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage(tdaf.getSelectedEarnCode(),
109 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getLeaveAmount(), lb));
110
111 errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, tdaf.getSelectedEarnCode(),
112 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getLeaveAmount(), lb));
113 }
114 return errorMsgList;
115 }
116
117
118
119 public ActionForward getDepartmentForJobNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
120 KualiMaintenanceForm kualiForm = (KualiMaintenanceForm) form;
121
122 String principalId = (String) request.getAttribute("principalId");
123 Long jobNumber = (Long) request.getAttribute("jobNumber");
124
125 Job job = TkServiceLocator.getJobService().getJob(principalId, jobNumber, TKUtils.getCurrentDate());
126 kualiForm.setAnnotation(job.getDept());
127
128 return mapping.findForward("ws");
129 }
130
131 public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
132 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
133 List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>();
134
135 if (StringUtils.isNotBlank(tdaf.getSelectedAssignment())) {
136 List<Assignment> assignments = tdaf.getTimesheetDocument().getAssignments();
137 AssignmentDescriptionKey key = new AssignmentDescriptionKey(tdaf.getSelectedAssignment());
138 Map<String, EarnCode> regEarnCodes = getRegularEarnCodes(tdaf.getTimesheetDocument());
139 for (Assignment assignment : assignments) {
140 if (assignment.getJobNumber().equals(key.getJobNumber()) &&
141 assignment.getWorkArea().equals(key.getWorkArea()) &&
142 assignment.getTask().equals(key.getTask())) {
143 List<EarnCode> earnCodes = new ArrayList<EarnCode>();
144 if (tdaf.isTimeBlockReadOnly()) {
145 if (regEarnCodes.containsKey(assignment.getAssignmentKey())) {
146 earnCodes.add(regEarnCodes.get(assignment.getAssignmentKey()));
147 }
148 } else {
149 earnCodes.addAll(TkServiceLocator.getEarnCodeService()
150 .getEarnCodesForTime(assignment, tdaf.getTimesheetDocument().getAsOfDate(), tdaf.isTimeBlockReadOnly()));
151 }
152 for (EarnCode earnCode : earnCodes) {
153 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
154 earnCodeMap.put("assignment", assignment.getAssignmentKey());
155 earnCodeMap.put("earnCode", earnCode.getEarnCode());
156 earnCodeMap.put("desc", earnCode.getDescription());
157 earnCodeMap.put("type", earnCode.getEarnCodeType());
158
159 earnCodeMap.put("leavePlan", earnCode.getLeavePlan());
160 if(StringUtils.isNotEmpty(earnCode.getLeavePlan())) {
161 earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed());
162 earnCodeMap.put("unitOfTime", ActionFormUtils.getUnitOfTimeForEarnCode(earnCode));
163 }
164 earnCodeMap.put("eligibleForAccrual", earnCode.getEligibleForAccrual());
165 earnCodeList.add(earnCodeMap);
166 }
167 }
168 }
169 }
170 LOG.info(tdaf.toString());
171 tdaf.setOutputString(JSONValue.toJSONString(earnCodeList));
172 return mapping.findForward("ws");
173 }
174
175 private Map<String, EarnCode> getRegularEarnCodes(TimesheetDocument td) {
176 Map<String, EarnCode> regEarnCodes = new HashMap<String, EarnCode>();
177 if (td != null) {
178 for (Assignment a : td.getAssignments()) {
179 if (a.getJob() != null
180 && a.getJob().getPayTypeObj() != null) {
181 PayType payType = a.getJob().getPayTypeObj();
182 EarnCode ec = payType.getRegEarnCodeObj();
183 if (ec == null
184 && StringUtils.isNotEmpty(payType.getRegEarnCode())) {
185 ec = TkServiceLocator.getEarnCodeService().getEarnCode(payType.getRegEarnCode(), payType.getEffectiveDate());
186 }
187 regEarnCodes.put(a.getAssignmentKey(), ec);
188 }
189 }
190 }
191 return regEarnCodes;
192 }
193
194 private List<Map<String, Object>> getAssignmentsForRegEarnCode(TimesheetDocument td, String earnCode) {
195 List<Map<String, Object>> assignments = new ArrayList<Map<String, Object>>();
196 if (td != null) {
197 for (Assignment a : td.getAssignments()) {
198 Map<String, Object> assignment = new HashMap<String, Object>();
199 if (earnCode.equals(a.getJob().getPayTypeObj().getRegEarnCode())) {
200 assignment.put("assignment", a.getAssignmentKey());
201 assignments.add(assignment);
202 }
203 }
204 }
205 return assignments;
206 }
207
208 public ActionForward getValidAssignments(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
209 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
210
211 String earnCode = tdaf.getSelectedEarnCode();
212
213 List<Map<String, Object>> assignments = new ArrayList<Map<String, Object>>();
214 if (tdaf.getTimesheetDocument() != null && StringUtils.isNotEmpty(earnCode)) {
215 assignments = getAssignmentsForRegEarnCode(tdaf.getTimesheetDocument(), earnCode);
216 }
217 tdaf.setOutputString(JSONValue.toJSONString(assignments));
218 return mapping.findForward("ws");
219 }
220
221 public ActionForward getOvertimeEarnCodes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
222 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
223 List<EarnCode> overtimeEarnCodes = TkServiceLocator.getEarnCodeService().getOvertimeEarnCodes(TKUtils.getCurrentDate());
224 List<Map<String, Object>> overtimeEarnCodeList = new LinkedList<Map<String, Object>>();
225
226 for (EarnCode earnCode : overtimeEarnCodes) {
227 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
228 earnCodeMap.put("earnCode", earnCode.getEarnCode());
229 earnCodeMap.put("desc", earnCode.getDescription());
230
231 overtimeEarnCodeList.add(earnCodeMap);
232 }
233
234 LOG.info(tdaf.toString());
235 tdaf.setOutputString(JSONValue.toJSONString(overtimeEarnCodeList));
236 return mapping.findForward("ws");
237 }
238
239 }