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.lm.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.struts.action.ActionForm;
027    import org.apache.struts.action.ActionForward;
028    import org.apache.struts.action.ActionMapping;
029    import org.hsqldb.lib.StringUtil;
030    import org.json.simple.JSONValue;
031    import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
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.util.TKContext;
037    import org.kuali.hr.time.util.TKUtils;
038    
039    public class LeaveApprovalWSAction extends TkAction {
040    
041             public ActionForward getLeaveSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
042            LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
043            String docId = laaf.getDocumentId();
044            LeaveCalendarDocumentHeader lcdh = TkServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(docId);
045            if(lcdh != null) {
046                    List<Map<String, Object>> detailMap = TkServiceLocator.getLeaveApprovalService().getLeaveApprovalDetailSections(lcdh);
047                    
048                    String jsonString = JSONValue.toJSONString(detailMap);
049                    laaf.setOutputString(jsonString);
050            }
051            
052            return mapping.findForward("ws");
053             }
054             
055              public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
056                      LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
057                    List<Map<String, String>> results = new LinkedList<Map<String, String>>();
058                    if(StringUtils.isNotEmpty(laaf.getPayBeginDateForSearch()) 
059                                    && StringUtils.isNotEmpty(laaf.getPayEndDateForSearch()) ) {
060                            Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayBeginDateForSearch());
061                            Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayEndDateForSearch());
062                    //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.
063                    endDate = DateUtils.addDays(endDate,1);
064                    List<String> workAreaList = new ArrayList<String>();
065                            if(StringUtil.isEmpty(laaf.getSelectedWorkArea())) {
066                        Set<Long> workAreas = TkServiceLocator.getTkRoleService().getWorkAreasForApprover(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
067                                    for(Long workArea : workAreas) {                             //laaf.getWorkAreaDescr().keySet()
068                                            workAreaList.add(workArea.toString());
069                                    }
070                            } else {
071                                    workAreaList.add(laaf.getSelectedWorkArea());
072                            } 
073                            List<String> principalIds = TkServiceLocator.getLeaveApprovalService()
074                                    .getLeavePrincipalIdsWithSearchCriteria(workAreaList, laaf.getSelectedPayCalendarGroup(),
075                                                    new java.sql.Date(endDate.getTime()), new java.sql.Date(beginDate.getTime()), new java.sql.Date(endDate.getTime())); 
076                            
077                            List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
078                            
079                            if (StringUtils.equals(laaf.getSearchField(), ApprovalForm.ORDER_BY_PRINCIPAL)) {
080                                for (String id : principalIds) {
081                                    if(StringUtils.contains(id, laaf.getSearchTerm())) {
082                                        Map<String, String> labelValue = new HashMap<String, String>();
083                                        labelValue.put("id", id);
084                                        labelValue.put("result", id);
085                                        results.add(labelValue);
086                                    }
087                                }
088                            } else if (StringUtils.equals(laaf.getSearchField(), ApprovalForm.ORDER_BY_DOCID)) {
089                                Map<String, LeaveCalendarDocumentHeader> principalDocumentHeaders =
090                                        TkServiceLocator.getLeaveApprovalService().getPrincipalDocumehtHeader(persons, beginDate, endDate);
091            
092                                for (Map.Entry<String,LeaveCalendarDocumentHeader> entry : principalDocumentHeaders.entrySet()) {
093                                    if (StringUtils.contains(entry.getValue().getDocumentId(), laaf.getSearchTerm())) {
094                                        Map<String, String> labelValue = new HashMap<String, String>();
095    //                            labelValue.put("id", entry.getValue().getDocumentId() + " (" + entry.getValue().getPrincipalId() + ")");
096                                //removing principalId to make select/submit the result from dropdown work
097                                labelValue.put("id", entry.getValue().getDocumentId());
098                                        labelValue.put("result", entry.getValue().getPrincipalId());
099                                        results.add(labelValue);
100                                    }
101                                }
102                            }
103                    }
104                    
105                  laaf.setOutputString(JSONValue.toJSONString(results));
106                    
107                  return mapping.findForward("ws");
108                }
109    }