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  package org.kuali.ole.gl.batch.service.impl;
17  
18  import java.util.HashSet;
19  import java.util.Iterator;
20  import java.util.Set;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.kuali.ole.gl.businessobject.CollectorDetail;
24  import org.kuali.ole.gl.businessobject.OriginEntryFull;
25  import org.kuali.ole.gl.businessobject.Transaction;
26  
27  /**
28   * This class represents document group-related data
29   */
30  public class DocumentGroupData {
31      protected String documentNumber;
32      protected String financialDocumentTypeCode;
33      protected String financialSystemOriginationCode;
34  
35      public DocumentGroupData(Transaction entry) {
36          documentNumber = entry.getDocumentNumber();
37          financialDocumentTypeCode = entry.getFinancialDocumentTypeCode();
38          financialSystemOriginationCode = entry.getFinancialSystemOriginationCode();
39      }
40  
41      public DocumentGroupData(String documentNumber, String financialDocumentTypeCode, String financialSystemOriginationCode) {
42          this.documentNumber = documentNumber;
43          this.financialDocumentTypeCode = financialDocumentTypeCode;
44          this.financialSystemOriginationCode = financialSystemOriginationCode;
45      }
46  
47      /**
48       * Returns true if DocumentGroupData objects have the same document number, document type code, and financial system origination code
49       * 
50       * @see java.lang.Object#equals(java.lang.Object)
51       */
52      @Override
53      public boolean equals(Object obj) {
54          if (obj == null || !(obj instanceof DocumentGroupData)) {
55              return false;
56          }
57          DocumentGroupData o2 = (DocumentGroupData) obj;
58          return StringUtils.equals(documentNumber, o2.documentNumber) && StringUtils.equals(financialDocumentTypeCode, o2.financialDocumentTypeCode) && StringUtils.equals(financialSystemOriginationCode, o2.financialSystemOriginationCode);
59      }
60  
61      /**
62       * 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 
63       * 
64       * @param transaction transaction to compare
65       * @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
66       */
67      public boolean matchesTransaction(Transaction transaction) {
68          return StringUtils.equals(documentNumber, transaction.getDocumentNumber()) && StringUtils.equals(financialDocumentTypeCode, transaction.getFinancialDocumentTypeCode()) && StringUtils.equals(financialSystemOriginationCode, transaction.getFinancialSystemOriginationCode());
69      }
70  
71      public boolean matchesCollectorDetail(CollectorDetail detail) {
72          return StringUtils.equals(documentNumber, detail.getDocumentNumber()) && StringUtils.equals(financialDocumentTypeCode, detail.getFinancialDocumentTypeCode()) && StringUtils.equals(financialSystemOriginationCode, detail.getFinancialSystemOriginationCode());
73      }
74  
75      /**
76       * 
77       * @see java.lang.Object#hashCode()
78       */
79      @Override
80      public int hashCode() {
81          // hash based on the doc #, because it's likely to have the most variation within an origin entry doc
82          if (documentNumber == null) {
83              return "".hashCode();
84          }
85          return documentNumber.hashCode();
86      }
87  
88      /**
89       * 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
90       * 
91       * @return populated origin entry  
92       */
93      public OriginEntryFull populateDocumentGroupDataFieldsInOriginEntry() {
94          OriginEntryFull entry = new OriginEntryFull();
95          entry.setDocumentNumber(documentNumber);
96          entry.setFinancialDocumentTypeCode(financialDocumentTypeCode);
97          entry.setFinancialSystemOriginationCode(financialSystemOriginationCode);
98          return entry;
99      }
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 }