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.leavepayout.service;
17  
18  import org.apache.commons.collections.CollectionUtils;
19  import org.kuali.hr.lm.LMConstants;
20  import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
21  import org.kuali.hr.lm.leavepayout.LeavePayout;
22  import org.kuali.hr.lm.leaveblock.LeaveBlock;
23  import org.kuali.hr.time.HrBusinessObject;
24  import org.kuali.hr.time.service.base.TkServiceLocator;
25  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
26  import org.kuali.rice.kew.api.document.DocumentStatus;
27  import org.kuali.rice.kew.api.exception.WorkflowException;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.krad.bo.DocumentHeader;
30  import org.kuali.rice.krad.service.DocumentService;
31  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  public class LeavePayoutMaintainableImpl extends
35          HrBusinessObjectMaintainableImpl {
36  
37      private static final long serialVersionUID = 1L;
38  
39      @Override
40      public void saveBusinessObject() {
41  		LeavePayout bt = (LeavePayout) this.getBusinessObject();
42  		
43  		LeavePayout existingBt = TkServiceLocator.getLeavePayoutService().getLeavePayoutById(bt.getId());
44  		
45  		if(ObjectUtils.isNotNull(existingBt)) {
46  			if(existingBt.getPayoutAmount().compareTo(bt.getPayoutAmount()) != 0) {
47  				//TODO: Create leave block reference within bt, and update leave amount.
48  			}
49  			if(existingBt.getForfeitedAmount().compareTo(bt.getForfeitedAmount()) != 0) {
50  				//TODO: Create reference within bt for forfeited leave block, update leave amount.
51  			}
52  			//Will approvers / department admins be changing accrual category? effective date?
53  		}
54      }
55      
56      @Override
57      public HrBusinessObject getObjectById(String id) {
58          // TODO Auto-generated method stub
59          return TkServiceLocator.getLeavePayoutService().getLeavePayoutById(id);
60      }
61  
62      @Override
63      public void doRouteStatusChange(DocumentHeader documentHeader) {
64          //ProcessDocReport pdr = new ProcessDocReport(true, "");
65          String documentId = documentHeader.getDocumentNumber();
66          LeavePayout payout = (LeavePayout)this.getDataObject();
67          DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
68  
69          DocumentStatus newDocumentStatus = documentHeader.getWorkflowDocument().getStatus();
70          String routedByPrincipalId = documentHeader.getWorkflowDocument().getRoutedByPrincipalId();
71          if (DocumentStatus.ENROUTE.equals(newDocumentStatus)
72                  && CollectionUtils.isEmpty(payout.getLeaveBlocks())) {
73              //when payout document is routed, initiate the leave payout - creating the leave blocks
74              try {
75                  MaintenanceDocument md = (MaintenanceDocument)KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
76  
77                  payout = TkServiceLocator.getLeavePayoutService().payout(payout);
78                  md.getNewMaintainableObject().setDataObject(payout);
79                  documentService.saveDocument(md);
80              }
81              catch (WorkflowException e) {
82                  LOG.error("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
83                  throw new RuntimeException("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
84              }
85          } else if (DocumentStatus.DISAPPROVED.equals(newDocumentStatus)) {
86              //When payout document is disapproved, set all leave block's request statuses to disapproved.
87              for(LeaveBlock lb : payout.getLeaveBlocks()) {
88                  if(ObjectUtils.isNotNull(lb)) {
89                      lb.setRequestStatus(LMConstants.REQUEST_STATUS.DISAPPROVED);
90                      TkServiceLocator.getLeaveBlockService().deleteLeaveBlock(lb.getLmLeaveBlockId(), routedByPrincipalId);
91                  }
92              }
93              //update status of document and associated leave blocks.
94          } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
95              //When payout document moves to final, set all leave block's request statuses to approved.
96              for(LeaveBlock lb : payout.getLeaveBlocks()) {
97                  if(ObjectUtils.isNotNull(lb)) {
98                      lb.setRequestStatus(LMConstants.REQUEST_STATUS.APPROVED);
99                      TkServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
100                 }
101             }
102         } else if (DocumentStatus.CANCELED.equals(newDocumentStatus)) {
103             //When payout document is canceled, set all leave block's request statuses to deferred
104             for(LeaveBlock lb : payout.getLeaveBlocks()) {
105                 if(ObjectUtils.isNotNull(lb)) {
106                     lb.setRequestStatus(LMConstants.REQUEST_STATUS.DEFERRED);
107                     TkServiceLocator.getLeaveBlockService().updateLeaveBlock(lb, routedByPrincipalId);
108                 }
109             }
110         }
111     }
112 
113 }