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