View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.detail.web;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.LinkedList;
21  import java.util.List;
22  import java.util.Map;
23  
24  import javax.servlet.http.HttpServletRequest;
25  import javax.servlet.http.HttpServletResponse;
26  
27  import org.apache.commons.lang.StringUtils;
28  import org.apache.log4j.Logger;
29  import org.apache.struts.action.ActionForm;
30  import org.apache.struts.action.ActionForward;
31  import org.apache.struts.action.ActionMapping;
32  import org.json.simple.JSONArray;
33  import org.json.simple.JSONValue;
34  import org.kuali.hr.job.Job;
35  import org.kuali.hr.lm.leaveSummary.LeaveSummary;
36  import org.kuali.hr.lm.leaveblock.LeaveBlock;
37  import org.kuali.hr.lm.leavecalendar.validation.LeaveCalendarValidationUtil;
38  import org.kuali.hr.time.assignment.Assignment;
39  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
40  import org.kuali.hr.time.calendar.CalendarEntries;
41  import org.kuali.hr.time.detail.validation.TimeDetailValidationUtil;
42  import org.kuali.hr.time.earncode.EarnCode;
43  import org.kuali.hr.time.service.base.TkServiceLocator;
44  import org.kuali.hr.time.timesheet.web.TimesheetAction;
45  import org.kuali.hr.time.util.TKContext;
46  import org.kuali.hr.time.util.TKUtils;
47  import org.kuali.hr.time.util.TkConstants;
48  import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
49  import org.kuali.rice.krad.util.ObjectUtils;
50  
51  public class TimeDetailWSAction extends TimesheetAction {
52  
53      private static final Logger LOG = Logger.getLogger(TimeDetailWSAction.class);
54  
55      @Override
56      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
57          return super.execute(mapping, form, request, response);
58      }
59  
60      /**
61       * This is an ajax call triggered after a user submits the time entry form.
62       * If there is any error, it will return error messages as a json object.
63       *
64       * @param mapping
65       * @param form
66       * @param request
67       * @param response
68       * @return jsonObj
69       * @throws Exception
70       */
71      @SuppressWarnings("unchecked")
72      public ActionForward validateTimeEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
73          TimeDetailActionFormBase tdaf = (TimeDetailActionFormBase) form;
74          JSONArray errorMsgList = new JSONArray();
75          List<String> errors;
76          
77      	EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(tdaf.getSelectedEarnCode(), tdaf.getTimesheetDocument().getAsOfDate());
78      	if(ec != null 
79      			&& (ec.getLeavePlan() != null || ec.getEligibleForAccrual().equals("N"))) {	// leave blocks changes
80      		errors = this.validateLeaveEntry(tdaf);
81      	} else {	// time blocks changes
82      		errors = TimeDetailValidationUtil.validateTimeEntryDetails(tdaf);
83      	}
84          
85  //        List<String> errors = TimeDetailValidationService.validateTimeEntryDetails(tdaf);
86          errorMsgList.addAll(errors);
87  
88          tdaf.setOutputString(JSONValue.toJSONString(errorMsgList));
89          return mapping.findForward("ws");
90      }
91  
92      public List<String> validateLeaveEntry(TimeDetailActionFormBase tdaf) throws Exception {
93      	List<String> errorMsgList = new ArrayList<String>();
94      	CalendarEntries payCalendarEntry = tdaf.getPayCalendarDates();
95      	if(ObjectUtils.isNotNull(payCalendarEntry)) {
96  			LeaveSummary ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(TKContext.getTargetPrincipalId(), tdaf.getPayCalendarDates());
97  			LeaveBlock lb = null;
98  			if(StringUtils.isNotEmpty(tdaf.getLmLeaveBlockId())) {
99  				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 }