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.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.struts.action.ActionForm;
031    import org.apache.struts.action.ActionForward;
032    import org.apache.struts.action.ActionMapping;
033    import org.hsqldb.lib.StringUtil;
034    import org.json.simple.JSONValue;
035    import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
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    
041    public class LeaveApprovalWSAction extends TkAction {
042    
043             public ActionForward getLeaveSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
044            LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
045            String docId = laaf.getDocumentId();
046            LeaveCalendarDocumentHeader lcdh = TkServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(docId);
047            if(lcdh != null) {
048                    List<Map<String, Object>> detailMap = TkServiceLocator.getLeaveApprovalService().getLeaveApprovalDetailSections(lcdh);
049                    
050                    String jsonString = JSONValue.toJSONString(detailMap);
051                    laaf.setOutputString(jsonString);
052            }
053            
054            return mapping.findForward("ws");
055             }
056             
057              public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
058                      LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
059                    List<Map<String, String>> results = new LinkedList<Map<String, String>>();
060                    if(StringUtils.isNotEmpty(laaf.getPayBeginDateForSearch()) 
061                                    && StringUtils.isNotEmpty(laaf.getPayEndDateForSearch()) ) {
062                            Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayBeginDateForSearch());
063                            Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayEndDateForSearch());
064                            
065                            List<String> workAreaList = new ArrayList<String>();
066                            if(StringUtil.isEmpty(laaf.getSelectedWorkArea())) {
067                                    for(Long aKey : laaf.getWorkAreaDescr().keySet()) {
068                                            workAreaList.add(aKey.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                                        labelValue.put("result", entry.getValue().getPrincipalId());
097                                        results.add(labelValue);
098                                    }
099                                }
100                            }
101                    }
102                    
103                  laaf.setOutputString(JSONValue.toJSONString(results));
104                    
105                  return mapping.findForward("ws");
106                }
107    }