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.time.detail.web; 017 018 import java.util.ArrayList; 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.json.simple.JSONArray; 033 import org.json.simple.JSONValue; 034 import org.kuali.hr.job.Job; 035 import org.kuali.hr.lm.leaveSummary.LeaveSummary; 036 import org.kuali.hr.lm.leaveblock.LeaveBlock; 037 import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil; 038 import org.kuali.hr.time.assignment.Assignment; 039 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 040 import org.kuali.hr.time.calendar.CalendarEntries; 041 import org.kuali.hr.time.detail.validation.TimeDetailValidationUtil; 042 import org.kuali.hr.time.earncode.EarnCode; 043 import org.kuali.hr.time.paytype.PayType; 044 import org.kuali.hr.time.service.base.TkServiceLocator; 045 import org.kuali.hr.time.timesheet.TimesheetDocument; 046 import org.kuali.hr.time.timesheet.web.TimesheetAction; 047 import org.kuali.hr.time.util.TKContext; 048 import org.kuali.hr.time.util.TKUtils; 049 import org.kuali.hr.time.util.TkConstants; 050 import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm; 051 import org.kuali.rice.krad.util.ObjectUtils; 052 053 public class TimeDetailWSAction extends TimesheetAction { 054 055 private static final Logger LOG = Logger.getLogger(TimeDetailWSAction.class); 056 057 @Override 058 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 059 return super.execute(mapping, form, request, response); 060 } 061 062 /** 063 * This is an ajax call triggered after a user submits the time entry form. 064 * If there is any error, it will return error messages as a json object. 065 * 066 * @param mapping 067 * @param form 068 * @param request 069 * @param response 070 * @return jsonObj 071 * @throws Exception 072 */ 073 @SuppressWarnings("unchecked") 074 public ActionForward validateTimeEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 075 TimeDetailActionFormBase tdaf = (TimeDetailActionFormBase) form; 076 JSONArray errorMsgList = new JSONArray(); 077 List<String> errors; 078 079 EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(tdaf.getSelectedEarnCode(), tdaf.getTimesheetDocument().getAsOfDate()); 080 if(ec != null 081 && (ec.getLeavePlan() != null || ec.getEligibleForAccrual().equals("N"))) { // leave blocks changes 082 errors = this.validateLeaveEntry(tdaf); 083 } else { // time blocks changes 084 errors = TimeDetailValidationUtil.validateTimeEntryDetails(tdaf); 085 } 086 087 // List<String> errors = TimeDetailValidationService.validateTimeEntryDetails(tdaf); 088 errorMsgList.addAll(errors); 089 090 tdaf.setOutputString(JSONValue.toJSONString(errorMsgList)); 091 return mapping.findForward("ws"); 092 } 093 094 public List<String> validateLeaveEntry(TimeDetailActionFormBase tdaf) throws Exception { 095 List<String> errorMsgList = new ArrayList<String>(); 096 CalendarEntries payCalendarEntry = tdaf.getPayCalendarDates(); 097 if(ObjectUtils.isNotNull(payCalendarEntry)) { 098 LeaveSummary ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(TKContext.getTargetPrincipalId(), tdaf.getPayCalendarDates()); 099 LeaveBlock lb = null; 100 if(StringUtils.isNotEmpty(tdaf.getLmLeaveBlockId())) { 101 lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock(tdaf.getLmLeaveBlockId()); 102 } 103 104 // Validate LeaveBlock timings and all that 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 //Validate leave block does not exceed max usage. Leave Calendar Validators at this point rely on a leave summary. 111 errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, tdaf.getSelectedEarnCode(), 112 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getLeaveAmount(), lb)); 113 } 114 return errorMsgList; 115 } 116 117 118 //this is an ajax call for the assignment maintenance page 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 // for leave blocks 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 }