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    
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            ClockLog lastClock = null;
055            if (StringUtils.equals(request.getParameter("command"), "initiate")) {
056                String tdocId = request.getParameter("tdocid");
057                TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
058                mpForm.setDocNum(mpDoc.getDocumentNumber());
059                mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
060                mpDoc.setTimesheetDocumentId(tdocId);
061                // set default document description
062                if (StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
063                    mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
064                }
065    
066                lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPerson().getPrincipalId());
067                if (lastClock != null) {
068                    MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
069                    if (lastDoc != null) {    // last action was a missed punch
070                        mpDoc.setAssignment(lastDoc.getAssignment());
071                    } else {    // last action was not a missed punch
072                        AssignmentDescriptionKey adk = new AssignmentDescriptionKey(lastClock.getJobNumber().toString(), lastClock.getWorkArea().toString(), lastClock.getTask().toString());
073                        mpDoc.setAssignment(adk.toAssignmentKeyString());
074                    }
075                }
076            }
077            if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
078                    || StringUtils.equals(request.getParameter("command"), "displayActionListView")) {
079                Person p = KimApiServiceLocator.getPersonService().getPerson(mpDoc.getPrincipalId());
080                TKContext.getUser().setTargetPerson(p);
081                mpForm.setDocId(mpDoc.getDocumentNumber());
082            }
083    //      mpForm.setAssignmentReadOnly(true);
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            if (StringUtils.equals(lastClock.getClockAction(),"CO")){        // lock on the current assignment if user is clocked in.
111                mpForm.setAssignmentReadOnly(false);
112            } else {
113                mpForm.setAssignmentReadOnly(true);
114            }
115            return act;
116        }
117    
118        @Override
119        public ActionForward route(ActionMapping mapping, ActionForm form,
120                                   HttpServletRequest request, HttpServletResponse response)
121                throws Exception {
122            MissedPunchForm mpForm = (MissedPunchForm) form;
123            mpForm.setEditingMode(new HashMap());
124            MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
125            mpDoc.setDocumentStatus("R");
126            request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
127            request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
128            ActionForward fwd = super.route(mapping, mpForm, request, response);
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    
160    }