1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.time.timesheet.web;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.commons.lang.time.DateUtils;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.apache.struts.action.ActionRedirect;
30 import org.joda.time.LocalDate;
31 import org.kuali.kpme.core.calendar.Calendar;
32 import org.kuali.kpme.core.calendar.entry.CalendarEntry;
33 import org.kuali.kpme.core.service.HrServiceLocator;
34 import org.kuali.kpme.core.util.HrConstants;
35 import org.kuali.kpme.core.util.HrContext;
36 import org.kuali.kpme.tklm.common.CalendarFormAction;
37 import org.kuali.kpme.tklm.common.TkConstants;
38 import org.kuali.kpme.tklm.time.service.TkServiceLocator;
39 import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
40 import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
41 import org.kuali.rice.kim.api.identity.principal.EntityNamePrincipalName;
42 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
43 import org.kuali.rice.krad.exception.AuthorizationException;
44 import org.kuali.rice.krad.util.GlobalVariables;
45 import org.kuali.rice.krad.util.KRADConstants;
46
47 public class TimesheetAction extends CalendarFormAction {
48
49 @Override
50 protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
51 TimesheetActionForm timesheetActionForm = (TimesheetActionForm) form;
52
53 String principalId = GlobalVariables.getUserSession().getPrincipalId();
54 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(timesheetActionForm.getDocumentId());
55 if (timesheetDocument != null && !HrServiceLocator.getHRPermissionService().canViewCalendarDocument(principalId, timesheetDocument)) {
56 throw new AuthorizationException(principalId, "TimesheetAction: docid: " + timesheetDocument.getDocumentId(), "");
57 }
58 }
59
60 public ActionForward docHandler(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
61 ActionForward forward = mapping.findForward("basic");
62 String command = request.getParameter("command");
63
64 if (StringUtils.equals(command, "displayDocSearchView")
65 || StringUtils.equals(command, "displayActionListView")
66 || StringUtils.equals(command, "displaySuperUserView")) {
67 String documentId = (String) request.getParameter("docId");
68 TimesheetDocument timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
69 String timesheetPrincipalName = KimApiServiceLocator.getPersonService().getPerson(timesheetDocument.getPrincipalId()).getPrincipalName();
70
71 String principalId = HrContext.getTargetPrincipalId();
72 String principalName = KimApiServiceLocator.getPersonService().getPerson(principalId).getPrincipalName();
73
74 StringBuilder builder = new StringBuilder();
75 if (!StringUtils.equals(principalName, timesheetPrincipalName)) {
76 if (StringUtils.equals(command, "displayDocSearchView")
77 || StringUtils.equals(command, "displaySuperUserView")) {
78 builder.append("changeTargetPerson.do?methodToCall=changeTargetPerson");
79 builder.append("&documentId=");
80 builder.append(documentId);
81 builder.append("&principalName=");
82 builder.append(timesheetPrincipalName);
83 builder.append("&targetUrl=TimeDetail.do");
84 builder.append("?documentId=" + documentId);
85 builder.append("&returnUrl=TimeApproval.do");
86 } else {
87 builder.append("TimeApproval.do");
88 builder.append("?documentId=").append(documentId);
89 }
90 } else {
91 builder.append("TimeDetail.do");
92 builder.append("?documentId=" + documentId);
93 }
94
95 forward = new ActionRedirect(builder.toString());
96 }
97
98 return forward;
99 }
100
101 @Override
102 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
103 TimesheetActionForm timesheetActionForm = (TimesheetActionForm) form;
104 String documentId = timesheetActionForm.getDocumentId();
105 String principalId = HrContext.getTargetPrincipalId();
106 timesheetActionForm.setPrincipalId(principalId);
107 CalendarEntry calendarEntry = null;
108 TimesheetDocument timesheetDocument = null;
109
110 if (StringUtils.isNotBlank(documentId)) {
111 timesheetDocument = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
112
113 if (timesheetDocument != null) {
114 calendarEntry = timesheetDocument.getCalendarEntry();
115 }
116 } else {
117 if (StringUtils.isNotBlank(timesheetActionForm.getHrCalendarEntryId())) {
118 calendarEntry = HrServiceLocator.getCalendarEntryService().getCalendarEntry(timesheetActionForm.getHrCalendarEntryId());
119 } else {
120 calendarEntry = HrServiceLocator.getCalendarEntryService().getCurrentCalendarDates(principalId, new LocalDate().toDateTimeAtStartOfDay());
121
122 }
123
124 if (calendarEntry != null) {
125 Calendar calendar = HrServiceLocator.getCalendarService().getCalendar(calendarEntry.getHrCalendarId());
126 if(calendar != null && StringUtils.equals(calendar.getCalendarTypes(),TkConstants.CALENDAR_TYPE_PAY)) {
127
128 timesheetDocument = TkServiceLocator.getTimesheetService().openTimesheetDocument(principalId, calendarEntry);
129 }
130 else if(calendar != null) {
131 calendarEntry = null;
132 }
133 }
134 }
135
136 if (calendarEntry != null || timesheetDocument != null) {
137 if (calendarEntry != null) {
138 timesheetActionForm.setHrCalendarEntryId(calendarEntry.getHrCalendarEntryId());
139 timesheetActionForm.setCalendarEntry(calendarEntry);
140 timesheetActionForm.setBeginCalendarEntryDate(calendarEntry.getBeginPeriodDateTime());
141 timesheetActionForm.setEndCalendarEntryDate(DateUtils.addMilliseconds(calendarEntry.getEndPeriodDateTime(), -1));
142
143 CalendarEntry prevCalendarEntry = HrServiceLocator.getCalendarEntryService().getPreviousCalendarEntryByCalendarId(calendarEntry.getHrCalendarId(), calendarEntry);
144 timesheetActionForm.setPrevHrCalendarEntryId(prevCalendarEntry != null ? prevCalendarEntry.getHrCalendarEntryId() : null);
145
146 CalendarEntry nextCalendarEntry = HrServiceLocator.getCalendarEntryService().getNextCalendarEntryByCalendarId(calendarEntry.getHrCalendarId(), calendarEntry);
147 timesheetActionForm.setNextHrCalendarEntryId(nextCalendarEntry != null ? nextCalendarEntry.getHrCalendarEntryId() : null);
148 }
149
150 if (timesheetDocument != null) {
151 timesheetActionForm.setTimesheetDocument(timesheetDocument);
152 timesheetActionForm.setDocumentId(timesheetDocument.getDocumentId());
153
154 TimesheetDocumentHeader prevTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getPreviousDocumentHeader(principalId, timesheetDocument.getAsOfDate().toDateTimeAtStartOfDay());
155 TimesheetDocumentHeader nextTdh = TkServiceLocator.getTimesheetDocumentHeaderService().getNextDocumentHeader(principalId, timesheetDocument.getDocEndDate().toDateTimeAtStartOfDay());
156
157 timesheetActionForm.setPrevDocumentId(prevTdh != null ? prevTdh.getDocumentId() : null);
158 timesheetActionForm.setNextDocumentId(nextTdh != null ? nextTdh.getDocumentId() : null);
159 }
160
161 setCalendarFields(request, timesheetActionForm);
162 } else {
163 EntityNamePrincipalName entityNamePrincipalName = KimApiServiceLocator.getIdentityService().getDefaultNamesForPrincipalId(principalId);
164 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, "error.missing.payCalendar", entityNamePrincipalName.getPrincipalName());
165 }
166
167 return super.execute(mapping, form, request, response);
168 }
169
170 @Override
171 protected List<CalendarEntry> getCalendarEntries(CalendarEntry currentCalendarEntry) {
172 List<CalendarEntry> calendarEntries = new ArrayList<CalendarEntry>();
173
174 List<TimesheetDocumentHeader> timesheetDocumentHeaders = TkServiceLocator.getTimesheetDocumentHeaderService().getDocumentHeadersForPrincipalId(HrContext.getTargetPrincipalId());
175 for (TimesheetDocumentHeader timesheetDocumentHeader : timesheetDocumentHeaders) {
176 calendarEntries.add(HrServiceLocator.getCalendarEntryService().getCalendarDatesByPayEndDate(timesheetDocumentHeader.getPrincipalId(), timesheetDocumentHeader.getEndDateTime(), HrConstants.PAY_CALENDAR_TYPE));
177 }
178
179 return calendarEntries;
180 }
181
182 }