View Javadoc
1   /**
2    * Copyright 2004-2014 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.kpme.tklm.leave.approval.web;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashMap;
21  import java.util.HashSet;
22  import java.util.LinkedList;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.commons.lang.StringUtils;
31  import org.apache.struts.action.ActionForm;
32  import org.apache.struts.action.ActionForward;
33  import org.apache.struts.action.ActionMapping;
34  import org.hsqldb.lib.StringUtil;
35  import org.joda.time.DateTime;
36  import org.joda.time.LocalDate;
37  import org.json.simple.JSONValue;
38  import org.kuali.kpme.core.KPMENamespace;
39  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
40  import org.kuali.kpme.core.role.KPMERole;
41  import org.kuali.kpme.core.service.HrServiceLocator;
42  import org.kuali.kpme.core.web.KPMEAction;
43  import org.kuali.kpme.tklm.common.CalendarApprovalForm;
44  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
45  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
46  import org.kuali.rice.kim.api.role.RoleService;
47  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
48  import org.kuali.rice.krad.util.GlobalVariables;
49  
50  public class LeaveApprovalWSAction extends KPMEAction {
51  
52  	 public ActionForward getLeaveSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
53      	LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
54      	String docId = laaf.getDocumentId();
55      	LeaveCalendarDocumentHeader lcdh = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(docId);
56      	if(lcdh != null) {
57      		List<Map<String, Object>> detailMap = LmServiceLocator.getLeaveApprovalService().getLeaveApprovalDetailSections(lcdh);
58      		
59      		String jsonString = JSONValue.toJSONString(detailMap);
60      		laaf.setOutputString(jsonString);
61      	}
62      	
63      	return mapping.findForward("ws");
64  	 }
65  	 
66  	  public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
67  		  LeaveApprovalWSActionForm laaf = (LeaveApprovalWSActionForm) form;
68  		  List<Map<String, String>> results = new LinkedList<Map<String, String>>();
69  
70  		  List<String> workAreaList = new ArrayList<String>();
71  		  if (StringUtil.isEmpty(laaf.getSelectedWorkArea())) {
72  			  String principalId = GlobalVariables.getUserSession().getPrincipalId();
73  			
74  			  Set<Long> workAreas = new HashSet<Long>();
75                List<String> roleIds = new ArrayList<String>();
76                RoleService roleService = KimApiServiceLocator.getRoleService();
77                roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName()));
78                roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName()));
79                roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName()));
80                roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName()));
81                roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName()));
82                workAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRoles(principalId, roleIds, LocalDate.now().toDateTimeAtStartOfDay(), true));
83  
84  			  for(Long workArea : workAreas) {
85  				  workAreaList.add(workArea.toString());
86  			  }
87  		  } else {
88  			  workAreaList.add(laaf.getSelectedWorkArea());
89  		  }
90  		  
91  	        if(StringUtils.isNotBlank(laaf.getSelectedPayPeriod())) {
92  	        	CalendarEntry currentCE = HrServiceLocator.getCalendarEntryService().getCalendarEntry(laaf.getSelectedPayPeriod());
93  	        	if(currentCE != null) {
94  				  LocalDate endDate = currentCE.getEndPeriodFullDateTime().toLocalDate();
95  				  LocalDate beginDate = currentCE.getBeginPeriodFullDateTime().toLocalDate();
96  			        
97  				  List<String> principalIds = LmServiceLocator.getLeaveApprovalService()
98  						  .getLeavePrincipalIdsWithSearchCriteria(workAreaList, laaf.getSelectedPayCalendarGroup(),
99  								  endDate, beginDate, endDate); 
100 				  
101 				  Collections.sort(principalIds);
102 				  
103 				  if (StringUtils.equals(laaf.getSearchField(), CalendarApprovalForm.ORDER_BY_PRINCIPAL)) {
104 					  for (String id : principalIds) {
105 						  if(StringUtils.contains(id, laaf.getSearchTerm())) {
106 							  Map<String, String> labelValue = new HashMap<String, String>();
107 							  labelValue.put("id", id);
108 							  labelValue.put("result", id);
109 							  results.add(labelValue);
110 						  }
111 					  }
112 				  } else if (StringUtils.equals(laaf.getSearchField(), CalendarApprovalForm.ORDER_BY_DOCID)) {
113 					  Map<String, LeaveCalendarDocumentHeader> principalDocumentHeaders =
114 							  LmServiceLocator.getLeaveApprovalService().getPrincipalDocumentHeader(principalIds, beginDate.toDateTimeAtStartOfDay(), endDate.toDateTimeAtStartOfDay());
115 					  List<String> docIdList = new ArrayList<String>();
116 					  
117 					  for (Map.Entry<String,LeaveCalendarDocumentHeader> entry : principalDocumentHeaders.entrySet()) {						 
118 						  if (StringUtils.contains(entry.getValue().getDocumentId(), laaf.getSearchTerm())) {
119 							  docIdList.add(entry.getValue().getDocumentId());							  
120 						  }
121 					  }
122 					  
123 					  Collections.sort(docIdList);
124 					  
125 					  for(String aString : docIdList) {
126 						  Map<String, String> labelValue = new HashMap<String, String>();
127 						  labelValue.put("id", aString);
128 						  labelValue.put("result", aString);
129 						  results.add(labelValue);
130 					  }
131 					  
132 					  
133 				  }
134 	        	}
135 	        }
136 		
137 	      laaf.setOutputString(JSONValue.toJSONString(results));
138 	        
139 	      return mapping.findForward("ws");
140 	    }
141 }