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.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
043 public class MissedPunchAction extends KualiTransactionalDocumentActionBase {
044
045 @Override
046 public ActionForward docHandler(ActionMapping mapping, ActionForm form,
047 HttpServletRequest request, HttpServletResponse response)
048 throws Exception {
049 ActionForward act = super.docHandler(mapping, form, request, response);
050 MissedPunchForm mpForm = (MissedPunchForm) form;
051 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
052 mpForm.setDocId(mpDoc.getTimesheetDocumentId());
053
054 if (StringUtils.equals(request.getParameter("command"), "initiate")) {
055 String tdocId = request.getParameter("tdocid");
056 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
057 mpForm.setDocNum(mpDoc.getDocumentNumber());
058 mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
059 mpDoc.setTimesheetDocumentId(tdocId);
060 // set default document description
061 if(StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
062 mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
063 }
064
065 ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPerson().getPrincipalId());
066 if(lastClock != null) {
067 MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
068 if(lastDoc != null) { // last action was a missed punch
069 mpDoc.setAssignment(lastDoc.getAssignment());
070 } else { // last action was not a missed punch
071 AssignmentDescriptionKey adk = new AssignmentDescriptionKey(lastClock.getJobNumber().toString(), lastClock.getWorkArea().toString(), lastClock.getTask().toString());
072 mpDoc.setAssignment(adk.toAssignmentKeyString());
073 }
074 }
075 }
076 if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
077 || StringUtils.equals(request.getParameter("command"), "displayActionListView") ) {
078 Person p = KimApiServiceLocator.getPersonService().getPerson(mpDoc.getPrincipalId());
079 TKContext.getUser().setTargetPerson(p);
080 mpForm.setDocId(mpDoc.getDocumentNumber());
081 }
082
083 mpForm.setAssignmentReadOnly(false);
084 TkClockActionValuesFinder finder = new TkClockActionValuesFinder();
085 List<KeyValue> keyLabels = (List<KeyValue>) finder.getKeyValues();
086 if(keyLabels.size() == 2){
087 // && !mpForm.getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
088 Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_IN);
089 boolean flag = true;
090 for (String entry : actions) {
091 if(!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
092 flag = false;
093 }
094 }
095 if(flag) {
096 mpForm.setAssignmentReadOnly(true);
097 }
098 } else if(keyLabels.size() == 1){
099 Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.LUNCH_IN);
100 boolean flag = true;
101 for (String entry : actions) {
102 if(!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
103 flag = false;
104 }
105 }
106 if(flag) {
107 mpForm.setAssignmentReadOnly(true);
108 }
109 }
110
111 return act;
112 }
113
114 @Override
115 public ActionForward route(ActionMapping mapping, ActionForm form,
116 HttpServletRequest request, HttpServletResponse response)
117 throws Exception {
118 MissedPunchForm mpForm = (MissedPunchForm) form;
119 mpForm.setEditingMode(new HashMap());
120 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
121 mpDoc.setDocumentStatus("R");
122 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
123 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
124 ActionForward fwd = super.route(mapping, mpForm, request, response);
125 mpForm.setDocId(mpDoc.getDocumentNumber());
126 mpForm.setAssignmentReadOnly(true);
127 return fwd;
128
129 }
130
131 @Override
132 public ActionForward approve(ActionMapping mapping, ActionForm form,
133 HttpServletRequest request, HttpServletResponse response)
134 throws Exception {
135 MissedPunchForm mpForm = (MissedPunchForm) form;
136 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
137 mpDoc.setDocumentStatus("A");
138 mpForm.setAssignmentReadOnly(true);
139 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
140 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
141 TkServiceLocator.getMissedPunchService().updateClockLogAndTimeBlockIfNecessary(mpDoc);
142 ActionForward fwd = super.approve(mapping, form, request, response);
143 return fwd;
144 }
145
146 @Override
147 public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
148 MissedPunchForm mpForm = (MissedPunchForm) form;
149 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
150 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
151 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
152 return super.reload(mapping, form, request, response);
153 }
154
155
156 }