001    /**
002     * Copyright 2004-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.time.timesheet.web;
017    
018    import java.sql.Date;
019    
020    import javax.servlet.http.HttpServletRequest;
021    import javax.servlet.http.HttpServletResponse;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.apache.log4j.Logger;
025    import org.apache.struts.action.ActionForm;
026    import org.apache.struts.action.ActionForward;
027    import org.apache.struts.action.ActionMapping;
028    import org.apache.struts.action.ActionRedirect;
029    import org.kuali.hr.time.base.web.TkAction;
030    import org.kuali.hr.time.calendar.CalendarEntries;
031    import org.kuali.hr.time.detail.web.ActionFormUtils;
032    import org.kuali.hr.time.roles.TkUserRoles;
033    import org.kuali.hr.time.roles.UserRoles;
034    import org.kuali.hr.time.service.base.TkServiceLocator;
035    import org.kuali.hr.time.timesheet.TimesheetDocument;
036    import org.kuali.hr.time.util.TKContext;
037    import org.kuali.hr.time.util.TKUser;
038    import org.kuali.hr.time.util.TKUtils;
039    import org.kuali.hr.time.util.TkConstants;
040    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
041    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
042    import org.kuali.rice.krad.exception.AuthorizationException;
043    import org.kuali.rice.krad.util.GlobalVariables;
044    
045    public class TimesheetAction extends TkAction {
046    
047            private static final Logger LOG = Logger.getLogger(TimesheetAction.class);
048    
049        @Override
050        protected void checkTKAuthorization(ActionForm form, String methodToCall) throws AuthorizationException {
051            TKUser user = TKContext.getUser();
052            UserRoles roles = TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId());
053            TimesheetDocument doc = TKContext.getCurrentTimesheetDocument();
054    
055            if (!roles.isDocumentReadable(doc)) {
056                throw new AuthorizationException(GlobalVariables.getUserSession().getPrincipalId(), "TimesheetAction: docid: " + doc.getDocumentId(), "");
057            }
058        }
059    
060        @Override
061            public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
062                    TimesheetActionForm taForm = (TimesheetActionForm)form;
063                    TKUser user = TKContext.getUser();
064                    String documentId = taForm.getDocumentId();
065    
066            LOG.debug("DOCID: " + documentId);
067    
068            // Here - viewPrincipal will be the principal of the user we intend to
069            // view, be it target user, backdoor or otherwise.
070            String viewPrincipal = TKUser.getCurrentTargetPerson().getPrincipalId();
071                    CalendarEntries payCalendarEntries;
072                    TimesheetDocument td;
073                    TimesheetDocumentHeader tsdh;
074    
075            // By handling the prev/next in the execute method, we are saving one
076            // fetch/construction of a TimesheetDocument. If it were broken out into
077            // methods, we would first fetch the current document, and then fetch
078            // the next one instead of doing it in the single action.
079            if (StringUtils.isNotBlank(documentId)) {
080                td = TkServiceLocator.getTimesheetService().getTimesheetDocument(documentId);
081            } else {
082                // Default to whatever is active for "today".
083                Date currentDate = TKUtils.getTimelessDate(null);
084                payCalendarEntries = TkServiceLocator.getCalendarService().getCurrentCalendarDates(viewPrincipal,  currentDate);
085                if(payCalendarEntries == null){
086                    throw new RuntimeException("No pay calendar entry for " + viewPrincipal);
087                }
088                td = TkServiceLocator.getTimesheetService().openTimesheetDocument(viewPrincipal, payCalendarEntries);
089            }
090    
091            // Set the TKContext for the current timesheet document id.
092            if (td != null) {
093               setupDocumentOnFormContext(taForm, td);
094            } else {
095                LOG.error("Null timesheet document in TimesheetAction.");
096            }
097    
098            // Do this at the end, so we load the document first,
099            // 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    }