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 org.apache.commons.lang.StringUtils;
19  import org.apache.commons.lang.time.DateUtils;
20  import org.apache.struts.action.ActionForm;
21  import org.apache.struts.action.ActionForward;
22  import org.apache.struts.action.ActionMapping;
23  import org.hsqldb.lib.StringUtil;
24  import org.json.simple.JSONValue;
25  import org.kuali.hr.lm.workflow.LeaveCalendarDocumentHeader;
26  import org.kuali.hr.time.base.web.ApprovalForm;
27  import org.kuali.hr.time.base.web.TkAction;
28  import org.kuali.hr.time.service.base.TkServiceLocator;
29  import org.kuali.hr.time.util.TKContext;
30  import org.kuali.hr.time.util.TKUtils;
31  
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import java.text.SimpleDateFormat;
35  import java.util.*;
36  
37  public class LeaveApprovalWSAction extends TkAction {
38  
39  	 public ActionForward getLeaveSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
40      	LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
41      	String docId = laaf.getDocumentId();
42      	LeaveCalendarDocumentHeader lcdh = TkServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(docId);
43      	if(lcdh != null) {
44      		List<Map<String, Object>> detailMap = TkServiceLocator.getLeaveApprovalService().getLeaveApprovalDetailSections(lcdh);
45      		
46      		String jsonString = JSONValue.toJSONString(detailMap);
47      		laaf.setOutputString(jsonString);
48      	}
49      	
50      	return mapping.findForward("ws");
51  	 }
52  	 
53  	  public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
54  		  LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
55  	        List<Map<String, String>> results = new LinkedList<Map<String, String>>();
56  	        if(StringUtils.isNotEmpty(laaf.getPayBeginDateForSearch()) 
57  	        		&& StringUtils.isNotEmpty(laaf.getPayEndDateForSearch()) ) {
58  		        Date beginDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayBeginDateForSearch());
59  		        Date endDate = new SimpleDateFormat("MM/dd/yyyy").parse(laaf.getPayEndDateForSearch());
60                  //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.
61                  endDate = DateUtils.addDays(endDate,1);
62                  List<String> workAreaList = new ArrayList<String>();
63  		        if(StringUtil.isEmpty(laaf.getSelectedWorkArea())) {
64                      Set<Long> workAreas = TkServiceLocator.getTkRoleService().getWorkAreasForApprover(TKContext.getPrincipalId(), TKUtils.getCurrentDate());
65  		        	for(Long workArea : workAreas) {                             //laaf.getWorkAreaDescr().keySet()
66  		        		workAreaList.add(workArea.toString());
67  		        	}
68  		        } else {
69  		        	workAreaList.add(laaf.getSelectedWorkArea());
70  		        } 
71  		        List<String> principalIds = TkServiceLocator.getLeaveApprovalService()
72          			.getLeavePrincipalIdsWithSearchCriteria(workAreaList, laaf.getSelectedPayCalendarGroup(),
73          					new java.sql.Date(endDate.getTime()), new java.sql.Date(beginDate.getTime()), new java.sql.Date(endDate.getTime())); 
74  		        
75  		        if (StringUtils.equals(laaf.getSearchField(), ApprovalForm.ORDER_BY_PRINCIPAL)) {
76  		            for (String id : principalIds) {
77  		                if(StringUtils.contains(id, laaf.getSearchTerm())) {
78  		                    Map<String, String> labelValue = new HashMap<String, String>();
79  		                    labelValue.put("id", id);
80  		                    labelValue.put("result", id);
81  		                    results.add(labelValue);
82  		                }
83  		            }
84  		        } else if (StringUtils.equals(laaf.getSearchField(), ApprovalForm.ORDER_BY_DOCID)) {
85  		            Map<String, LeaveCalendarDocumentHeader> principalDocumentHeaders =
86  		                    TkServiceLocator.getLeaveApprovalService().getPrincipalDocumentHeader(principalIds, beginDate, endDate);
87  	
88  		            for (Map.Entry<String,LeaveCalendarDocumentHeader> entry : principalDocumentHeaders.entrySet()) {
89  		                if (StringUtils.contains(entry.getValue().getDocumentId(), laaf.getSearchTerm())) {
90  		                    Map<String, String> labelValue = new HashMap<String, String>();
91  //                            labelValue.put("id", entry.getValue().getDocumentId() + " (" + entry.getValue().getPrincipalId() + ")");
92                              //removing principalId to make select/submit the result from dropdown work
93                              labelValue.put("id", entry.getValue().getDocumentId());
94  		                    labelValue.put("result", entry.getValue().getPrincipalId());
95  		                    results.add(labelValue);
96  		                }
97  		            }
98  		        }
99  	        }
100 		
101 	      laaf.setOutputString(JSONValue.toJSONString(results));
102 	        
103 	      return mapping.findForward("ws");
104 	    }
105 }