1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 lbdf.setYear(LocalDate.now().getYear());
63 } else if(lbdf.getNavString().equals("NEXT")) {
64 lbdf.setYear(lbdf.getYear() + 1);
65 } else if(lbdf.getNavString().equals("PREV")) {
66 lbdf.setYear(lbdf.getYear() - 1);
67 }
68 LocalDate beginDate = new LocalDate(lbdf.getYear(), 1, 1);
69 LocalDate serviceDate = (principalHRAttributes != null) ? principalHRAttributes.getServiceLocalDate() : beginDate;
70 LocalDate endDate = new LocalDate(lbdf.getYear(), 12, 31);
71
72 lbdf.setAccrualCategories(getAccrualCategories(leavePlan));
73 lbdf.setLeaveEntries(getLeaveEntries(HrContext.getTargetPrincipalId(), serviceDate, beginDate, endDate, lbdf.getAccrualCategories()));
74
75 List<LeaveBlockHistory> correctedLeaveEntries = LmServiceLocator.getLeaveBlockHistoryService().getLeaveBlockHistoriesForLeaveDisplay(HrContext.getTargetPrincipalId(), beginDate, endDate, Boolean.TRUE);
76 if (correctedLeaveEntries != null) {
77 for (LeaveBlockHistory leaveBlockHistory : correctedLeaveEntries) {
78 if (leaveBlockHistory.getAction() != null && leaveBlockHistory.getAction().equalsIgnoreCase(HrConstants.ACTION.DELETE)) {
79 leaveBlockHistory.setPrincipalIdModified(leaveBlockHistory.getPrincipalIdDeleted());
80 leaveBlockHistory.setTimestamp(leaveBlockHistory.getTimestampDeleted());
81 }
82
83 if(leaveBlockHistory.getDescription() == null || leaveBlockHistory.getDescription().trim().isEmpty()) {
84 leaveBlockHistory.setDescription(this.retrieveDescriptionAccordingToLeaveType(leaveBlockHistory.getLeaveBlockType()));
85 }
86 }
87 }
88 lbdf.setCorrectedLeaveEntries(correctedLeaveEntries);
89
90 List<LeaveBlockHistory> inActiveLeaveEntries = LmServiceLocator.getLeaveBlockHistoryService() .getLeaveBlockHistoriesForLeaveDisplay(HrContext.getTargetPrincipalId(), beginDate, endDate, Boolean.FALSE);
91 List<LeaveBlockHistory> leaveEntries = null;
92 if (inActiveLeaveEntries != null) {
93 leaveEntries = new ArrayList<LeaveBlockHistory>();
94 for (LeaveBlockHistory leaveBlockHistory:inActiveLeaveEntries) {
95 if (leaveBlockHistory.getAccrualGenerated() == null || !leaveBlockHistory.getAccrualGenerated()) {
96 if (leaveBlockHistory.getAction()!= null) {
97 if (leaveBlockHistory.getAction().equalsIgnoreCase(HrConstants.ACTION.DELETE)) {
98 leaveBlockHistory.setPrincipalIdModified(leaveBlockHistory.getPrincipalIdDeleted());
99 leaveBlockHistory.setTimestamp(leaveBlockHistory.getTimestampDeleted());
100 } else if (leaveBlockHistory.getAction().equalsIgnoreCase(HrConstants.ACTION.MODIFIED)) {
101 leaveBlockHistory.setPrincipalIdModified(leaveBlockHistory.getPrincipalIdDeleted());
102 }
103 }
104
105 this.assignDocumentStatusToLeaveBlock(leaveBlockHistory);
106
107
108 if(leaveBlockHistory.getDescription() == null || leaveBlockHistory.getDescription().trim().isEmpty()) {
109 leaveBlockHistory.setDescription(this.retrieveDescriptionAccordingToLeaveType(leaveBlockHistory.getLeaveBlockType()));
110 }
111 if (StringUtils.isNotBlank(leaveBlockHistory.getRequestStatus())) {
112 leaveBlockHistory.setRequestStatus(HrConstants.REQUEST_STATUS_STRINGS.get(leaveBlockHistory.getRequestStatus()));
113 }
114 leaveEntries.add(leaveBlockHistory);
115 }
116 }
117 }
118 lbdf.setInActiveLeaveEntries(leaveEntries);
119
120 return forward;
121 }
122
123 private List<AccrualCategory> getAccrualCategories(String leavePlan) {
124 List<AccrualCategory> accrualCategories = new ArrayList<AccrualCategory>();
125
126 List<AccrualCategory> allAccrualCategories = HrServiceLocator.getAccrualCategoryService().getActiveAccrualCategoriesForLeavePlan(leavePlan, LocalDate.now());
127 if (allAccrualCategories != null) {
128 for (AccrualCategory ac : allAccrualCategories) {
129 if (StringUtils.equalsIgnoreCase(ac.getShowOnGrid(), "Y")) {
130 accrualCategories.add(ac);
131 }
132 }
133 Collections.sort(accrualCategories, new Comparator<AccrualCategory>() {
134 @Override
135 public int compare(AccrualCategory o1, AccrualCategory o2) {
136 return ObjectUtils.compare(o1.getAccrualCategory(), o2.getAccrualCategory());
137 }
138 });
139 }
140
141 return accrualCategories;
142 }
143
144 private List<LeaveBlockDisplay> getLeaveEntries(String principalId, LocalDate serviceDate, LocalDate beginDate, LocalDate endDate, List<AccrualCategory> accrualCategories) {
145 List<LeaveBlockDisplay> leaveEntries = new ArrayList<LeaveBlockDisplay>();
146
147 List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocks(principalId, beginDate, endDate);
148
149 for (LeaveBlock leaveBlock : leaveBlocks) {
150 if (!leaveBlock.getLeaveBlockType().equals(LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER)) {
151 leaveEntries.add(new LeaveBlockDisplay(leaveBlock));
152 }
153 }
154 Collections.sort(leaveEntries, new Comparator<LeaveBlockDisplay>() {
155 @Override
156 public int compare(LeaveBlockDisplay o1, LeaveBlockDisplay o2) {
157 return ObjectUtils.compare(o1.getLeaveDate(), o2.getLeaveDate());
158 }
159 });
160
161 SortedMap<String, BigDecimal> accrualBalances = getPreviousAccrualBalances(principalId, serviceDate, beginDate, accrualCategories);
162
163 for (LeaveBlockDisplay leaveEntry : leaveEntries) {
164 for (AccrualCategory accrualCategory : accrualCategories) {
165 if (!accrualBalances.containsKey(accrualCategory.getAccrualCategory())) {
166 accrualBalances.put(accrualCategory.getAccrualCategory(), BigDecimal.ZERO);
167 }
168 BigDecimal currentAccrualBalance = accrualBalances.get(accrualCategory.getAccrualCategory());
169
170 if (StringUtils.equals(leaveEntry.getAccrualCategory(), accrualCategory.getAccrualCategory())) {
171 BigDecimal accruedBalance = currentAccrualBalance.add(leaveEntry.getLeaveAmount());
172 accrualBalances.put(accrualCategory.getAccrualCategory(), accruedBalance);
173 }
174
175 leaveEntry.setAccrualBalance(accrualCategory.getAccrualCategory(), accrualBalances.get(accrualCategory.getAccrualCategory()));
176 }
177 }
178
179 return leaveEntries;
180 }
181
182 private SortedMap<String, BigDecimal> getPreviousAccrualBalances(String principalId, LocalDate serviceDate, LocalDate beginDate, List<AccrualCategory> accrualCategories) {
183 SortedMap<String, BigDecimal> previousAccrualBalances = new TreeMap<String, BigDecimal>();
184
185 LeaveSummary leaveSummary = LmServiceLocator.getLeaveSummaryService().getLeaveSummaryAsOfDateWithoutFuture(principalId, beginDate);
186
187 for (LeaveSummaryRow row : leaveSummary.getLeaveSummaryRows()) {
188 previousAccrualBalances.put(row.getAccrualCategory(), row.getLeaveBalance());
189 }
190
191 return previousAccrualBalances;
192 }
193
194 private void assignDocumentStatusToLeaveBlock(LeaveBlock leaveBlock) {
195
196 if(StringUtils.isNotEmpty(leaveBlock.getDocumentId())) {
197 LeaveCalendarDocumentHeader lcdh = LmServiceLocator.getLeaveCalendarDocumentHeaderService().getDocumentHeader(leaveBlock.getDocumentId());
198 if(lcdh != null ) {
199 leaveBlock.setDocumentStatus(KewApiConstants.DOCUMENT_STATUSES.get(lcdh.getDocumentStatus()));
200 }
201 }
202 }
203
204 private String retrieveDescriptionAccordingToLeaveType(String leaveType) {
205 String description = null;
206 description = LMConstants.LEAVE_BLOCK_TYPE_MAP.get(leaveType);
207 return description;
208 }
209 }