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.accrual.AccrualCategory; 036 import org.kuali.hr.lm.leaveSummary.LeaveSummary; 037 import org.kuali.hr.lm.leavecalendar.LeaveCalendarDocument; 038 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil; 039 import org.kuali.hr.lm.leavecalendar.web.LeaveCalendarForm; 040 import org.kuali.hr.time.assignment.Assignment; 041 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 042 import org.kuali.hr.time.base.web.TkAction; 043 import org.kuali.hr.time.calendar.CalendarEntries; 044 import org.kuali.hr.time.detail.web.ActionFormUtils; 045 import org.kuali.hr.time.earncode.EarnCode; 046 import org.kuali.hr.time.service.base.TkServiceLocator; 047 import org.kuali.hr.time.util.TKContext; 048 import org.kuali.hr.time.util.TKUser; 049 import org.kuali.hr.time.util.TKUtils; 050 051 public class LeaveCalendarWSAction extends TkAction { 052 053 private static final Logger LOG = Logger.getLogger(LeaveCalendarWSAction.class); 054 055 @Override 056 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 057 //return super.execute(mapping, form, request, response); 058 LeaveCalendarWSForm lcf = (LeaveCalendarWSForm) form; 059 060 String documentId = lcf.getDocumentId(); 061 // if the reload was trigger by changing of the selectedPayPeriod, use the passed in parameter as the calendar entry id 062 String calendarEntryId = StringUtils.isNotBlank(request.getParameter("selectedPP")) ? request.getParameter("selectedPP") : lcf.getCalEntryId(); 063 064 // Here - viewPrincipal will be the principal of the user we intend to 065 // view, be it target user, backdoor or otherwise. 066 String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId(); 067 CalendarEntries calendarEntry = null; 068 069 LeaveCalendarDocument lcd = null; 070 // By handling the prev/next in the execute method, we are saving one 071 // fetch/construction of a TimesheetDocument. If it were broken out into 072 // methods, we would first fetch the current document, and then fetch 073 // the next one instead of doing it in the single action. 074 if (StringUtils.isNotBlank(documentId)) { 075 lcd = TkServiceLocator.getLeaveCalendarService() 076 .getLeaveCalendarDocument(documentId); 077 calendarEntry = lcd.getCalendarEntry(); 078 } else if (StringUtils.isNotBlank(calendarEntryId)) { 079 // do further procedure 080 calendarEntry = TkServiceLocator.getCalendarEntriesService() 081 .getCalendarEntries(calendarEntryId); 082 lcd = TkServiceLocator.getLeaveCalendarService() 083 .getLeaveCalendarDocument(viewPrincipal, calendarEntry); 084 } else { 085 // Default to whatever is active for "today". 086 Date currentDate = TKUtils.getTimelessDate(null); 087 calendarEntry = TkServiceLocator.getCalendarService() 088 .getCurrentCalendarDatesForLeaveCalendar(viewPrincipal, currentDate); 089 lcd = TkServiceLocator.getLeaveCalendarService() 090 .openLeaveCalendarDocument(viewPrincipal, calendarEntry); 091 } 092 093 lcf.setCalendarEntry(calendarEntry); 094 lcf.setAssignmentDescriptions(TkServiceLocator.getAssignmentService().getAssignmentDescriptions(lcd)); 095 096 if (lcd != null) { 097 setupDocumentOnFormContext(lcf, lcd); 098 } else { 099 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 //TODO: copied from TimeDetailWSAction. Need to reduce code duplication 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 //LOG.info(lcf.toString()); 169 lcf.setOutputString(JSONValue.toJSONString(earnCodeList)); 170 return mapping.findForward("ws"); 171 } 172 173 /** 174 * This is an ajax call triggered after a user submits the leave entry form. 175 * If there is any error, it will return error messages as a json object. 176 * 177 * @param mapping 178 * @param form 179 * @param request 180 * @param response 181 * @return jsonObj 182 * @throws Exception 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 //KPME-1263 195 errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(lcf)); 196 197 //KPME-2010 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 }