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