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