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.TKContext;
35  import org.kuali.hr.time.util.TKUser;
36  import org.kuali.hr.time.util.TkConstants;
37  import org.kuali.rice.core.api.util.ConcreteKeyValue;
38  import org.kuali.rice.core.api.util.KeyValue;
39  import org.kuali.rice.kim.api.identity.Person;
40  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
41  import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
42  
43  public class MissedPunchAction extends KualiTransactionalDocumentActionBase {
44  
45      @Override
46      public ActionForward docHandler(ActionMapping mapping, ActionForm form,
47                                      HttpServletRequest request, HttpServletResponse response)
48              throws Exception {
49          ActionForward act = super.docHandler(mapping, form, request, response);
50          MissedPunchForm mpForm = (MissedPunchForm) form;
51          MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
52          mpForm.setDocId(mpDoc.getTimesheetDocumentId());
53  
54          ClockLog lastClock = null;
55          if (StringUtils.equals(request.getParameter("command"), "initiate")) {
56              String tdocId = request.getParameter("tdocid");
57              TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
58              mpForm.setDocNum(mpDoc.getDocumentNumber());
59              mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
60              mpDoc.setTimesheetDocumentId(tdocId);
61              // set default document description
62              if (StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
63                  mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
64              }
65  
66              lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPerson().getPrincipalId());
67              if (lastClock != null) {
68                  MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
69                  if (lastDoc != null) {    // last action was a missed punch
70                      mpDoc.setAssignment(lastDoc.getAssignment());
71                  } else {    // last action was not a missed punch
72                      AssignmentDescriptionKey adk = new AssignmentDescriptionKey(lastClock.getJobNumber().toString(), lastClock.getWorkArea().toString(), lastClock.getTask().toString());
73                      mpDoc.setAssignment(adk.toAssignmentKeyString());
74                  }
75              }
76          }
77          if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
78                  || StringUtils.equals(request.getParameter("command"), "displayActionListView")) {
79              Person p = KimApiServiceLocator.getPersonService().getPerson(mpDoc.getPrincipalId());
80              TKContext.getUser().setTargetPerson(p);
81              mpForm.setDocId(mpDoc.getDocumentNumber());
82          }
83  //      mpForm.setAssignmentReadOnly(true);
84          TkClockActionValuesFinder finder = new TkClockActionValuesFinder();
85          List<KeyValue> keyLabels = (List<KeyValue>) finder.getKeyValues();
86          if (keyLabels.size() == 2) {
87  //        		&& !mpForm.getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
88              Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_IN);
89              boolean flag = true;
90              for (String entry : actions) {
91                  if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
92                      flag = false;
93                  }
94              }
95              if (flag) {
96                  mpForm.setAssignmentReadOnly(true);
97              }
98          } else if (keyLabels.size() == 1) {
99              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 }