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 = new ArrayList<String>();
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      		String leaveStart = tdaf.getStartDate();
97  			LeaveSummary ls = TkServiceLocator.getLeaveSummaryService().getLeaveSummary(TKContext.getTargetPrincipalId(), tdaf.getPayCalendarDates());
98  			LeaveBlock lb = null;
99  			if(StringUtils.isNotEmpty(tdaf.getLmLeaveBlockId())) {
100 				lb = TkServiceLocator.getLeaveBlockService().getLeaveBlock(tdaf.getLmLeaveBlockId());
101 			}
102 			errorMsgList.addAll(LeaveCalendarValidationUtil.validateAvailableLeaveBalanceForUsage(tdaf.getSelectedEarnCode(), 
103 					tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getLeaveAmount(), lb));
104 			//Validate leave block does not exceed max usage. Leave Calendar Validators at this point rely on a leave summary.
105 	        errorMsgList.addAll(LeaveCalendarValidationUtil.validateLeaveAccrualRuleMaxUsage(ls, tdaf.getSelectedEarnCode(),
106                     tdaf.getStartDate(), tdaf.getEndDate(), tdaf.getLeaveAmount(), lb));
107 		}
108 		return errorMsgList;
109     }
110     
111     
112     //this is an ajax call for the assignment maintenance page
113     public ActionForward getDepartmentForJobNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
114         KualiMaintenanceForm kualiForm = (KualiMaintenanceForm) form;
115 
116         String principalId = (String) request.getAttribute("principalId");
117         Long jobNumber = (Long) request.getAttribute("jobNumber");
118 
119         Job job = TkServiceLocator.getJobService().getJob(principalId, jobNumber, TKUtils.getCurrentDate());
120         kualiForm.setAnnotation(job.getDept());
121 
122         return mapping.findForward("ws");
123     }
124 
125     public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
126         TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
127         List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>();
128 
129         if (StringUtils.isNotBlank(tdaf.getSelectedAssignment())) {
130             List<Assignment> assignments = tdaf.getTimesheetDocument().getAssignments();
131             AssignmentDescriptionKey key = new AssignmentDescriptionKey(tdaf.getSelectedAssignment());
132             for (Assignment assignment : assignments) {
133                 if (assignment.getJobNumber().compareTo(key.getJobNumber()) == 0 &&
134                         assignment.getWorkArea().compareTo(key.getWorkArea()) == 0 &&
135                         assignment.getTask().compareTo(key.getTask()) == 0) {
136                     List<EarnCode> earnCodes = TkServiceLocator.getEarnCodeService().getEarnCodesForTime(assignment, tdaf.getTimesheetDocument().getAsOfDate());
137                     for (EarnCode earnCode : earnCodes) {
138                         Map<String, Object> earnCodeMap = new HashMap<String, Object>();
139                         earnCodeMap.put("assignment", assignment.getAssignmentKey());
140                         earnCodeMap.put("earnCode", earnCode.getEarnCode());
141                         earnCodeMap.put("desc", earnCode.getDescription());
142                         earnCodeMap.put("type", earnCode.getEarnCodeType());
143                         // for leave blocks
144                         earnCodeMap.put("leavePlan", earnCode.getLeavePlan());
145                         if(StringUtils.isNotEmpty(earnCode.getLeavePlan())) {
146                             earnCodeMap.put("fractionalTimeAllowed", earnCode.getFractionalTimeAllowed());
147                             earnCodeMap.put("unitOfTime", ActionFormUtils.getUnitOfTimeForEarnCode(earnCode));
148                         }
149                         earnCodeMap.put("eligibleForAccrual", earnCode.getEligibleForAccrual());
150                         earnCodeList.add(earnCodeMap);
151                     }
152                 }
153             }
154         }
155         LOG.info(tdaf.toString());
156         tdaf.setOutputString(JSONValue.toJSONString(earnCodeList));
157         return mapping.findForward("ws");
158     }
159 
160     public ActionForward getOvertimeEarnCodes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
161         TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
162         List<EarnCode> overtimeEarnCodes = TkServiceLocator.getEarnCodeService().getOvertimeEarnCodes(TKUtils.getCurrentDate());
163         List<Map<String, Object>> overtimeEarnCodeList = new LinkedList<Map<String, Object>>();
164 
165         for (EarnCode earnCode : overtimeEarnCodes) {
166             Map<String, Object> earnCodeMap = new HashMap<String, Object>();
167             earnCodeMap.put("earnCode", earnCode.getEarnCode());
168             earnCodeMap.put("desc", earnCode.getDescription());
169 
170             overtimeEarnCodeList.add(earnCodeMap);
171         }
172 
173         LOG.info(tdaf.toString());
174         tdaf.setOutputString(JSONValue.toJSONString(overtimeEarnCodeList));
175         return mapping.findForward("ws");
176     }
177 
178 }