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.TKUser;
035 import org.kuali.hr.time.util.TkConstants;
036 import org.kuali.rice.core.api.util.ConcreteKeyValue;
037 import org.kuali.rice.core.api.util.KeyValue;
038 import org.kuali.rice.core.api.util.RiceConstants;
039 import org.kuali.rice.kew.api.KewApiConstants;
040 import org.kuali.rice.kew.api.KewApiServiceLocator;
041 import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
042 import org.kuali.rice.krad.exception.ValidationException;
043
044 public class MissedPunchAction extends KualiTransactionalDocumentActionBase {
045
046 @Override
047 public ActionForward docHandler(ActionMapping mapping, ActionForm form,
048 HttpServletRequest request, HttpServletResponse response)
049 throws Exception {
050 //HACK!!!
051 MissedPunchForm mpForm = (MissedPunchForm) form;
052 String command = mpForm.getCommand();
053 if (StringUtils.equals(KewApiConstants.INITIATE_COMMAND, command)) {
054 ((MissedPunchForm) form).setDocId(null);
055 }
056 //END HACK!!!!
057
058 ActionForward act = super.docHandler(mapping, form, request, response);
059 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
060 //mpForm.setDocId(mpDoc.getTimesheetDocumentId());
061
062 if (StringUtils.equals(request.getParameter("command"), "initiate")) {
063 String tdocId = request.getParameter("tdocid");
064 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
065 mpForm.setDocNum(mpDoc.getDocumentNumber());
066 mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
067 mpDoc.setTimesheetDocumentId(tdocId);
068 // set default document description
069
070 if (StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
071 mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
072 }
073 }
074 if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
075 || StringUtils.equals(request.getParameter("command"), "displayActionListView")
076 || StringUtils.equals(command, "displaySuperUserView")) {
077 TKUser.setTargetPerson(mpDoc.getPrincipalId());
078 mpForm.setDocId(mpDoc.getDocumentNumber());
079 }
080 // mpForm.setAssignmentReadOnly(true);
081 TkClockActionValuesFinder finder = new TkClockActionValuesFinder();
082 List<KeyValue> keyLabels = (List<KeyValue>) finder.getKeyValues();
083 if (keyLabels.size() == 2) {
084 // && !mpForm.getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
085 Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_IN);
086 boolean flag = true;
087 for (String entry : actions) {
088 if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
089 flag = false;
090 }
091 }
092 if (flag) {
093 mpForm.setAssignmentReadOnly(true);
094 }
095 } else if (keyLabels.size() == 1) {
096 Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.LUNCH_IN);
097 boolean flag = true;
098 for (String entry : actions) {
099 if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
100 flag = false;
101 }
102 }
103 if (flag) {
104 mpForm.setAssignmentReadOnly(true);
105 }
106 }
107
108 ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPersonId());
109 if (lastClock != null && !StringUtils.equals(lastClock.getClockAction(), TkConstants.CLOCK_OUT)) {
110 MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
111 // Default the assignment if last clock was a clock in.
112 defaultMissedPunchAssignment(mpDoc, lastDoc, lastClock);
113 mpForm.setAssignmentReadOnly(true);
114 } else {
115 mpForm.setAssignmentReadOnly(false);
116 }
117
118 return act;
119 }
120
121 private void defaultMissedPunchAssignment(MissedPunchDocument mpDoc, MissedPunchDocument lastDoc, ClockLog lastClock) {
122 if (lastDoc != null) { // last action was a missed punch
123 mpDoc.setAssignment(lastDoc.getAssignment());
124 } else { // last action was not a missed punch
125 AssignmentDescriptionKey adk = new AssignmentDescriptionKey(lastClock.getJobNumber().toString(), lastClock.getWorkArea().toString(), lastClock.getTask().toString());
126 mpDoc.setAssignment(adk.toAssignmentKeyString());
127 }
128 }
129
130 @Override
131 public ActionForward route(ActionMapping mapping, ActionForm form,
132 HttpServletRequest request, HttpServletResponse response)
133 throws Exception {
134 MissedPunchForm mpForm = (MissedPunchForm) form;
135 mpForm.setEditingMode(new HashMap());
136 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
137 mpDoc.setDocumentStatus("R");
138 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
139 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
140 //TkServiceLocator.getMissedPunchService().addClockLogForMissedPunch(mpDoc);
141 mpForm.setDocId(mpDoc.getDocumentNumber());
142 mpForm.setAssignmentReadOnly(true);
143 try {
144 return super.route(mapping, mpForm, request, response);
145 } catch (ValidationException e) {
146 mpForm.setAssignmentReadOnly(false);
147 return mapping.findForward(RiceConstants.MAPPING_BASIC);
148 }
149 }
150
151 @Override
152 public ActionForward approve(ActionMapping mapping, ActionForm form,
153 HttpServletRequest request, HttpServletResponse response)
154 throws Exception {
155 MissedPunchForm mpForm = (MissedPunchForm) form;
156 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
157 mpDoc.setDocumentStatus("A");
158 mpForm.setAssignmentReadOnly(true);
159 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
160 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
161 TkServiceLocator.getMissedPunchService().updateClockLogAndTimeBlockIfNecessary(mpDoc);
162 return super.approve(mapping, form, request, response);
163 }
164
165 @Override
166 public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
167 MissedPunchForm mpForm = (MissedPunchForm) form;
168 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
169 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
170 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
171 return super.reload(mapping, form, request, response);
172 }
173
174 @Override
175 public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
176 MissedPunchForm mpForm = (MissedPunchForm) form;
177 mpForm.setEditingMode(new HashMap());
178 MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
179 mpDoc.setDocumentStatus("S");
180 request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
181 request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
182 mpForm.setDocId(mpDoc.getDocumentNumber());
183 mpForm.setAssignmentReadOnly(true);
184 try {
185 return super.save(mapping, mpForm, request, response);
186 } catch (ValidationException e) {
187 mpForm.setAssignmentReadOnly(false);
188 return mapping.findForward(RiceConstants.MAPPING_BASIC);
189 }
190
191 }
192 }