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 UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
52 TimesheetDocument doc = TKContext.getCurrentTimesheetDocument();
53
54 if (!roles.isDocumentReadable(doc)) {
55 throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "TimesheetAction: docid: " + doc.getDocumentId(), "");
56 }
57 }
58
59 @Override
60 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
61 TimesheetActionForm taForm = (TimesheetActionForm) form;
62 String documentId = taForm.getDocumentId();
63
64 if (StringUtils.equals(request.getParameter("command"), "displayDocSearchView")
65 || StringUtils.equals(request.getParameter("command"), "displayActionListView") ) {
66 documentId = (String) request.getParameter("docId");
67 }
68
69 LOG.debug("DOCID: " + documentId);
70
71
72
73 String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
74
75
76
77
78
79 TimesheetDocument td;
80 if (StringUtils.isNotBlank(documentId)) {
81 td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
82 } else {
83
84 Date currentDate = TKUtils.getTimelessDate(null);
85 CalendarEntries payCalendarEntries = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal, currentDate);
86 if (payCalendarEntries == null) {
87 throw new RuntimeException("No pay calendar entry for " + viewPrincipal);
88 }
89 td = TkServiceLocator.getTimesheetService().openTimesheetDocument(viewPrincipal, payCalendarEntries);
90 }
91
92
93 if (td != null) {
94 setupDocumentOnFormContext(taForm, td);
95 } else {
96 LOG.error("Null timesheet document in TimesheetAction.");
97 }
98
99
100
101 return super.execute(mapping, form, request, response);
102 }
103
104 public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
105 ActionForward forward = mapping.findForward("basic");
106 String command = request.getParameter("command");
107
108 if (StringUtils.equals(command, "displayDocSearchView") || StringUtils.equals(command, "displayActionListView")) {
109 String docId = (String) request.getParameter("docId");
110 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(docId);
111 String timesheetPrincipalName = KimApiServiceLocator.getPersonService().getPerson(timesheetDocument.getPrincipalId()).getPrincipalName();
112
113 String principalId = TKUser.getCurrentTargetPerson().getPrincipalId();
114 String principalName = KimApiServiceLocator.getPersonService().getPerson(principalId).getPrincipalName();
115
116 StringBuilder builder = new StringBuilder();
117 if (!StringUtils.equals(principalName, timesheetPrincipalName)) {
118 if (StringUtils.equals(command, "displayDocSearchView")) {
119 builder.append("changeTargetPerson.do?methodToCall=changeTargetPerson");
120 builder.append("&documentId=");
121 builder.append(docId);
122 builder.append("&principalName=");
123 builder.append(timesheetPrincipalName);
124 builder.append("&targetUrl=TimeDetail.do");
125 builder.append("?docmentId=" + docId);
126 builder.append("&returnUrl=TimeApproval.do");
127 } else {
128 builder.append("TimeApproval.do");
129 }
130 } else {
131 builder.append("TimeDetail.do");
132 builder.append("?docmentId=" + docId);
133 }
134
135 forward = new ActionRedirect(builder.toString());
136 }
137
138 return forward;
139 }
140
141 protected void setupDocumentOnFormContext(TimesheetActionForm taForm, TimesheetDocument td){
142 String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
143 TKContext.setCurrentTimesheetDocumentId(td.getDocumentId());
144 TKContext.setCurrentTimesheetDocument(td);
145 taForm.setTimesheetDocument(td);
146 taForm.setDocumentId(td.getDocumentId());
147 TimesheetDocumentHeader prevTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.PREV_TIMESHEET, viewPrincipal);
148 TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPrevOrNextDocumentHeader(TkConstants.NEXT_TIMESHEET, viewPrincipal);
149
150 taForm.setPrevDocumentId(prevTdh != null ? prevTdh.getDocumentId() : null);
151 taForm.setNextDocumentId(nextTdh != null ? nextTdh.getDocumentId() : null);
152
153 taForm.setPayCalendarDates(td.getPayCalendarEntry());
154 taForm.setOnCurrentPeriod(ActionFormUtils.getOnCurrentPeriodFlag(taForm.getPayCalendarDates()));
155 }
156
157 }