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.core.api.util.RiceConstants;
39  import org.kuali.rice.kew.api.KewApiConstants;
40  import org.kuali.rice.kew.api.KewApiServiceLocator;
41  import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
42  import org.kuali.rice.krad.exception.ValidationException;
43  
44  public class MissedPunchAction extends KualiTransactionalDocumentActionBase {
45  
46      @Override
47      public ActionForward docHandler(ActionMapping mapping, ActionForm form,
48                                      HttpServletRequest request, HttpServletResponse response)
49              throws Exception {
50          //HACK!!!
51          MissedPunchForm mpForm = (MissedPunchForm) form;
52          String command = mpForm.getCommand();
53          if (StringUtils.equals(KewApiConstants.INITIATE_COMMAND, command)) {
54              ((MissedPunchForm) form).setDocId(null);
55          }
56          //END HACK!!!!
57  
58          ActionForward act = super.docHandler(mapping, form, request, response);
59          MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
60          //mpForm.setDocId(mpDoc.getTimesheetDocumentId());
61  
62          if (StringUtils.equals(request.getParameter("command"), "initiate")) {
63              String tdocId = request.getParameter("tdocid");
64              TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
65              mpForm.setDocNum(mpDoc.getDocumentNumber());
66              mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
67              mpDoc.setTimesheetDocumentId(tdocId);
68              // set default document description
69  
70              if (StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
71                  mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
72              }
73          }
74          if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
75                  || StringUtils.equals(request.getParameter("command"), "displayActionListView")
76                  || StringUtils.equals(command, "displaySuperUserView")) {
77              TKUser.setTargetPerson(mpDoc.getPrincipalId());
78              mpForm.setDocId(mpDoc.getDocumentNumber());
79          }
80  //      mpForm.setAssignmentReadOnly(true);
81          TkClockActionValuesFinder finder = new TkClockActionValuesFinder();
82          List<KeyValue> keyLabels = (List<KeyValue>) finder.getKeyValues();
83          if (keyLabels.size() == 2) {
84  //        		&& !mpForm.getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
85              Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_IN);
86              boolean flag = true;
87              for (String entry : actions) {
88                  if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
89                      flag = false;
90                  }
91              }
92              if (flag) {
93                  mpForm.setAssignmentReadOnly(true);
94              }
95          } else if (keyLabels.size() == 1) {
96              Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.LUNCH_IN);
97              boolean flag = true;
98              for (String entry : actions) {
99                  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 }