001/*
002 * Copyright 2008-2009 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 */
016package org.kuali.ole.module.purap.service.impl;
017
018import org.apache.commons.lang.builder.EqualsBuilder;
019import org.apache.commons.lang.builder.HashCodeBuilder;
020import org.kuali.ole.module.purap.businessobject.PurApAccountingLineBase;
021import org.kuali.rice.core.api.util.type.KualiDecimal;
022
023import java.math.BigDecimal;
024import java.util.ArrayList;
025import java.util.List;
026
027
028/**
029 * Purap account history grouping
030 */
031public class PurapAccountRevisionGroup {
032    protected String chartOfAccountsCode;
033    protected String accountNumber;
034    protected String subAccountNumber;
035    protected String financialObjectCode;
036    protected String financialSubObjectCode;
037    protected Integer postingYear;
038    protected String postingPeriodCode;
039    protected KualiDecimal amount;
040    protected Integer itemIdentifier;
041    private List<PurApAccountingLineBase> sourceEntries = new ArrayList<PurApAccountingLineBase>();
042    private KualiDecimal changeAmount;
043    // non-key attributes
044    private String projectCode;
045    private String organizationReferenceId;
046    private BigDecimal accountLinePercent;
047
048
049    public PurapAccountRevisionGroup(PurApAccountingLineBase entry) {
050        setChartOfAccountsCode(entry.getChartOfAccountsCode());
051        setAccountNumber(entry.getAccountNumber());
052        setSubAccountNumber(entry.getSubAccountNumber());
053        setFinancialObjectCode(entry.getFinancialObjectCode());
054        setFinancialSubObjectCode(entry.getFinancialSubObjectCode());
055        setItemIdentifier(entry.getItemIdentifier());
056        setPostingYear(entry.getPostingYear());
057        setPostingPeriodCode(entry.getPostingPeriodCode());
058        setProjectCode(entry.getProjectCode());
059        setOrganizationReferenceId(entry.getOrganizationReferenceId());
060        setAccountLinePercent(entry.getAccountLinePercent());
061        this.sourceEntries.add(entry);
062        setAmount(entry.getAmount());
063    }
064
065
066    /**
067     * Gets the chartOfAccountsCode attribute.
068     *
069     * @return Returns the chartOfAccountsCode
070     */
071    public String getChartOfAccountsCode() {
072        return chartOfAccountsCode;
073    }
074
075    /**
076     * Sets the chartOfAccountsCode attribute.
077     *
078     * @param chartOfAccountsCode The chartOfAccountsCode to set.
079     */
080    public void setChartOfAccountsCode(String chartOfAccountsCode) {
081        this.chartOfAccountsCode = chartOfAccountsCode;
082    }
083
084    /**
085     * Gets the accountNumber attribute.
086     *
087     * @return Returns the accountNumber
088     */
089    public String getAccountNumber() {
090        return accountNumber;
091    }
092
093    /**
094     * Sets the accountNumber attribute.
095     *
096     * @param accountNumber The accountNumber to set.
097     */
098    public void setAccountNumber(String accountNumber) {
099        this.accountNumber = accountNumber;
100    }
101
102    /**
103     * Gets the subAccountNumber attribute.
104     *
105     * @return Returns the subAccountNumber
106     */
107    public String getSubAccountNumber() {
108        return subAccountNumber;
109    }
110
111    /**
112     * Sets the subAccountNumber attribute.
113     *
114     * @param subAccountNumber The subAccountNumber to set.
115     */
116    public void setSubAccountNumber(String subAccountNumber) {
117        this.subAccountNumber = subAccountNumber;
118    }
119
120    /**
121     * Gets the financialObjectCode attribute.
122     *
123     * @return Returns the financialObjectCode
124     */
125    public String getFinancialObjectCode() {
126        return financialObjectCode;
127    }
128
129    /**
130     * Sets the financialObjectCode attribute.
131     *
132     * @param financialObjectCode The financialObjectCode to set.
133     */
134    public void setFinancialObjectCode(String financialObjectCode) {
135        this.financialObjectCode = financialObjectCode;
136    }
137
138    /**
139     * Gets the financialSubObjectCode attribute.
140     *
141     * @return Returns the financialSubObjectCode
142     */
143    public String getFinancialSubObjectCode() {
144        return financialSubObjectCode;
145    }
146
147    /**
148     * Sets the financialSubObjectCode attribute.
149     *
150     * @param financialSubObjectCode The financialSubObjectCode to set.
151     */
152    public void setFinancialSubObjectCode(String financialSubObjectCode) {
153        this.financialSubObjectCode = financialSubObjectCode;
154    }
155
156
157    /**
158     * Gets the postingYear attribute.
159     *
160     * @return Returns the postingYear.
161     */
162    public Integer getPostingYear() {
163        return postingYear;
164    }
165
166
167    /**
168     * Sets the postingYear attribute value.
169     *
170     * @param postingYear The postingYear to set.
171     */
172    public void setPostingYear(Integer postingYear) {
173        this.postingYear = postingYear;
174    }
175
176
177    /**
178     * Gets the postingPeriodCode attribute.
179     *
180     * @return Returns the postingPeriodCode.
181     */
182    public String getPostingPeriodCode() {
183        return postingPeriodCode;
184    }
185
186
187    /**
188     * Sets the postingPeriodCode attribute value.
189     *
190     * @param postingPeriodCode The postingPeriodCode to set.
191     */
192    public void setPostingPeriodCode(String postingPeriodCode) {
193        this.postingPeriodCode = postingPeriodCode;
194    }
195
196
197    /**
198     * Overridden so that group by statement can be easily implemented.
199     * <li>DO NOT REMOVE this method, it is critical to reconciliation process</li>
200     *
201     * @see java.lang.Object#equals(java.lang.Object)
202     */
203    @Override
204    public boolean equals(Object obj) {
205        if (this == obj) {
206            return true;
207        }
208        if (obj == null || !PurapAccountRevisionGroup.class.isAssignableFrom(obj.getClass())) {
209            return false;
210        }
211        PurapAccountRevisionGroup test = (PurapAccountRevisionGroup) obj;
212        EqualsBuilder equalsBuilder = new EqualsBuilder();
213        equalsBuilder.append(this.postingYear, test.getPostingYear());
214        equalsBuilder.append(itemIdentifier, test.getItemIdentifier());
215        equalsBuilder.append(replaceFiller(chartOfAccountsCode), replaceFiller(test.getChartOfAccountsCode()));
216        equalsBuilder.append(replaceFiller(accountNumber), replaceFiller(test.getAccountNumber()));
217        equalsBuilder.append(replaceFiller(subAccountNumber), replaceFiller(test.getSubAccountNumber()));
218        equalsBuilder.append(replaceFiller(financialObjectCode), replaceFiller(test.getFinancialObjectCode()));
219        equalsBuilder.append(replaceFiller(financialSubObjectCode), replaceFiller(test.getFinancialSubObjectCode()));
220        equalsBuilder.append(replaceFiller(postingPeriodCode), replaceFiller(test.getPostingPeriodCode()));
221        equalsBuilder.append(replaceFiller(projectCode), replaceFiller(test.getProjectCode()));
222        equalsBuilder.append(replaceFiller(organizationReferenceId), replaceFiller(test.getOrganizationReferenceId()));
223        return equalsBuilder.isEquals();
224    }
225
226    protected String replaceFiller(String val) {
227        if (val == null) {
228            return "";
229        }
230        char[] charArray = val.trim().toCharArray();
231        for (char c : charArray) {
232            if (c != '-') {
233                return val;
234            }
235        }
236        return "";
237    }
238
239    /**
240     * Overridden so that group by statement can be easily implemented.
241     * <li>DO NOT REMOVE this method, it is critical to reconciliation process</li>
242     *
243     * @see java.lang.Object#hashCode(java.lang.Object)
244     */
245    @Override
246    public int hashCode() {
247        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(37, 41);
248        hashCodeBuilder.append(this.postingYear);
249        hashCodeBuilder.append(itemIdentifier);
250        hashCodeBuilder.append(replaceFiller(chartOfAccountsCode));
251        hashCodeBuilder.append(replaceFiller(accountNumber));
252        hashCodeBuilder.append(replaceFiller(subAccountNumber));
253        hashCodeBuilder.append(replaceFiller(financialObjectCode));
254        hashCodeBuilder.append(replaceFiller(financialSubObjectCode));
255        hashCodeBuilder.append(replaceFiller(postingPeriodCode));
256        hashCodeBuilder.append(replaceFiller(projectCode));
257        hashCodeBuilder.append(replaceFiller(organizationReferenceId));
258        return hashCodeBuilder.toHashCode();
259    }
260
261    /**
262     * Gets the absAmout attribute.
263     *
264     * @return Returns the absAmout
265     */
266
267    public KualiDecimal getAmount() {
268        return amount;
269    }
270
271    /**
272     * Sets the amount attribute.
273     *
274     * @param amount The amount to set.
275     */
276
277    public void setAmount(KualiDecimal absAmount) {
278        this.amount = absAmount;
279    }
280
281    @Override
282    public String toString() {
283        return "" + postingYear + "-" + chartOfAccountsCode + "-" + accountNumber + "-" + replaceFiller(subAccountNumber) + "-" + financialObjectCode + "-" + replaceFiller(financialSubObjectCode) + "-" + postingPeriodCode + "-" + itemIdentifier;
284    }
285
286    /**
287     * Gets the itemIdentifier attribute.
288     *
289     * @return Returns the itemIdentifier.
290     */
291    public Integer getItemIdentifier() {
292        return itemIdentifier;
293    }
294
295    /**
296     * Sets the itemIdentifier attribute value.
297     *
298     * @param itemIdentifier The itemIdentifier to set.
299     */
300    public void setItemIdentifier(Integer itemIdentifier) {
301        this.itemIdentifier = itemIdentifier;
302    }
303
304    /**
305     * This method will combine multiple Purap account entries for the same account line group.
306     *
307     * @param entry PurApAccountingLineBase
308     */
309    public void combineEntry(PurApAccountingLineBase newEntry) {
310        this.sourceEntries.add(newEntry);
311        this.amount = this.amount.add(newEntry.getAmount());
312    }
313
314    /**
315     * Gets the changeAmount attribute.
316     *
317     * @return Returns the changeAmount.
318     */
319    public KualiDecimal getChangeAmount() {
320        return changeAmount;
321    }
322
323    /**
324     * Sets the changeAmount attribute value.
325     *
326     * @param changeAmount The changeAmount to set.
327     */
328    public void setChangeAmount(KualiDecimal changeAmount) {
329        this.changeAmount = changeAmount;
330    }
331
332
333    /**
334     * Gets the projectCode attribute.
335     *
336     * @return Returns the projectCode.
337     */
338    public String getProjectCode() {
339        return projectCode;
340    }
341
342    /**
343     * Sets the projectCode attribute value.
344     *
345     * @param projectCode The projectCode to set.
346     */
347    public void setProjectCode(String projectCode) {
348        this.projectCode = projectCode;
349    }
350
351    /**
352     * Gets the organizationReferenceId attribute.
353     *
354     * @return Returns the organizationReferenceId.
355     */
356    public String getOrganizationReferenceId() {
357        return organizationReferenceId;
358    }
359
360    /**
361     * Sets the organizationReferenceId attribute value.
362     *
363     * @param organizationReferenceId The organizationReferenceId to set.
364     */
365    public void setOrganizationReferenceId(String organizationReferenceId) {
366        this.organizationReferenceId = organizationReferenceId;
367    }
368
369
370    /**
371     * Gets the accountLinePercent attribute.
372     *
373     * @return Returns the accountLinePercent.
374     */
375    public BigDecimal getAccountLinePercent() {
376        return accountLinePercent;
377    }
378
379    /**
380     * Sets the accountLinePercent attribute value.
381     *
382     * @param accountLinePercent The accountLinePercent to set.
383     */
384    public void setAccountLinePercent(BigDecimal accountLinePercent) {
385        this.accountLinePercent = accountLinePercent;
386    }
387
388    public PurApAccountingLineBase buildRevisionRecord(Class<? extends PurApAccountingLineBase> clazz) {
389        PurApAccountingLineBase histRecord = null;
390        try {
391            histRecord = clazz.newInstance();
392            histRecord.setItemIdentifier(this.getItemIdentifier());
393            histRecord.setChartOfAccountsCode(this.getChartOfAccountsCode());
394            histRecord.setAccountNumber(this.getAccountNumber());
395            histRecord.setSubAccountNumber(this.getSubAccountNumber());
396            histRecord.setFinancialObjectCode(this.getFinancialObjectCode());
397            histRecord.setFinancialSubObjectCode(this.getFinancialSubObjectCode());
398            histRecord.setProjectCode(this.getProjectCode());
399            histRecord.setOrganizationReferenceId(this.getOrganizationReferenceId());
400            histRecord.setAmount(this.getChangeAmount());
401            histRecord.setPostingYear(this.getPostingYear());
402            histRecord.setPostingPeriodCode(this.getPostingPeriodCode());
403            histRecord.setAccountLinePercent(this.getAccountLinePercent());
404        } catch (Exception e) {
405            throw new RuntimeException(e);
406        }
407        return histRecord;
408    }
409}