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.time.assignment.Assignment;
36  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
37  import org.kuali.hr.time.detail.validation.TimeDetailValidationService;
38  import org.kuali.hr.time.earncode.EarnCode;
39  import org.kuali.hr.time.service.base.TkServiceLocator;
40  import org.kuali.hr.time.timesheet.web.TimesheetAction;
41  import org.kuali.hr.time.util.TKContext;
42  import org.kuali.hr.time.util.TKUtils;
43  import org.kuali.hr.time.util.TkConstants;
44  import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
45  
46  public class TimeDetailWSAction extends TimesheetAction {
47  
48      private static final Logger LOG = Logger.getLogger(TimeDetailWSAction.class);
49  
50      @Override
51      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
52          return super.execute(mapping, form, request, response);
53      }
54  
55      /**
56       * This is an ajax call triggered after a user submits the time entry form.
57       * If there is any error, it will return error messages as a json object.
58       *
59       * @param mapping
60       * @param form
61       * @param request
62       * @param response
63       * @return jsonObj
64       * @throws Exception
65       */
66      @SuppressWarnings("unchecked")
67      public ActionForward validateTimeEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
68          TimeDetailActionFormBase tdaf = (TimeDetailActionFormBase) form;
69          JSONArray errorMsgList = new JSONArray();
70  
71          List<String> errors = TimeDetailValidationService.validateTimeEntryDetails(tdaf);
72          errorMsgList.addAll(errors);
73  
74          tdaf.setOutputString(JSONValue.toJSONString(errorMsgList));
75          return mapping.findForward("ws");
76      }
77  
78      //this is an ajax call for the assignment maintenance page
79      public ActionForward getDepartmentForJobNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
80          KualiMaintenanceForm kualiForm = (KualiMaintenanceForm) form;
81  
82          String principalId = (String) request.getAttribute("principalId");
83          Long jobNumber = (Long) request.getAttribute("jobNumber");
84  
85          Job job = TkServiceLocator.getJobService().getJob(principalId, jobNumber, TKUtils.getCurrentDate());
86          kualiForm.setAnnotation(job.getDept());
87  
88          return mapping.findForward("ws");
89      }
90  
91      public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
92          TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
93          List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>();
94  
95          if (StringUtils.isNotBlank(tdaf.getSelectedAssignment())) {
96              List<Assignment> assignments = tdaf.getTimesheetDocument().getAssignments();
97              AssignmentDescriptionKey key = new AssignmentDescriptionKey(tdaf.getSelectedAssignment());
98              for (Assignment assignment : assignments) {
99                  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                         Map<String, Object> earnCodeMap = new HashMap<String, Object>();
105                         earnCodeMap.put("assignment", assignment.getAssignmentKey());
106                         earnCodeMap.put("earnCode", earnCode.getEarnCode());
107                         earnCodeMap.put("desc", earnCode.getDescription());
108                         earnCodeMap.put("type", earnCode.getEarnCodeType());
109                         earnCodeList.add(earnCodeMap);
110                     }
111                 }
112             }
113         }
114         LOG.info(tdaf.toString());
115         tdaf.setOutputString(JSONValue.toJSONString(earnCodeList));
116         return mapping.findForward("ws");
117     }
118 
119     private boolean shouldAddEarnCode(Assignment assignment, EarnCode earnCode, boolean isTimeBlockReadOnly) {
120 
121         Boolean shouldAddEarnCode;
122 
123         shouldAddEarnCode = earnCode.getEarnCode().equals(TkConstants.HOLIDAY_EARN_CODE)
124                 && !(TKContext.getUser().isSystemAdmin() || TKContext.getUser().isTimesheetApprover());
125 
126         shouldAddEarnCode |= !(assignment.getTimeCollectionRule().isClockUserFl() &&
127                 StringUtils.equals(assignment.getJob().getPayTypeObj().getRegEarnCode(), earnCode.getEarnCode()) &&
128                 StringUtils.equals(TKContext.getPrincipalId(), assignment.getPrincipalId()));
129 
130         // If the timeblock is readonly (happens when a sync user is editing a sync timeblock) and the earn code is RGH,
131         // it should still add the RGH earn code.
132         shouldAddEarnCode |= isTimeBlockReadOnly;
133 
134         return shouldAddEarnCode;
135 
136     }
137 
138     public ActionForward getOvertimeEarnCodes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
139         TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
140         List<EarnCode> overtimeEarnCodes = TkServiceLocator.getEarnCodeService().getOvertimeEarnCodes(TKUtils.getCurrentDate());
141         List<Map<String, Object>> overtimeEarnCodeList = new LinkedList<Map<String, Object>>();
142 
143         for (EarnCode earnCode : overtimeEarnCodes) {
144             Map<String, Object> earnCodeMap = new HashMap<String, Object>();
145             earnCodeMap.put("earnCode", earnCode.getEarnCode());
146             earnCodeMap.put("desc", earnCode.getDescription());
147 
148             overtimeEarnCodeList.add(earnCodeMap);
149         }
150 
151         LOG.info(tdaf.toString());
152         tdaf.setOutputString(JSONValue.toJSONString(overtimeEarnCodeList));
153         return mapping.findForward("ws");
154     }
155 
156 }