001    /*
002     * Copyright 2011 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.kfs.module.ld.document;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.kfs.module.ld.businessobject.LaborLedgerPendingEntry;
022    import org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService;
023    import org.kuali.kfs.sys.KFSConstants;
024    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySequenceHelper;
025    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.kfs.sys.document.AccountingDocumentBase;
028    import org.kuali.kfs.sys.document.AmountTotaling;
029    import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
030    import org.kuali.rice.kns.exception.ValidationException;
031    import org.kuali.rice.kns.rule.event.KualiDocumentEvent;
032    
033    /**
034     * Labor Document base class implementation for all labor eDocs that are transactional, meaning implementing
035     * TransactionalDocumentBase. Additional functionality for labor is provided by this class, suchc as retrieving labor ledger pending
036     * entries.
037     */
038    public abstract class LaborLedgerPostingDocumentBase extends AccountingDocumentBase implements LaborLedgerPostingDocument {
039        protected List<LaborLedgerPendingEntry> laborLedgerPendingEntries;
040        
041        protected final static String LABOR_LEDGER_GENERAL_LEDGER_POSTING_HELPER_BEAN_ID = "kfsDoNothingGeneralLedgerPostingHelper";
042    
043        /**
044         * Initializes the pending entries.
045         */
046        public LaborLedgerPostingDocumentBase() {
047            super();
048            setLaborLedgerPendingEntries(new ArrayList<LaborLedgerPendingEntry>());
049        }
050    
051        /**
052         * @see org.kuali.kfs.module.ld.document.LaborLedgerPostingDocument#getLaborLedgerPendingEntries()
053         */
054        public List<LaborLedgerPendingEntry> getLaborLedgerPendingEntries() {
055            return this.laborLedgerPendingEntries;
056        }
057    
058        /**
059         * @see org.kuali.kfs.module.ld.document.LaborLedgerPostingDocument#setLaborLedgerPendingEntries(java.util.List)
060         */
061        public void setLaborLedgerPendingEntries(List<LaborLedgerPendingEntry> laborLedgerPendingEntries) {
062            this.laborLedgerPendingEntries = laborLedgerPendingEntries;
063        }
064    
065        /**
066         * Override to call super and then iterate over all GLPEs and update the approved code appropriately.
067         * 
068         * @see Document#doRouteStatusChange()
069         */
070        @Override
071        public void doRouteStatusChange(DocumentRouteStatusChangeDTO statusChangeEvent) {
072            super.doRouteStatusChange(statusChangeEvent);
073            if (getDocumentHeader().getWorkflowDocument().stateIsProcessed()) {
074                changeLedgerPendingEntriesApprovedStatusCode();
075            }
076            else if (getDocumentHeader().getWorkflowDocument().stateIsCanceled() || getDocumentHeader().getWorkflowDocument().stateIsDisapproved()) {
077                removeLedgerPendingEntries();
078            }
079        }
080    
081        /**
082         * This method iterates over all of the pending entries for a document and sets their approved status code to APPROVED "A".
083         */
084        protected void changeLedgerPendingEntriesApprovedStatusCode() {
085            for (LaborLedgerPendingEntry pendingEntry : laborLedgerPendingEntries) {
086                pendingEntry.setFinancialDocumentApprovedCode(KFSConstants.DocumentStatusCodes.APPROVED);
087            }
088        }
089    
090        /**
091         * This method calls the service to remove all of the pending entries associated with this document
092         */
093        protected void removeLedgerPendingEntries() {
094            LaborLedgerPendingEntryService laborLedgerPendingEntryService = SpringContext.getBean(LaborLedgerPendingEntryService.class);
095            laborLedgerPendingEntryService.delete(getDocumentHeader().getDocumentNumber());
096        }
097    
098        /**
099         * This implementation is coupled tightly with some underlying issues that the Struts PojoProcessor plugin has with how objects
100         * get instantiated within lists. The first three lines are required otherwise when the PojoProcessor tries to automatically
101         * inject values into the list, it will get an index out of bounds error if the instance at an index is being called and prior
102         * instances at indices before that one are not being instantiated. So changing the code below will cause things to break.
103         * 
104         * @param index of Labor Ledger Pending Entry to retrieve
105         * @return LaborLedgerPendingEntry
106         */
107        public LaborLedgerPendingEntry getLaborLedgerPendingEntry(int index) {
108            while (laborLedgerPendingEntries.size() <= index) {
109                laborLedgerPendingEntries.add(new LaborLedgerPendingEntry());
110            }
111            return laborLedgerPendingEntries.get(index);
112        }
113    
114        /**
115         * @see org.kuali.kfs.sys.document.AccountingDocumentBase#prepareForSave(org.kuali.rice.kns.rule.event.KualiDocumentEvent)
116         */
117        @Override
118        public void prepareForSave(KualiDocumentEvent event) {
119            if (!SpringContext.getBean(LaborLedgerPendingEntryService.class).generateLaborLedgerPendingEntries(this)) {
120                logErrors();
121                throw new ValidationException("labor ledger LLPE generation failed");
122            }
123        }
124        
125        /**
126         * This is a "do nothing" version of the method - it just won't create GLPEs
127         * @see org.kuali.kfs.sys.document.AccountingDocumentBase#generateGeneralLedgerPendingEntries(org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySourceDetail, org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySequenceHelper)
128         */
129        @Override
130        public boolean generateGeneralLedgerPendingEntries(GeneralLedgerPendingEntrySourceDetail glpeSourceDetail, GeneralLedgerPendingEntrySequenceHelper sequenceHelper) {
131            return true;
132        }
133    
134        /**
135         * Labor docs never generate general ledger pending entries, and therefore, this method is never called, but we always return true, since
136         * we're required to have a concrete representation
137         * @see org.kuali.kfs.sys.document.AccountingDocumentBase#isDebit(org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySourceDetail)
138         */
139        @Override
140        public boolean isDebit(GeneralLedgerPendingEntrySourceDetail postable) {
141            return true;
142        }
143        
144    }