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.service.base.TkServiceLocator;
044 import org.kuali.hr.time.timesheet.web.TimesheetAction;
045 import org.kuali.hr.time.util.TKContext;
046 import org.kuali.hr.time.util.TKUtils;
047 import org.kuali.hr.time.util.TkConstants;
048 import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
049 import org.kuali.rice.krad.util.ObjectUtils;
050
051 public class TimeDetailWSAction extends TimesheetAction {
052
053 private static final Logger LOG = Logger.getLogger(TimeDetailWSAction.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 }
059
060 /**
061 * This is an ajax call triggered after a user submits the time entry form.
062 * If there is any error, it will return error messages as a json object.
063 *
064 * @param mapping
065 * @param form
066 * @param request
067 * @param response
068 * @return jsonObj
069 * @throws Exception
070 */
071 @SuppressWarnings("unchecked")
072 public ActionForward validateTimeEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
073 TimeDetailActionFormBase tdaf = (TimeDetailActionFormBase) form;
074 JSONArray errorMsgList = new JSONArray();
075 List<String> errors;
076
077 EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(tdaf.getSelectedEarnCode(), tdaf.getTimesheetDocument().getAsOfDate());
078 if(ec != null
079 && (ec.getLeavePlan() != null || ec.getEligibleForAccrual().equals("N"))) { // leave blocks changes
080 errors = this.validateLeaveEntry(tdaf);
081 } else { // time blocks changes
082 errors = TimeDetailValidationUtil.validateTimeEntryDetails(tdaf);
083 }
084
085 // List<String> errors = TimeDetailValidationService.validateTimeEntryDetails(tdaf);
086 errorMsgList.addAll(errors);
087
088 tdaf.setOutputString(JSONValue.toJSONString(errorMsgList));
089 return mapping.findForward("ws");
090 }
091
092 public List<String> validateLeaveEntry(TimeDetailActionFormBase tdaf) throws Exception {
093 List<String> errorMsgList = new ArrayList<String>();
094 CalendarEntries payCalendarEntry = tdaf.getPayCalendarDates();
095 if(ObjectUtils.isNotNull(payCalendarEntry)) {
096 LeaveSummary ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(TKContext.getTargetPrincipalId(), tdaf.getPayCalendarDates());
097 LeaveBlock lb = null;
098 if(StringUtils.isNotEmpty(tdaf.getLmLeaveBlockId())) {
099 lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock(tdaf.getLmLeaveBlockId());
100 }
101
102 // Validate LeaveBlock timings and all that
103 errorMsgList.addAll(LeaveCalendarValidationUtil.validateParametersForLeaveEntry(tdaf.getSelectedEarnCode(), tdaf.getPayCalendarDates(),
104 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getStartTime(), tdaf.getEndTime(), tdaf.getSelectedAssignment(), null, tdaf.getLmLeaveBlockId()));
105
106 errorMsgList.addAll(LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage(tdaf.getSelectedEarnCode(),
107 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getLeaveAmount(), lb));
108 //Validate leave block does not exceed max usage. Leave Calendar Validators at this point rely on a leave summary.
109 errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, tdaf.getSelectedEarnCode(),
110 tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getLeaveAmount(), lb));
111 }
112 return errorMsgList;
113 }
114
115
116 //this is an ajax call for the assignment maintenance page
117 public ActionForward getDepartmentForJobNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
118 KualiMaintenanceForm kualiForm = (KualiMaintenanceForm) form;
119
120 String principalId = (String) request.getAttribute("principalId");
121 Long jobNumber = (Long) request.getAttribute("jobNumber");
122
123 Job job = TkServiceLocator.getJobService().getJob(principalId, jobNumber, TKUtils.getCurrentDate());
124 kualiForm.setAnnotation(job.getDept());
125
126 return mapping.findForward("ws");
127 }
128
129 public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
130 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
131 List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>();
132
133 if (StringUtils.isNotBlank(tdaf.getSelectedAssignment())) {
134 List<Assignment> assignments = tdaf.getTimesheetDocument().getAssignments();
135 AssignmentDescriptionKey key = new AssignmentDescriptionKey(tdaf.getSelectedAssignment());
136 for (Assignment assignment : assignments) {
137 if (assignment.getJobNumber().compareTo(key.getJobNumber()) == 0 &&
138 assignment.getWorkArea().compareTo(key.getWorkArea()) == 0 &&
139 assignment.getTask().compareTo(key.getTask()) == 0) {
140 List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodesForTime(assignment, tdaf.getTimesheetDocument().getAsOfDate());
141 for (EarnCode earnCode : earnCodes) {
142 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
143 earnCodeMap.put("assignment", assignment.getAssignmentKey());
144 earnCodeMap.put("earnCode", earnCode.getEarnCode());
145 earnCodeMap.put("desc", earnCode.getDescription());
146 earnCodeMap.put("type", earnCode.getEarnCodeType());
147 // for leave blocks
148 earnCodeMap.put("leavePlan", earnCode.getLeavePlan());
149 if(StringUtils.isNotEmpty(earnCode.getLeavePlan())) {
150 earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed());
151 earnCodeMap.put("unitOfTime", ActionFormUtils.getUnitOfTimeForEarnCode(earnCode));
152 }
153 earnCodeMap.put("eligibleForAccrual", earnCode.getEligibleForAccrual());
154 earnCodeList.add(earnCodeMap);
155 }
156 }
157 }
158 }
159 LOG.info(tdaf.toString());
160 tdaf.setOutputString(JSONValue.toJSONString(earnCodeList));
161 return mapping.findForward("ws");
162 }
163
164 public ActionForward getOvertimeEarnCodes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
165 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
166 List<EarnCode> overtimeEarnCodes = TkServiceLocator.getEarnCodeService().getOvertimeEarnCodes(TKUtils.getCurrentDate());
167 List<Map<String, Object>> overtimeEarnCodeList = new LinkedList<Map<String, Object>>();
168
169 for (EarnCode earnCode : overtimeEarnCodes) {
170 Map<String, Object> earnCodeMap = new HashMap<String, Object>();
171 earnCodeMap.put("earnCode", earnCode.getEarnCode());
172 earnCodeMap.put("desc", earnCode.getDescription());
173
174 overtimeEarnCodeList.add(earnCodeMap);
175 }
176
177 LOG.info(tdaf.toString());
178 tdaf.setOutputString(JSONValue.toJSONString(overtimeEarnCodeList));
179 return mapping.findForward("ws");
180 }
181
182 }