001    /**
002     * Copyright 2004-2013 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.approval.web;
017    
018    import java.text.SimpleDateFormat;
019    import java.util.*;
020    
021    import javax.servlet.http.HttpServletRequest;
022    import javax.servlet.http.HttpServletResponse;
023    
024    import org.apache.commons.lang.StringUtils;
025    import org.apache.commons.lang.time.DateUtils;
026    import org.apache.log4j.Logger;
027    import org.apache.struts.action.ActionForm;
028    import org.apache.struts.action.ActionForward;
029    import org.apache.struts.action.ActionMapping;
030    import org.hsqldb.lib.StringUtil;
031    import org.json.simple.JSONValue;
032    import org.kuali.hr.time.base.web.TkAction;
033    import org.kuali.hr.time.base.web.ApprovalForm;
034    import org.kuali.hr.time.person.TKPerson;
035    import org.kuali.hr.time.service.base.TkServiceLocator;
036    import org.kuali.hr.time.timesheet.TimesheetDocument;
037    import org.kuali.hr.time.timesummary.TimeSummary;
038    import org.kuali.hr.time.util.TKContext;
039    import org.kuali.hr.time.util.TKUtils;
040    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
041    
042    public class TimeApprovalWSAction extends TkAction {
043    
044        private static final Logger LOG = Logger.getLogger(TimeApprovalWSAction.class);
045    
046        /**
047         * Action called via AJAX. (ajaj really...)
048         * <p/>
049         * This search returns quick-results to the search box for the user to further
050         * refine upon. The end value can then be form submitted.
051         */
052        public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
053            TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
054            List<Map<String, String>> results = new LinkedList<Map<String, String>>();
055            if(StringUtils.isNotEmpty(taaf.getPayBeginDateForSearch()) 
056                            && StringUtils.isNotEmpty(taaf.getPayEndDateForSearch()) ) {
057                    Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayBeginDateForSearch());
058                    Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayEndDateForSearch());
059                //the endDate we get here is coming from approval.js and is extracted from html. we need to add a day to cover the last day in the pay period.
060                endDate = DateUtils.addDays(endDate,1);
061                List<String> workAreaList = new ArrayList<String>();
062                    if(StringUtil.isEmpty(taaf.getSelectedWorkArea())) {
063                    Set<Long> workAreas = TkServiceLocator.getTkRoleService().getWorkAreasForApprover(TKContext.getTargetPrincipalId(), TKUtils.getCurrentDate());
064                            for(Long workArea : workAreas) {    //taaf.getWorkAreaDescr().keySet()
065                                    workAreaList.add(workArea.toString());
066                            }
067                    } else {
068                            workAreaList.add(taaf.getSelectedWorkArea());
069                    }
070                    List<String> principalIds = TkServiceLocator.getTimeApproveService()
071                                    .getTimePrincipalIdsWithSearchCriteria(workAreaList, taaf.getSelectedPayCalendarGroup(),
072                                            new java.sql.Date(endDate.getTime()), new java.sql.Date(beginDate.getTime()), new java.sql.Date(endDate.getTime())); 
073                    
074                    List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
075                    
076                    if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_PRINCIPAL)) {
077                        for (String id : principalIds) {
078                            if(StringUtils.contains(id, taaf.getSearchTerm())) {
079                                Map<String, String> labelValue = new HashMap<String, String>();
080                                labelValue.put("id", id);
081                                labelValue.put("result", id);
082                                results.add(labelValue);
083                            }
084                        }
085                    } else if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_DOCID)) {
086                        Map<String, TimesheetDocumentHeader> principalDocumentHeaders =
087                                TkServiceLocator.getTimeApproveService().getPrincipalDocumehtHeader(persons, beginDate, endDate);
088    
089                        for (Map.Entry<String,TimesheetDocumentHeader> entry : principalDocumentHeaders.entrySet()) {
090                            if (StringUtils.contains(entry.getValue().getDocumentId(), taaf.getSearchTerm())) {
091                                Map<String, String> labelValue = new HashMap<String, String>();
092    //                        labelValue.put("id", entry.getValue().getDocumentId() + " (" + entry.getValue().getPrincipalId() + ")");
093                            labelValue.put("id", entry.getValue().getDocumentId());
094                                labelValue.put("result", entry.getValue().getPrincipalId());
095                                results.add(labelValue);
096                            }
097                        }
098                    }
099            }
100         
101            taaf.setOutputString(JSONValue.toJSONString(results));
102            return mapping.findForward("ws");
103        }
104        
105        public ActionForward getTimeSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
106            TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
107            TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(taaf.getDocumentId());
108                    TimeSummary ts = TkServiceLocator.getTimeSummaryService().getTimeSummary(td);
109                    
110            taaf.setOutputString(ts.toJsonString());
111            return mapping.findForward("ws");
112        }
113            
114    }