View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.time.missedpunch;
17  
18  import java.util.HashMap;
19  import java.util.List;
20  import java.util.Set;
21  
22  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.struts.action.ActionForm;
27  import org.apache.struts.action.ActionForward;
28  import org.apache.struts.action.ActionMapping;
29  import org.kuali.hr.time.assignment.AssignmentDescriptionKey;
30  import org.kuali.hr.time.clocklog.ClockLog;
31  import org.kuali.hr.time.clocklog.TkClockActionValuesFinder;
32  import org.kuali.hr.time.service.base.TkServiceLocator;
33  import org.kuali.hr.time.timesheet.TimesheetDocument;
34  import org.kuali.hr.time.util.TKUser;
35  import org.kuali.hr.time.util.TkConstants;
36  import org.kuali.rice.core.api.util.ConcreteKeyValue;
37  import org.kuali.rice.core.api.util.KeyValue;
38  import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
39  
40  public class MissedPunchAction extends KualiTransactionalDocumentActionBase {
41  
42      @Override
43      public ActionForward docHandler(ActionMapping mapping, ActionForm form,
44                                      HttpServletRequest request, HttpServletResponse response)
45              throws Exception {
46          ActionForward act = super.docHandler(mapping, form, request, response);
47          MissedPunchForm mpForm = (MissedPunchForm) form;
48          MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
49          mpForm.setDocId(mpDoc.getTimesheetDocumentId());
50  
51          if (StringUtils.equals(request.getParameter("command"), "initiate")) {
52              String tdocId = request.getParameter("tdocid");
53              TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
54              mpForm.setDocNum(mpDoc.getDocumentNumber());
55              mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
56              mpDoc.setTimesheetDocumentId(tdocId);
57              // set default document description
58              if (StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
59                  mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
60              }
61          }
62          if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
63                  || StringUtils.equals(request.getParameter("command"), "displayActionListView")) {
64              TKUser.setTargetPerson(mpDoc.getPrincipalId());
65              mpForm.setDocId(mpDoc.getDocumentNumber());
66          }
67  //      mpForm.setAssignmentReadOnly(true);
68          TkClockActionValuesFinder finder = new TkClockActionValuesFinder();
69          List<KeyValue> keyLabels = (List<KeyValue>) finder.getKeyValues();
70          if (keyLabels.size() == 2) {
71  //        		&& !mpForm.getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
72              Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_IN);
73              boolean flag = true;
74              for (String entry : actions) {
75                  if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
76                      flag = false;
77                  }
78              }
79              if (flag) {
80                  mpForm.setAssignmentReadOnly(true);
81              }
82          } else if (keyLabels.size() == 1) {
83              Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.LUNCH_IN);
84              boolean flag = true;
85              for (String entry : actions) {
86                  if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
87                      flag = false;
88                  }
89              }
90              if (flag) {
91                  mpForm.setAssignmentReadOnly(true);
92              }
93          }
94          
95          ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPersonId());
96          if (lastClock != null && !StringUtils.equals(lastClock.getClockAction(), TkConstants.CLOCK_OUT)) {
97          	MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
98              // Default the assignment if last clock was a clock in.
99              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 }