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    
017    package org.kuali.kfs.pdp.businessobject;
018    
019    import java.util.ArrayList;
020    import java.util.Iterator;
021    import java.util.LinkedHashMap;
022    import java.util.List;
023    
024    import org.apache.commons.lang.ObjectUtils;
025    import org.kuali.kfs.pdp.PdpPropertyConstants;
026    import org.kuali.kfs.pdp.service.PaymentGroupService;
027    import org.kuali.kfs.sys.context.SpringContext;
028    import org.kuali.rice.kns.bo.TransientBusinessObjectBase;
029    import org.kuali.rice.kns.service.BusinessObjectService;
030    import org.kuali.rice.kns.util.KualiDecimal;
031    import org.kuali.rice.kns.util.KualiInteger;
032    
033    /**
034     * This class collects the summary information for payment Format Process
035     */
036    public class FormatProcessSummary extends TransientBusinessObjectBase {
037    
038        private List<ProcessSummary> processSummaryList;
039        private KualiInteger totalCount;
040        private KualiDecimal totalAmount;
041        private KualiInteger processId;
042    
043        /**
044         * Constructs a FormatProcessSummary.java.
045         */
046        public FormatProcessSummary() {
047            super();
048            processSummaryList = new ArrayList<ProcessSummary>();
049            totalCount = KualiInteger.ZERO;
050            totalAmount = KualiDecimal.ZERO;
051        }
052    
053        /**
054         * Add the payment detail info to our summary list
055         * 
056         * @param paymentGroup
057         */
058        public void add(PaymentGroup paymentGroup) {
059            ProcessSummary ps = findProcessSummary(paymentGroup);
060    
061            // If it's not in our list, add it
062            if (ps == null) {
063                ps = new ProcessSummary();
064                ps.setBeginDisbursementNbr(KualiInteger.ZERO);
065                ps.setCustomer(paymentGroup.getBatch().getCustomerProfile());
066                ps.setDisbursementType(paymentGroup.getDisbursementType());
067                ps.setEndDisbursementNbr(KualiInteger.ZERO);
068                ps.setProcess(paymentGroup.getProcess());
069                ps.setProcessTotalAmount(KualiDecimal.ZERO);
070                ps.setProcessTotalCount(KualiInteger.ZERO);
071                ps.setSortGroupId(new KualiInteger(SpringContext.getBean(PaymentGroupService.class).getSortGroupId(paymentGroup)));
072                processSummaryList.add(ps);
073                
074                // if first one added set the process id
075                if (processId == null) {
076                    processId = paymentGroup.getProcessId();
077                }
078            }
079    
080            // Update the total & count
081            ps.setProcessTotalAmount(ps.getProcessTotalAmount().add(paymentGroup.getNetPaymentAmount()));
082            ps.setProcessTotalCount(new KualiInteger(ps.getProcessTotalCount().intValue() + paymentGroup.getPaymentDetails().size()));
083            totalAmount = totalAmount.add(paymentGroup.getNetPaymentAmount());
084            totalCount = totalCount.add(new KualiInteger(paymentGroup.getPaymentDetails().size()));
085        }
086        
087        /**
088         * Save all the process summary records
089         * 
090         * @param pdd
091         */
092        public void save() {
093            for (Iterator<ProcessSummary> iter = processSummaryList.iterator(); iter.hasNext();) {
094                ProcessSummary ps = (ProcessSummary) iter.next();
095                
096                SpringContext.getBean(BusinessObjectService.class).save(ps);
097            }
098        }
099    
100        /**
101         * This method checks if we already have a summary record. 
102         * @param paymentGroup
103         * @return If we we already have a summary record return it, if not, return null;
104         */
105        private ProcessSummary findProcessSummary(PaymentGroup paymentGroup) {
106    
107            for (Iterator<ProcessSummary> iter = processSummaryList.iterator(); iter.hasNext();) {
108                ProcessSummary processSummary = (ProcessSummary) iter.next();
109                
110                if(ObjectUtils.equals(processSummary.getCustomer(), paymentGroup.getBatch().getCustomerProfile()) && ObjectUtils.equals(processSummary.getDisbursementType(), paymentGroup.getDisbursementType()) && (processSummary.getSortGroupId().intValue()==SpringContext.getBean(PaymentGroupService.class).getSortGroupId(paymentGroup)) && ObjectUtils.equals(processSummary.getProcess(), paymentGroup.getProcess())) {
111                    return processSummary;
112                }
113            }
114            return null;
115        }
116    
117        /**
118         * @param pg Update the disbursement number information
119         * @param range
120         */
121        public void setDisbursementNumber(PaymentGroup paymentGroup, Integer disbursementNumber) {
122            ProcessSummary processSummary = findProcessSummary(paymentGroup);
123            if (processSummary != null) {
124                if (processSummary.getBeginDisbursementNbr().isZero()) {
125                    processSummary.setBeginDisbursementNbr(new KualiInteger(disbursementNumber));
126                    processSummary.setEndDisbursementNbr(new KualiInteger(disbursementNumber));
127                }
128                else {
129                    processSummary.setEndDisbursementNbr(new KualiInteger(disbursementNumber));
130                }
131            }
132        }
133    
134        /**
135         * @see org.kuali.rice.kns.bo.BusinessObjectBase#toStringMapper()
136         */
137        @SuppressWarnings("rawtypes")
138        @Override
139        protected LinkedHashMap toStringMapper() {
140            LinkedHashMap<String, List<ProcessSummary>> m = new LinkedHashMap<String, List<ProcessSummary>>();
141    
142            m.put(PdpPropertyConstants.FormatProcessSummary.PROCESS_SUMMARY, this.processSummaryList);
143    
144            return m;
145        }
146    
147        /**
148         * This method gets the total amount.
149         * @return totalAmount
150         */
151        public KualiDecimal getTotalAmount() {
152            return totalAmount;
153        }
154    
155        /**
156         * This method sets the total amount.
157         * @param totalAmount
158         */
159        public void setTotalAmount(KualiDecimal totalAmount) {
160            this.totalAmount = totalAmount;
161        }
162    
163        /**
164         * This method gets the total count.
165         * @return totalCount
166         */
167        public KualiInteger getTotalCount() {
168            return totalCount;
169        }
170    
171        /**
172         * This method sets the total count.
173         * @param totalCount
174         */
175        public void setTotalCount(KualiInteger totalCount) {
176            this.totalCount = totalCount;
177        }
178    
179        /**
180         * This method retrieves a specific process summary from the list, by index
181         * 
182         * @param index the index of the results to retrieve the process summary from
183         * @return a ProcessSummary
184         */
185        public ProcessSummary getProcessSummary(int index) {
186            if (index >= processSummaryList.size()) {
187                for (int i = processSummaryList.size(); i <= index; i++) {
188                    processSummaryList.add(new ProcessSummary());
189                }
190            }
191            return (ProcessSummary) processSummaryList.get(index);
192        }
193    
194        /**
195         * This method sets a process summary value at a given index in the process summary list.
196         * 
197         * @param key the index
198         * @param value the new value
199         */
200        public void setProcessSummary(int key, ProcessSummary value) {
201            processSummaryList.set(key, value);
202        }
203    
204        /**
205         * This method gets the process summary list
206         * @return processSummaryList
207         */
208        public List<ProcessSummary> getProcessSummaryList() {
209            return processSummaryList;
210        }
211    
212        /**
213         * This method sets the process summary list.
214         * @param processSummaryList
215         */
216        public void setProcessSummaryList(List<ProcessSummary> processSummaryList) {
217            this.processSummaryList = processSummaryList;
218        }
219    
220        /**
221         * This method gets the process id.
222         * @return the processId
223         */
224        public KualiInteger getProcessId() {
225            return processId;
226        }
227    
228        /**
229         * This method sets the process id.
230         * @param processId
231         */
232        public void setProcessId(KualiInteger processId) {
233            this.processId = processId;
234        }
235    }