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