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