001 /** 002 * Copyright 2004-2013 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.missedpunch; 017 018 import java.util.HashMap; 019 import java.util.List; 020 import java.util.Set; 021 022 import javax.servlet.http.HttpServletRequest; 023 import javax.servlet.http.HttpServletResponse; 024 025 import org.apache.commons.lang.StringUtils; 026 import org.apache.struts.action.ActionForm; 027 import org.apache.struts.action.ActionForward; 028 import org.apache.struts.action.ActionMapping; 029 import org.kuali.hr.time.assignment.AssignmentDescriptionKey; 030 import org.kuali.hr.time.clocklog.ClockLog; 031 import org.kuali.hr.time.clocklog.TkClockActionValuesFinder; 032 import org.kuali.hr.time.service.base.TkServiceLocator; 033 import org.kuali.hr.time.timesheet.TimesheetDocument; 034 import org.kuali.hr.time.util.TKContext; 035 import org.kuali.hr.time.util.TKUser; 036 import org.kuali.hr.time.util.TkConstants; 037 import org.kuali.rice.core.api.util.ConcreteKeyValue; 038 import org.kuali.rice.core.api.util.KeyValue; 039 import org.kuali.rice.kim.api.identity.Person; 040 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 041 import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase; 042 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase; 043 import org.kuali.rice.krad.util.ObjectUtils; 044 045 public class MissedPunchAction extends KualiTransactionalDocumentActionBase { 046 047 @Override 048 public ActionForward docHandler(ActionMapping mapping, ActionForm form, 049 HttpServletRequest request, HttpServletResponse response) 050 throws Exception { 051 ActionForward act = super.docHandler(mapping, form, request, response); 052 MissedPunchForm mpForm = (MissedPunchForm) form; 053 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument(); 054 mpForm.setDocId(mpDoc.getTimesheetDocumentId()); 055 056 ClockLog lastClock = null; 057 if (StringUtils.equals(request.getParameter("command"), "initiate")) { 058 String tdocId = request.getParameter("tdocid"); 059 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId); 060 mpForm.setDocNum(mpDoc.getDocumentNumber()); 061 mpDoc.setPrincipalId(timesheetDocument.getPrincipalId()); 062 mpDoc.setTimesheetDocumentId(tdocId); 063 // set default document description 064 if (StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) { 065 mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId()); 066 } 067 068 lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPerson().getPrincipalId()); 069 if (lastClock != null) { 070 MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId()); 071 if (lastDoc != null) { // last action was a missed punch 072 mpDoc.setAssignment(lastDoc.getAssignment()); 073 } else { // last action was not a missed punch 074 AssignmentDescriptionKey adk = new AssignmentDescriptionKey(lastClock.getJobNumber().toString(), lastClock.getWorkArea().toString(), lastClock.getTask().toString()); 075 mpDoc.setAssignment(adk.toAssignmentKeyString()); 076 } 077 } 078 } 079 if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView") 080 || StringUtils.equals(request.getParameter("command"), "displayActionListView")) { 081 Person p = KimApiServiceLocator.getPersonService().getPerson(mpDoc.getPrincipalId()); 082 TKContext.getUser().setTargetPerson(p); 083 mpForm.setDocId(mpDoc.getDocumentNumber()); 084 } 085 // mpForm.setAssignmentReadOnly(true); 086 TkClockActionValuesFinder finder = new TkClockActionValuesFinder(); 087 List<KeyValue> keyLabels = (List<KeyValue>) finder.getKeyValues(); 088 if (keyLabels.size() == 2) { 089 // && !mpForm.getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT)) { 090 Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_IN); 091 boolean flag = true; 092 for (String entry : actions) { 093 if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) { 094 flag = false; 095 } 096 } 097 if (flag) { 098 mpForm.setAssignmentReadOnly(true); 099 } 100 } else if (keyLabels.size() == 1) { 101 Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.LUNCH_IN); 102 boolean flag = true; 103 for (String entry : actions) { 104 if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) { 105 flag = false; 106 } 107 } 108 if (flag) { 109 mpForm.setAssignmentReadOnly(true); 110 } 111 } 112 if (ObjectUtils.isNotNull(lastClock)) { 113 if (StringUtils.equals(lastClock.getClockAction(),"CO")){ // lock on the current assignment if user is clocked in. 114 mpForm.setAssignmentReadOnly(false); 115 } else { 116 mpForm.setAssignmentReadOnly(true); 117 } 118 } 119 120 return act; 121 } 122 123 @Override 124 public ActionForward route(ActionMapping mapping, ActionForm form, 125 HttpServletRequest request, HttpServletResponse response) 126 throws Exception { 127 MissedPunchForm mpForm = (MissedPunchForm) form; 128 mpForm.setEditingMode(new HashMap()); 129 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument(); 130 mpDoc.setDocumentStatus("R"); 131 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber()); 132 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId()); 133 ActionForward fwd = super.route(mapping, mpForm, request, response); 134 TkServiceLocator.getMissedPunchService().addClockLogForMissedPunch(mpDoc); 135 mpForm.setDocId(mpDoc.getDocumentNumber()); 136 mpForm.setAssignmentReadOnly(true); 137 return fwd; 138 139 } 140 141 @Override 142 public ActionForward approve(ActionMapping mapping, ActionForm form, 143 HttpServletRequest request, HttpServletResponse response) 144 throws Exception { 145 MissedPunchForm mpForm = (MissedPunchForm) form; 146 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument(); 147 mpDoc.setDocumentStatus("A"); 148 mpForm.setAssignmentReadOnly(true); 149 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber()); 150 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId()); 151 TkServiceLocator.getMissedPunchService().updateClockLogAndTimeBlockIfNecessary(mpDoc); 152 ActionForward fwd = super.approve(mapping, form, request, response); 153 return fwd; 154 } 155 156 @Override 157 public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 158 MissedPunchForm mpForm = (MissedPunchForm) form; 159 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument(); 160 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber()); 161 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId()); 162 return super.reload(mapping, form, request, response); 163 } 164 165 @Override 166 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 167 MissedPunchForm mpForm = (MissedPunchForm) form; 168 mpForm.setEditingMode(new HashMap()); 169 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument(); 170 mpDoc.setDocumentStatus("S"); 171 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber()); 172 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId()); 173 ActionForward fwd = super.save(mapping, mpForm, request, response); 174 mpForm.setDocId(mpDoc.getDocumentNumber()); 175 mpForm.setAssignmentReadOnly(true); 176 return fwd; 177 178 } 179 }