View Javadoc

1   /**
2    * Copyright 2004-2012 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                         // 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 }