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.block.web;
17  
18  import org.apache.commons.lang.ObjectUtils;
19  import org.apache.commons.lang.StringUtils;
20  import org.apache.struts.action.ActionForm;
21  import org.apache.struts.action.ActionForward;
22  import org.apache.struts.action.ActionMapping;
23  import org.joda.time.LocalDate;
24  import org.kuali.kpme.core.api.accrualcategory.AccrualCategory;
25  import org.kuali.kpme.core.principal.PrincipalHRAttributesBo;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.core.util.HrConstants;
28  import org.kuali.kpme.core.util.HrContext;
29  import org.kuali.kpme.core.web.KPMEAction;
30  import org.kuali.kpme.tklm.api.leave.block.LeaveBlock;
31  import org.kuali.kpme.tklm.api.leave.summary.LeaveSummaryContract;
32  import org.kuali.kpme.tklm.api.leave.summary.LeaveSummaryRowContract;
33  import org.kuali.kpme.tklm.common.LMConstants;
34  import org.kuali.kpme.tklm.leave.block.LeaveBlockBo;
35  import org.kuali.kpme.tklm.leave.block.LeaveBlockHistory;
36  import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
37  import org.kuali.kpme.tklm.leave.workflow.LeaveCalendarDocumentHeader;
38  import org.kuali.rice.kew.api.KewApiConstants;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import java.math.BigDecimal;
43  import java.util.*;
44  
45  public class LeaveBlockDisplayAction extends KPMEAction {
46  
47  	@Override
48  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
49  		ActionForward forward = super.execute(mapping, form, request, response);
50  		
51  		LeaveBlockDisplayForm lbdf = (LeaveBlockDisplayForm) form;	
52  
53  		PrincipalHRAttributesBo principalHRAttributes = PrincipalHRAttributesBo.from(HrServiceLocator.getPrincipalHRAttributeService().getPrincipalCalendar(HrContext.getTargetPrincipalId(), LocalDate.now()));
54  		String leavePlan = (principalHRAttributes != null) ? principalHRAttributes.getLeavePlan() : null;
55  
56  		if (lbdf.getNavString() == null) {
57              if (lbdf.getYear() == 0) {
58  			    lbdf.setYear(LocalDate.now().getYear());
59              }
60  		} else if(lbdf.getNavString().equals("NEXT")) {
61  			lbdf.setYear(lbdf.getYear() + 1);
62  		} else if(lbdf.getNavString().equals("PREV")) {
63  			lbdf.setYear(lbdf.getYear() - 1);
64  		}
65  		LocalDate beginDate = new LocalDate(lbdf.getYear(), 1, 1);
66  		LocalDate serviceDate = (principalHRAttributes != null) ? principalHRAttributes.getServiceLocalDate() : beginDate;
67  		LocalDate endDate = new LocalDate(lbdf.getYear(), 12, 31);
68  
69  		lbdf.setAccrualCategories(getAccrualCategories(leavePlan));
70  		lbdf.setLeaveEntries(getLeaveEntries(HrContext.getTargetPrincipalId(), serviceDate, beginDate, endDate, lbdf.getAccrualCategories()));
71  
72  		List<LeaveBlockHistory> correctedLeaveEntries = LmServiceLocator.getLeaveBlockHistoryService().getLeaveBlockHistoriesForLeaveDisplay(HrContext.getTargetPrincipalId(), beginDate, endDate, Boolean.TRUE);
73  		if (correctedLeaveEntries != null) {
74  			for (LeaveBlockHistory leaveBlockHistory : correctedLeaveEntries) {
75  				if (leaveBlockHistory.getAction() != null && leaveBlockHistory.getAction().equalsIgnoreCase(HrConstants.ACTION.DELETE)) {
76  					leaveBlockHistory.setPrincipalIdModified(leaveBlockHistory.getPrincipalIdDeleted());
77  					leaveBlockHistory.setTimestamp(leaveBlockHistory.getTimestampDeleted());
78  				}
79  				// Set Description
80  				if(leaveBlockHistory.getDescription() == null || leaveBlockHistory.getDescription().trim().isEmpty()) {
81  					leaveBlockHistory.setDescription(this.retrieveDescriptionAccordingToLeaveType(leaveBlockHistory.getLeaveBlockType()));
82  				}
83  			}
84  		}
85  		lbdf.setCorrectedLeaveEntries(correctedLeaveEntries);
86  		
87  		List<LeaveBlockHistory> inActiveLeaveEntries = LmServiceLocator.getLeaveBlockHistoryService() .getLeaveBlockHistoriesForLeaveDisplay(HrContext.getTargetPrincipalId(), beginDate, endDate, Boolean.FALSE);
88  		List<LeaveBlockHistory> leaveEntries = null;
89  		if (inActiveLeaveEntries != null) {
90  			leaveEntries = new ArrayList<LeaveBlockHistory>();
91  			for (LeaveBlockHistory leaveBlockHistory:inActiveLeaveEntries) {
92  				if (leaveBlockHistory.isAccrualGenerated() == null || !leaveBlockHistory.isAccrualGenerated()) {
93  					if (leaveBlockHistory.getAction()!= null) {
94                          if (leaveBlockHistory.getAction().equalsIgnoreCase(HrConstants.ACTION.DELETE)) {
95                              leaveBlockHistory.setPrincipalIdModified(leaveBlockHistory.getPrincipalIdDeleted());
96                              leaveBlockHistory.setTimestamp(leaveBlockHistory.getTimestampDeleted());
97                          } else if (leaveBlockHistory.getAction().equalsIgnoreCase(HrConstants.ACTION.MODIFIED)) {
98                              leaveBlockHistory.setPrincipalIdModified(leaveBlockHistory.getPrincipalIdDeleted());
99                          }
100                     }
101 
102 					this.assignDocumentStatusToLeaveBlock(leaveBlockHistory);
103 					// if it is not generated by accrual then add it to the inactivelist
104 					// Set Description
105 					if(leaveBlockHistory.getDescription() == null || leaveBlockHistory.getDescription().trim().isEmpty()) {
106 						leaveBlockHistory.setDescription(this.retrieveDescriptionAccordingToLeaveType(leaveBlockHistory.getLeaveBlockType()));
107 					}
108 					if (StringUtils.isNotBlank(leaveBlockHistory.getRequestStatus())) {
109 						leaveBlockHistory.setRequestStatus(HrConstants.REQUEST_STATUS_STRINGS.get(leaveBlockHistory.getRequestStatus()));
110 					}
111 					leaveEntries.add(leaveBlockHistory);
112 				}
113 			}
114 		}
115 		lbdf.setInActiveLeaveEntries(leaveEntries);
116 		
117 	    return forward;
118 	}
119 	
120 	private List<AccrualCategory> getAccrualCategories(String leavePlan) {
121 		List<AccrualCategory> accrualCategories = new ArrayList<AccrualCategory>();
122 		
123 		List<AccrualCategory> allAccrualCategories = HrServiceLocator.getAccrualCategoryService().getActiveAccrualCategoriesForLeavePlan(leavePlan, LocalDate.now());
124 		if (allAccrualCategories != null) {
125 			for (AccrualCategory ac : allAccrualCategories) {
126 				if (StringUtils.equalsIgnoreCase(ac.getShowOnGrid(), "Y")) {
127 					accrualCategories.add(ac);
128 				}
129 			}
130 			Collections.sort(accrualCategories, new Comparator<AccrualCategory>() {
131 				@Override
132 				public int compare(AccrualCategory o1, AccrualCategory o2) {
133 					return ObjectUtils.compare(o1.getAccrualCategory(), o2.getAccrualCategory());
134 				}
135 			});
136 		}
137 		
138 		return accrualCategories;
139 	}
140 	
141 	private List<LeaveBlockDisplay> getLeaveEntries(String principalId, LocalDate serviceDate, LocalDate beginDate, LocalDate endDate, List<AccrualCategory> accrualCategories) {
142 		List<LeaveBlockDisplay> leaveEntries = new ArrayList<LeaveBlockDisplay>();
143 		
144 		List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, beginDate, endDate);
145 		
146 		for (LeaveBlock leaveBlock : leaveBlocks) {
147             if (!leaveBlock.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER)) {
148 			    leaveEntries.add(new LeaveBlockDisplay(leaveBlock));
149             }
150 		}
151 		Collections.sort(leaveEntries, new Comparator<LeaveBlockDisplay>() {
152 			@Override
153 			public int compare(LeaveBlockDisplay o1, LeaveBlockDisplay o2) {
154 				return ObjectUtils.compare(o1.getLeaveDate(), o2.getLeaveDate());
155 			}
156 		});
157 		
158 		SortedMap<String, BigDecimal> accrualBalances = getPreviousAccrualBalances(principalId, serviceDate, beginDate, accrualCategories);
159 				
160 		for (LeaveBlockDisplay leaveEntry : leaveEntries) {
161 			for (AccrualCategory accrualCategory : accrualCategories) {
162 				if (!accrualBalances.containsKey(accrualCategory.getAccrualCategory())) {
163 					accrualBalances.put(accrualCategory.getAccrualCategory(), BigDecimal.ZERO);
164 				}
165 				BigDecimal currentAccrualBalance = accrualBalances.get(accrualCategory.getAccrualCategory());
166 				
167 				if (StringUtils.equals(leaveEntry.getAccrualCategory(), accrualCategory.getAccrualCategory())) {
168 					BigDecimal accruedBalance = currentAccrualBalance.add(leaveEntry.getLeaveAmount());
169 					accrualBalances.put(accrualCategory.getAccrualCategory(), accruedBalance);
170 				}
171 				
172 				leaveEntry.setAccrualBalance(accrualCategory.getAccrualCategory(), accrualBalances.get(accrualCategory.getAccrualCategory()));
173 			}
174 		}
175 		
176 		return leaveEntries;
177 	}
178 	
179 	private SortedMap<String, BigDecimal> getPreviousAccrualBalances(String principalId, LocalDate serviceDate, LocalDate beginDate, List<AccrualCategory> accrualCategories) {
180 		SortedMap<String, BigDecimal> previousAccrualBalances = new TreeMap<String, BigDecimal>();
181 
182         LeaveSummaryContract leaveSummary = LmServiceLocator.getLeaveSummaryService().getLeaveSummaryAsOfDateWithoutFuture(principalId, beginDate);
183 
184         for (LeaveSummaryRowContract row : leaveSummary.getLeaveSummaryRows()) {
185             previousAccrualBalances.put(row.getAccrualCategory(), row.getLeaveBalance());
186         }
187 		
188 		return previousAccrualBalances;
189 	}
190 
191 	private void assignDocumentStatusToLeaveBlock(LeaveBlockBo leaveBlock) {
192 		//lookup document associated with this leave block and assign document status
193 		if(StringUtils.isNotEmpty(leaveBlock.getDocumentId())) {
194 			LeaveCalendarDocumentHeader lcdh = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(leaveBlock.getDocumentId());
195 			if(lcdh != null ) {
196 				leaveBlock.setDocumentStatus(KewApiConstants.DOCUMENT_STATUSES.get(lcdh.getDocumentStatus()));
197 			}
198 		}
199 	}
200 	
201 	private String retrieveDescriptionAccordingToLeaveType(String leaveType) {
202 		String description = null;
203 		description = LMConstants.LEAVE_BLOCK_TYPE_MAP.get(leaveType);
204 		return description;
205 	}
206 }