1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
69
70 String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
71 CalendarEntries payCalendarEntries;
72 TimesheetDocument td;
73 TimesheetDocumentHeader tsdh;
74
75
76
77
78
79 if (StringUtils.isNotBlank(documentId)) {
80 td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
81 } else {
82
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
92 if (td != null) {
93 setupDocumentOnFormContext(taForm, td);
94 } else {
95 LOG.error("Null timesheet document in TimesheetAction.");
96 }
97
98
99
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 }