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.time.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.collections.MapUtils;
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  import org.hsqldb.lib.StringUtil;
36  import org.joda.time.DateTime;
37  import org.joda.time.LocalDate;
38  import org.json.simple.JSONValue;
39  import org.kuali.kpme.core.KPMENamespace;
40  import org.kuali.kpme.core.assignment.Assignment;
41  import org.kuali.kpme.core.calendar.entry.CalendarEntry;
42  import org.kuali.kpme.core.role.KPMERole;
43  import org.kuali.kpme.core.service.HrServiceLocator;
44  import org.kuali.kpme.core.util.HrContext;
45  import org.kuali.kpme.core.web.KPMEAction;
46  import org.kuali.kpme.tklm.common.CalendarApprovalForm;
47  import org.kuali.kpme.tklm.leave.block.LeaveBlock;
48  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
49  import org.kuali.kpme.tklm.time.detail.web.ActionFormUtils;
50  import org.kuali.kpme.tklm.time.service.TkServiceLocator;
51  import org.kuali.kpme.tklm.time.timeblock.TimeBlock;
52  import org.kuali.kpme.tklm.time.timesheet.TimesheetDocument;
53  import org.kuali.kpme.tklm.time.timesummary.AssignmentColumn;
54  import org.kuali.kpme.tklm.time.timesummary.AssignmentRow;
55  import org.kuali.kpme.tklm.time.timesummary.EarnCodeSection;
56  import org.kuali.kpme.tklm.time.timesummary.EarnGroupSection;
57  import org.kuali.kpme.tklm.time.timesummary.TimeSummary;
58  import org.kuali.kpme.tklm.time.workflow.TimesheetDocumentHeader;
59  
60  import com.google.common.collect.Lists;
61  import org.kuali.rice.kim.api.role.RoleService;
62  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
63  
64  public class TimeApprovalWSAction extends KPMEAction {
65  
66      /**
67       * Action called via AJAX. (ajaj really...)
68       * <p/>
69       * This search returns quick-results to the search box for the user to further
70       * refine upon. The end value can then be form submitted.
71       */
72      public ActionForward searchApprovalRows(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
73          TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
74          List<Map<String, String>> results = new LinkedList<Map<String, String>>();
75  
76          List<String> workAreaList = new ArrayList<String>();
77          if(StringUtil.isEmpty(taaf.getSelectedWorkArea())) {
78          	String principalId = HrContext.getTargetPrincipalId();
79          	
80          	Set<Long> workAreas = new HashSet<Long>();
81              RoleService roleService = KimApiServiceLocator.getRoleService();
82              List<String> roleIds = new ArrayList<String>();
83              roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.REVIEWER.getRoleName()));
84              roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER_DELEGATE.getRoleName()));
85              roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.APPROVER.getRoleName()));
86              roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR.getRoleName()));
87              roleIds.add(roleService.getRoleIdByNamespaceCodeAndName(KPMENamespace.KPME_HR.getNamespaceCode(), KPMERole.PAYROLL_PROCESSOR_DELEGATE.getRoleName()));
88  
89              workAreas.addAll(HrServiceLocator.getKPMERoleService().getWorkAreasForPrincipalInRoles(principalId, roleIds, LocalDate.now().toDateTimeAtStartOfDay(), true));
90          	for(Long workArea : workAreas) {
91          		workAreaList.add(workArea.toString());
92          	}
93          } else {
94          	workAreaList.add(taaf.getSelectedWorkArea());
95          }
96          
97          if(StringUtils.isNotBlank(taaf.getSelectedPayPeriod())) {
98          	CalendarEntry currentCE = HrServiceLocator.getCalendarEntryService().getCalendarEntry(taaf.getSelectedPayPeriod());
99          	if(currentCE != null) {
100         		 LocalDate endDate = currentCE.getEndPeriodFullDateTime().toLocalDate();
101         	     LocalDate beginDate = currentCE.getBeginPeriodFullDateTime().toLocalDate();
102         	     List<String> principalIds = TkServiceLocator.getTimeApproveService()
103         	 			.getTimePrincipalIdsWithSearchCriteria(workAreaList, taaf.getSelectedPayCalendarGroup(),
104         	 					endDate, beginDate, endDate);        	         
105         	     Collections.sort(principalIds);
106         	     
107     	         if (StringUtils.equals(taaf.getSearchField(), CalendarApprovalForm.ORDER_BY_PRINCIPAL)) {
108     	             for (String id : principalIds) {
109     	                 if(StringUtils.contains(id, taaf.getSearchTerm())) {
110     	                     Map<String, String> labelValue = new HashMap<String, String>();
111     	                     labelValue.put("id", id);
112     	                     labelValue.put("result", id);
113     	                     results.add(labelValue);
114     	                 }
115     	             }
116     	         } else if (StringUtils.equals(taaf.getSearchField(), CalendarApprovalForm.ORDER_BY_DOCID)) {
117     	             Map<String, TimesheetDocumentHeader> principalDocumentHeaders =
118     	             		TkServiceLocator.getTimeApproveService().getPrincipalDocumentHeader(principalIds, beginDate.toDateTimeAtStartOfDay(), endDate.toDateTimeAtStartOfDay(), "");
119     	             List<String> docIdList = new ArrayList<String>();
120     	             
121     	             for (Map.Entry<String,TimesheetDocumentHeader> entry : principalDocumentHeaders.entrySet()) {
122     	                 if (StringUtils.contains(entry.getValue().getDocumentId(), taaf.getSearchTerm())) {
123     	                	 docIdList.add(entry.getValue().getDocumentId());    	                    
124     	                 }
125     	             }
126     	             Collections.sort(docIdList);					  
127 					 for(String aString : docIdList) {
128 	    	             Map<String, String> labelValue = new HashMap<String, String>();
129 	                     labelValue.put("id", aString);
130 	                     labelValue.put("result", aString);
131 	                     results.add(labelValue);
132 					 }
133     	         }        	     
134         	}
135         }
136       
137         taaf.setOutputString(JSONValue.toJSONString(results));
138         return mapping.findForward("ws");
139     }
140     
141     public ActionForward getTimeSummary(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
142         TimeApprovalActionForm taaf = (TimeApprovalActionForm) form;
143         TimesheetDocument td = TkServiceLocator.getTimesheetService().getTimesheetDocument(taaf.getDocumentId());
144 		TimeSummary ts = TkServiceLocator.getTimeSummaryService().getTimeSummary(td);
145 		
146 		List<Assignment> assignments = td.getAssignments();
147 		List<String> assignmentKeys = new ArrayList<String>();
148 		for(Assignment assignment : assignments) {
149 			assignmentKeys.add(assignment.getAssignmentKey());
150 		}
151 		List<TimeBlock> timeBlocks = td.getTimeBlocks();
152 		List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksForTimeCalendar(td.getPrincipalId(), td.getAsOfDate(), td.getDocEndDate(), assignmentKeys);
153 		Map<String, String> aMap = ActionFormUtils.buildAssignmentStyleClassMap(timeBlocks, leaveBlocks);
154 		// set css classes for each assignment row
155 		for (EarnGroupSection earnGroupSection : ts.getSections()) {
156 			for (EarnCodeSection section : earnGroupSection.getEarnCodeSections()) {
157 				for (AssignmentRow assignRow : section.getAssignmentsRows()) {
158 					String assignmentCssStyle = MapUtils.getString(aMap, assignRow.getAssignmentKey());
159 					assignRow.setCssClass(assignmentCssStyle);
160 					for (AssignmentColumn assignmentColumn : assignRow.getAssignmentColumns().values()) {
161 						assignmentColumn.setCssClass(assignmentCssStyle);
162 					}
163 				}
164 			}
165 		}
166 	 	
167 		//reverse sections for javascripts $parent.after (always inserting directly after parent element, which reverses order)
168 
169 		ts.setSections(Lists.reverse(ts.getSections()));
170         taaf.setOutputString(ts.toJsonString());
171         return mapping.findForward("ws");
172     }
173         
174 }