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