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    package org.kuali.kfs.gl.batch.service.impl;
017    
018    import java.util.HashSet;
019    import java.util.Iterator;
020    import java.util.Set;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.gl.businessobject.CollectorDetail;
024    import org.kuali.kfs.gl.businessobject.OriginEntryFull;
025    import org.kuali.kfs.gl.businessobject.Transaction;
026    
027    /**
028     * This class represents document group-related data
029     */
030    public class DocumentGroupData {
031        protected String documentNumber;
032        protected String financialDocumentTypeCode;
033        protected String financialSystemOriginationCode;
034    
035        public DocumentGroupData(Transaction entry) {
036            documentNumber = entry.getDocumentNumber();
037            financialDocumentTypeCode = entry.getFinancialDocumentTypeCode();
038            financialSystemOriginationCode = entry.getFinancialSystemOriginationCode();
039        }
040    
041        public DocumentGroupData(String documentNumber, String financialDocumentTypeCode, String financialSystemOriginationCode) {
042            this.documentNumber = documentNumber;
043            this.financialDocumentTypeCode = financialDocumentTypeCode;
044            this.financialSystemOriginationCode = financialSystemOriginationCode;
045        }
046    
047        /**
048         * Returns true if DocumentGroupData objects have the same document number, document type code, and financial system origination code
049         * 
050         * @see java.lang.Object#equals(java.lang.Object)
051         */
052        @Override
053        public boolean equals(Object obj) {
054            if (obj == null || !(obj instanceof DocumentGroupData)) {
055                return false;
056            }
057            DocumentGroupData o2 = (DocumentGroupData) obj;
058            return StringUtils.equals(documentNumber, o2.documentNumber) && StringUtils.equals(financialDocumentTypeCode, o2.financialDocumentTypeCode) && StringUtils.equals(financialSystemOriginationCode, o2.financialSystemOriginationCode);
059        }
060    
061        /**
062         * Returns true if this document group data object's and the transaction have the same document number, document type code, and origination code match the passed 
063         * 
064         * @param transaction transaction to compare
065         * @return true if this document group data object's and the transaction have the same document number, document type code, and origination code match the passed
066         */
067        public boolean matchesTransaction(Transaction transaction) {
068            return StringUtils.equals(documentNumber, transaction.getDocumentNumber()) && StringUtils.equals(financialDocumentTypeCode, transaction.getFinancialDocumentTypeCode()) && StringUtils.equals(financialSystemOriginationCode, transaction.getFinancialSystemOriginationCode());
069        }
070    
071        public boolean matchesCollectorDetail(CollectorDetail detail) {
072            return StringUtils.equals(documentNumber, detail.getDocumentNumber()) && StringUtils.equals(financialDocumentTypeCode, detail.getFinancialDocumentTypeCode()) && StringUtils.equals(financialSystemOriginationCode, detail.getFinancialSystemOriginationCode());
073        }
074    
075        /**
076         * 
077         * @see java.lang.Object#hashCode()
078         */
079        @Override
080        public int hashCode() {
081            // hash based on the doc #, because it's likely to have the most variation within an origin entry doc
082            if (documentNumber == null) {
083                return "".hashCode();
084            }
085            return documentNumber.hashCode();
086        }
087    
088        /**
089         * This returns an origin entry with document number, document type code, origination code set from this DocumentGroupData's document number, document type code, and origination code
090         * 
091         * @return populated origin entry  
092         */
093        public OriginEntryFull populateDocumentGroupDataFieldsInOriginEntry() {
094            OriginEntryFull entry = new OriginEntryFull();
095            entry.setDocumentNumber(documentNumber);
096            entry.setFinancialDocumentTypeCode(financialDocumentTypeCode);
097            entry.setFinancialSystemOriginationCode(financialSystemOriginationCode);
098            return entry;
099        }
100    
101        /**
102         * Gets the documentNumber attribute.
103         * 
104         * @return Returns the documentNumber.
105         */
106        public String getDocumentNumber() {
107            return documentNumber;
108        }
109    
110        /**
111         * Sets the documentNumber attribute value.
112         * 
113         * @param documentNumber The documentNumber to set.
114         */
115        public void setDocumentNumber(String documentNumber) {
116            this.documentNumber = documentNumber;
117        }
118    
119        /**
120         * Gets the financialDocumentTypeCode attribute.
121         * 
122         * @return Returns the financialDocumentTypeCode.
123         */
124        public String getFinancialDocumentTypeCode() {
125            return financialDocumentTypeCode;
126        }
127    
128        /**
129         * Sets the financialDocumentTypeCode attribute value.
130         * 
131         * @param financialDocumentTypeCode The financialDocumentTypeCode to set.
132         */
133        public void setFinancialDocumentTypeCode(String financialDocumentTypeCode) {
134            this.financialDocumentTypeCode = financialDocumentTypeCode;
135        }
136    
137        /**
138         * Gets the financialSystemOriginationCode attribute.
139         * 
140         * @return Returns the financialSystemOriginationCode.
141         */
142        public String getFinancialSystemOriginationCode() {
143            return financialSystemOriginationCode;
144        }
145    
146        /**
147         * Sets the financialSystemOriginationCode attribute value.
148         * 
149         * @param financialSystemOriginationCode The financialSystemOriginationCode to set.
150         */
151        public void setFinancialSystemOriginationCode(String financialSystemOriginationCode) {
152            this.financialSystemOriginationCode = financialSystemOriginationCode;
153        }
154    
155        /**
156         * Given an iterator of {@link Transaction} objects, return a set of all the document groups (doc #, doc type, origination code)
157         * for these transactions
158         * 
159         * @param transactions iterator of transactions
160         * @return Set of all of the document groups for this these trasnactions
161         */
162        public static <E extends Transaction> Set<DocumentGroupData> getDocumentGroupDatasForTransactions(Iterator<E> transactions) {
163            Set<DocumentGroupData> documentGroupDatas = new HashSet<DocumentGroupData>();
164            while (transactions.hasNext()) {
165                Transaction transaction = transactions.next();
166                documentGroupDatas.add(new DocumentGroupData(transaction));
167            }
168            return documentGroupDatas;
169        }
170    }