1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.hr.lm.leave.web;
17
18 import java.sql.Date;
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.joda.time.DateTime;
33 import org.json.simple.JSONArray;
34 import org.json.simple.JSONValue;
35 import org.kuali.hr.lm.accrual.AccrualCategory;
36 import org.kuali.hr.lm.leaveSummary.LeaveSummary;
37 import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument;
38 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
39 import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarForm;
40 import org.kuali.hr.time.assignment.Assignment;
41 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
42 import org.kuali.hr.time.base.web.TkAction;
43 import org.kuali.hr.time.calendar.CalendarEntries;
44 import org.kuali.hr.time.detail.web.ActionFormUtils;
45 import org.kuali.hr.time.earncode.EarnCode;
46 import org.kuali.hr.time.service.base.TkServiceLocator;
47 import org.kuali.hr.time.util.TKContext;
48 import org.kuali.hr.time.util.TKUser;
49 import org.kuali.hr.time.util.TKUtils;
50
51 public class LeaveCalendarWSAction extends TkAction {
52
53 private static final Logger LOG = Logger.getLogger(LeaveCalendarWSAction.class);
54
55 @Override
56 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
57
58 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form;
59
60 String documentId = lcf.getDocumentId();
61
62 String calendarEntryId = StringUtils.isNotBlank(request.getParameter("selectedPP")) ? request.getParameter("selectedPP") : lcf.getCalEntryId();
63
64
65
66 String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
67 CalendarEntries calendarEntry = null;
68
69 LeaveCalendarDocument lcd = null;
70
71
72
73
74 if (StringUtils.isNotBlank(documentId)) {
75 lcd = TkServiceLocator.getLeaveCalendarService()
76 .getLeaveCalendarDocument(documentId);
77 calendarEntry = lcd.getCalendarEntry();
78 } else if (StringUtils.isNotBlank(calendarEntryId)) {
79
80 calendarEntry = TkServiceLocator.getCalendarEntriesService()
81 .getCalendarEntries(calendarEntryId);
82 lcd = TkServiceLocator.getLeaveCalendarService()
83 .getLeaveCalendarDocument(viewPrincipal, calendarEntry);
84 } else {
85
86 Date currentDate = TKUtils.getTimelessDate(null);
87 calendarEntry = TkServiceLocator.getCalendarService()
88 .getCurrentCalendarDatesForLeaveCalendar(viewPrincipal, currentDate);
89 lcd = TkServiceLocator.getLeaveCalendarService()
90 .openLeaveCalendarDocument(viewPrincipal, calendarEntry);
91 }
92
93 lcf.setCalendarEntry(calendarEntry);
94 lcf.setAssignmentDescriptions(TkServiceLocator.getAssignmentService().getAssignmentDescriptions(lcd));
95
96 if (lcd != null) {
97 setupDocumentOnFormContext(lcf, lcd);
98 } else {
99 LOG.error("Null leave calendar document in LeaveCalendarAction.");
100 }
101
102 ActionForward forward = super.execute(mapping, form, request, response);
103 if (forward.getRedirect()) {
104 return forward;
105 }
106
107 return forward;
108 }
109
110
111 public ActionForward getEarnCodeInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
112 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form;
113 LOG.info(lcf.toString());
114 EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCodeById(lcf.getSelectedEarnCode());
115 String unitTime = ActionFormUtils.getUnitOfTimeForEarnCode(earnCode);
116 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
117 earnCodeMap.put("unitOfTime", unitTime);
118 earnCodeMap.put("defaultAmountofTime", earnCode.getDefaultAmountofTime());
119 earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed());
120 lcf.setOutputString(JSONValue.toJSONString(earnCodeMap));
121 return mapping.findForward("ws");
122 }
123
124 public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
125
126 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form;
127 CalendarEntries ce = new CalendarEntries();
128
129 if(request.getParameter("selectedPayPeriod") != null) {
130 lcf.setSelectedPayPeriod(request.getParameter("selectedPayPeriod"));
131 ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(request.getParameter("selectedPayPeriod"));
132 lcf.setCalendarEntry(ce);
133 }
134 lcf.setPrincipalId(TKUser.getCurrentTargetPerson().getPrincipalId());
135 boolean isPlanningCal = TkServiceLocator.getLeaveCalendarService().isLeavePlanningCalendar(lcf.getPrincipalId(), lcf.getCalendarEntry().getBeginPeriodDateTime(), lcf.getCalendarEntry().getEndPeriodDateTime());
136 lcf.setLeavePlanningCalendar(isPlanningCal);
137
138 List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>();
139
140 if (StringUtils.isNotBlank(lcf.getSelectedAssignment())) {
141 List<Assignment> assignments = lcf.getLeaveCalendarDocument().getAssignments();
142 AssignmentDescriptionKey key = new AssignmentDescriptionKey(lcf.getSelectedAssignment());
143 for (Assignment assignment : assignments) {
144 if (assignment.getJobNumber().compareTo(key.getJobNumber()) == 0 &&
145 assignment.getWorkArea().compareTo(key.getWorkArea()) == 0 &&
146 assignment.getTask().compareTo(key.getTask()) == 0) {
147 List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodesForLeave(assignment, new java.sql.Date(TKUtils.convertDateStringToTimestamp(lcf.getStartDate()).getTime()), lcf.isLeavePlanningCalendar());
148 for (EarnCode earnCode : earnCodes) {
149 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
150 earnCodeMap.put("assignment", assignment.getAssignmentKey());
151 earnCodeMap.put("earnCode", earnCode.getEarnCode());
152 earnCodeMap.put("desc", earnCode.getDescription());
153 earnCodeMap.put("type", earnCode.getEarnCodeType());
154 earnCodeMap.put("earnCodeId", earnCode.getHrEarnCodeId());
155 AccrualCategory acObj = null;
156 if(earnCode.getAccrualCategory() != null) {
157 acObj = TkServiceLocator.getAccrualCategoryService().getAccrualCategory(earnCode.getAccrualCategory(), TKUtils.getCurrentDate());
158 }
159 String unitTime = (acObj!= null ? acObj.getUnitOfTime() : earnCode.getRecordMethod()) ;
160 earnCodeMap.put("unitOfTime", unitTime);
161 earnCodeMap.put("defaultAmountofTime", earnCode.getDefaultAmountofTime());
162 earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed());
163 earnCodeList.add(earnCodeMap);
164 }
165 }
166 }
167 }
168
169 lcf.setOutputString(JSONValue.toJSONString(earnCodeList));
170 return mapping.findForward("ws");
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184 @SuppressWarnings("unchecked")
185 public ActionForward validateLeaveEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
186 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form;
187 JSONArray errorMsgList = new JSONArray();
188
189 if(lcf.getLeaveSummary() == null && lcf.getCalendarEntry() != null) {
190 LeaveSummary ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(TKContext.getTargetPrincipalId(), lcf.getCalendarEntry());
191 lcf.setLeaveSummary(ls);
192 }
193 errorMsgList.addAll(LeaveCalendarValidationUtil.validateAvailableLeaveBalance(lcf));
194
195 errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(lcf));
196
197
198 errorMsgList.addAll(LeaveCalendarValidationUtil.validateSpanningWeeks(lcf));
199
200 lcf.setOutputString(JSONValue.toJSONString(errorMsgList));
201
202 return mapping.findForward("ws");
203 }
204
205 protected void setupDocumentOnFormContext(LeaveCalendarForm leaveForm,
206 LeaveCalendarDocument lcd) {
207 leaveForm.setLeaveCalendarDocument(lcd);
208 }
209
210 }