001 /**
002 * Copyright 2004-2012 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.time.assignment.Assignment;
036 import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
037 import org.kuali.hr.time.detail.validation.TimeDetailValidationService;
038 import org.kuali.hr.time.earncode.EarnCode;
039 import org.kuali.hr.time.service.base.TkServiceLocator;
040 import org.kuali.hr.time.timesheet.web.TimesheetAction;
041 import org.kuali.hr.time.util.TKContext;
042 import org.kuali.hr.time.util.TKUtils;
043 import org.kuali.hr.time.util.TkConstants;
044 import org.kuali.rice.kns.web.struts.form.KualiMaintenanceForm;
045
046 public class TimeDetailWSAction extends TimesheetAction {
047
048 private static final Logger LOG = Logger.getLogger(TimeDetailWSAction.class);
049
050 @Override
051 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
052 return super.execute(mapping, form, request, response);
053 }
054
055 /**
056 * This is an ajax call triggered after a user submits the time entry form.
057 * If there is any error, it will return error messages as a json object.
058 *
059 * @param mapping
060 * @param form
061 * @param request
062 * @param response
063 * @return jsonObj
064 * @throws Exception
065 */
066 @SuppressWarnings("unchecked")
067 public ActionForward validateTimeEntry(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
068 TimeDetailActionFormBase tdaf = (TimeDetailActionFormBase) form;
069 JSONArray errorMsgList = new JSONArray();
070
071 List<String> errors = TimeDetailValidationService.validateTimeEntryDetails(tdaf);
072 errorMsgList.addAll(errors);
073
074 tdaf.setOutputString(JSONValue.toJSONString(errorMsgList));
075 return mapping.findForward("ws");
076 }
077
078 //this is an ajax call for the assignment maintenance page
079 public ActionForward getDepartmentForJobNumber(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
080 KualiMaintenanceForm kualiForm = (KualiMaintenanceForm) form;
081
082 String principalId = (String) request.getAttribute("principalId");
083 Long jobNumber = (Long) request.getAttribute("jobNumber");
084
085 Job job = TkServiceLocator.getJobService().getJob(principalId, jobNumber, TKUtils.getCurrentDate());
086 kualiForm.setAnnotation(job.getDept());
087
088 return mapping.findForward("ws");
089 }
090
091 public ActionForward getEarnCodeJson(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
092 TimeDetailWSActionForm tdaf = (TimeDetailWSActionForm) form;
093 List<Map<String, Object>> earnCodeList = new LinkedList<Map<String, Object>>();
094
095 if (StringUtils.isNotBlank(tdaf.getSelectedAssignment())) {
096 List<Assignment> assignments = tdaf.getTimesheetDocument().getAssignments();
097 AssignmentDescriptionKey key = new AssignmentDescriptionKey(tdaf.getSelectedAssignment());
098 for (Assignment assignment : assignments) {
099 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 }