001 /** 002 * Copyright 2004-2012 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.time.assignment.Assignment; 036 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 037 import org.kuali.hr.time.detail.validation.TimeDetailValidationService; 038 import org.kuali.hr.time.earncode.EarnCode; 039 import org.kuali.hr.time.service.base.TkServiceLocator; 040 import org.kuali.hr.time.timesheet.web.TimesheetAction; 041 import org.kuali.hr.time.util.TKContext; 042 import org.kuali.hr.time.util.TKUtils; 043 import org.kuali.hr.time.util.TkConstants; 044 import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm; 045 046 public class TimeDetailWSAction extends TimesheetAction { 047 048 private static final Logger LOG = Logger.getLogger(TimeDetailWSAction.class); 049 050 @Override 051 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 052 return super.execute(mapping, form, request, response); 053 } 054 055 /** 056 * This is an ajax call triggered after a user submits the time entry form. 057 * If there is any error, it will return error messages as a json object. 058 * 059 * @param mapping 060 * @param form 061 * @param request 062 * @param response 063 * @return jsonObj 064 * @throws Exception 065 */ 066 @SuppressWarnings("unchecked") 067 public ActionForward validateTimeEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 068 TimeDetailActionFormBase tdaf = (TimeDetailActionFormBase) form; 069 JSONArray errorMsgList = new JSONArray(); 070 071 List<String> errors = TimeDetailValidationService.validateTimeEntryDetails(tdaf); 072 errorMsgList.addAll(errors); 073 074 tdaf.setOutputString(JSONValue.toJSONString(errorMsgList)); 075 return mapping.findForward("ws"); 076 } 077 078 //this is an ajax call for the assignment maintenance page 079 public ActionForward getDepartmentForJobNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 080 KualiMaintenanceForm kualiForm = (KualiMaintenanceForm) form; 081 082 String principalId = (String) request.getAttribute("principalId"); 083 Long jobNumber = (Long) request.getAttribute("jobNumber"); 084 085 Job job = TkServiceLocator.getJobService().getJob(principalId, jobNumber, TKUtils.getCurrentDate()); 086 kualiForm.setAnnotation(job.getDept()); 087 088 return mapping.findForward("ws"); 089 } 090 091 public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 092 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form; 093 List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>(); 094 095 if (StringUtils.isNotBlank(tdaf.getSelectedAssignment())) { 096 List<Assignment> assignments = tdaf.getTimesheetDocument().getAssignments(); 097 AssignmentDescriptionKey key = new AssignmentDescriptionKey(tdaf.getSelectedAssignment()); 098 for (Assignment assignment : assignments) { 099 if (assignment.getJobNumber().compareTo(key.getJobNumber()) == 0 && 100 assignment.getWorkArea().compareTo(key.getWorkArea()) == 0 && 101 assignment.getTask().compareTo(key.getTask()) == 0) { 102 List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodesForTime(assignment, tdaf.getTimesheetDocument().getAsOfDate()); 103 for (EarnCode earnCode : earnCodes) { 104 // TODO: minimize / compress the crazy if logics below 105 if (earnCode.getEarnCode().equals(TkConstants.HOLIDAY_EARN_CODE) 106 && !(TKContext.getUser().isSystemAdmin() || TKContext.getUser().isTimesheetApprover())) { 107 continue; 108 } 109 if (!(assignment.getTimeCollectionRule().isClockUserFl() && 110 StringUtils.equals(assignment.getJob().getPayTypeObj().getRegEarnCode(), earnCode.getEarnCode()) 111 && StringUtils.equals(TKContext.getPrincipalId(), assignment.getPrincipalId()))) { 112 Map<String, Object> earnCodeMap = new HashMap<String, Object>(); 113 earnCodeMap.put("assignment", assignment.getAssignmentKey()); 114 earnCodeMap.put("earnCode", earnCode.getEarnCode()); 115 earnCodeMap.put("desc", earnCode.getDescription()); 116 earnCodeMap.put("type", earnCode.getEarnCodeType()); 117 118 earnCodeList.add(earnCodeMap); 119 } else if(!StringUtils.equals(assignment.getJob().getPayTypeObj().getRegEarnCode(), earnCode.getEarnCode())){ 120 Map<String, Object> earnCodeMap = new HashMap<String, Object>(); 121 earnCodeMap.put("assignment", assignment.getAssignmentKey()); 122 earnCodeMap.put("earnCode", earnCode.getEarnCode()); 123 earnCodeMap.put("desc", earnCode.getDescription()); 124 earnCodeMap.put("type", earnCode.getEarnCodeType()); 125 126 earnCodeList.add(earnCodeMap); 127 } 128 } 129 } 130 } 131 } 132 LOG.info(tdaf.toString()); 133 tdaf.setOutputString(JSONValue.toJSONString(earnCodeList)); 134 return mapping.findForward("ws"); 135 } 136 137 private boolean shouldAddEarnCode(Assignment assignment, EarnCode earnCode, boolean isTimeBlockReadOnly) { 138 139 Boolean shouldAddEarnCode; 140 141 shouldAddEarnCode = earnCode.getEarnCode().equals(TkConstants.HOLIDAY_EARN_CODE) 142 && !(TKContext.getUser().isSystemAdmin() || TKContext.getUser().isTimesheetApprover()); 143 144 shouldAddEarnCode |= !(assignment.getTimeCollectionRule().isClockUserFl() && 145 StringUtils.equals(assignment.getJob().getPayTypeObj().getRegEarnCode(), earnCode.getEarnCode()) && 146 StringUtils.equals(TKContext.getPrincipalId(), assignment.getPrincipalId())); 147 148 // If the timeblock is readonly (happens when a sync user is editing a sync timeblock) and the earn code is RGH, 149 // it should still add the RGH earn code. 150 shouldAddEarnCode |= isTimeBlockReadOnly; 151 152 return shouldAddEarnCode; 153 154 } 155 156 public ActionForward getOvertimeEarnCodes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 157 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form; 158 List<EarnCode> overtimeEarnCodes = TkServiceLocator.getEarnCodeService().getOvertimeEarnCodes(TKUtils.getCurrentDate()); 159 List<Map<String, Object>> overtimeEarnCodeList = new LinkedList<Map<String, Object>>(); 160 161 for (EarnCode earnCode : overtimeEarnCodes) { 162 Map<String, Object> earnCodeMap = new HashMap<String, Object>(); 163 earnCodeMap.put("earnCode", earnCode.getEarnCode()); 164 earnCodeMap.put("desc", earnCode.getDescription()); 165 166 overtimeEarnCodeList.add(earnCodeMap); 167 } 168 169 LOG.info(tdaf.toString()); 170 tdaf.setOutputString(JSONValue.toJSONString(overtimeEarnCodeList)); 171 return mapping.findForward("ws"); 172 } 173 174 }