001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.lm.leave.web; 017 018 import java.sql.Date; 019 import java.util.HashMap; 020 import java.util.LinkedList; 021 import java.util.List; 022 import java.util.Map; 023 024 import javax.servlet.http.HttpServletRequest; 025 import javax.servlet.http.HttpServletResponse; 026 027 import org.apache.commons.lang.StringUtils; 028 import org.apache.log4j.Logger; 029 import org.apache.struts.action.ActionForm; 030 import org.apache.struts.action.ActionForward; 031 import org.apache.struts.action.ActionMapping; 032 import org.joda.time.DateTime; 033 import org.json.simple.JSONArray; 034 import org.json.simple.JSONValue; 035 import org.kuali.hr.lm.LMConstants; 036 import org.kuali.hr.lm.accrual.AccrualCategory; 037 import org.kuali.hr.lm.leaveSummary.LeaveSummary; 038 import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument; 039 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil; 040 import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarForm; 041 import org.kuali.hr.time.assignment.Assignment; 042 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 043 import org.kuali.hr.time.base.web.TkAction; 044 import org.kuali.hr.time.calendar.CalendarEntries; 045 import org.kuali.hr.time.detail.web.ActionFormUtils; 046 import org.kuali.hr.time.earncode.EarnCode; 047 import org.kuali.hr.time.service.base.TkServiceLocator; 048 import org.kuali.hr.time.util.TKContext; 049 import org.kuali.hr.time.util.TKUser; 050 import org.kuali.hr.time.util.TKUtils; 051 052 public class LeaveCalendarWSAction extends TkAction { 053 054 private static final Logger LOG = Logger.getLogger(LeaveCalendarWSAction.class); 055 056 @Override 057 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 058 //return super.execute(mapping, form, request, response); 059 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form; 060 061 String documentId = lcf.getDocumentId(); 062 // if the reload was trigger by changing of the selectedPayPeriod, use the passed in parameter as the calendar entry id 063 String calendarEntryId = StringUtils.isNotBlank(request.getParameter("selectedPP")) ? request.getParameter("selectedPP") : lcf.getCalEntryId(); 064 065 // Here - viewPrincipal will be the principal of the user we intend to 066 // view, be it target user, backdoor or otherwise. 067 String viewPrincipal = TKUser.getCurrentTargetPersonId(); 068 CalendarEntries calendarEntry = null; 069 070 LeaveCalendarDocument lcd = null; 071 // By handling the prev/next in the execute method, we are saving one 072 // fetch/construction of a TimesheetDocument. If it were broken out into 073 // methods, we would first fetch the current document, and then fetch 074 // the next one instead of doing it in the single action. 075 if (StringUtils.isNotBlank(documentId)) { 076 lcd = TkServiceLocator.getLeaveCalendarService() 077 .getLeaveCalendarDocument(documentId); 078 calendarEntry = lcd.getCalendarEntry(); 079 } else if (StringUtils.isNotBlank(calendarEntryId)) { 080 // do further procedure 081 calendarEntry = TkServiceLocator.getCalendarEntriesService() 082 .getCalendarEntries(calendarEntryId); 083 lcd = TkServiceLocator.getLeaveCalendarService() 084 .getLeaveCalendarDocument(viewPrincipal, calendarEntry); 085 } else { 086 // Default to whatever is active for "today". 087 Date currentDate = TKUtils.getTimelessDate(null); 088 calendarEntry = TkServiceLocator.getCalendarService() 089 .getCurrentCalendarDatesForLeaveCalendar(viewPrincipal, currentDate); 090 lcd = TkServiceLocator.getLeaveCalendarService() 091 .openLeaveCalendarDocument(viewPrincipal, calendarEntry); 092 } 093 094 lcf.setCalendarEntry(calendarEntry); 095 lcf.setAssignmentDescriptions(TkServiceLocator.getAssignmentService().getAssignmentDescriptions(lcd)); 096 097 if (lcd != null) { 098 setupDocumentOnFormContext(lcf, lcd); 099 } else { 100 LOG.error("Null leave calendar document in LeaveCalendarAction."); 101 } 102 103 ActionForward forward = super.execute(mapping, form, request, response); 104 if (forward.getRedirect()) { 105 return forward; 106 } 107 108 return forward; 109 } 110 111 112 public ActionForward getEarnCodeInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 113 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form; 114 LOG.info(lcf.toString()); 115 EarnCode earnCode = TkServiceLocator.getEarnCodeService().getEarnCodeById(lcf.getSelectedEarnCode()); 116 String unitTime = ActionFormUtils.getUnitOfTimeForEarnCode(earnCode); 117 Map<String, Object> earnCodeMap = new HashMap<String, Object>(); 118 earnCodeMap.put("unitOfTime", unitTime); 119 earnCodeMap.put("defaultAmountofTime", earnCode.getDefaultAmountofTime()); 120 earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed()); 121 lcf.setOutputString(JSONValue.toJSONString(earnCodeMap)); 122 return mapping.findForward("ws"); 123 } 124 125 public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 126 //TODO: copied from TimeDetailWSAction. Need to reduce code duplication 127 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form; 128 129 130 if(request.getParameter("selectedPayPeriod") != null) { 131 lcf.setSelectedPayPeriod(request.getParameter("selectedPayPeriod")); 132 CalendarEntries ce = TkServiceLocator.getCalendarEntriesService().getCalendarEntries(request.getParameter("selectedPayPeriod")); 133 lcf.setCalendarEntry(ce); 134 } 135 lcf.setPrincipalId(TKUser.getCurrentTargetPersonId()); 136 boolean isPlanningCal = TkServiceLocator.getLeaveCalendarService().isLeavePlanningCalendar(lcf.getPrincipalId(), lcf.getCalendarEntry().getBeginPeriodDateTime(), lcf.getCalendarEntry().getEndPeriodDateTime()); 137 lcf.setLeavePlanningCalendar(isPlanningCal); 138 139 List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>(); 140 141 if (StringUtils.isNotBlank(lcf.getSelectedAssignment())) { 142 List<Assignment> assignments = lcf.getLeaveCalendarDocument().getAssignments(); 143 AssignmentDescriptionKey key = new AssignmentDescriptionKey(lcf.getSelectedAssignment()); 144 for (Assignment assignment : assignments) { 145 if (assignment.getJobNumber().compareTo(key.getJobNumber()) == 0 && 146 assignment.getWorkArea().compareTo(key.getWorkArea()) == 0 && 147 assignment.getTask().compareTo(key.getTask()) == 0) { 148 List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodesForLeave(assignment, new java.sql.Date(TKUtils.convertDateStringToTimestamp(lcf.getStartDate()).getTime()), lcf.isLeavePlanningCalendar()); 149 for (EarnCode earnCode : earnCodes) { 150 Map<String, Object> earnCodeMap = new HashMap<String, Object>(); 151 earnCodeMap.put("assignment", assignment.getAssignmentKey()); 152 earnCodeMap.put("earnCode", earnCode.getEarnCode()); 153 earnCodeMap.put("desc", earnCode.getDescription()); 154 earnCodeMap.put("type", earnCode.getEarnCodeType()); 155 earnCodeMap.put("earnCodeId", earnCode.getHrEarnCodeId()); 156 earnCodeMap.put("unitOfTime", earnCode.getRecordMethod()); 157 earnCodeMap.put("defaultAmountofTime", earnCode.getDefaultAmountofTime()); 158 earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed()); 159 earnCodeList.add(earnCodeMap); 160 } 161 } 162 } 163 } 164 //LOG.info(lcf.toString()); 165 lcf.setOutputString(JSONValue.toJSONString(earnCodeList)); 166 return mapping.findForward("ws"); 167 } 168 169 /** 170 * This is an ajax call triggered after a user submits the leave entry form. 171 * If there is any error, it will return error messages as a json object. 172 * 173 * @param mapping 174 * @param form 175 * @param request 176 * @param response 177 * @return jsonObj 178 * @throws Exception 179 */ 180 @SuppressWarnings("unchecked") 181 public ActionForward validateLeaveEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 182 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form; 183 JSONArray errorMsgList = new JSONArray(); 184 185 if(lcf.getLeaveSummary() == null && lcf.getCalendarEntry() != null) { 186 LeaveSummary ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(TKContext.getTargetPrincipalId(), lcf.getCalendarEntry()); 187 lcf.setLeaveSummary(ls); 188 } 189 190 errorMsgList.addAll(LeaveCalendarValidationUtil.validateParametersAccordingToSelectedEarnCodeRecordMethod(lcf)); 191 192 errorMsgList.addAll(LeaveCalendarValidationUtil.validateAvailableLeaveBalance(lcf)); 193 //KPME-1263 194 errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(lcf)); 195 196 //KPME-2010 197 errorMsgList.addAll(LeaveCalendarValidationUtil.validateSpanningWeeks(lcf)); 198 199 lcf.setOutputString(JSONValue.toJSONString(errorMsgList)); 200 201 return mapping.findForward("ws"); 202 } 203 204 protected void setupDocumentOnFormContext(LeaveCalendarForm leaveForm, 205 LeaveCalendarDocument lcd) { 206 leaveForm.setLeaveCalendarDocument(lcd); 207 } 208 209 }