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  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
43  import org.kuali.rice.krad.util.ObjectUtils;
44  
45  public class MissedPunchAction extends KualiTransactionalDocumentActionBase {
46  
47      @Override
48      public ActionForward docHandler(ActionMapping mapping, ActionForm form,
49                                      HttpServletRequest request, HttpServletResponse response)
50              throws Exception {
51          ActionForward act = super.docHandler(mapping, form, request, response);
52          MissedPunchForm mpForm = (MissedPunchForm) form;
53          MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
54          mpForm.setDocId(mpDoc.getTimesheetDocumentId());
55  
56          ClockLog lastClock = null;
57          if (StringUtils.equals(request.getParameter("command"), "initiate")) {
58              String tdocId = request.getParameter("tdocid");
59              TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
60              mpForm.setDocNum(mpDoc.getDocumentNumber());
61              mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
62              mpDoc.setTimesheetDocumentId(tdocId);
63              // set default document description
64              if (StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
65                  mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
66              }
67  
68              lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPerson().getPrincipalId());
69              if (lastClock != null) {
70                  MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
71                  if (lastDoc != null) {    // last action was a missed punch
72                      mpDoc.setAssignment(lastDoc.getAssignment());
73                  } else {    // last action was not a missed punch
74                      AssignmentDescriptionKey adk = new AssignmentDescriptionKey(lastClock.getJobNumber().toString(), lastClock.getWorkArea().toString(), lastClock.getTask().toString());
75                      mpDoc.setAssignment(adk.toAssignmentKeyString());
76                  }
77              }
78          }
79          if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
80                  || StringUtils.equals(request.getParameter("command"), "displayActionListView")) {
81              Person p = KimApiServiceLocator.getPersonService().getPerson(mpDoc.getPrincipalId());
82              TKContext.getUser().setTargetPerson(p);
83              mpForm.setDocId(mpDoc.getDocumentNumber());
84          }
85  //      mpForm.setAssignmentReadOnly(true);
86          TkClockActionValuesFinder finder = new TkClockActionValuesFinder();
87          List<KeyValue> keyLabels = (List<KeyValue>) finder.getKeyValues();
88          if (keyLabels.size() == 2) {
89  //        		&& !mpForm.getDocumentActions().containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT)) {
90              Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.CLOCK_IN);
91              boolean flag = true;
92              for (String entry : actions) {
93                  if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
94                      flag = false;
95                  }
96              }
97              if (flag) {
98                  mpForm.setAssignmentReadOnly(true);
99              }
100         } else if (keyLabels.size() == 1) {
101             Set<String> actions = TkConstants.CLOCK_ACTION_TRANSITION_MAP.get(TkConstants.LUNCH_IN);
102             boolean flag = true;
103             for (String entry : actions) {
104                 if (!keyLabels.contains(new ConcreteKeyValue(entry, TkConstants.CLOCK_ACTION_STRINGS.get(entry)))) {
105                     flag = false;
106                 }
107             }
108             if (flag) {
109                 mpForm.setAssignmentReadOnly(true);
110             }
111         }
112         if (ObjectUtils.isNotNull(lastClock)) {
113             if (StringUtils.equals(lastClock.getClockAction(),"CO")){        // lock on the current assignment if user is clocked in.
114                 mpForm.setAssignmentReadOnly(false);
115             } else {
116                 mpForm.setAssignmentReadOnly(true);
117             }
118         }
119 
120         return act;
121     }
122 
123     @Override
124     public ActionForward route(ActionMapping mapping, ActionForm form,
125                                HttpServletRequest request, HttpServletResponse response)
126             throws Exception {
127         MissedPunchForm mpForm = (MissedPunchForm) form;
128         mpForm.setEditingMode(new HashMap());
129         MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
130         mpDoc.setDocumentStatus("R");
131         request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
132         request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
133         ActionForward fwd = super.route(mapping, mpForm, request, response);
134         TkServiceLocator.getMissedPunchService().addClockLogForMissedPunch(mpDoc);
135         mpForm.setDocId(mpDoc.getDocumentNumber());
136         mpForm.setAssignmentReadOnly(true);
137         return fwd;
138 
139     }
140 
141     @Override
142     public ActionForward approve(ActionMapping mapping, ActionForm form,
143                                  HttpServletRequest request, HttpServletResponse response)
144             throws Exception {
145         MissedPunchForm mpForm = (MissedPunchForm) form;
146         MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
147         mpDoc.setDocumentStatus("A");
148         mpForm.setAssignmentReadOnly(true);
149         request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
150         request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
151         TkServiceLocator.getMissedPunchService().updateClockLogAndTimeBlockIfNecessary(mpDoc);
152         ActionForward fwd = super.approve(mapping, form, request, response);
153         return fwd;
154     }
155     
156   @Override
157   public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
158 	  MissedPunchForm mpForm = (MissedPunchForm) form;
159       MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
160       request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
161       request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
162   	  return super.reload(mapping, form, request, response);
163   }
164 
165     @Override
166     public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
167         MissedPunchForm mpForm = (MissedPunchForm) form;
168         mpForm.setEditingMode(new HashMap());
169         MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
170         mpDoc.setDocumentStatus("S");
171         request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
172         request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
173         ActionForward fwd = super.save(mapping, mpForm, request, response);
174         mpForm.setDocId(mpDoc.getDocumentNumber());
175         mpForm.setAssignmentReadOnly(true);
176         return fwd;
177 
178     }
179 }