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.cam.document.gl;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.kuali.kfs.sys.KFSConstants;
022    import org.kuali.kfs.sys.businessobject.FinancialSystemDocumentHeader;
023    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry;
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.GeneralLedgerPendingEntrySource;
028    import org.kuali.kfs.sys.service.GeneralLedgerPendingEntryService;
029    import org.kuali.kfs.sys.service.UniversityDateService;
030    import org.kuali.rice.kns.service.BusinessObjectService;
031    import org.kuali.rice.kns.util.KualiDecimal;
032    
033    public abstract class CamsGeneralLedgerPendingEntrySourceBase implements GeneralLedgerPendingEntrySource {
034    
035        private List<GeneralLedgerPendingEntry> pendingEntries = new ArrayList<GeneralLedgerPendingEntry>();
036        private FinancialSystemDocumentHeader documentHeader;
037        private List<GeneralLedgerPendingEntrySourceDetail> postables = new ArrayList<GeneralLedgerPendingEntrySourceDetail>();
038    
039    
040        public CamsGeneralLedgerPendingEntrySourceBase(FinancialSystemDocumentHeader documentHeader) {
041            this.documentHeader = documentHeader;
042        }
043    
044        public void addPendingEntry(GeneralLedgerPendingEntry entry) {
045            this.pendingEntries.add(entry);
046        }
047    
048        public void clearAnyGeneralLedgerPendingEntries() {
049            this.pendingEntries.clear();
050    
051        }
052    
053        public void customizeExplicitGeneralLedgerPendingEntry(GeneralLedgerPendingEntrySourceDetail postable, GeneralLedgerPendingEntry explicitEntry) {
054            // over ride if needed
055        }
056    
057        public boolean generateDocumentGeneralLedgerPendingEntries(GeneralLedgerPendingEntrySequenceHelper sequenceHelper) {
058            return true;
059        }
060    
061        public FinancialSystemDocumentHeader getDocumentHeader() {
062            return documentHeader;
063        }
064    
065        public KualiDecimal getGeneralLedgerPendingEntryAmountForDetail(GeneralLedgerPendingEntrySourceDetail postable) {
066            return postable.getAmount().abs();
067        }
068    
069        public List<GeneralLedgerPendingEntrySourceDetail> getGeneralLedgerPendingEntrySourceDetails() {
070            return this.postables;
071        }
072    
073        public Integer getPostingYear() {
074            return SpringContext.getBean(UniversityDateService.class).getCurrentFiscalYear();
075        }
076    
077        public List<GeneralLedgerPendingEntry> getPendingEntries() {
078            return pendingEntries;
079        }
080    
081        public void setPendingEntries(List<GeneralLedgerPendingEntry> pendingEntries) {
082            this.pendingEntries = pendingEntries;
083        }
084    
085        public List<GeneralLedgerPendingEntrySourceDetail> getPostables() {
086            return postables;
087        }
088    
089        public void setPostables(List<GeneralLedgerPendingEntrySourceDetail> postables) {
090            this.postables = postables;
091        }
092    
093        public void setDocumentHeader(FinancialSystemDocumentHeader documentHeader) {
094            this.documentHeader = documentHeader;
095        }
096    
097        public void doRouteStatusChange(List<GeneralLedgerPendingEntry> glPendingEntries) {
098            if (glPendingEntries == null || glPendingEntries.isEmpty()) {
099                return;
100            }
101            if (documentHeader.getWorkflowDocument().stateIsProcessed()) {
102                for (GeneralLedgerPendingEntry glpe : glPendingEntries) {
103                    glpe.setFinancialDocumentApprovedCode(KFSConstants.DocumentStatusCodes.APPROVED);
104                }
105                SpringContext.getBean(BusinessObjectService.class).save(glPendingEntries);
106            }
107            else if (getDocumentHeader().getWorkflowDocument().stateIsCanceled() || documentHeader.getWorkflowDocument().stateIsDisapproved()) {
108                removeGeneralLedgerPendingEntries(documentHeader.getDocumentNumber());
109            }
110        }
111    
112    
113        /**
114         * This method calls the service to remove all of the GLPE's associated with this document
115         */
116        private void removeGeneralLedgerPendingEntries(String docNumber) {
117            GeneralLedgerPendingEntryService glpeService = SpringContext.getBean(GeneralLedgerPendingEntryService.class);
118            glpeService.delete(docNumber);
119        }
120    
121        public boolean generateGeneralLedgerPendingEntries(GeneralLedgerPendingEntrySourceDetail postable, GeneralLedgerPendingEntrySequenceHelper sequenceHelper) {
122            GeneralLedgerPendingEntry explicitEntry = new GeneralLedgerPendingEntry();
123            SpringContext.getBean(GeneralLedgerPendingEntryService.class).populateExplicitGeneralLedgerPendingEntry(this, postable, sequenceHelper, explicitEntry);
124            customizeExplicitGeneralLedgerPendingEntry(postable, explicitEntry);
125            addPendingEntry(explicitEntry);
126            return true;
127        }
128    
129    }