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.Date;
020    import java.util.HashMap;
021    import java.util.LinkedList;
022    import java.util.List;
023    import java.util.Map;
024    
025    import javax.servlet.http.HttpServletRequest;
026    import javax.servlet.http.HttpServletResponse;
027    
028    import org.apache.commons.lang.StringUtils;
029    import org.apache.log4j.Logger;
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionForward;
032    import org.apache.struts.action.ActionMapping;
033    import org.json.simple.JSONValue;
034    import org.kuali.hr.time.base.web.TkAction;
035    import org.kuali.hr.time.base.web.ApprovalForm;
036    import org.kuali.hr.time.person.TKPerson;
037    import org.kuali.hr.time.service.base.TkServiceLocator;
038    import org.kuali.hr.time.timesheet.TimesheetDocument;
039    import org.kuali.hr.time.timesummary.TimeSummary;
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                    
060                    List<String> principalIds = TkServiceLocator.getTimeApproveService().getPrincipalIdsByDeptWorkAreaRolename(taaf.getRoleName(), taaf.getSelectedDept(), taaf.getSelectedWorkArea(), new java.sql.Date(beginDate.getTime()), new java.sql.Date(endDate.getTime()), taaf.getSelectedPayCalendarGroup());
061                    List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
062                    
063                    if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_PRINCIPAL)) {
064                        for (String id : principalIds) {
065                            if(StringUtils.contains(id, taaf.getSearchTerm())) {
066                                Map<String, String> labelValue = new HashMap<String, String>();
067                                labelValue.put("id", id);
068                                labelValue.put("result", id);
069                                results.add(labelValue);
070                            }
071                        }
072                    } else if (StringUtils.equals(taaf.getSearchField(), ApprovalForm.ORDER_BY_DOCID)) {
073                        Map<String, TimesheetDocumentHeader> principalDocumentHeaders =
074                                TkServiceLocator.getTimeApproveService().getPrincipalDocumehtHeader(persons, beginDate, endDate);
075            
076                        for (Map.Entry<String,TimesheetDocumentHeader> entry : principalDocumentHeaders.entrySet()) {
077                            if (StringUtils.contains(entry.getValue().getDocumentId(), taaf.getSearchTerm())) {
078                                Map<String, String> labelValue = new HashMap<String, String>();
079                                labelValue.put("id", entry.getValue().getDocumentId() + " (" + entry.getValue().getPrincipalId() + ")");
080                                labelValue.put("result", entry.getValue().getPrincipalId());
081                                results.add(labelValue);
082                            }
083                        }
084                    }
085            }
086         
087            taaf.setOutputString(JSONValue.toJSONString(results));
088            return mapping.findForward("ws");
089        }
090        
091        public ActionForward getTimeSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
092            TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
093            TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(taaf.getDocumentId());
094                    TimeSummary ts = TkServiceLocator.getTimeSummaryService().getTimeSummary(td);
095                    
096            taaf.setOutputString(ts.toJsonString());
097            return mapping.findForward("ws");
098        }
099            
100    }