001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.hr.lm.leavepayout.service;
017    
018    import org.apache.commons.collections.CollectionUtils;
019    import org.kuali.hr.lm.LMConstants;
020    import org.kuali.hr.lm.balancetransfer.BalanceTransfer;
021    import org.kuali.hr.lm.leavepayout.LeavePayout;
022    import org.kuali.hr.lm.leaveblock.LeaveBlock;
023    import org.kuali.hr.time.HrBusinessObject;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
026    import org.kuali.rice.kew.api.document.DocumentStatus;
027    import org.kuali.rice.kew.api.exception.WorkflowException;
028    import org.kuali.rice.kns.document.MaintenanceDocument;
029    import org.kuali.rice.krad.bo.DocumentHeader;
030    import org.kuali.rice.krad.service.DocumentService;
031    import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
032    import org.kuali.rice.krad.util.ObjectUtils;
033    
034    public class LeavePayoutMaintainableImpl extends
035            HrBusinessObjectMaintainableImpl {
036    
037        private static final long serialVersionUID = 1L;
038    
039        @Override
040        public void saveBusinessObject() {
041                    LeavePayout bt = (LeavePayout) this.getBusinessObject();
042                    
043                    LeavePayout existingBt = TkServiceLocator.getLeavePayoutService().getLeavePayoutById(bt.getId());
044                    
045                    if(ObjectUtils.isNotNull(existingBt)) {
046                            if(existingBt.getPayoutAmount().compareTo(bt.getPayoutAmount()) != 0) {
047                                    //TODO: Create leave block reference within bt, and update leave amount.
048                            }
049                            if(existingBt.getForfeitedAmount().compareTo(bt.getForfeitedAmount()) != 0) {
050                                    //TODO: Create reference within bt for forfeited leave block, update leave amount.
051                            }
052                            //Will approvers / department admins be changing accrual category? effective date?
053                    }
054        }
055        
056        @Override
057        public HrBusinessObject getObjectById(String id) {
058            // TODO Auto-generated method stub
059            return TkServiceLocator.getLeavePayoutService().getLeavePayoutById(id);
060        }
061    
062        @Override
063        public void doRouteStatusChange(DocumentHeader documentHeader) {
064            //ProcessDocReport pdr = new ProcessDocReport(true, "");
065            String documentId = documentHeader.getDocumentNumber();
066            LeavePayout payout = (LeavePayout)this.getDataObject();
067            DocumentService documentService = KRADServiceLocatorWeb.getDocumentService();
068    
069            DocumentStatus newDocumentStatus = documentHeader.getWorkflowDocument().getStatus();
070            String routedByPrincipalId = documentHeader.getWorkflowDocument().getRoutedByPrincipalId();
071            if (DocumentStatus.ENROUTE.equals(newDocumentStatus)
072                    && CollectionUtils.isEmpty(payout.getLeaveBlocks())) {
073                //when payout document is routed, initiate the leave payout - creating the leave blocks
074                try {
075                    MaintenanceDocument md = (MaintenanceDocument)KRADServiceLocatorWeb.getDocumentService().getByDocumentHeaderId(documentId);
076    
077                    payout = TkServiceLocator.getLeavePayoutService().payout(payout);
078                    md.getNewMaintainableObject().setDataObject(payout);
079                    documentService.saveDocument(md);
080                }
081                catch (WorkflowException e) {
082                    LOG.error("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
083                    throw new RuntimeException("caught exception while handling doRouteStatusChange -> documentService.getByDocumentHeaderId(" + documentHeader.getDocumentNumber() + "). ", e);
084                }
085            } else if (DocumentStatus.DISAPPROVED.equals(newDocumentStatus)) {
086                //When payout document is disapproved, set all leave block's request statuses to disapproved.
087                for(LeaveBlock lb : payout.getLeaveBlocks()) {
088                    if(ObjectUtils.isNotNull(lb)) {
089                        lb.setRequestStatus(LMConstants.REQUEST_STATUS.DISAPPROVED);
090                        TkServiceLocator.getLeaveBlockService().deleteLeaveBlock(lb.getLmLeaveBlockId(), routedByPrincipalId);
091                    }
092                }
093                //update status of document and associated leave blocks.
094            } else if (DocumentStatus.FINAL.equals(newDocumentStatus)) {
095                //When payout document moves to final, set all leave block's request statuses to approved.
096                for(LeaveBlock lb : payout.getLeaveBlocks()) {
097                    if(ObjectUtils.isNotNull(lb)) {
098                        lb.setRequestStatus(LMConstants.REQUEST_STATUS.APPROVED);
099                        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    }