View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package org.kuali.kfs.gl.businessobject;
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.Comparator;
25  import java.util.Iterator;
26  import java.util.LinkedHashMap;
27  import java.util.List;
28  
29  import org.kuali.kfs.sys.KFSPropertyConstants;
30  import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
31  
32  /**
33   * This class represents a GLCP correction change group
34   */
35  public class CorrectionChangeGroup extends PersistableBusinessObjectBase implements Comparable {
36      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CorrectionChangeGroup.class);
37  
38      private String documentNumber;
39      private Integer correctionChangeGroupLineNumber;
40      private Integer correctionCriteriaNextLineNumber;
41      private Integer correctionChangeNextLineNumber;
42      private List<CorrectionCriteria> correctionCriteria;
43      private List<CorrectionChange> correctionChange;
44  
45      public CorrectionChangeGroup(String documentNumber, Integer correctionChangeGroupLineNumber) {
46          setCorrectionChangeGroupLineNumber(correctionChangeGroupLineNumber);
47  
48          correctionCriteria = new ArrayList();
49          correctionChange = new ArrayList();
50          correctionCriteriaNextLineNumber = new Integer(0);
51          correctionChangeNextLineNumber = new Integer(0);
52  
53          setDocumentNumber(documentNumber);
54      }
55  
56      public CorrectionChangeGroup() {
57          super();
58          correctionCriteria = new ArrayList();
59          correctionChange = new ArrayList();
60          correctionCriteriaNextLineNumber = new Integer(0);
61          correctionChangeNextLineNumber = new Integer(0);
62      }
63  
64      /**
65       * Add correction change to this correction change group
66       * 
67       * @param cc correction change to add
68       */
69      public void addCorrectionChange(CorrectionChange cc) {
70          LOG.debug("addCorrectionChange() started");
71  
72          cc.setDocumentNumber(documentNumber);
73          cc.setCorrectionChangeGroupLineNumber(correctionChangeGroupLineNumber);
74          cc.setCorrectionChangeLineNumber(correctionChangeNextLineNumber++);
75          correctionChange.add(cc);
76      }
77  
78      /**
79       * Add correction criteria to this correction change group
80       * 
81       * @param cc correction criteria to add to this correction change group
82       */
83      public void addCorrectionCriteria(CorrectionCriteria cc) {
84          cc.setDocumentNumber(documentNumber);
85          cc.setCorrectionChangeGroupLineNumber(correctionChangeGroupLineNumber);
86          cc.setCorrectionCriteriaLineNumber(correctionCriteriaNextLineNumber++);
87          correctionCriteria.add(cc);
88      }
89  
90      /**
91       * Remove correction change item
92       * 
93       * @param changeNumber correction change line number used to determine which correction change item to remove
94       */
95      public void removeCorrectionChangeItem(int changeNumber) {
96          for (Iterator iter = correctionChange.iterator(); iter.hasNext();) {
97              CorrectionChange element = (CorrectionChange) iter.next();
98              if (changeNumber == element.getCorrectionChangeLineNumber().intValue()) {
99                  iter.remove();
100             }
101         }
102     }
103 
104     /**
105      * Remove correction criteria item 
106      * 
107      * @param criteriaNumber correction criteria line number used to determine which correction change to remove
108      */
109     public void removeCorrectionCriteriaItem(int criteriaNumber) {
110         for (Iterator iter = correctionCriteria.iterator(); iter.hasNext();) {
111             CorrectionCriteria element = (CorrectionCriteria) iter.next();
112             if (criteriaNumber == element.getCorrectionCriteriaLineNumber().intValue()) {
113                 iter.remove();
114             }
115         }
116     }
117 
118     /**
119      * Get correction change item 
120      * 
121      * @param changeNumber correction change line number of object to return
122      * @return CorrectionChange correction change object with specified line number to return
123      */
124     public CorrectionChange getCorrectionChangeItem(int changeNumber) {
125         for (Iterator iter = correctionChange.iterator(); iter.hasNext();) {
126             CorrectionChange element = (CorrectionChange) iter.next();
127             if (changeNumber == element.getCorrectionChangeLineNumber().intValue()) {
128                 return element;
129             }
130         }
131 
132         CorrectionChange cc = new CorrectionChange(getDocumentNumber(), correctionChangeGroupLineNumber, changeNumber);
133         correctionChange.add(cc);
134 
135         return cc;
136     }
137     
138     
139     /**
140      * Get correction criteria item 
141      * 
142      * @param criteriaNumber correction change line number of object to return
143      * @return CorrectionChange correction change object with specified line number to return
144      */
145     public CorrectionCriteria getCorrectionCriteriaItem(int criteriaNumber) {
146         for (Iterator iter = correctionCriteria.iterator(); iter.hasNext();) {
147             CorrectionCriteria element = (CorrectionCriteria) iter.next();
148             if (criteriaNumber == element.getCorrectionCriteriaLineNumber().intValue()) {
149                 return element;
150             }
151         }
152 
153         CorrectionCriteria cc = new CorrectionCriteria(getDocumentNumber(), correctionChangeGroupLineNumber, criteriaNumber);
154         correctionCriteria.add(cc);
155         return cc;
156     }
157 
158     public String getDocumentNumber() {
159         return documentNumber;
160     }
161 
162     /**
163      * Set document number for this correction change group.  This also sets the document number for this correction change group's 
164      * correction criteria and correction change
165      * 
166      * @param documentNumber new document number
167      */
168     public void setDocumentNumber(String documentNumber) {
169         this.documentNumber = documentNumber;
170 
171         for (Iterator iter = correctionCriteria.iterator(); iter.hasNext();) {
172             CorrectionCriteria element = (CorrectionCriteria) iter.next();
173             element.setDocumentNumber(documentNumber);
174         }
175         for (Iterator iter = correctionChange.iterator(); iter.hasNext();) {
176             CorrectionChange element = (CorrectionChange) iter.next();
177             element.setDocumentNumber(documentNumber);
178         }
179     }
180 
181     public Integer getCorrectionChangeGroupLineNumber() {
182         return correctionChangeGroupLineNumber;
183     }
184 
185     public void setCorrectionChangeGroupLineNumber(Integer correctionChangeGroupLineNumber) {
186         this.correctionChangeGroupLineNumber = correctionChangeGroupLineNumber;
187     }
188 
189     public Integer getCorrectionCriteriaNextLineNumber() {
190         return correctionCriteriaNextLineNumber;
191     }
192 
193     public void setCorrectionCriteriaNextLineNumber(Integer correctionCriteriaNextLineNumber) {
194         this.correctionCriteriaNextLineNumber = correctionCriteriaNextLineNumber;
195     }
196 
197     public Integer getCorrectionChangeNextLineNumber() {
198         return correctionChangeNextLineNumber;
199     }
200 
201     public void setCorrectionChangeNextLineNumber(Integer correctionChangeNextLineNumber) {
202         this.correctionChangeNextLineNumber = correctionChangeNextLineNumber;
203     }
204 
205     public List<CorrectionCriteria> getCorrectionCriteria() {
206         Collections.sort(correctionCriteria);
207         return correctionCriteria;
208     }
209 
210     public void setCorrectionCriteria(List<CorrectionCriteria> correctionCriteria) {
211         this.correctionCriteria = correctionCriteria;
212     }
213 
214     public List<CorrectionChange> getCorrectionChange() {
215         Collections.sort(correctionChange);
216         return correctionChange;
217     }
218 
219     public void setCorrectionChange(List<CorrectionChange> correctionChange) {
220         this.correctionChange = correctionChange;
221     }
222 
223     /**
224      * Compares this correction change group to another correction change group object by comparing document number and correction group line number
225      * 
226      * @see java.lang.Comparable#compareTo(java.lang.Object)
227      */
228     public int compareTo(Object o) {
229         CorrectionChangeGroup other = (CorrectionChangeGroup) o;
230 
231         String thisFdocNbr = documentNumber == null ? "" : documentNumber;
232         String thatFdocNbr = other.documentNumber == null ? "" : other.documentNumber;
233 
234         int c = thisFdocNbr.compareTo(thatFdocNbr);
235         if (c == 0) {
236             Integer thisNbr = correctionChangeGroupLineNumber == null ? 0 : correctionChangeGroupLineNumber;
237             Integer thatNbr = other.correctionChangeGroupLineNumber == null ? 0 : other.correctionChangeGroupLineNumber;
238             return thisNbr.compareTo(thatNbr);
239         }
240         else {
241             return c;
242         }
243     }
244 
245     /**
246      * @see org.kuali.rice.krad.bo.BusinessObjectBase#toStringMapper()
247      */
248     protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
249         LinkedHashMap m = new LinkedHashMap();
250         m.put(KFSPropertyConstants.DOCUMENT_NUMBER, this.documentNumber);
251         if (this.correctionChangeGroupLineNumber != null) {
252             m.put("correctionChangeGroupLineNumber", this.correctionChangeGroupLineNumber.toString());
253         }
254         return m;
255     }
256     
257     /**
258      * A comparator that compares to GLCP correction change groups based on their group line numbers
259      * within the GLCP document
260      */
261     public static class CorrectionGroupLineNumberComparator implements Comparator {
262 
263         /**
264          * Constructs a CorrectionGroupLineNumberComparator instance
265          */
266         public CorrectionGroupLineNumberComparator() {
267         }
268 
269         /**
270          * Compares two CorrectionChangeGroups based on thier line numbers within a GLCP document
271          *
272          * @param c1 a correction change group to compare
273          * @param c2 another correction change group to compare the first one to
274          * @return a negative integer if c1 has a lower line number than c2, 0 if the two line numbers are equal, a positive number if c1 has a greater line number than c2 
275          * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
276          */
277         public int compare(Object c1, Object c2) {
278 
279             CorrectionChangeGroup ccg1 = (CorrectionChangeGroup) c1;
280             CorrectionChangeGroup ccg2 = (CorrectionChangeGroup) c2;
281 
282             return ccg1.getCorrectionChangeGroupLineNumber().compareTo(ccg2.getCorrectionChangeGroupLineNumber());
283         }
284 
285     }
286 }