1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.tklm.leave.transfer.web;
17
18 import java.math.BigDecimal;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.commons.collections.CollectionUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.kuali.kpme.core.accrualcategory.AccrualCategory;
25 import org.kuali.kpme.core.bo.HrBusinessObject;
26 import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
27 import org.kuali.kpme.core.service.HrServiceLocator;
28 import org.kuali.kpme.core.util.HrConstants;
29 import org.kuali.kpme.core.util.HrContext;
30 import org.kuali.kpme.core.util.TKUtils;
31 import org.kuali.kpme.tklm.common.LMConstants;
32 import org.kuali.kpme.tklm.leave.block.LeaveBlock;
33 import org.kuali.kpme.tklm.leave.service.LmServiceLocator;
34 import org.kuali.kpme.tklm.leave.transfer.BalanceTransfer;
35 import org.kuali.rice.kew.api.document.DocumentStatus;
36 import org.kuali.rice.kew.api.exception.WorkflowException;
37 import org.kuali.rice.kns.document.MaintenanceDocument;
38 import org.kuali.rice.krad.bo.DocumentHeader;
39 import org.kuali.rice.krad.service.DocumentService;
40 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
41 import org.kuali.rice.krad.util.ObjectUtils;
42
43 @SuppressWarnings("deprecation")
44 public class BalanceTransferMaintainableImpl extends
45 HrBusinessObjectMaintainableImpl {
46
47 private static final long serialVersionUID = -789218061798169466L;
48
49
50 @Override
51 public HrBusinessObject getObjectById(String id) {
52 return LmServiceLocator.getBalanceTransferService().getBalanceTransferById(id);
53 }
54
55
56 @Override
57 public void doRouteStatusChange(DocumentHeader documentHeader) {
58
59 String documentId = documentHeader.getDocumentNumber();
60 BalanceTransfer balanceTransfer = (BalanceTransfer)this.getDataObject();
61 DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
62 balanceTransfer.setDocumentHeaderId(documentId);
63 DocumentStatus newDocumentStatus = documentHeader.getWorkflowDocument().getStatus();
64 String routedByPrincipalId = documentHeader.getWorkflowDocument().getRoutedByPrincipalId();
65
66
67
68
69
70
71
72 if (DocumentStatus.ENROUTE.equals(newDocumentStatus)
73 && CollectionUtils.isEmpty(balanceTransfer.getLeaveBlocks())) {
74
75 if(StringUtils.isNotEmpty(balanceTransfer.getSstoId())) {
76 try {
77 MaintenanceDocument md = (MaintenanceDocument)KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
78 balanceTransfer = LmServiceLocator.getBalanceTransferService().transferSsto(balanceTransfer);
79 md.getDocumentHeader().setDocumentDescription(TKUtils.getDocumentDescription(balanceTransfer.getPrincipalId(), balanceTransfer.getEffectiveLocalDate()));
80 md.getNewMaintainableObject().setDataObject(balanceTransfer);
81 documentService.saveDocument(md);
82 }
83 catch (WorkflowException e) {
84 LOG.error("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
85 throw new RuntimeException("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
86 }
87 } else {
88
89 try {
90 MaintenanceDocument md = (MaintenanceDocument)KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
91 md.getDocumentHeader().setDocumentDescription(TKUtils.getDocumentDescription(balanceTransfer.getPrincipalId(), balanceTransfer.getEffectiveLocalDate()));
92 balanceTransfer = LmServiceLocator.getBalanceTransferService().transfer(balanceTransfer);
93 md.getNewMaintainableObject().setDataObject(balanceTransfer);
94 documentService.saveDocument(md);
95 }
96 catch (WorkflowException e) {
97 LOG.error("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
98 throw new RuntimeException("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
99 }
100 }
101 } else if (DocumentStatus.DISAPPROVED.equals(newDocumentStatus)) {
102
103
104
105
106 if(StringUtils.isNotEmpty(balanceTransfer.getSstoId())) {
107
108 List<LeaveBlock> lbList = buildSstoLeaveBlockList(balanceTransfer);
109 LmServiceLocator.getLeaveBlockService().saveLeaveBlocks(lbList);
110 }
111
112 for(LeaveBlock lb : balanceTransfer.getLeaveBlocks()) {
113 if(ObjectUtils.isNotNull(lb)) {
114 lb.setRequestStatus(HrConstants.REQUEST_STATUS.DISAPPROVED);
115 LmServiceLocator.getLeaveBlockService().deleteLeaveBlock(lb.getLmLeaveBlockId(), routedByPrincipalId);
116 }
117 }
118
119 } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
120
121 for(LeaveBlock lb : balanceTransfer.getLeaveBlocks()) {
122 if(ObjectUtils.isNotNull(lb)) {
123
124 lb.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
125 LmServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
126 }
127 }
128 List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksForDocumentId(balanceTransfer.getLeaveCalendarDocumentId());
129 for(LeaveBlock lb : leaveBlocks) {
130 if(StringUtils.equals(lb.getAccrualCategory(),balanceTransfer.getFromAccrualCategory())
131 && StringUtils.equals(lb.getLeaveBlockType(),LMConstants.LEAVE_BLOCK_TYPE.CARRY_OVER_ADJUSTMENT)) {
132 BigDecimal adjustment = new BigDecimal(0);
133 if(balanceTransfer.getTransferAmount() != null)
134 adjustment = adjustment.add(balanceTransfer.getTransferAmount().abs());
135 if(balanceTransfer.getForfeitedAmount() != null)
136 adjustment = adjustment.add(balanceTransfer.getForfeitedAmount().abs());
137 BigDecimal adjustedLeaveAmount = lb.getLeaveAmount().abs().subtract(adjustment);
138 lb.setLeaveAmount(adjustedLeaveAmount.negate());
139 LmServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
140 }
141 }
142 } else if (DocumentStatus.CANCELED.equals(newDocumentStatus)) {
143
144
145 for(LeaveBlock lb : balanceTransfer.getLeaveBlocks()) {
146 if(ObjectUtils.isNotNull(lb)) {
147 lb.setRequestStatus(HrConstants.REQUEST_STATUS.DEFERRED);
148 LmServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
149 }
150 }
151 }
152 }
153
154 private List<LeaveBlock> buildSstoLeaveBlockList(BalanceTransfer bt) {
155 String leaveDocId = CollectionUtils.isNotEmpty(bt.getLeaveBlocks())? bt.getLeaveBlocks().get(0).getDocumentId() : "";
156 List<LeaveBlock> lbList = new ArrayList<LeaveBlock>();
157 AccrualCategory fromAC = HrServiceLocator.getAccrualCategoryService().getAccrualCategory(bt.getFromAccrualCategory(), bt.getEffectiveLocalDate());
158
159 LeaveBlock accruedLeaveBlock = new LeaveBlock();
160 accruedLeaveBlock.setAccrualCategory(bt.getFromAccrualCategory());
161 accruedLeaveBlock.setLeaveDate(bt.getEffectiveDate());
162 accruedLeaveBlock.setPrincipalId(bt.getPrincipalId());
163 accruedLeaveBlock.setEarnCode(fromAC.getEarnCode());
164 accruedLeaveBlock.setAccrualGenerated(true);
165 accruedLeaveBlock.setBlockId(0L);
166 accruedLeaveBlock.setScheduleTimeOffId(bt.getSstoId());
167 accruedLeaveBlock.setLeaveAmount(bt.getTransferAmount());
168 accruedLeaveBlock.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE);
169 accruedLeaveBlock.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
170 accruedLeaveBlock.setDocumentId(leaveDocId);
171 accruedLeaveBlock.setPrincipalIdModified(HrContext.getPrincipalId());
172 lbList.add(accruedLeaveBlock);
173
174 LeaveBlock usageLeaveBlock = new LeaveBlock();
175 usageLeaveBlock.setAccrualCategory(bt.getFromAccrualCategory());
176 usageLeaveBlock.setLeaveDate(bt.getEffectiveDate());
177 usageLeaveBlock.setPrincipalId(bt.getPrincipalId());
178 usageLeaveBlock.setEarnCode(fromAC.getEarnCode());
179 usageLeaveBlock.setAccrualGenerated(true);
180 usageLeaveBlock.setBlockId(0L);
181 usageLeaveBlock.setScheduleTimeOffId(bt.getSstoId());
182 usageLeaveBlock.setLeaveAmount(bt.getTransferAmount().negate());
183 usageLeaveBlock.setLeaveBlockType(LMConstants.LEAVE_BLOCK_TYPE.ACCRUAL_SERVICE);
184 usageLeaveBlock.setRequestStatus(HrConstants.REQUEST_STATUS.APPROVED);
185 usageLeaveBlock.setDocumentId(leaveDocId);
186 usageLeaveBlock.setPrincipalIdModified(HrContext.getPrincipalId());
187 lbList.add(usageLeaveBlock);
188
189 return lbList;
190 }
191 }