View Javadoc

1   /**
2    * Copyright 2004-2012 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          if (StringUtils.equals(request.getParameter("command"), "initiate")) {
55              String tdocId = request.getParameter("tdocid");
56              TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(tdocId);
57              mpForm.setDocNum(mpDoc.getDocumentNumber());
58              mpDoc.setPrincipalId(timesheetDocument.getPrincipalId());
59              mpDoc.setTimesheetDocumentId(tdocId);
60              // set default document description
61              if(StringUtils.isEmpty(mpDoc.getDocumentHeader().getDocumentDescription())) {
62              	mpDoc.getDocumentHeader().setDocumentDescription("Missed Punch: " + timesheetDocument.getPrincipalId());
63              }
64              
65              ClockLog lastClock = TkServiceLocator.getClockLogService().getLastClockLog(TKUser.getCurrentTargetPerson().getPrincipalId());
66              if(lastClock != null) {
67  	            MissedPunchDocument lastDoc = TkServiceLocator.getMissedPunchService().getMissedPunchByClockLogId(lastClock.getTkClockLogId());
68  	            if(lastDoc != null) {	// last action was a missed punch
69  	            	mpDoc.setAssignment(lastDoc.getAssignment());
70  	            } else {	// last action was not a missed punch
71  	            	AssignmentDescriptionKey adk = new AssignmentDescriptionKey(lastClock.getJobNumber().toString(), lastClock.getWorkArea().toString(), lastClock.getTask().toString());
72  	            	mpDoc.setAssignment(adk.toAssignmentKeyString());
73  	            }
74              }
75          }
76          if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
77          		|| StringUtils.equals(request.getParameter("command"), "displayActionListView") ) {
78              Person p = KimApiServiceLocator.getPersonService().getPerson(mpDoc.getPrincipalId());
79              TKContext.getUser().setTargetPerson(p);
80              mpForm.setDocId(mpDoc.getDocumentNumber());
81          }
82          
83          mpForm.setAssignmentReadOnly(false);
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         
111         return act;
112     }
113 
114     @Override
115     public ActionForward route(ActionMapping mapping, ActionForm form,
116                                HttpServletRequest request, HttpServletResponse response)
117             throws Exception {
118         MissedPunchForm mpForm = (MissedPunchForm) form;
119         mpForm.setEditingMode(new HashMap());
120         MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
121         mpDoc.setDocumentStatus("R");
122         request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
123         request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
124         ActionForward fwd = super.route(mapping, mpForm, request, response);
125         mpForm.setDocId(mpDoc.getDocumentNumber());
126         mpForm.setAssignmentReadOnly(true);
127         return fwd;
128 
129     }
130 
131     @Override
132     public ActionForward approve(ActionMapping mapping, ActionForm form,
133                                  HttpServletRequest request, HttpServletResponse response)
134             throws Exception {
135         MissedPunchForm mpForm = (MissedPunchForm) form;
136         MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
137         mpDoc.setDocumentStatus("A");
138         mpForm.setAssignmentReadOnly(true);
139         request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
140         request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
141         TkServiceLocator.getMissedPunchService().updateClockLogAndTimeBlockIfNecessary(mpDoc);
142         ActionForward fwd = super.approve(mapping, form, request, response);
143         return fwd;
144     }
145     
146   @Override
147   public ActionForward reload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
148 	  MissedPunchForm mpForm = (MissedPunchForm) form;
149       MissedPunchDocument mpDoc = (MissedPunchDocument) mpForm.getDocument();
150       request.setAttribute(TkConstants.DOCUMENT_ID_REQUEST_NAME, mpDoc.getDocumentNumber());
151       request.setAttribute(TkConstants.TIMESHEET_DOCUMENT_ID_REQUEST_NAME, mpDoc.getTimesheetDocumentId());
152   	  return super.reload(mapping, form, request, response);
153   }
154 
155 
156 }