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.gl.businessobject;
018    
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.Comparator;
022    import java.util.Iterator;
023    import java.util.LinkedHashMap;
024    import java.util.List;
025    
026    import org.kuali.kfs.sys.KFSPropertyConstants;
027    import org.kuali.rice.kns.bo.PersistableBusinessObjectBase;
028    
029    /**
030     * This class represents a GLCP correction change group
031     */
032    public class CorrectionChangeGroup extends PersistableBusinessObjectBase implements Comparable {
033        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CorrectionChangeGroup.class);
034    
035        private String documentNumber;
036        private Integer correctionChangeGroupLineNumber;
037        private Integer correctionCriteriaNextLineNumber;
038        private Integer correctionChangeNextLineNumber;
039        private List<CorrectionCriteria> correctionCriteria;
040        private List<CorrectionChange> correctionChange;
041    
042        public CorrectionChangeGroup(String documentNumber, Integer correctionChangeGroupLineNumber) {
043            setCorrectionChangeGroupLineNumber(correctionChangeGroupLineNumber);
044    
045            correctionCriteria = new ArrayList();
046            correctionChange = new ArrayList();
047            correctionCriteriaNextLineNumber = new Integer(0);
048            correctionChangeNextLineNumber = new Integer(0);
049    
050            setDocumentNumber(documentNumber);
051        }
052    
053        public CorrectionChangeGroup() {
054            super();
055            correctionCriteria = new ArrayList();
056            correctionChange = new ArrayList();
057            correctionCriteriaNextLineNumber = new Integer(0);
058            correctionChangeNextLineNumber = new Integer(0);
059        }
060    
061        /**
062         * Add correction change to this correction change group
063         * 
064         * @param cc correction change to add
065         */
066        public void addCorrectionChange(CorrectionChange cc) {
067            LOG.debug("addCorrectionChange() started");
068    
069            cc.setDocumentNumber(documentNumber);
070            cc.setCorrectionChangeGroupLineNumber(correctionChangeGroupLineNumber);
071            cc.setCorrectionChangeLineNumber(correctionChangeNextLineNumber++);
072            correctionChange.add(cc);
073        }
074    
075        /**
076         * Add correction criteria to this correction change group
077         * 
078         * @param cc correction criteria to add to this correction change group
079         */
080        public void addCorrectionCriteria(CorrectionCriteria cc) {
081            cc.setDocumentNumber(documentNumber);
082            cc.setCorrectionChangeGroupLineNumber(correctionChangeGroupLineNumber);
083            cc.setCorrectionCriteriaLineNumber(correctionCriteriaNextLineNumber++);
084            correctionCriteria.add(cc);
085        }
086    
087        /**
088         * Remove correction change item
089         * 
090         * @param changeNumber correction change line number used to determine which correction change item to remove
091         */
092        public void removeCorrectionChangeItem(int changeNumber) {
093            for (Iterator iter = correctionChange.iterator(); iter.hasNext();) {
094                CorrectionChange element = (CorrectionChange) iter.next();
095                if (changeNumber == element.getCorrectionChangeLineNumber().intValue()) {
096                    iter.remove();
097                }
098            }
099        }
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.kns.bo.BusinessObjectBase#toStringMapper()
244         */
245        protected LinkedHashMap toStringMapper() {
246            LinkedHashMap m = new LinkedHashMap();
247            m.put(KFSPropertyConstants.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    }