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.ArrayList;
020    import java.util.Date;
021    import java.util.HashMap;
022    import java.util.LinkedList;
023    import java.util.List;
024    import java.util.Map;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.apache.commons.lang.StringUtils;
030    import org.apache.log4j.Logger;
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionForward;
033    import org.apache.struts.action.ActionMapping;
034    import org.hsqldb.lib.StringUtil;
035    import org.json.simple.JSONValue;
036    import org.kuali.hr.time.base.web.TkAction;
037    import org.kuali.hr.time.base.web.ApprovalForm;
038    import org.kuali.hr.time.person.TKPerson;
039    import org.kuali.hr.time.service.base.TkServiceLocator;
040    import org.kuali.hr.time.timesheet.TimesheetDocument;
041    import org.kuali.hr.time.timesummary.TimeSummary;
042    import org.kuali.hr.time.workflow.TimesheetDocumentHeader;
043    
044    public class TimeApprovalWSAction extends TkAction {
045    
046        private static final Logger LOG = Logger.getLogger(TimeApprovalWSAction.class);
047    
048        /**
049         * Action called via AJAX. (ajaj really...)
050         * <p/>
051         * This search returns quick-results to the search box for the user to further
052         * refine upon. The end value can then be form submitted.
053         */
054        public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
055            TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
056            List<Map<String, String>> results = new LinkedList<Map<String, String>>();
057            if(StringUtils.isNotEmpty(taaf.getPayBeginDateForSearch()) 
058                            && StringUtils.isNotEmpty(taaf.getPayEndDateForSearch()) ) {
059                    Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayBeginDateForSearch());
060                    Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(taaf.getPayEndDateForSearch());
061                    
062                    List<String> workAreaList = new ArrayList<String>();
063                    if(StringUtil.isEmpty(taaf.getSelectedWorkArea())) {
064                            for(Long aKey : taaf.getWorkAreaDescr().keySet()) {
065                                    workAreaList.add(aKey.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("result", entry.getValue().getPrincipalId());
094                                results.add(labelValue);
095                            }
096                        }
097                    }
098            }
099         
100            taaf.setOutputString(JSONValue.toJSONString(results));
101            return mapping.findForward("ws");
102        }
103        
104        public ActionForward getTimeSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
105            TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
106            TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(taaf.getDocumentId());
107                    TimeSummary ts = TkServiceLocator.getTimeSummaryService().getTimeSummary(td);
108                    
109            taaf.setOutputString(ts.toJsonString());
110            return mapping.findForward("ws");
111        }
112            
113    }