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.timesheet.web;
17  
18  import java.sql.Date;
19  
20  import javax.servlet.http.HttpServletRequest;
21  import javax.servlet.http.HttpServletResponse;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.log4j.Logger;
25  import org.apache.struts.action.ActionForm;
26  import org.apache.struts.action.ActionForward;
27  import org.apache.struts.action.ActionMapping;
28  import org.apache.struts.action.ActionRedirect;
29  import org.kuali.hr.time.base.web.TkAction;
30  import org.kuali.hr.time.calendar.CalendarEntries;
31  import org.kuali.hr.time.detail.web.ActionFormUtils;
32  import org.kuali.hr.time.roles.TkUserRoles;
33  import org.kuali.hr.time.roles.UserRoles;
34  import org.kuali.hr.time.service.base.TkServiceLocator;
35  import org.kuali.hr.time.timesheet.TimesheetDocument;
36  import org.kuali.hr.time.util.TKContext;
37  import org.kuali.hr.time.util.TKUser;
38  import org.kuali.hr.time.util.TKUtils;
39  import org.kuali.hr.time.util.TkConstants;
40  import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
41  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
42  import org.kuali.rice.krad.exception.AuthorizationException;
43  import org.kuali.rice.krad.util.GlobalVariables;
44  
45  public class TimesheetAction extends TkAction {
46  
47  	private static final Logger LOG = Logger.getLogger(TimesheetAction.class);
48  
49      @Override
50      protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
51          TKUser user = TKContext.getUser();
52          UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
53          TimesheetDocument doc = TKContext.getCurrentTimesheetDocument();
54  
55          if (!roles.isDocumentReadable(doc)) {
56              throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "TimesheetAction: docid: " + doc.getDocumentId(), "");
57          }
58      }
59  
60      @Override
61  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
62  		TimesheetActionForm taForm = (TimesheetActionForm)form;
63  		TKUser user = TKContext.getUser();
64  		String documentId = taForm.getDocumentId();
65  
66          LOG.debug("DOCID: " + documentId);
67  
68          // Here - viewPrincipal will be the principal of the user we intend to
69          // view, be it target user, backdoor or otherwise.
70          String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
71  		CalendarEntries payCalendarEntries;
72  		TimesheetDocument td;
73  		TimesheetDocumentHeader tsdh;
74  
75          // By handling the prev/next in the execute method, we are saving one
76          // fetch/construction of a TimesheetDocument. If it were broken out into
77          // methods, we would first fetch the current document, and then fetch
78          // the next one instead of doing it in the single action.
79          if (StringUtils.isNotBlank(documentId)) {
80              td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
81          } else {
82              // Default to whatever is active for "today".
83              Date currentDate = TKUtils.getTimelessDate(null);
84              payCalendarEntries = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal,  currentDate);
85              if(payCalendarEntries == null){
86                  throw new RuntimeException("No pay calendar entry for " + viewPrincipal);
87              }
88              td = TkServiceLocator.getTimesheetService().openTimesheetDocument(viewPrincipal, payCalendarEntries);
89          }
90  
91          // Set the TKContext for the current timesheet document id.
92          if (td != null) {
93             setupDocumentOnFormContext(taForm, td);
94          } else {
95              LOG.error("Null timesheet document in TimesheetAction.");
96          }
97  
98          // Do this at the end, so we load the document first,
99          // then check security permissions via the superclass execution chain.
100 		return super.execute(mapping, form, request, response);
101 	}
102 
103     public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
104         if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
105         		|| StringUtils.equals(request.getParameter("command"), "displayActionListView") ) {
106         	final String docId = (String)request.getParameter("docId");
107         	TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(docId);
108         	final String principalName = KimApiServiceLocator.getPersonService().getPerson(td.getPrincipalId()).getPrincipalName();
109         	
110         	return new ActionRedirect("/changeTargetPerson.do?methodToCall=changeTargetPerson&documentId" + docId + "&principalName=" + principalName + "&targetUrl=TimeDetail.do%3FdocmentId=" + docId + "&returnUrl=TimeApproval.do");
111         }
112     	
113     	return mapping.findForward("basic");
114     }
115 
116     protected void setupDocumentOnFormContext(TimesheetActionForm taForm, TimesheetDocument td){
117     	String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
118     	TKContext.setCurrentTimesheetDocumentId(td.getDocumentId());
119         TKContext.setCurrentTimesheetDocument(td);
120 	    taForm.setTimesheetDocument(td);
121 	    taForm.setDocumentId(td.getDocumentId());
122         TimesheetDocumentHeader prevTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.PREV_TIMESHEET, viewPrincipal);
123         TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.NEXT_TIMESHEET, viewPrincipal);
124         if( prevTdh != null ) {
125             taForm.setPrevDocumentId(prevTdh.getDocumentId());
126         }
127         if( nextTdh != null) {
128             taForm.setNextDocumentId(nextTdh.getDocumentId());
129         }
130         taForm.setPayCalendarDates(td.getPayCalendarEntry());
131         taForm.setOnCurrentPeriod(ActionFormUtils.getOnCurrentPeriodFlag(taForm.getPayCalendarDates()));
132     }
133 
134 }