View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.hr.lm.approval.web;
17  
18  import java.text.SimpleDateFormat;
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.HashMap;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionForward;
32  import org.apache.struts.action.ActionMapping;
33  import org.hsqldb.lib.StringUtil;
34  import org.json.simple.JSONValue;
35  import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
36  import org.kuali.hr.time.base.web.TkAction;
37  import org.kuali.hr.time.base.web.ApprovalForm;
38  import org.kuali.hr.time.person.TKPerson;
39  import org.kuali.hr.time.service.base.TkServiceLocator;
40  
41  public class LeaveApprovalWSAction extends TkAction {
42  
43  	 public ActionForward getLeaveSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
44      	LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
45      	String docId = laaf.getDocumentId();
46      	LeaveCalendarDocumentHeader lcdh = TkServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(docId);
47      	if(lcdh != null) {
48      		List<Map<String, Object>> detailMap = TkServiceLocator.getLeaveApprovalService().getLeaveApprovalDetailSections(lcdh);
49      		
50      		String jsonString = JSONValue.toJSONString(detailMap);
51      		laaf.setOutputString(jsonString);
52      	}
53      	
54      	return mapping.findForward("ws");
55  	 }
56  	 
57  	  public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
58  		  LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
59  	        List<Map<String, String>> results = new LinkedList<Map<String, String>>();
60  	        if(StringUtils.isNotEmpty(laaf.getPayBeginDateForSearch()) 
61  	        		&& StringUtils.isNotEmpty(laaf.getPayEndDateForSearch()) ) {
62  		        Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayBeginDateForSearch());
63  		        Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayEndDateForSearch());
64  		        
65  		        List<String> workAreaList = new ArrayList<String>();
66  		        if(StringUtil.isEmpty(laaf.getSelectedWorkArea())) {
67  		        	for(Long aKey : laaf.getWorkAreaDescr().keySet()) {
68  		        		workAreaList.add(aKey.toString());
69  		        	}
70  		        } else {
71  		        	workAreaList.add(laaf.getSelectedWorkArea());
72  		        } 
73  		        List<String> principalIds = TkServiceLocator.getLeaveApprovalService()
74          			.getLeavePrincipalIdsWithSearchCriteria(workAreaList, laaf.getSelectedPayCalendarGroup(),
75          					new java.sql.Date(endDate.getTime()), new java.sql.Date(beginDate.getTime()), new java.sql.Date(endDate.getTime())); 
76  		        
77  		        List<TKPerson> persons = TkServiceLocator.getPersonService().getPersonCollection(principalIds);
78  		        
79  		        if (StringUtils.equals(laaf.getSearchField(), ApprovalForm.ORDER_BY_PRINCIPAL)) {
80  		            for (String id : principalIds) {
81  		                if(StringUtils.contains(id, laaf.getSearchTerm())) {
82  		                    Map<String, String> labelValue = new HashMap<String, String>();
83  		                    labelValue.put("id", id);
84  		                    labelValue.put("result", id);
85  		                    results.add(labelValue);
86  		                }
87  		            }
88  		        } else if (StringUtils.equals(laaf.getSearchField(), ApprovalForm.ORDER_BY_DOCID)) {
89  		            Map<String, LeaveCalendarDocumentHeader> principalDocumentHeaders =
90  		                    TkServiceLocator.getLeaveApprovalService().getPrincipalDocumehtHeader(persons, beginDate, endDate);
91  	
92  		            for (Map.Entry<String,LeaveCalendarDocumentHeader> entry : principalDocumentHeaders.entrySet()) {
93  		                if (StringUtils.contains(entry.getValue().getDocumentId(), laaf.getSearchTerm())) {
94  		                    Map<String, String> labelValue = new HashMap<String, String>();
95  		                    labelValue.put("id", entry.getValue().getDocumentId() + " (" + entry.getValue().getPrincipalId() + ")");
96  		                    labelValue.put("result", entry.getValue().getPrincipalId());
97  		                    results.add(labelValue);
98  		                }
99  		            }
100 		        }
101 	        }
102 		
103 	      laaf.setOutputString(JSONValue.toJSONString(results));
104 	        
105 	      return mapping.findForward("ws");
106 	    }
107 }