View Javadoc

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