View Javadoc
1   /*
2    * Copyright 2007 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  
17  package org.kuali.ole.pdp.businessobject;
18  
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.LinkedHashMap;
22  import java.util.List;
23  
24  import org.apache.commons.lang.ObjectUtils;
25  import org.kuali.ole.pdp.PdpPropertyConstants;
26  import org.kuali.ole.pdp.service.PaymentGroupService;
27  import org.kuali.ole.sys.context.SpringContext;
28  import org.kuali.rice.core.api.util.type.KualiDecimal;
29  import org.kuali.rice.core.api.util.type.KualiInteger;
30  import org.kuali.rice.krad.bo.TransientBusinessObjectBase;
31  import org.kuali.rice.krad.service.BusinessObjectService;
32  
33  /**
34   * This class collects the summary information for payment Format Process
35   */
36  public class FormatProcessSummary extends TransientBusinessObjectBase {
37  
38      private List<ProcessSummary> processSummaryList;
39      private KualiInteger totalCount;
40      private KualiDecimal totalAmount;
41      private KualiInteger processId;
42  
43      /**
44       * Constructs a FormatProcessSummary.java.
45       */
46      public FormatProcessSummary() {
47          super();
48          processSummaryList = new ArrayList<ProcessSummary>();
49          totalCount = KualiInteger.ZERO;
50          totalAmount = KualiDecimal.ZERO;
51      }
52  
53      /**
54       * Add the payment detail info to our summary list
55       * 
56       * @param paymentGroup
57       */
58      public void add(PaymentGroup paymentGroup) {
59          ProcessSummary ps = findProcessSummary(paymentGroup);
60  
61          // If it's not in our list, add it
62          if (ps == null) {
63              ps = new ProcessSummary();
64              ps.setBeginDisbursementNbr(KualiInteger.ZERO);
65              ps.setCustomer(paymentGroup.getBatch().getCustomerProfile());
66              ps.setDisbursementType(paymentGroup.getDisbursementType());
67              ps.setEndDisbursementNbr(KualiInteger.ZERO);
68              ps.setProcess(paymentGroup.getProcess());
69              ps.setProcessTotalAmount(KualiDecimal.ZERO);
70              ps.setProcessTotalCount(KualiInteger.ZERO);
71              ps.setSortGroupId(new KualiInteger(SpringContext.getBean(PaymentGroupService.class).getSortGroupId(paymentGroup)));
72              processSummaryList.add(ps);
73              
74              // if first one added set the process id
75              if (processId == null) {
76                  processId = paymentGroup.getProcessId();
77              }
78          }
79  
80          // Update the total & count
81          ps.setProcessTotalAmount(ps.getProcessTotalAmount().add(paymentGroup.getNetPaymentAmount()));
82          ps.setProcessTotalCount(new KualiInteger(ps.getProcessTotalCount().intValue() + paymentGroup.getPaymentDetails().size()));
83          totalAmount = totalAmount.add(paymentGroup.getNetPaymentAmount());
84          totalCount = totalCount.add(new KualiInteger(paymentGroup.getPaymentDetails().size()));
85      }
86      
87      /**
88       * Save all the process summary records
89       * 
90       * @param pdd
91       */
92      public void save() {
93          for (Iterator<ProcessSummary> iter = processSummaryList.iterator(); iter.hasNext();) {
94              ProcessSummary ps = (ProcessSummary) iter.next();
95              
96              SpringContext.getBean(BusinessObjectService.class).save(ps);
97          }
98      }
99  
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.krad.bo.BusinessObjectBase#toStringMapper()
136      */
137     @SuppressWarnings("rawtypes")
138     
139     protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
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 }