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